-
Notifications
You must be signed in to change notification settings - Fork 25.1k
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
Updated sort.py #2613
base: master
Are you sure you want to change the base?
Updated sort.py #2613
Conversation
In the last code of sort.py a 'w+' is missing when writing the sorted lists. So I have add it to be in the side of Python 3.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#!/usr/bin/env python
coding: utf-8
def read_read_me():
"""Reads the README.md file."""
with open("README.md", "r") as read_me_file:
read_me = read_me_file.readlines()
return read_me
def sort_blocks():
"""Sorts the blocks in the README.md file."""
with open('README.md', 'r') as read_me_file:
read_me = read_me_file.read()
table_of_contents, blocks = read_me.split('- - -', 1)
blocks = blocks.split('\n# ')
for i in range(len(blocks)):
if i == 0:
blocks[i] += '\n'
else:
blocks[i] = '# ' + blocks[i] + '\n'
# Sort inner blocks
inner_blocks = sorted(blocks[0].split('##'))
blocks[0] = '##'.join(inner_blocks)
# Write sorted content back to the file
final_read_me = table_of_contents + '- - -' + ''.join(blocks)
with open('README.md', 'w') as sorted_file:
sorted_file.write(final_read_me)
def main():
"""Main function to cluster, sort, and rewrite README.md."""
read_me = read_read_me()
blocks = []
current_block = []
for line in read_me:
stripped_line = line.lstrip()
if stripped_line.startswith(('* [', '- [')):
current_block.append(line)
else:
if current_block:
blocks.append(current_block)
current_block = []
blocks.append([line])
if current_block:
blocks.append(current_block)
# Sort each block that starts with '* [' or '- ['
sorted_blocks = []
for block in blocks:
if block and isinstance(block, list) and block[0].lstrip().startswith(('* [', '- [')):
sorted_block = sorted(block, key=lambda x: x.lower())
sorted_blocks.append(sorted_block)
else:
sorted_blocks.append(block)
# Flatten the sorted blocks for writing
flat_sorted_blocks = [''.join(block) for block in sorted_blocks]
with open("README.md", "w") as read_me_file:
read_me_file.write(''.join(flat_sorted_blocks))
sort_blocks()
if name == "main":
main()
|
In the last code of sort.py a 'w+' is missing when writing the sorted lists. So I have add it to be in the side of Python 3.
What is this Python project?
Describe features.
What's the difference between this Python project and similar ones?
Enumerate comparisons.
--
Anyone who agrees with this pull request could submit an Approve review to it.