Skip to content

Commit c8856ee

Browse files
author
Andreas Kull
committed
Added the script for sorting
Please note that this comes from https://github.com/vinta/awesome-python/.
1 parent bb140a6 commit c8856ee

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

.scripts/sort.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# coding: utf-8
2+
3+
"""
4+
The approach taken is explained below. I decided to do it simply.
5+
Initially I was considering parsing the data into some sort of
6+
structure and then generating an appropriate README. I am still
7+
considering doing it - but for now this should work. The only issue
8+
I see is that it only sorts the entries at the lowest level, and that
9+
the order of the top-level contents do not match the order of the actual
10+
entries.
11+
12+
This could be extended by having nested blocks, sorting them recursively
13+
and flattening the end structure into a list of lines. Revision 2 maybe ^.^.
14+
"""
15+
16+
17+
def main():
18+
# First, we load the current README into memory as an array of lines
19+
with open('README.md', 'r') as read_me_file:
20+
read_me = read_me_file.readlines()
21+
22+
# Then we cluster the lines together as blocks
23+
# Each block represents a collection of lines that should be sorted
24+
# This was done by assuming only links ([...](...)) are meant to be sorted
25+
# Clustering is done by indentation
26+
blocks = []
27+
last_indent = None
28+
for line in read_me:
29+
s_line = line.lstrip()
30+
indent = len(line) - len(s_line)
31+
32+
if any([s_line.startswith(s) for s in ['* [', '- [']]):
33+
if indent == last_indent:
34+
blocks[-1].append(line)
35+
else:
36+
blocks.append([line])
37+
last_indent = indent
38+
else:
39+
blocks.append([line])
40+
last_indent = None
41+
42+
with open('README.md', 'w+') as sorted_file:
43+
# Then all of the blocks are sorted individually
44+
blocks = [''.join(sorted(block, key=lambda s: s.lower())) for block in blocks]
45+
# And the result is written back to README.md
46+
sorted_file.write(''.join(blocks))
47+
48+
49+
if __name__ == "__main__":
50+
main()

0 commit comments

Comments
 (0)