同步操作将从 awesome-lib/awesome-python 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
#!/usr/bin/env python# 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 ofstructure and then generating an appropriate README. I am stillconsidering doing it - but for now this should work. The only issueI see is that it only sorts the entries at the lowest level, and thatthe order of the top-level contents do not match the order of the actualentries.This could be extended by having nested blocks, sorting them recursivelyand flattening the end structure into a list of lines. Revision 2 maybe ^.^."""def sort_blocks():# First, we load the current README into memorywith 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 librariesinner_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 fileblocks[0] = inner_blocksfinal_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 lineswith 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 indentationblocks = []last_indent = Nonefor 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 = indentelse:blocks.append([line])last_indent = Nonewith open('README.md', 'w+') as sorted_file:# Then all of the blocks are sorted individuallyblocks = [''.join(sorted(block, key=str.lower)) for block in blocks]# And the result is written back to README.mdsorted_file.write(''.join(blocks))# Then we call the sorting methodsort_blocks()if __name__ == "__main__":main()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。