-
Notifications
You must be signed in to change notification settings - Fork 1
/
reconstruct-latest.py
executable file
·62 lines (44 loc) · 1.57 KB
/
reconstruct-latest.py
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
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python3
import os
import os.path
import random
import itertools
from datetime import date, datetime, timedelta
# A pair of names: order doesn't matter; define a custom class so we
# can use these as keys in maps.
class Pair:
def __init__(self, n1, n2): self.p = (min(n1, n2), max(n1, n2))
def __getitem__(self, i): return self.p[i]
def __eq__(self, other): return self.p == other.p
def __hash__(self): return self.p.__hash__()
def __repr__(self): return '({}, {})'.format(self.p[0], self.p[1])
def other(self, n):
if self.p[0] == n:
return self.p[1]
elif self.p[1] == n:
return self.p[0]
else:
return None
# Read "latest call" map between pairs and dates.
def reconstruct_latest(names):
latest = {}
weeks = [os.path.splitext(f)[0] for f in sorted(os.listdir('weeks'))]
for w in weeks:
yr, mon, day = w.split('-')
d = date(int(yr), int(mon), int(day))
for l in open(os.path.join('weeks', w + '.csv')):
n1, n2 = l.replace("\n", '').split(',')
latest[Pair(n1, n2)] = d
return latest
# Write "latest call" map between pairs and dates.
def write_latest(latest):
with open('reconstructed-latest.txt', 'w') as fp:
for k, v in latest.items():
print('{},{},{}'.format(k[0], k[1], v.strftime('%Y-%m-%d')),
file=fp)
def main():
names = [n.replace("\n", '') for n in open('names.txt')]
latest = reconstruct_latest(names)
write_latest(latest)
if __name__ == '__main__':
main()