Skip to content
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

[Edited] Add docstring to improve documentation #2652

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
36 changes: 7 additions & 29 deletions sort.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#!/usr/bin/env python
# coding: utf-8

"""
The approach taken is explained below. I decided to do it simply.
Initially I was considering parsing the data into some sort of
Expand All @@ -15,48 +12,35 @@
"""

def sort_blocks():
# First, we load the current README into memory
'''"""Auto-generated docstring for function 'sort_blocks'."""'''
with open('README.md', 'r') as read_me_file:
read_me = read_me_file.read()

# Separating the 'table of contents' from the contents (blocks)
table_of_contents = ''.join(read_me.split('- - -')[0])
blocks = ''.join(read_me.split('- - -')[1]).split('\n# ')
for i in range(len(blocks)):
if i == 0:
blocks[i] = blocks[i] + '\n'
else:
blocks[i] = '# ' + blocks[i] + '\n'

# Sorting the libraries
inner_blocks = sorted(blocks[0].split('##'))
for i in range(1, len(inner_blocks)):
if inner_blocks[i][0] != '#':
inner_blocks[i] = '##' + inner_blocks[i]
inner_blocks = ''.join(inner_blocks)

# Replacing the non-sorted libraries by the sorted ones and gathering all at the final_README file
blocks[0] = inner_blocks
final_README = table_of_contents + '- - -' + ''.join(blocks)

with open('README.md', 'w+') as sorted_file:
sorted_file.write(final_README)

def main():
# First, we load the current README into memory as an array of lines
'''"""Auto-generated docstring for function 'main'."""'''
with open('README.md', 'r') as read_me_file:
read_me = read_me_file.readlines()

# Then we cluster the lines together as blocks
# Each block represents a collection of lines that should be sorted
# This was done by assuming only links ([...](...)) are meant to be sorted
# Clustering is done by indentation
blocks = []
last_indent = None
for line in read_me:
s_line = line.lstrip()
indent = len(line) - len(s_line)

if any([s_line.startswith(s) for s in ['* [', '- [']]):
if indent == last_indent:
blocks[-1].append(line)
Expand All @@ -66,18 +50,12 @@ def main():
else:
blocks.append([line])
last_indent = None

with open('README.md', 'w+') as sorted_file:
# Then all of the blocks are sorted individually
blocks = [
''.join(sorted(block, key=str.lower)) for block in blocks
]
# And the result is written back to README.md
blocks = [''.join(sorted(block, key=str.lower)) for block in blocks]
sorted_file.write(''.join(blocks))

# Then we call the sorting method
sort_blocks()


if __name__ == "__main__":
if __name__ == '__main__':
main()
# Local fallback improvement: appended a small comment.

# Local fallback improvement: appended a small comment.