Skip to content

Rewrite build_directory_md.py #1076

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ script:
--ignore=machine_learning/random_forest_classification/random_forest_classification.py
--ignore=machine_learning/random_forest_regression/random_forest_regression.py
after_success:
- python scripts/build_directory_md.py
- scripts/build_directory_md.py > DIRECTORY.md
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this line mean travis will write to Directory on successful builds?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes but... It is only writing in its local directory. This does not commit those changes back into the repo. I could investigate how to do the latter on branch==master if that would be of interest.

- cat DIRECTORY.md
23 changes: 13 additions & 10 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## Arithmetic Analysis
* [bisection](https://github.com/TheAlgorithms/Python/blob/master/arithmetic_analysis/bisection.py)
* [in static equilibrium](https://github.com/TheAlgorithms/Python/blob/master/arithmetic_analysis/in_static_equilibrium.py)
* [intersection](https://github.com/TheAlgorithms/Python/blob/master/arithmetic_analysis/intersection.py)
* [lu decomposition](https://github.com/TheAlgorithms/Python/blob/master/arithmetic_analysis/lu_decomposition.py)
* [newton method](https://github.com/TheAlgorithms/Python/blob/master/arithmetic_analysis/newton_method.py)
Expand Down Expand Up @@ -42,7 +43,6 @@
* [burrows wheeler](https://github.com/TheAlgorithms/Python/blob/master/compression/burrows_wheeler.py)
* [huffman](https://github.com/TheAlgorithms/Python/blob/master/compression/huffman.py)
* [peak signal to noise ratio](https://github.com/TheAlgorithms/Python/blob/master/compression/peak_signal_to_noise_ratio.py)
* Image Data
## Conversions
* [decimal to binary](https://github.com/TheAlgorithms/Python/blob/master/conversions/decimal_to_binary.py)
* [decimal to hexadecimal](https://github.com/TheAlgorithms/Python/blob/master/conversions/decimal_to_hexadecimal.py)
Expand All @@ -62,9 +62,9 @@
* [double hash](https://github.com/TheAlgorithms/Python/blob/master/data_structures/hashing/double_hash.py)
* [hash table](https://github.com/TheAlgorithms/Python/blob/master/data_structures/hashing/hash_table.py)
* [hash table with linked list](https://github.com/TheAlgorithms/Python/blob/master/data_structures/hashing/hash_table_with_linked_list.py)
* [quadratic probing](https://github.com/TheAlgorithms/Python/blob/master/data_structures/hashing/quadratic_probing.py)
* Number Theory
* [prime numbers](https://github.com/TheAlgorithms/Python/blob/master/data_structures/hashing/number_theory/prime_numbers.py)
* [quadratic probing](https://github.com/TheAlgorithms/Python/blob/master/data_structures/hashing/quadratic_probing.py)
* Heap
* [heap](https://github.com/TheAlgorithms/Python/blob/master/data_structures/heap/heap.py)
* Linked List
Expand All @@ -87,14 +87,14 @@
* Trie
* [trie](https://github.com/TheAlgorithms/Python/blob/master/data_structures/trie/trie.py)
## Digital Image Processing
* [change contrast](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/change_contrast.py)
* Edge Detection
* [canny](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/edge_detection/canny.py)
* Filters
* [convolve](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/filters/convolve.py)
* [gaussian filter](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/filters/gaussian_filter.py)
* [median filter](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/filters/median_filter.py)
* [sobel filter](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/filters/sobel_filter.py)
* Image Data
## Divide And Conquer
* [closest pair of points](https://github.com/TheAlgorithms/Python/blob/master/divide_and_conquer/closest_pair_of_points.py)
* [max subarray sum](https://github.com/TheAlgorithms/Python/blob/master/divide_and_conquer/max_subarray_sum.py)
Expand Down Expand Up @@ -167,24 +167,22 @@
* [lib](https://github.com/TheAlgorithms/Python/blob/master/linear_algebra_python/src/lib.py)
* [tests](https://github.com/TheAlgorithms/Python/blob/master/linear_algebra_python/src/tests.py)
## Machine Learning
* [NaiveBayes](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/NaiveBayes.ipynb)
* [decision tree](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/decision_tree.py)
* [gradient descent](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/gradient_descent.py)
* [k means clust](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/k_means_clust.py)
* [knn sklearn](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/knn_sklearn.py)
* [linear regression](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/linear_regression.py)
* [logistic regression](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/logistic_regression.py)
* [NaiveBayes](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/NaiveBayes.ipynb)
* [perceptron](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/perceptron.py)
* [reuters one vs rest classifier](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/reuters_one_vs_rest_classifier.ipynb)
* [scoring functions](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/scoring_functions.py)
* Random Forest Classification
* [random forest classification](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/random_forest_classification/random_forest_classification.py)
* [random forest classifier](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/random_forest_classification/random_forest_classifier.ipynb)
* [Social Network Ads](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/random_forest_classification/Social_Network_Ads.csv)
* Random Forest Regression
* [Position Salaries](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/random_forest_regression/Position_Salaries.csv)
* [random forest regression](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/random_forest_regression/random_forest_regression.ipynb)
* [random forest regression](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/random_forest_regression/random_forest_regression.py)
* [reuters one vs rest classifier](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/reuters_one_vs_rest_classifier.ipynb)
* [scoring functions](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/scoring_functions.py)
## Maths
* [3n+1](https://github.com/TheAlgorithms/Python/blob/master/maths/3n+1.py)
* [abs](https://github.com/TheAlgorithms/Python/blob/master/maths/abs.py)
Expand All @@ -203,11 +201,15 @@
* [find lcm](https://github.com/TheAlgorithms/Python/blob/master/maths/find_lcm.py)
* [find max](https://github.com/TheAlgorithms/Python/blob/master/maths/find_max.py)
* [find min](https://github.com/TheAlgorithms/Python/blob/master/maths/find_min.py)
* [gaussian](https://github.com/TheAlgorithms/Python/blob/master/maths/gaussian.py)
* [greater common divisor](https://github.com/TheAlgorithms/Python/blob/master/maths/greater_common_divisor.py)
* [is square free](https://github.com/TheAlgorithms/Python/blob/master/maths/is_square_free.py)
* [lucas series](https://github.com/TheAlgorithms/Python/blob/master/maths/lucas_series.py)
* [mobius function](https://github.com/TheAlgorithms/Python/blob/master/maths/mobius_function.py)
* [modular exponential](https://github.com/TheAlgorithms/Python/blob/master/maths/modular_exponential.py)
* [newton raphson](https://github.com/TheAlgorithms/Python/blob/master/maths/newton_raphson.py)
* [prime check](https://github.com/TheAlgorithms/Python/blob/master/maths/prime_check.py)
* [prime factors](https://github.com/TheAlgorithms/Python/blob/master/maths/prime_factors.py)
* [segmented sieve](https://github.com/TheAlgorithms/Python/blob/master/maths/segmented_sieve.py)
* [sieve of eratosthenes](https://github.com/TheAlgorithms/Python/blob/master/maths/sieve_of_eratosthenes.py)
* [simpson rule](https://github.com/TheAlgorithms/Python/blob/master/maths/simpson_rule.py)
Expand All @@ -219,6 +221,8 @@
* [rotate matrix](https://github.com/TheAlgorithms/Python/blob/master/matrix/rotate_matrix.py)
* [searching in sorted matrix](https://github.com/TheAlgorithms/Python/blob/master/matrix/searching_in_sorted_matrix.py)
* [spiral print](https://github.com/TheAlgorithms/Python/blob/master/matrix/spiral_print.py)
* Tests
* [test matrix operation](https://github.com/TheAlgorithms/Python/blob/master/matrix/tests/test_matrix_operation.py)
## Networking Flow
* [ford fulkerson](https://github.com/TheAlgorithms/Python/blob/master/networking_flow/ford_fulkerson.py)
* [minimum cut](https://github.com/TheAlgorithms/Python/blob/master/networking_flow/minimum_cut.py)
Expand All @@ -228,14 +232,14 @@
* [fully connected neural network](https://github.com/TheAlgorithms/Python/blob/master/neural_network/fully_connected_neural_network.ipynb)
* [perceptron](https://github.com/TheAlgorithms/Python/blob/master/neural_network/perceptron.py)
## Other
* [Food wastage analysis from 1961-2013 (FAO)](https://github.com/TheAlgorithms/Python/blob/master/other/Food%20wastage%20analysis%20from%201961-2013%20(FAO).ipynb)
* [anagrams](https://github.com/TheAlgorithms/Python/blob/master/other/anagrams.py)
* [binary exponentiation](https://github.com/TheAlgorithms/Python/blob/master/other/binary_exponentiation.py)
* [binary exponentiation 2](https://github.com/TheAlgorithms/Python/blob/master/other/binary_exponentiation_2.py)
* [detecting english programmatically](https://github.com/TheAlgorithms/Python/blob/master/other/detecting_english_programmatically.py)
* [euclidean gcd](https://github.com/TheAlgorithms/Python/blob/master/other/euclidean_gcd.py)
* [finding primes](https://github.com/TheAlgorithms/Python/blob/master/other/finding_primes.py)
* [fischer yates shuffle](https://github.com/TheAlgorithms/Python/blob/master/other/fischer_yates_shuffle.py)
* [Food wastage analysis from 1961-2013 (FAO)](https://github.com/TheAlgorithms/Python/blob/master/other/Food%20wastage%20analysis%20from%201961-2013%20(FAO).ipynb)
* [frequency finder](https://github.com/TheAlgorithms/Python/blob/master/other/frequency_finder.py)
* [game of life](https://github.com/TheAlgorithms/Python/blob/master/other/game_of_life.py)
* [linear congruential generator](https://github.com/TheAlgorithms/Python/blob/master/other/linear_congruential_generator.py)
Expand All @@ -247,7 +251,6 @@
* [tower of hanoi](https://github.com/TheAlgorithms/Python/blob/master/other/tower_of_hanoi.py)
* [two sum](https://github.com/TheAlgorithms/Python/blob/master/other/two_sum.py)
* [word patterns](https://github.com/TheAlgorithms/Python/blob/master/other/word_patterns.py)
* [words](https://github.com/TheAlgorithms/Python/blob/master/other/words)
## Project Euler
* Problem 01
* [sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_01/sol1.py)
Expand Down
94 changes: 34 additions & 60 deletions scripts/build_directory_md.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,71 +1,45 @@
"""
This is a simple script that will scan through the current directory
and generate the corresponding DIRECTORY.md file, can also specify
files or folders to be ignored.
"""
#!/usr/bin/env python3

import os
from typing import Iterator

URL_BASE = "https://github.com/TheAlgorithms/Python/blob/master"


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

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

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

def tree(d, ignores, ignores_ext):
return _markdown(d, ignores, ignores_ext, 0)


def _markdown(parent, ignores, ignores_ext, depth):
out = ""
dirs, files = [], []
for i in os.listdir(parent):
full = os.path.join(parent, i)
name, ext = os.path.splitext(i)
if i not in ignores and ext not in ignores_ext:
if os.path.isfile(full):
# generate list
pre = parent.replace("./", "").replace(" ", "%20")
# replace all spaces to safe URL
child = i.replace(" ", "%20")
files.append((pre, child, name))
else:
dirs.append(i)
# Sort files
files.sort(key=lambda e: e[2].lower())
for f in files:
pre, child, name = f
out += " " * depth + "* [" + name.replace("_", " ") + "](" + URL + pre + "/" + child + ")\n"
# Sort directories
dirs.sort()
for i in dirs:
full = os.path.join(parent, i)
i = i.replace("_", " ").title()
if depth == 0:
out += "## " + i + "\n"
else:
out += " " * depth + "* " + i + "\n"
out += _markdown(full, ignores, ignores_ext, depth+1)
return out
def print_path(old_path: str, new_path: str) -> str:
old_parts = old_path.split(os.sep)
for i, new_part in enumerate(new_path.split(os.sep)):
if i + 1 > len(old_parts) or old_parts[i] != new_part:
if new_part:
print(f"{md_prefix(i)} {new_part.replace('_', ' ').title()}")
return new_path


# Specific files or folders with the given names will be ignored
ignores = [".vs",
".gitignore",
".git",
"scripts",
"__init__.py",
"requirements.txt",
".github"
]
# Files with given entensions will be ignored
ignores_ext = [
".md",
".ipynb",
".png",
".jpg",
".yml"
]
def print_directory_md(top_dir: str = ".") -> None:
old_path = ""
for filepath in sorted(good_filepaths()):
filepath, filename = os.path.split(filepath)
if filepath != old_path:
old_path = print_path(old_path, filepath)
indent = (filepath.count(os.sep) + 1) if filepath else 0
url = "/".join((URL_BASE, filepath, filename)).replace(" ", "%20")
filename = os.path.splitext(filename.replace("_", " "))[0]
print(f"{md_prefix(indent)} [{filename}]({url})")


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