-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathparse-ndf
37 lines (29 loc) · 1.41 KB
/
parse-ndf
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
#!/usr/bin/env python
from datetime import date
import json
import sys
from split.parse import unpack, cnv_full_date, loop, fare_file
from split.utils import ticket_types
lookups = (
('from', 4), ('to', 4), ('route', 5), ('railcard', 3), ('ticket', 3), ('type', 1), ('end_date', 8, cnv_full_date), ('start_date', 8, cnv_full_date), ('quote_date', 8, cnv_full_date), ('suppress', 1), ('adult', 8), ('child', 8), ('restriction', 2), ('composite', 1), ('cross-london', 1), ('private-settlement', 1)
)
def parse_file(file, type):
for row in loop(file):
row = unpack(row, *lookups)
assert row['type'] == type
if row['railcard'].strip(): continue
if row['end_date'] < date.today(): continue
if row['start_date'] > date.today(): continue
if row['ticket'] not in ticket_types: continue
if row['composite'] == 'N': continue
del row['railcard'], row['end_date'], row['quote_date'], row['start_date'], row['composite'], row['child'], row['cross-london'], row['private-settlement'], row['type']
key = '%(from)s-%(to)s-%(route)s-%(ticket)s' % row
yield key, row
# ---
data = {}
for key, row in parse_file(fare_file('NFO'), 'O'):
assert row['suppress'] == 'N'
del row['suppress']
data[key] = row
print("%d rows from NFO file" % len(data), file=sys.stderr)
json.dump(data, open('data/ndf.json', 'w'), separators=(',', ': '), indent=2, sort_keys=True)