-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
88 lines (75 loc) · 3.04 KB
/
utils.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import csv
from datetime import datetime
ESTIMATED_JOURNEY_DURATION = {
'Hammersmith (District, Piccadilly lines)': {
'Hatton Cross': 27,
'Barons Court': 1,
'West Kensington': 3
}
}
MINIMUM_DELAY_REFUND = 15
# CSV Keys
KEY_DATE = "Date"
KEY_START_TIME = "Start Time"
KEY_END_TIME = "End Time"
KEY_JOURNEY = "Journey/Action"
# TODO:
# - handle the no touch-out case
def parse_date(string_day, string_time):
return datetime.strptime("{} {}".format(string_day, string_time), '%d-%b-%Y %H:%M')
def is_delayed_journey(station_a, station_b, duration_dt):
is_delayed = False
effective_duration_minutes = duration_dt.seconds / 60
theoric_duration = None
if station_a in ESTIMATED_JOURNEY_DURATION:
tmp = ESTIMATED_JOURNEY_DURATION[station_a]
if station_b in tmp:
theoric_duration = tmp[station_b]
if not theoric_duration:
if station_b in ESTIMATED_JOURNEY_DURATION:
tmp = ESTIMATED_JOURNEY_DURATION[station_b]
if station_a in tmp:
theoric_duration = tmp[station_a]
if not theoric_duration:
print("Could not find theoric duration from {} to {}".format(station_a, station_b))
else:
# print("Estimated duration: {}".format(theoric_duration))
delay = effective_duration_minutes - theoric_duration
is_delayed = delay >= MINIMUM_DELAY_REFUND
if is_delayed:
print("DELAY! Estimated: {}, effective: {}. Delay {} minutes".format(theoric_duration, effective_duration_minutes, delay))
return is_delayed
def is_tube_journey(str_journey):
BUS_STRING_PATTERN = "Bus journey, route "
REFUND_PATTERN = "Oyster helpline refund"
return not (str_journey.startswith(BUS_STRING_PATTERN) or str_journey.startswith(REFUND_PATTERN))
def extract_departure_arrival(journey_str):
JOURNEY_SEPARATOR = " to "
departure_station, arrival_station = journey_str.split(JOURNEY_SEPARATOR)
return departure_station, arrival_station
def parse_csv(filename):
with open(filename) as csvfile:
next(csvfile)
csvfile
reader = csv.DictReader(csvfile)
for row in reader:
if is_tube_journey(row[KEY_JOURNEY]):
start_date = row[KEY_DATE]
start_dt = parse_date(start_date, row[KEY_START_TIME])
end_dt = parse_date(start_date, row[KEY_END_TIME])
if end_dt < start_dt:
# case arrival on the next day
print("TODO handle the case")
duration = end_dt - start_dt
delayed_journeys += 1
departure_station, arrival_station = extract_departure_arrival(row[KEY_JOURNEY])
# print(row)
if is_delayed_journey(departure_station, arrival_station, duration):
# print("Delayed!")
pass
else:
# print("OK")
pass
if __name__ == '__main__':
FILENAME = "test-data/input.csv"
parse_csv(FILENAME)