Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ordonnancement des citations dans le script #115

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions reorder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""Reordering of sounds.json based on livre/episode data.

usage:
python reorder.py <sounds.json pathname>

This script do not modify given file in-place,
but create the reordered one next to it, with .reordered suffix

"""

import os
import re
import sys
import json

REGEX_INFO = re.compile(r'Livre ([IV]+), ([0-9]+) - (.*)')
LIVRE_NUMBER = {'I': 1, 'II': 2, 'III': 3, 'IV': 4, 'V': 5, 'VI': 6, 'VII': 7}

def rank(citation:dict) -> int:
match = REGEX_INFO.fullmatch(citation['episode'])
assert match, citation['episode']
livre, episode, _ = match.groups(0)
return LIVRE_NUMBER[livre], int(episode)

if __name__ == '__main__':
if len(sys.argv) != 2:
print(__doc__)
exit(1)
with open(sys.argv[1]) as fd:
data = json.load(fd)
data.sort(key=rank)
# data = ''.join(' '*4 + line for line in .splitlines(True)) + '\n'
data = json.dumps(data, indent=' '*4, ensure_ascii=False)
with open(sys.argv[1] + '.reordered', 'w') as fd:
fd.write(data)
Loading