readme-script

This commit is contained in:
Rohit Tanwar
2018-02-20 22:42:11 +05:30
parent 7cc78ca4c4
commit 385bf72a3e
34 changed files with 222 additions and 161 deletions

3
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,3 @@
{
"python.linting.enabled": false
}

View File

@ -1,5 +1,5 @@
import re import util
codeRe = "```\s*python([\s\S]*?)```" from collections import defaultdict
def title_case(str): def title_case(str):
return str[:1].upper() + str[1:].lower() return str[:1].upper() + str[1:].lower()
EMOJIS = { EMOJIS = {
@ -18,34 +18,24 @@ EMOJIS = {
'utility': ':wrench:' 'utility': ':wrench:'
} }
def tagger(): def tagger():
tag_data = open('tag_database').read() tag_db = defaultdict(list)
tag_dict = {} for snippet in util.read_snippets():
tag_list = tag_data.split('\n') tag_db[snippet.category[0]].append(snippet)
for tag in tag_list: return tag_db
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
start = open("static-parts/readme-start.md").read() + '\n\n' start = util.read_readme_start()
end = open("static-parts/readme-end.md").read() end = util.read_readme_end()
toAppend = '' toAppend = ''
tag_dict = tagger() tag_dict = tagger()
toAppend += '## Table of Contents \n' for category in sorted(tag_dict):
for category in tag_dict:
toAppend = toAppend + '### ' + EMOJIS[category] + ' ' + title_case(category) +'\n\n<details><summary>View contents</summary> <ul>' toAppend = toAppend + '### ' + EMOJIS[category] + ' ' + title_case(category) +'\n\n<details><summary>View contents</summary> <ul>'
for snippet in sorted(tag_dict[category]): for snippet in sorted(tag_dict[category],key=lambda snippet : snippet.name):
toAppend += f'<li><a href = "#{snippet}"><code>{snippet}</code></a></li>\n' toAppend += f'<li><a href = "#{snippet.name}"><code>{snippet.name}</code></a></li>\n'
toAppend += '</ul></details>\n\n' toAppend += '</ul></details>\n\n'
toAppend += '<hr></hr> \n\n' toAppend += '<hr></hr> \n\n'
for category in tag_dict: for category in sorted(tag_dict):
toAppend = toAppend + '## ' + EMOJIS[category] + ' ' + title_case(category) +'\n\n' toAppend = toAppend + '## ' + EMOJIS[category] + ' ' + title_case(scategory) +'\n\n'
for snippet in sorted(tag_dict[category]): for snippet in sorted(tag_dict[category],key=lambda snippet : snippet.name):
someFile = open("snippets/" + snippet + '.md')
fileData = someFile.read() fileData = someFile.read()
codeParts = re.split(codeRe,fileData) toAppend += f'###{snippet.title}\n\n```py{snippet.read_code()} \n ```'.format(codeParts= codeParts) +codeParts[2] + '<details><summary>View Examples</summary>\n\n```py\n{codeParts[3]}\n```\n</details>\n\n<br><a href = "#table-of-contents">:arrow_up: Back to top</a>\n '.format(codeParts=codeParts) + '\n'
toAppend += codeParts[0] + '```py{codeParts[1]} \n ```'.format(codeParts= codeParts) +codeParts[2] + '<details><summary>View Examples</summary>\n\n```py\n{codeParts[3]}\n```\n</details>\n\n<br><a href = "#table-of-contents">:arrow_up: Back to top</a>\n '.format(codeParts=codeParts) + '\n' open("README.md",'w').write(start+toAppend+'\n'+end)'''
open("README.md",'w').write(start+toAppend+'\n'+end)

50
scripts/util.py Normal file
View File

@ -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

View File

@ -18,7 +18,7 @@ def spread(arg):
def deep_flatten(arr): def deep_flatten(arr):
result = [] result = []
result.extend( 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 return result
``` ```

View File

@ -25,7 +25,7 @@ def lcm(*args):
numbers.extend(spread(list(args))) numbers.extend(spread(list(args)))
def _gcd(x, y): 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): def _lcm(x, y):
return x * y / _gcd(x, y) return x * y / _gcd(x, y)

View File

@ -7,3 +7,5 @@
>Python implementation of 30-seconds-of-code. >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/). **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

View File

@ -22,4 +22,5 @@ is_upper_case:string
is_lower_case:string is_lower_case:string
count_by:list count_by:list
difference_by:list difference_by:list
insertion_sort:list insertion_sort:list

View File

@ -1,2 +1,2 @@
def average(*args): def average(*args):
return sum(args, 0.0) / len(args) return sum(args, 0.0) / len(args)

View File

@ -1,2 +1,2 @@
def byte_size(string): def byte_size(string):
return(len(string.encode('utf-8'))) return(len(string.encode('utf-8')))

View File

@ -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:]) return string[:1].upper() + (string[1:].lower() if lower_rest else string[1:])

View File

@ -1,2 +1,2 @@
def capitalize_every_word(string): def capitalize_every_word(string):
return string.title() return string.title()

View File

@ -1,7 +1,7 @@
from math import ceil from math import ceil
def chunk(arr, size): def chunk(arr, size):
return list( return list(
map(lambda x: arr[x * size:x * size + size], map(lambda x: arr[x * size:x * size + size],
list(range(0, ceil(len(arr) / size))))) list(range(0, ceil(len(arr) / size)))))

View File

@ -1,2 +1,2 @@
def compact(arr): def compact(arr):
return list(filter(lambda x: bool(x), arr)) return list(filter(lambda x: bool(x), arr))

View File

@ -1,6 +1,6 @@
def count_by(arr, fn=lambda x: x): def count_by(arr, fn=lambda x: x):
key = {} key = {}
for el in map(fn, arr): for el in map(fn, arr):
key[el] = 0 if not el in key else key[el] key[el] = 0 if not el in key else key[el]
key[el] += 1 key[el] += 1
return key return key

View File

@ -1,4 +1,4 @@
def count_occurences(arr, val): def count_occurences(arr, val):
return reduce( return reduce(
(lambda x, y: x + 1 if y == val and type(y) == type(val) else x + 0), (lambda x, y: x + 1 if y == val and type(y) == type(val) else x + 0),
arr) arr)

View File

@ -1,5 +1,5 @@
import re import re
def count_vowels(str): def count_vowels(str):
return len(len(re.findall(r'[aeiou]', str, re.IGNORECASE))) return len(len(re.findall(r'[aeiou]', str, re.IGNORECASE)))

View File

@ -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:]) return str[:1].lower() + (str[1:].upper() if upper_rest else str[1:])

View File

@ -1,15 +1,15 @@
def spread(arg): def spread(arg):
ret = [] ret = []
for i in arg: for i in arg:
if isinstance(i, list): if isinstance(i, list):
ret.extend(i) ret.extend(i)
else: else:
ret.append(i) ret.append(i)
return ret return ret
def deep_flatten(arr): def deep_flatten(arr):
result = [] result = []
result.extend( 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 return result

View File

@ -1,3 +1,3 @@
def difference(a, b): def difference(a, b):
b = set(b) b = set(b)
return [item for item in a if item not in b] return [item for item in a if item not in b]

View File

@ -1,3 +1,3 @@
def difference_by(a, b, fn): def difference_by(a, b, fn):
b = set(map(fn, b)) b = set(map(fn, b))
return [item for item in a if fn(item) not in b] return [item for item in a if fn(item) not in b]

View File

@ -1,5 +1,5 @@
def factorial(num): def factorial(num):
if not ((num >= 0) & (num % 1 == 0)): if not ((num >= 0) & (num % 1 == 0)):
raise Exception( raise Exception(
f"Number( {num} ) can't be floating point or negative ") f"Number( {num} ) can't be floating point or negative ")
return 1 if num == 0 else num * factorial(num - 1) return 1 if num == 0 else num * factorial(num - 1)

View File

@ -1,21 +1,21 @@
from functools import reduce from functools import reduce
def spread(arg): def spread(arg):
ret = [] ret = []
for i in arg: for i in arg:
if isinstance(i, list): if isinstance(i, list):
ret.extend(i) ret.extend(i)
else: else:
ret.append(i) ret.append(i)
return ret return ret
def gcd(*args): def gcd(*args):
numbers = [] numbers = []
numbers.extend(spread(list(args))) numbers.extend(spread(list(args)))
def _gcd(x, y): def _gcd(x, y):
return x if not y else gcd(y, x % y) return x if not y else gcd(y, x % y)
return reduce((lambda x, y: _gcd(x, y)), numbers) return reduce((lambda x, y: _gcd(x, y)), numbers)

View File

@ -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

View File

@ -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)

View File

@ -1,2 +1,2 @@
def is_lower_case(str): def is_lower_case(str):
return str == str.lower() return str == str.lower()

View File

@ -1,2 +1,2 @@
def is_upper_case(str): def is_upper_case(str):
return str == str.upper() return str == str.upper()

View File

@ -1,24 +1,24 @@
from functools import reduce from functools import reduce
def spread(arg): def spread(arg):
ret = [] ret = []
for i in arg: for i in arg:
if isinstance(i, list): if isinstance(i, list):
ret.extend(i) ret.extend(i)
else: else:
ret.append(i) ret.append(i)
return ret return ret
def lcm(*args): def lcm(*args):
numbers = [] numbers = []
numbers.extend(spread(list(args))) numbers.extend(spread(list(args)))
def _gcd(x, y): 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): def _lcm(x, y):
return x * y / _gcd(x, y) return x * y / _gcd(x, y)
return reduce((lambda x, y: _lcm(x, y)), numbers) return reduce((lambda x, y: _lcm(x, y)), numbers)

View File

@ -1,8 +1,8 @@
from copy import deepcopy from copy import deepcopy
def max_n(arr, n=1): def max_n(arr, n=1):
numbers = deepcopy(arr) numbers = deepcopy(arr)
numbers.sort() numbers.sort()
numbers.reverse() numbers.reverse()
return numbers[:n] return numbers[:n]

View File

@ -1,7 +1,7 @@
from copy import deepcopy from copy import deepcopy
def min_n(arr, n=1): def min_n(arr, n=1):
numbers = deepcopy(arr) numbers = deepcopy(arr)
numbers.sort() numbers.sort()
return numbers[:n] return numbers[:n]

View File

@ -1,4 +1,4 @@
def palindrome(string): def palindrome(string):
from re import sub from re import sub
s = sub('[\W_]', '', string.lower()) s = sub('[\W_]', '', string.lower())
return s == s[::-1] return s == s[::-1]

View File

@ -1,12 +1,12 @@
from copy import deepcopy from copy import deepcopy
from random import randint from random import randint
def shuffle(arr): def shuffle(arr):
temp_arr = deepcopy(arr) temp_arr = deepcopy(arr)
m = len(temp_arr) m = len(temp_arr)
while (m): while (m):
m -= 1 m -= 1
i = randint(0, m) i = randint(0, m)
temp_arr[m], temp_arr[i] = temp_arr[i], temp_arr[m] temp_arr[m], temp_arr[i] = temp_arr[i], temp_arr[m]
return temp_arr return temp_arr

View File

@ -1,8 +1,8 @@
def spread(arg): def spread(arg):
ret = [] ret = []
for i in arg: for i in arg:
if isinstance(i, list): if isinstance(i, list):
ret.extend(i) ret.extend(i)
else: else:
ret.append(i) ret.append(i)
return ret return ret

View File

@ -1,8 +1,8 @@
def zip(*args, fillvalue=None): def zip(*args, fillvalue=None):
max_length = max([len(arr) for arr in args]) max_length = max([len(arr) for arr in args])
result = [] result = []
for i in range(max_length): for i in range(max_length):
result.append([ result.append([
args[k][i] if i < len(args[k]) else None for k in range(len(args)) args[k][i] if i < len(args[k]) else None for k in range(len(args))
]) ])
return result return result