Advertisement
Guest User

Cubing Nemeses

a guest
Nov 12th, 2013
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.81 KB | None | 0 0
  1. import zipfile, csv
  2.  
  3. source = 'WCA_export196_20131111.tsv.zip'
  4. countryId = 'Germany'
  5. upto_nemeses = 10
  6.  
  7. names, ranks = {}, {}
  8. with zipfile.ZipFile(source) as zf:
  9.     with zf.open('WCA_export_Persons.tsv') as pf:
  10.         for row in csv.DictReader(pf, delimiter='\t'):
  11.             if row['subid'] == '1' and row['countryId'] == countryId:
  12.                 names[row['id']] = row['name'].decode('utf-8')
  13.     for kind in ('Single', 'Average'):
  14.         with zf.open('WCA_export_Ranks'+kind+'.tsv') as rf:
  15.             for row in csv.DictReader(rf, delimiter='\t'):
  16.                 if row['personId'] in names:
  17.                     ranks.setdefault(row['personId'], {})[kind+row['eventId']] = int(row['worldRank'])
  18.  
  19. def is_nemesis(badguy, victim):
  20.     badguy_ranks = ranks[badguy]
  21.     for event, rank in ranks[victim].items():
  22.         if event not in badguy_ranks or badguy_ranks[event] >= rank:
  23.             return False
  24.     return True
  25.  
  26. people = sorted(ranks, key=lambda p: (-len(ranks[p]), names[p]))
  27. nemeses = {p:[] for p in people}
  28. for p in people[:]:
  29.     if p in people:
  30.         for q in people:
  31.             if is_nemesis(p, q):
  32.                 nemeses[q].append(names[p])
  33.         people = [q for q in people if len(nemeses[q]) <= upto_nemeses]
  34.  
  35. out = []
  36. for nc in range(upto_nemeses+1):
  37.     foo = [p for p in people if len(nemeses[p]) == nc]
  38.     out.append('[spoiler="{} Cuber with {} Nemeses"][code]'.format(len(foo), nc))
  39.     out.append('{:25} {:6} Nemeses\n-------------------------------------------------'.format('Cuber', 'Events'))
  40.     for p in foo:
  41.         cells = (names[p], len(ranks[p]), ', '.join(sorted(nemeses[p])))
  42.         out.append(u'{:25} {:4}   {}'.format(*cells))
  43.     out.append('[/code][/spoiler]')
  44.  
  45. with open('few_nemeses_one_country.txt', 'w') as f:
  46.     f.write('\n'.join(out).encode('utf-8'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement