mirror of
https://github.com/vinta/awesome-python.git
synced 2025-01-18 07:16:59 +00:00
Updated sort.py
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.
This commit is contained in:
parent
2252650cfd
commit
0d31d6b5ff
117
sort.py
117
sort.py
|
@ -1,83 +1,54 @@
|
||||||
#!/usr/bin/env python
|
def read_read_me():
|
||||||
# coding: utf-8
|
# read file README.md en modo lectura
|
||||||
|
|
||||||
"""
|
with open("README.md", "r") as read_me_file:
|
||||||
The approach taken is explained below. I decided to do it simply.
|
read_me = read_me_file.readlines() # Leer todas las líneas
|
||||||
Initially I was considering parsing the data into some sort of
|
|
||||||
structure and then generating an appropriate README. I am still
|
|
||||||
considering doing it - but for now this should work. The only issue
|
|
||||||
I see is that it only sorts the entries at the lowest level, and that
|
|
||||||
the order of the top-level contents do not match the order of the actual
|
|
||||||
entries.
|
|
||||||
|
|
||||||
This could be extended by having nested blocks, sorting them recursively
|
|
||||||
and flattening the end structure into a list of lines. Revision 2 maybe ^.^.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def sort_blocks():
|
|
||||||
# First, we load the current README into memory
|
|
||||||
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
|
|
||||||
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 = []
|
blocks = []
|
||||||
last_indent = None
|
current_block = []
|
||||||
|
|
||||||
|
# Detecting list items by starting character
|
||||||
for line in read_me:
|
for line in read_me:
|
||||||
s_line = line.lstrip()
|
stripped_line = line.lstrip()
|
||||||
indent = len(line) - len(s_line)
|
|
||||||
|
# check is the line is part of the list then add it to a block
|
||||||
if any([s_line.startswith(s) for s in ['* [', '- [']]):
|
if stripped_line.startswith(('* [')):
|
||||||
if indent == last_indent:
|
if current_block:
|
||||||
blocks[-1].append(line)
|
blocks.append(current_block)
|
||||||
else:
|
current_block = [line]
|
||||||
blocks.append([line])
|
|
||||||
last_indent = indent
|
|
||||||
else:
|
else:
|
||||||
|
if current_block:
|
||||||
|
blocks.append(current_block)
|
||||||
|
current_block = []
|
||||||
blocks.append([line])
|
blocks.append([line])
|
||||||
last_indent = None
|
|
||||||
|
|
||||||
with open('README.md', 'w+') as sorted_file:
|
if current_block:
|
||||||
# Then all of the blocks are sorted individually
|
blocks.append(current_block)
|
||||||
blocks = [
|
|
||||||
''.join(sorted(block, key=str.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
|
# show block before sorting
|
||||||
sort_blocks()
|
print("before sorted block:")
|
||||||
|
for block in blocks:
|
||||||
|
print(block)
|
||||||
|
|
||||||
|
# sort each block individually
|
||||||
|
sorted_blocks = []
|
||||||
|
for block in blocks:
|
||||||
|
# Solo ordenar bloques que comiencen con '* [' o '- ['
|
||||||
|
if block and isinstance(block, str) and block.lstrip().startswith(('* [')):
|
||||||
|
sorted_block = sorted(block, key = lambda x: x.lower())
|
||||||
|
sorted_block.append(sorted_blocks)
|
||||||
|
else:
|
||||||
|
sorted_blocks.append(block)
|
||||||
|
|
||||||
|
# Show sorted block
|
||||||
|
print("after sorted block:")
|
||||||
|
for block in sorted_blocks:
|
||||||
|
print(block)
|
||||||
|
|
||||||
|
# Write sorted blocks in README.md
|
||||||
|
with open("README.md", "w+") as read_me_file:
|
||||||
|
for block in sorted_blocks:
|
||||||
|
read_me_file.write(''.join(block))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
read_read_me()
|
||||||
main()
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user