From 385bf72a3e0e3ef8e1dfbebf60678e7d09b4fafc Mon Sep 17 00:00:00 2001 From: Rohit Tanwar Date: Tue, 20 Feb 2018 22:42:11 +0530 Subject: [PATCH] readme-script --- .vscode/settings.json | 3 ++ scripts/readme.py | 42 ++++++---------- scripts/util.py | 50 +++++++++++++++++++ snippets/deep_flatten.md | 2 +- snippets/lcm.md | 2 +- static-parts/index-start.html | 0 static-parts/readme-start.md | 2 + tag_database | 3 +- test/average/average.py | 2 +- test/byte_size/byte_size.py | 2 +- test/capitalize/capitalize.py | 2 +- .../capitalize_every_word.py | 2 +- test/chunk/chunk.py | 12 ++--- test/compact/compact.py | 2 +- test/count_by/count_by.py | 10 ++-- test/count_occurences/count_occurences.py | 6 +-- test/count_vowels/count_vowels.py | 8 +-- test/decapitalize/decapitalize.py | 2 +- test/deep_flatten/deep_flatten.py | 28 +++++------ test/difference/difference.py | 4 +- test/difference_by/difference_by.py | 4 +- test/factorial/factorial.py | 8 +-- test/gcd/gcd.py | 40 +++++++-------- test/insertion_sort/insertion_sort.py | 9 ++++ test/insertion_sort/insertion_sort.test.py | 6 +++ test/is_lower_case/is_lower_case.py | 2 +- test/is_upper_case/is_upper_case.py | 2 +- test/lcm/lcm.py | 46 ++++++++--------- test/max_n/max_n.py | 14 +++--- test/min_n/min_n.py | 12 ++--- test/palindrome/palindrome.py | 6 +-- test/shuffle/shuffle.py | 22 ++++---- test/spread/spread.py | 14 +++--- test/zip/zip.py | 14 +++--- 34 files changed, 222 insertions(+), 161 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 scripts/util.py delete mode 100644 static-parts/index-start.html create mode 100644 test/insertion_sort/insertion_sort.py create mode 100644 test/insertion_sort/insertion_sort.test.py diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 000000000..26df38bf5 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "python.linting.enabled": false +} \ No newline at end of file diff --git a/scripts/readme.py b/scripts/readme.py index 190a54eb8..5a3137441 100644 --- a/scripts/readme.py +++ b/scripts/readme.py @@ -1,5 +1,5 @@ -import re -codeRe = "```\s*python([\s\S]*?)```" +import util +from collections import defaultdict def title_case(str): return str[:1].upper() + str[1:].lower() EMOJIS = { @@ -18,34 +18,24 @@ EMOJIS = { 'utility': ':wrench:' } def tagger(): - tag_data = open('tag_database').read() - tag_dict = {} - tag_list = tag_data.split('\n') - for tag in tag_list: - category = tag.split(':')[1] - snippet = tag.split(':')[0] - if category in tag_dict: - tag_dict[category].append(snippet) - else: - tag_dict[category] = [snippet] - return tag_dict + tag_db = defaultdict(list) + for snippet in util.read_snippets(): + tag_db[snippet.category[0]].append(snippet) + return tag_db -start = open("static-parts/readme-start.md").read() + '\n\n' -end = open("static-parts/readme-end.md").read() +start = util.read_readme_start() +end = util.read_readme_end() toAppend = '' tag_dict = tagger() -toAppend += '## Table of Contents \n' -for category in tag_dict: +for category in sorted(tag_dict): toAppend = toAppend + '### ' + EMOJIS[category] + ' ' + title_case(category) +'\n\n
View contents
\n\n' toAppend += '
\n\n' -for category in tag_dict: - toAppend = toAppend + '## ' + EMOJIS[category] + ' ' + title_case(category) +'\n\n' - for snippet in sorted(tag_dict[category]): - someFile = open("snippets/" + snippet + '.md') +for category in sorted(tag_dict): + toAppend = toAppend + '## ' + EMOJIS[category] + ' ' + title_case(scategory) +'\n\n' + for snippet in sorted(tag_dict[category],key=lambda snippet : snippet.name): fileData = someFile.read() - codeParts = re.split(codeRe,fileData) - toAppend += codeParts[0] + '```py{codeParts[1]} \n ```'.format(codeParts= codeParts) +codeParts[2] + '
View Examples\n\n```py\n{codeParts[3]}\n```\n
\n\n
:arrow_up: Back to top\n '.format(codeParts=codeParts) + '\n' -open("README.md",'w').write(start+toAppend+'\n'+end) \ No newline at end of file + toAppend += f'###{snippet.title}\n\n```py{snippet.read_code()} \n ```'.format(codeParts= codeParts) +codeParts[2] + '
View Examples\n\n```py\n{codeParts[3]}\n```\n
\n\n
:arrow_up: Back to top\n '.format(codeParts=codeParts) + '\n' +open("README.md",'w').write(start+toAppend+'\n'+end)''' \ No newline at end of file diff --git a/scripts/util.py b/scripts/util.py new file mode 100644 index 000000000..8e20d4b5e --- /dev/null +++ b/scripts/util.py @@ -0,0 +1,50 @@ +import os,re +def tagger(): + snippet_files = [snippet.replace('.md','')for snippet in os.listdir('snippets')] + + tag_database_file = open('tag_database') + + tag_database = tag_database_file.read() + + tag_database_file.close() + + tag_database = [tag for tag in tag_database.split('\n') if tag.strip() != ''] + + tag_db = {} + + for tag in tag_database: + tag_db[tag.split(':')[0].strip()] = tag.split(':')[1].strip().split(',') + return tag_db +def read_snippets(): + snippet_files = os.listdir('snippets') + + snippets = [] + + class snippet(): + def __init__(self,file_location): + with open(file_location) as f: + self.content = f.read() + self.codeRe = "```\s*python([\s\S]*?)```" + self.titleRe = '###\\s*([\\w]+)' + self.descRe = '###\s*\w+\s*([\s\S]+)```\s*python[\s\S]+```\s*```\s*python[\s\S]+```' + self.name = self.read_title() + self.category = tagger()[self.name] + def read_code(self): + return re.findall(self.codeRe,self.content)[0].strip() + def read_title(self): + return re.findall(self.titleRe,self.content)[0].strip() + def read_description(self): + return re.findall(self.descRe,self.content)[0].strip() + def read_example(self): + return re.findall(self.codeRe,self.content)[1].strip() + for snippet_file in snippet_files: + snippets.append(snippet(f'snippets/{snippet_file}')) + return snippets +def read_readme_start(): + with open('static-part/readme-start.md') as f: + readme_start = f.read() + return readme_start +def read_readme_end(): + with open('static-part/readme-end.md') as f: + readme_end = f.read() + return readme_end diff --git a/snippets/deep_flatten.md b/snippets/deep_flatten.md index 3da31478d..62d086589 100644 --- a/snippets/deep_flatten.md +++ b/snippets/deep_flatten.md @@ -18,7 +18,7 @@ def spread(arg): def deep_flatten(arr): result = [] result.extend( - spread(list(map(lambda x: deep(x) if type(x) == list else x, arr)))) + spread(list(map(lambda x: deep_flatten(x) if type(x) == list else x, arr)))) return result ``` diff --git a/snippets/lcm.md b/snippets/lcm.md index 7400a6d4c..17969c9b0 100644 --- a/snippets/lcm.md +++ b/snippets/lcm.md @@ -25,7 +25,7 @@ def lcm(*args): numbers.extend(spread(list(args))) def _gcd(x, y): - return x if not y else gcd(y, x % y) + return x if not y else _gcd(y, x % y) def _lcm(x, y): return x * y / _gcd(x, y) diff --git a/static-parts/index-start.html b/static-parts/index-start.html deleted file mode 100644 index e69de29bb..000000000 diff --git a/static-parts/readme-start.md b/static-parts/readme-start.md index 5a6b0cd06..a7b8802a6 100644 --- a/static-parts/readme-start.md +++ b/static-parts/readme-start.md @@ -7,3 +7,5 @@ >Python implementation of 30-seconds-of-code. **Note**:- This is in no way affiliated with the original [30-seconds-of-code](https://github.com/Chalarangelo/30-seconds-of-code/). + +### Table of contents diff --git a/tag_database b/tag_database index b3ba59a8f..d542f2886 100644 --- a/tag_database +++ b/tag_database @@ -22,4 +22,5 @@ is_upper_case:string is_lower_case:string count_by:list difference_by:list -insertion_sort:list \ No newline at end of file +insertion_sort:list + diff --git a/test/average/average.py b/test/average/average.py index 64087c958..1ff6e2ef9 100644 --- a/test/average/average.py +++ b/test/average/average.py @@ -1,2 +1,2 @@ -def average(*args): +def average(*args): return sum(args, 0.0) / len(args) \ No newline at end of file diff --git a/test/byte_size/byte_size.py b/test/byte_size/byte_size.py index fdb82b196..5050bb37d 100644 --- a/test/byte_size/byte_size.py +++ b/test/byte_size/byte_size.py @@ -1,2 +1,2 @@ -def byte_size(string): +def byte_size(string): return(len(string.encode('utf-8'))) \ No newline at end of file diff --git a/test/capitalize/capitalize.py b/test/capitalize/capitalize.py index 737f60a11..0eb21baa1 100644 --- a/test/capitalize/capitalize.py +++ b/test/capitalize/capitalize.py @@ -1,2 +1,2 @@ -def capitalize(string, lower_rest=False): +def capitalize(string, lower_rest=False): return string[:1].upper() + (string[1:].lower() if lower_rest else string[1:]) \ No newline at end of file diff --git a/test/capitalize_every_word/capitalize_every_word.py b/test/capitalize_every_word/capitalize_every_word.py index 554cc5068..7f7d7bf5a 100644 --- a/test/capitalize_every_word/capitalize_every_word.py +++ b/test/capitalize_every_word/capitalize_every_word.py @@ -1,2 +1,2 @@ -def capitalize_every_word(string): +def capitalize_every_word(string): return string.title() \ No newline at end of file diff --git a/test/chunk/chunk.py b/test/chunk/chunk.py index a88109e26..6f247c2c9 100644 --- a/test/chunk/chunk.py +++ b/test/chunk/chunk.py @@ -1,7 +1,7 @@ -from math import ceil - - -def chunk(arr, size): - return list( - map(lambda x: arr[x * size:x * size + size], +from math import ceil + + +def chunk(arr, size): + return list( + map(lambda x: arr[x * size:x * size + size], list(range(0, ceil(len(arr) / size))))) \ No newline at end of file diff --git a/test/compact/compact.py b/test/compact/compact.py index 20420541c..f90cc92d7 100644 --- a/test/compact/compact.py +++ b/test/compact/compact.py @@ -1,2 +1,2 @@ -def compact(arr): +def compact(arr): return list(filter(lambda x: bool(x), arr)) \ No newline at end of file diff --git a/test/count_by/count_by.py b/test/count_by/count_by.py index 90c0fc813..aae034ead 100644 --- a/test/count_by/count_by.py +++ b/test/count_by/count_by.py @@ -1,6 +1,6 @@ -def count_by(arr, fn=lambda x: x): - key = {} - for el in map(fn, arr): - key[el] = 0 if not el in key else key[el] - key[el] += 1 +def count_by(arr, fn=lambda x: x): + key = {} + for el in map(fn, arr): + key[el] = 0 if not el in key else key[el] + key[el] += 1 return key \ No newline at end of file diff --git a/test/count_occurences/count_occurences.py b/test/count_occurences/count_occurences.py index e98edff74..161f8f86f 100644 --- a/test/count_occurences/count_occurences.py +++ b/test/count_occurences/count_occurences.py @@ -1,4 +1,4 @@ -def count_occurences(arr, val): - return reduce( - (lambda x, y: x + 1 if y == val and type(y) == type(val) else x + 0), +def count_occurences(arr, val): + return reduce( + (lambda x, y: x + 1 if y == val and type(y) == type(val) else x + 0), arr) \ No newline at end of file diff --git a/test/count_vowels/count_vowels.py b/test/count_vowels/count_vowels.py index 04c7fa737..05217ac13 100644 --- a/test/count_vowels/count_vowels.py +++ b/test/count_vowels/count_vowels.py @@ -1,5 +1,5 @@ -import re - - -def count_vowels(str): +import re + + +def count_vowels(str): return len(len(re.findall(r'[aeiou]', str, re.IGNORECASE))) \ No newline at end of file diff --git a/test/decapitalize/decapitalize.py b/test/decapitalize/decapitalize.py index ed4ffd6c9..4518a6599 100644 --- a/test/decapitalize/decapitalize.py +++ b/test/decapitalize/decapitalize.py @@ -1,2 +1,2 @@ -def decapitalize(string, upper_rest=False): +def decapitalize(string, upper_rest=False): return str[:1].lower() + (str[1:].upper() if upper_rest else str[1:]) \ No newline at end of file diff --git a/test/deep_flatten/deep_flatten.py b/test/deep_flatten/deep_flatten.py index 2a6c2095e..190bfd53a 100644 --- a/test/deep_flatten/deep_flatten.py +++ b/test/deep_flatten/deep_flatten.py @@ -1,15 +1,15 @@ -def spread(arg): - ret = [] - for i in arg: - if isinstance(i, list): - ret.extend(i) - else: - ret.append(i) - return ret - - -def deep_flatten(arr): - result = [] - result.extend( - spread(list(map(lambda x: deep(x) if type(x) == list else x, arr)))) +def spread(arg): + ret = [] + for i in arg: + if isinstance(i, list): + ret.extend(i) + else: + ret.append(i) + return ret + + +def deep_flatten(arr): + result = [] + result.extend( + spread(list(map(lambda x: deep_flatten(x) if type(x) == list else x, arr)))) return result \ No newline at end of file diff --git a/test/difference/difference.py b/test/difference/difference.py index 87a05ff45..b375d7748 100644 --- a/test/difference/difference.py +++ b/test/difference/difference.py @@ -1,3 +1,3 @@ -def difference(a, b): - b = set(b) +def difference(a, b): + b = set(b) return [item for item in a if item not in b] \ No newline at end of file diff --git a/test/difference_by/difference_by.py b/test/difference_by/difference_by.py index f2fe98623..fb639d430 100644 --- a/test/difference_by/difference_by.py +++ b/test/difference_by/difference_by.py @@ -1,3 +1,3 @@ -def difference_by(a, b, fn): - b = set(map(fn, b)) +def difference_by(a, b, fn): + b = set(map(fn, b)) return [item for item in a if fn(item) not in b] \ No newline at end of file diff --git a/test/factorial/factorial.py b/test/factorial/factorial.py index 49fba02ba..999f1fb52 100644 --- a/test/factorial/factorial.py +++ b/test/factorial/factorial.py @@ -1,5 +1,5 @@ -def factorial(num): - if not ((num >= 0) & (num % 1 == 0)): - raise Exception( - f"Number( {num} ) can't be floating point or negative ") +def factorial(num): + if not ((num >= 0) & (num % 1 == 0)): + raise Exception( + f"Number( {num} ) can't be floating point or negative ") return 1 if num == 0 else num * factorial(num - 1) \ No newline at end of file diff --git a/test/gcd/gcd.py b/test/gcd/gcd.py index fed4f0941..a4ad73048 100644 --- a/test/gcd/gcd.py +++ b/test/gcd/gcd.py @@ -1,21 +1,21 @@ -from functools import reduce - - -def spread(arg): - ret = [] - for i in arg: - if isinstance(i, list): - ret.extend(i) - else: - ret.append(i) - return ret - - -def gcd(*args): - numbers = [] - numbers.extend(spread(list(args))) - - def _gcd(x, y): - return x if not y else gcd(y, x % y) - +from functools import reduce + + +def spread(arg): + ret = [] + for i in arg: + if isinstance(i, list): + ret.extend(i) + else: + ret.append(i) + return ret + + +def gcd(*args): + numbers = [] + numbers.extend(spread(list(args))) + + def _gcd(x, y): + return x if not y else gcd(y, x % y) + return reduce((lambda x, y: _gcd(x, y)), numbers) \ No newline at end of file diff --git a/test/insertion_sort/insertion_sort.py b/test/insertion_sort/insertion_sort.py new file mode 100644 index 000000000..393b52b0d --- /dev/null +++ b/test/insertion_sort/insertion_sort.py @@ -0,0 +1,9 @@ +def insertionsort(arr): + + for i in range(1, len(arr)): + key = arr[i] + j = i - 1 + while j >= 0 and key < arr[j]: + arr[j + 1] = arr[j] + j -= 1 + arr[j + 1] = key \ No newline at end of file diff --git a/test/insertion_sort/insertion_sort.test.py b/test/insertion_sort/insertion_sort.test.py new file mode 100644 index 000000000..8415b1154 --- /dev/null +++ b/test/insertion_sort/insertion_sort.test.py @@ -0,0 +1,6 @@ +import types,functools +from pytape import test +from insertion_sort import insertion_sort +def insertion_sort_test(t): + t.true(isinstance(insertion_sort, (types.BuiltinFunctionType, types.FunctionType, functools.partial)),'insertion_sort is a function') +test('Testing insertion_sort',insertion_sort_test) \ No newline at end of file diff --git a/test/is_lower_case/is_lower_case.py b/test/is_lower_case/is_lower_case.py index b7e414afd..3b914e8b9 100644 --- a/test/is_lower_case/is_lower_case.py +++ b/test/is_lower_case/is_lower_case.py @@ -1,2 +1,2 @@ -def is_lower_case(str): +def is_lower_case(str): return str == str.lower() \ No newline at end of file diff --git a/test/is_upper_case/is_upper_case.py b/test/is_upper_case/is_upper_case.py index 0d59887ed..062b4f8d2 100644 --- a/test/is_upper_case/is_upper_case.py +++ b/test/is_upper_case/is_upper_case.py @@ -1,2 +1,2 @@ -def is_upper_case(str): +def is_upper_case(str): return str == str.upper() \ No newline at end of file diff --git a/test/lcm/lcm.py b/test/lcm/lcm.py index 917f638ac..85d347fd8 100644 --- a/test/lcm/lcm.py +++ b/test/lcm/lcm.py @@ -1,24 +1,24 @@ -from functools import reduce - - -def spread(arg): - ret = [] - for i in arg: - if isinstance(i, list): - ret.extend(i) - else: - ret.append(i) - return ret - - -def lcm(*args): - numbers = [] - numbers.extend(spread(list(args))) - - def _gcd(x, y): - return x if not y else gcd(y, x % y) - - def _lcm(x, y): - return x * y / _gcd(x, y) - +from functools import reduce + + +def spread(arg): + ret = [] + for i in arg: + if isinstance(i, list): + ret.extend(i) + else: + ret.append(i) + return ret + + +def lcm(*args): + numbers = [] + numbers.extend(spread(list(args))) + + def _gcd(x, y): + return x if not y else _gcd(y, x % y) + + def _lcm(x, y): + return x * y / _gcd(x, y) + return reduce((lambda x, y: _lcm(x, y)), numbers) \ No newline at end of file diff --git a/test/max_n/max_n.py b/test/max_n/max_n.py index a50b47bb6..70006802c 100644 --- a/test/max_n/max_n.py +++ b/test/max_n/max_n.py @@ -1,8 +1,8 @@ -from copy import deepcopy - - -def max_n(arr, n=1): - numbers = deepcopy(arr) - numbers.sort() - numbers.reverse() +from copy import deepcopy + + +def max_n(arr, n=1): + numbers = deepcopy(arr) + numbers.sort() + numbers.reverse() return numbers[:n] \ No newline at end of file diff --git a/test/min_n/min_n.py b/test/min_n/min_n.py index 0026858be..c973b3a61 100644 --- a/test/min_n/min_n.py +++ b/test/min_n/min_n.py @@ -1,7 +1,7 @@ -from copy import deepcopy - - -def min_n(arr, n=1): - numbers = deepcopy(arr) - numbers.sort() +from copy import deepcopy + + +def min_n(arr, n=1): + numbers = deepcopy(arr) + numbers.sort() return numbers[:n] \ No newline at end of file diff --git a/test/palindrome/palindrome.py b/test/palindrome/palindrome.py index 73679f3e7..f7b5165e7 100644 --- a/test/palindrome/palindrome.py +++ b/test/palindrome/palindrome.py @@ -1,4 +1,4 @@ -def palindrome(string): - from re import sub - s = sub('[\W_]', '', string.lower()) +def palindrome(string): + from re import sub + s = sub('[\W_]', '', string.lower()) return s == s[::-1] \ No newline at end of file diff --git a/test/shuffle/shuffle.py b/test/shuffle/shuffle.py index 120264534..2d16df93f 100644 --- a/test/shuffle/shuffle.py +++ b/test/shuffle/shuffle.py @@ -1,12 +1,12 @@ -from copy import deepcopy -from random import randint - - -def shuffle(arr): - temp_arr = deepcopy(arr) - m = len(temp_arr) - while (m): - m -= 1 - i = randint(0, m) - temp_arr[m], temp_arr[i] = temp_arr[i], temp_arr[m] +from copy import deepcopy +from random import randint + + +def shuffle(arr): + temp_arr = deepcopy(arr) + m = len(temp_arr) + while (m): + m -= 1 + i = randint(0, m) + temp_arr[m], temp_arr[i] = temp_arr[i], temp_arr[m] return temp_arr \ No newline at end of file diff --git a/test/spread/spread.py b/test/spread/spread.py index 0efae561c..02baa7f82 100644 --- a/test/spread/spread.py +++ b/test/spread/spread.py @@ -1,8 +1,8 @@ -def spread(arg): - ret = [] - for i in arg: - if isinstance(i, list): - ret.extend(i) - else: - ret.append(i) +def spread(arg): + ret = [] + for i in arg: + if isinstance(i, list): + ret.extend(i) + else: + ret.append(i) return ret \ No newline at end of file diff --git a/test/zip/zip.py b/test/zip/zip.py index ed2680f60..ef9aa5593 100644 --- a/test/zip/zip.py +++ b/test/zip/zip.py @@ -1,8 +1,8 @@ -def zip(*args, fillvalue=None): - max_length = max([len(arr) for arr in args]) - result = [] - for i in range(max_length): - result.append([ - args[k][i] if i < len(args[k]) else None for k in range(len(args)) - ]) +def zip(*args, fillvalue=None): + max_length = max([len(arr) for arr in args]) + result = [] + for i in range(max_length): + result.append([ + args[k][i] if i < len(args[k]) else None for k in range(len(args)) + ]) return result \ No newline at end of file