Skip to content

Commit c711b20

Browse files
cclaussstokhos
authored andcommitted
Rewrite build_directory_md.py (TheAlgorithms#1076)
* Rewrite build_directory_md.py * Regenerate DIRECTORY.md
1 parent adf1671 commit c711b20

File tree

3 files changed

+48
-71
lines changed

3 files changed

+48
-71
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ script:
1919
--ignore=machine_learning/random_forest_classification/random_forest_classification.py
2020
--ignore=machine_learning/random_forest_regression/random_forest_regression.py
2121
after_success:
22-
- python scripts/build_directory_md.py
22+
- scripts/build_directory_md.py > DIRECTORY.md
2323
- cat DIRECTORY.md

DIRECTORY.md

+13-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## Arithmetic Analysis
22
* [bisection](https://github.com/TheAlgorithms/Python/blob/master/arithmetic_analysis/bisection.py)
3+
* [in static equilibrium](https://github.com/TheAlgorithms/Python/blob/master/arithmetic_analysis/in_static_equilibrium.py)
34
* [intersection](https://github.com/TheAlgorithms/Python/blob/master/arithmetic_analysis/intersection.py)
45
* [lu decomposition](https://github.com/TheAlgorithms/Python/blob/master/arithmetic_analysis/lu_decomposition.py)
56
* [newton method](https://github.com/TheAlgorithms/Python/blob/master/arithmetic_analysis/newton_method.py)
@@ -42,7 +43,6 @@
4243
* [burrows wheeler](https://github.com/TheAlgorithms/Python/blob/master/compression/burrows_wheeler.py)
4344
* [huffman](https://github.com/TheAlgorithms/Python/blob/master/compression/huffman.py)
4445
* [peak signal to noise ratio](https://github.com/TheAlgorithms/Python/blob/master/compression/peak_signal_to_noise_ratio.py)
45-
* Image Data
4646
## Conversions
4747
* [decimal to binary](https://github.com/TheAlgorithms/Python/blob/master/conversions/decimal_to_binary.py)
4848
* [decimal to hexadecimal](https://github.com/TheAlgorithms/Python/blob/master/conversions/decimal_to_hexadecimal.py)
@@ -62,9 +62,9 @@
6262
* [double hash](https://github.com/TheAlgorithms/Python/blob/master/data_structures/hashing/double_hash.py)
6363
* [hash table](https://github.com/TheAlgorithms/Python/blob/master/data_structures/hashing/hash_table.py)
6464
* [hash table with linked list](https://github.com/TheAlgorithms/Python/blob/master/data_structures/hashing/hash_table_with_linked_list.py)
65-
* [quadratic probing](https://github.com/TheAlgorithms/Python/blob/master/data_structures/hashing/quadratic_probing.py)
6665
* Number Theory
6766
* [prime numbers](https://github.com/TheAlgorithms/Python/blob/master/data_structures/hashing/number_theory/prime_numbers.py)
67+
* [quadratic probing](https://github.com/TheAlgorithms/Python/blob/master/data_structures/hashing/quadratic_probing.py)
6868
* Heap
6969
* [heap](https://github.com/TheAlgorithms/Python/blob/master/data_structures/heap/heap.py)
7070
* Linked List
@@ -87,14 +87,14 @@
8787
* Trie
8888
* [trie](https://github.com/TheAlgorithms/Python/blob/master/data_structures/trie/trie.py)
8989
## Digital Image Processing
90+
* [change contrast](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/change_contrast.py)
9091
* Edge Detection
9192
* [canny](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/edge_detection/canny.py)
9293
* Filters
9394
* [convolve](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/filters/convolve.py)
9495
* [gaussian filter](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/filters/gaussian_filter.py)
9596
* [median filter](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/filters/median_filter.py)
9697
* [sobel filter](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/filters/sobel_filter.py)
97-
* Image Data
9898
## Divide And Conquer
9999
* [closest pair of points](https://github.com/TheAlgorithms/Python/blob/master/divide_and_conquer/closest_pair_of_points.py)
100100
* [max subarray sum](https://github.com/TheAlgorithms/Python/blob/master/divide_and_conquer/max_subarray_sum.py)
@@ -167,24 +167,22 @@
167167
* [lib](https://github.com/TheAlgorithms/Python/blob/master/linear_algebra_python/src/lib.py)
168168
* [tests](https://github.com/TheAlgorithms/Python/blob/master/linear_algebra_python/src/tests.py)
169169
## Machine Learning
170+
* [NaiveBayes](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/NaiveBayes.ipynb)
170171
* [decision tree](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/decision_tree.py)
171172
* [gradient descent](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/gradient_descent.py)
172173
* [k means clust](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/k_means_clust.py)
173174
* [knn sklearn](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/knn_sklearn.py)
174175
* [linear regression](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/linear_regression.py)
175176
* [logistic regression](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/logistic_regression.py)
176-
* [NaiveBayes](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/NaiveBayes.ipynb)
177177
* [perceptron](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/perceptron.py)
178-
* [reuters one vs rest classifier](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/reuters_one_vs_rest_classifier.ipynb)
179-
* [scoring functions](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/scoring_functions.py)
180178
* Random Forest Classification
181179
* [random forest classification](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/random_forest_classification/random_forest_classification.py)
182180
* [random forest classifier](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/random_forest_classification/random_forest_classifier.ipynb)
183-
* [Social Network Ads](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/random_forest_classification/Social_Network_Ads.csv)
184181
* Random Forest Regression
185-
* [Position Salaries](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/random_forest_regression/Position_Salaries.csv)
186182
* [random forest regression](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/random_forest_regression/random_forest_regression.ipynb)
187183
* [random forest regression](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/random_forest_regression/random_forest_regression.py)
184+
* [reuters one vs rest classifier](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/reuters_one_vs_rest_classifier.ipynb)
185+
* [scoring functions](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/scoring_functions.py)
188186
## Maths
189187
* [3n+1](https://github.com/TheAlgorithms/Python/blob/master/maths/3n+1.py)
190188
* [abs](https://github.com/TheAlgorithms/Python/blob/master/maths/abs.py)
@@ -203,11 +201,15 @@
203201
* [find lcm](https://github.com/TheAlgorithms/Python/blob/master/maths/find_lcm.py)
204202
* [find max](https://github.com/TheAlgorithms/Python/blob/master/maths/find_max.py)
205203
* [find min](https://github.com/TheAlgorithms/Python/blob/master/maths/find_min.py)
204+
* [gaussian](https://github.com/TheAlgorithms/Python/blob/master/maths/gaussian.py)
206205
* [greater common divisor](https://github.com/TheAlgorithms/Python/blob/master/maths/greater_common_divisor.py)
206+
* [is square free](https://github.com/TheAlgorithms/Python/blob/master/maths/is_square_free.py)
207207
* [lucas series](https://github.com/TheAlgorithms/Python/blob/master/maths/lucas_series.py)
208+
* [mobius function](https://github.com/TheAlgorithms/Python/blob/master/maths/mobius_function.py)
208209
* [modular exponential](https://github.com/TheAlgorithms/Python/blob/master/maths/modular_exponential.py)
209210
* [newton raphson](https://github.com/TheAlgorithms/Python/blob/master/maths/newton_raphson.py)
210211
* [prime check](https://github.com/TheAlgorithms/Python/blob/master/maths/prime_check.py)
212+
* [prime factors](https://github.com/TheAlgorithms/Python/blob/master/maths/prime_factors.py)
211213
* [segmented sieve](https://github.com/TheAlgorithms/Python/blob/master/maths/segmented_sieve.py)
212214
* [sieve of eratosthenes](https://github.com/TheAlgorithms/Python/blob/master/maths/sieve_of_eratosthenes.py)
213215
* [simpson rule](https://github.com/TheAlgorithms/Python/blob/master/maths/simpson_rule.py)
@@ -219,6 +221,8 @@
219221
* [rotate matrix](https://github.com/TheAlgorithms/Python/blob/master/matrix/rotate_matrix.py)
220222
* [searching in sorted matrix](https://github.com/TheAlgorithms/Python/blob/master/matrix/searching_in_sorted_matrix.py)
221223
* [spiral print](https://github.com/TheAlgorithms/Python/blob/master/matrix/spiral_print.py)
224+
* Tests
225+
* [test matrix operation](https://github.com/TheAlgorithms/Python/blob/master/matrix/tests/test_matrix_operation.py)
222226
## Networking Flow
223227
* [ford fulkerson](https://github.com/TheAlgorithms/Python/blob/master/networking_flow/ford_fulkerson.py)
224228
* [minimum cut](https://github.com/TheAlgorithms/Python/blob/master/networking_flow/minimum_cut.py)
@@ -228,14 +232,14 @@
228232
* [fully connected neural network](https://github.com/TheAlgorithms/Python/blob/master/neural_network/fully_connected_neural_network.ipynb)
229233
* [perceptron](https://github.com/TheAlgorithms/Python/blob/master/neural_network/perceptron.py)
230234
## Other
235+
* [Food wastage analysis from 1961-2013 (FAO)](https://github.com/TheAlgorithms/Python/blob/master/other/Food%20wastage%20analysis%20from%201961-2013%20(FAO).ipynb)
231236
* [anagrams](https://github.com/TheAlgorithms/Python/blob/master/other/anagrams.py)
232237
* [binary exponentiation](https://github.com/TheAlgorithms/Python/blob/master/other/binary_exponentiation.py)
233238
* [binary exponentiation 2](https://github.com/TheAlgorithms/Python/blob/master/other/binary_exponentiation_2.py)
234239
* [detecting english programmatically](https://github.com/TheAlgorithms/Python/blob/master/other/detecting_english_programmatically.py)
235240
* [euclidean gcd](https://github.com/TheAlgorithms/Python/blob/master/other/euclidean_gcd.py)
236241
* [finding primes](https://github.com/TheAlgorithms/Python/blob/master/other/finding_primes.py)
237242
* [fischer yates shuffle](https://github.com/TheAlgorithms/Python/blob/master/other/fischer_yates_shuffle.py)
238-
* [Food wastage analysis from 1961-2013 (FAO)](https://github.com/TheAlgorithms/Python/blob/master/other/Food%20wastage%20analysis%20from%201961-2013%20(FAO).ipynb)
239243
* [frequency finder](https://github.com/TheAlgorithms/Python/blob/master/other/frequency_finder.py)
240244
* [game of life](https://github.com/TheAlgorithms/Python/blob/master/other/game_of_life.py)
241245
* [linear congruential generator](https://github.com/TheAlgorithms/Python/blob/master/other/linear_congruential_generator.py)
@@ -247,7 +251,6 @@
247251
* [tower of hanoi](https://github.com/TheAlgorithms/Python/blob/master/other/tower_of_hanoi.py)
248252
* [two sum](https://github.com/TheAlgorithms/Python/blob/master/other/two_sum.py)
249253
* [word patterns](https://github.com/TheAlgorithms/Python/blob/master/other/word_patterns.py)
250-
* [words](https://github.com/TheAlgorithms/Python/blob/master/other/words)
251254
## Project Euler
252255
* Problem 01
253256
* [sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_01/sol1.py)

scripts/build_directory_md.py

100644100755
+34-60
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,45 @@
1-
"""
2-
This is a simple script that will scan through the current directory
3-
and generate the corresponding DIRECTORY.md file, can also specify
4-
files or folders to be ignored.
5-
"""
1+
#!/usr/bin/env python3
2+
63
import os
4+
from typing import Iterator
5+
6+
URL_BASE = "https://github.com/TheAlgorithms/Python/blob/master"
7+
78

9+
def good_filepaths(top_dir: str = ".") -> Iterator[str]:
10+
for dirpath, dirnames, filenames in os.walk(top_dir):
11+
dirnames[:] = [d for d in dirnames if d != "scripts" and d[0] not in "._"]
12+
for filename in filenames:
13+
if filename == "__init__.py":
14+
continue
15+
if os.path.splitext(filename)[1] in (".py", ".ipynb"):
16+
yield os.path.join(dirpath, filename).lstrip("./")
817

9-
# Target URL (master)
10-
URL = "https://github.com/TheAlgorithms/Python/blob/master/"
1118

19+
def md_prefix(i):
20+
return f"{i * ' '}*" if i else "##"
1221

13-
def tree(d, ignores, ignores_ext):
14-
return _markdown(d, ignores, ignores_ext, 0)
15-
1622

17-
def _markdown(parent, ignores, ignores_ext, depth):
18-
out = ""
19-
dirs, files = [], []
20-
for i in os.listdir(parent):
21-
full = os.path.join(parent, i)
22-
name, ext = os.path.splitext(i)
23-
if i not in ignores and ext not in ignores_ext:
24-
if os.path.isfile(full):
25-
# generate list
26-
pre = parent.replace("./", "").replace(" ", "%20")
27-
# replace all spaces to safe URL
28-
child = i.replace(" ", "%20")
29-
files.append((pre, child, name))
30-
else:
31-
dirs.append(i)
32-
# Sort files
33-
files.sort(key=lambda e: e[2].lower())
34-
for f in files:
35-
pre, child, name = f
36-
out += " " * depth + "* [" + name.replace("_", " ") + "](" + URL + pre + "/" + child + ")\n"
37-
# Sort directories
38-
dirs.sort()
39-
for i in dirs:
40-
full = os.path.join(parent, i)
41-
i = i.replace("_", " ").title()
42-
if depth == 0:
43-
out += "## " + i + "\n"
44-
else:
45-
out += " " * depth + "* " + i + "\n"
46-
out += _markdown(full, ignores, ignores_ext, depth+1)
47-
return out
23+
def print_path(old_path: str, new_path: str) -> str:
24+
old_parts = old_path.split(os.sep)
25+
for i, new_part in enumerate(new_path.split(os.sep)):
26+
if i + 1 > len(old_parts) or old_parts[i] != new_part:
27+
if new_part:
28+
print(f"{md_prefix(i)} {new_part.replace('_', ' ').title()}")
29+
return new_path
4830

4931

50-
# Specific files or folders with the given names will be ignored
51-
ignores = [".vs",
52-
".gitignore",
53-
".git",
54-
"scripts",
55-
"__init__.py",
56-
"requirements.txt",
57-
".github"
58-
]
59-
# Files with given entensions will be ignored
60-
ignores_ext = [
61-
".md",
62-
".ipynb",
63-
".png",
64-
".jpg",
65-
".yml"
66-
]
32+
def print_directory_md(top_dir: str = ".") -> None:
33+
old_path = ""
34+
for filepath in sorted(good_filepaths()):
35+
filepath, filename = os.path.split(filepath)
36+
if filepath != old_path:
37+
old_path = print_path(old_path, filepath)
38+
indent = (filepath.count(os.sep) + 1) if filepath else 0
39+
url = "/".join((URL_BASE, filepath, filename)).replace(" ", "%20")
40+
filename = os.path.splitext(filename.replace("_", " "))[0]
41+
print(f"{md_prefix(indent)} [{filename}]({url})")
6742

6843

6944
if __name__ == "__main__":
70-
with open("DIRECTORY.md", "w+") as f:
71-
f.write(tree(".", ignores, ignores_ext))
45+
print_directory_md(".")

0 commit comments

Comments
 (0)