mirror of
https://github.com/vinta/awesome-python.git
synced 2025-01-31 05:33:41 +00:00
Update sort.py
This commit is contained in:
parent
ebab050c3b
commit
53cc1fafd0
38
sort.py
38
sort.py
|
@ -12,9 +12,7 @@
|
||||||
This could be extended by having nested blocks, sorting them recursively
|
This could be extended by having nested blocks, sorting them recursively
|
||||||
and flattening the end structure into a list of lines. Revision 2 maybe ^.^.
|
and flattening the end structure into a list of lines. Revision 2 maybe ^.^.
|
||||||
"""
|
"""
|
||||||
|
def sort_blocks():
|
||||||
|
|
||||||
def main():
|
|
||||||
# First, we load the current README into memory
|
# First, we load the current README into memory
|
||||||
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()
|
||||||
|
@ -42,6 +40,40 @@ def main():
|
||||||
with open('README.md', 'w+') as sorted_file:
|
with open('README.md', 'w+') as sorted_file:
|
||||||
sorted_file.write(final_README)
|
sorted_file.write(final_README)
|
||||||
|
|
||||||
|
def main():
|
||||||
|
# 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()
|
||||||
|
|
||||||
|
# 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)
|
||||||
|
else:
|
||||||
|
blocks.append([line])
|
||||||
|
last_indent = indent
|
||||||
|
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=lambda s: s.lower())) for block in blocks]
|
||||||
|
# And the result is written back to README.md
|
||||||
|
sorted_file.write(''.join(blocks))
|
||||||
|
|
||||||
|
# Then we call the sorting method
|
||||||
|
sort_blocks()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user