-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild_readme.py
98 lines (80 loc) · 2.92 KB
/
build_readme.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/Users/juan/miniconda3/bin/python3
import os
from datetime import datetime
## FUNCTIONS
def display(path, root):
print((len(path) - 2) * '---', os.path.basename(root), f'[directory]')
def intersection(lst1, lst2):
return list(set(lst1) & set(lst2))
def count_files(files:list)->int:
return len([file for file in files if 'py' in file or '.ipynb' in file])
def write_readme_file(file_output:str, records:list):
with open(file_output, 'w') as outfile:
# loop of records
for r in records:
outfile.write(f'{r}\n')
## MAIN FUNCTION
def main(file_output:str):
# initialize
omossions_root = ['.', '.ipynb_checkpoints']
omossions_in_path = ['.git', '.img']
records = list()
stab = ' '
url_root = "https://github.com/jmquintana79/utilsDS/blob/master"
# header
records.append('# Tools for a Data Science projects')
# introduction
records.append("This repository is a compendium of notebooks and scripts to be used in my daily work for Data Science projects. I will try to use this landscape as reference:")
# add picture
records.append('<img align="center" width="100%" src=".img/DS_picture.jpg">')
# add content title
records.append('## Content')
# traverse root directory, and list directories as dirs and files as files
for root, dirs, files in os.walk("."):
# get path (splitted root)
path = root.split(os.sep)
# build name
name = os.path.basename(root)
if name == 'nlp' or name == 'ADA' or name == 'gam' or name == 'EDA' or name == 'KDE':
s_name = name.upper()
else:
s_name = f"{name.replace('_', ' ').capitalize() }"
# build counter files
nfiles = count_files(files)
s_nfiles = f'*[{count_files(files)}]*'
# number of tabs
ntabs = len(path)-2
if ntabs >= 2:
ntabs = 2**(ntabs - 1)
# build url
url = os.path.join(url_root, root[2:])
# build name with url
s_name_url = f'[{s_name}]({url})'
# omit folders in path
if len(intersection(path, omossions_in_path)) > 0:
pass
else:
# omit folders
if not os.path.basename(root) in omossions_root:
# display
display(path, root)
# add sub-headers
if ntabs == 0:
records.append(f'### {s_name_url}')
elif ntabs == 1:
records.append(f'- {s_name_url} *[{nfiles}]*')
else:
records.append(f'{ntabs*stab}- {s_name_url} {s_nfiles}')
# foot
s_foot = f'> Updated on {datetime.now().strftime("%Y-%m-%d %H:%M:%S")}'
records.append(s_foot)
## WRITE FILE
write_readme_file(file_output, records)
## MAIN ENV
if __name__ == '__main__':
# output file
file_output = 'README.md'
# launching
main(file_output)
# end
quit('done')