Uh oh!
There was an error while loading. Please reload this page.
forked from vinta/awesome-python
- Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathsort.py
More file actions
Latest commit
50 lines (41 loc) · 1.78 KB
/
Copy pathsort.py
File metadata and controls
50 lines (41 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# coding: utf-8
"""
The approach taken is explained below. I decided to do it simply.
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 ^.^.
"""
defmain():
# First, we load the current README into memory as an array of lines
withopen('README.md', 'r') asread_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
forlineinread_me:
s_line=line.lstrip()
indent=len(line) -len(s_line)
ifany([s_line.startswith(s) forsin ['* [', '- [']]):
ifindent==last_indent:
blocks[-1].append(line)
else:
blocks.append([line])
last_indent=indent
else:
blocks.append([line])
last_indent=None
withopen('README.md', 'w+') assorted_file:
# Then all of the blocks are sorted individually
blocks= [''.join(sorted(block, key=lambdas: s.lower())) forblockinblocks]
# And the result is written back to README.md
sorted_file.write(''.join(blocks))
if__name__=="__main__":
main()