Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import zipfile, csv
- source = 'WCA_export196_20131111.tsv.zip'
- countryId = 'Germany'
- upto_nemeses = 10
- names, ranks = {}, {}
- with zipfile.ZipFile(source) as zf:
- with zf.open('WCA_export_Persons.tsv') as pf:
- for row in csv.DictReader(pf, delimiter='\t'):
- if row['subid'] == '1' and row['countryId'] == countryId:
- names[row['id']] = row['name'].decode('utf-8')
- for kind in ('Single', 'Average'):
- with zf.open('WCA_export_Ranks'+kind+'.tsv') as rf:
- for row in csv.DictReader(rf, delimiter='\t'):
- if row['personId'] in names:
- ranks.setdefault(row['personId'], {})[kind+row['eventId']] = int(row['worldRank'])
- def is_nemesis(badguy, victim):
- badguy_ranks = ranks[badguy]
- for event, rank in ranks[victim].items():
- if event not in badguy_ranks or badguy_ranks[event] >= rank:
- return False
- return True
- people = sorted(ranks, key=lambda p: (-len(ranks[p]), names[p]))
- nemeses = {p:[] for p in people}
- for p in people[:]:
- if p in people:
- for q in people:
- if is_nemesis(p, q):
- nemeses[q].append(names[p])
- people = [q for q in people if len(nemeses[q]) <= upto_nemeses]
- out = []
- for nc in range(upto_nemeses+1):
- foo = [p for p in people if len(nemeses[p]) == nc]
- out.append('[spoiler="{} Cuber with {} Nemeses"][code]'.format(len(foo), nc))
- out.append('{:25} {:6} Nemeses\n-------------------------------------------------'.format('Cuber', 'Events'))
- for p in foo:
- cells = (names[p], len(ranks[p]), ', '.join(sorted(nemeses[p])))
- out.append(u'{:25} {:4} {}'.format(*cells))
- out.append('[/code][/spoiler]')
- with open('few_nemeses_one_country.txt', 'w') as f:
- f.write('\n'.join(out).encode('utf-8'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement