mirror of
https://github.com/vinta/awesome-python.git
synced 2025-02-22 16:22:03 +00:00
Added docstrings to improve documentation in sort_blocks and in main function and also handeled the exception in the main block
This commit is contained in:
parent
2252650cfd
commit
2ef8d01320
76
sort.py
76
sort.py
|
@ -16,6 +16,13 @@
|
||||||
|
|
||||||
def sort_blocks():
|
def sort_blocks():
|
||||||
# First, we load the current README into memory
|
# First, we load the current README into memory
|
||||||
|
def sort_blocks():
|
||||||
|
"""Sorts and restructures the README file.
|
||||||
|
|
||||||
|
The function reads the file(README.md) and sorts its library entries,
|
||||||
|
alphabetically while maintaining section headers, and writes the sorted,
|
||||||
|
content back to the file."""
|
||||||
|
|
||||||
with open('README.md', 'r') as read_me_file:
|
with open('README.md', 'r') as read_me_file:
|
||||||
read_me = read_me_file.read()
|
read_me = read_me_file.read()
|
||||||
|
|
||||||
|
@ -43,40 +50,55 @@ def sort_blocks():
|
||||||
sorted_file.write(final_README)
|
sorted_file.write(final_README)
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
"""
|
||||||
|
Main function to process and sort blocks in `README.md`.
|
||||||
|
|
||||||
|
This function reads the file, groups list entries into sortable blocks,
|
||||||
|
sorts them alphabetically while preserving indentation and headers,
|
||||||
|
and writes the sorted content back to `README.md`.
|
||||||
|
|
||||||
|
It then calls `sort_blocks()` to further refine the sorting.
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
Exception: If any error occurs while processing the file.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
# First, we load the current README into memory as an array of lines
|
# First, we load the current README into memory as an array of lines
|
||||||
with open('README.md', 'r') as read_me_file:
|
with open('README.md', 'r') as read_me_file:
|
||||||
read_me = read_me_file.readlines()
|
read_me = read_me_file.readlines()
|
||||||
|
|
||||||
# Then we cluster the lines together as blocks
|
# Then we cluster the lines together as blocks
|
||||||
# Each block represents a collection of lines that should be sorted
|
# Each block represents a collection of lines that should be sorted
|
||||||
# This was done by assuming only links ([...](...)) are meant to be sorted
|
# This was done by assuming only links ([...](...)) are meant to be sorted
|
||||||
# Clustering is done by indentation
|
# Clustering is done by indentation
|
||||||
blocks = []
|
blocks = []
|
||||||
last_indent = None
|
last_indent = None
|
||||||
for line in read_me:
|
for line in read_me:
|
||||||
s_line = line.lstrip()
|
s_line = line.lstrip()
|
||||||
indent = len(line) - len(s_line)
|
indent = len(line) - len(s_line)
|
||||||
|
|
||||||
if any([s_line.startswith(s) for s in ['* [', '- [']]):
|
if any([s_line.startswith(s) for s in ['* [', '- [']]):
|
||||||
if indent == last_indent:
|
if indent == last_indent:
|
||||||
blocks[-1].append(line)
|
blocks[-1].append(line)
|
||||||
|
else:
|
||||||
|
blocks.append([line])
|
||||||
|
last_indent = indent
|
||||||
else:
|
else:
|
||||||
blocks.append([line])
|
blocks.append([line])
|
||||||
last_indent = indent
|
last_indent = None
|
||||||
else:
|
|
||||||
blocks.append([line])
|
|
||||||
last_indent = None
|
|
||||||
|
|
||||||
with open('README.md', 'w+') as sorted_file:
|
with open('README.md', 'w+') as sorted_file:
|
||||||
# Then all of the blocks are sorted individually
|
# Then all of the blocks are sorted individually
|
||||||
blocks = [
|
blocks = [
|
||||||
''.join(sorted(block, key=str.lower)) for block in blocks
|
''.join(sorted(block, key=str.lower)) for block in blocks
|
||||||
]
|
]
|
||||||
# And the result is written back to README.md
|
# And the result is written back to README.md
|
||||||
sorted_file.write(''.join(blocks))
|
sorted_file.write(''.join(blocks))
|
||||||
|
|
||||||
# Then we call the sorting method
|
# Then we call the sorting method
|
||||||
sort_blocks()
|
sort_blocks()
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error in processing README.md file: {e}")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
Loading…
Reference in New Issue
Block a user