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:
Dheeraj 2025-02-16 10:49:58 +05:30
parent 2252650cfd
commit 2ef8d01320

22
sort.py
View File

@ -16,6 +16,13 @@
def sort_blocks():
# 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:
read_me = read_me_file.read()
@ -43,6 +50,19 @@ def sort_blocks():
sorted_file.write(final_README)
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
with open('README.md', 'r') as read_me_file:
read_me = read_me_file.readlines()
@ -77,6 +97,8 @@ def main():
# Then we call the sorting method
sort_blocks()
except Exception as e:
print(f"Error in processing README.md file: {e}")
if __name__ == "__main__":