-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmail_item.py
More file actions
118 lines (96 loc) · 4.7 KB
/
mail_item.py
File metadata and controls
118 lines (96 loc) · 4.7 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import re
from delivery_status import DeliveryStatus
from utils import *
class MailItem():
def __init__(self, id=None, address=None, city=None, state=None, zip=None, deadline=None, weightKILO=None, notes=None):
self.id = int(id)
self.address = address
self.city = city
self.state = state
self.zip = int(zip)
self.deadline = deadline
self.weight = int(weightKILO)
self.notes = notes
self.delivery_status = None
self.delivery_time = None
self.hash = None
self.status_log = []
self.address_log = []
self.address_log.append((0, [self.address, self.city, self.state, self.zip]))
self.required_truck = None
self.shipped_using_truck_id = "Not loaded on truck"
self.co_delivery_restrictions = None
self.delayed_until = None
self.has_incorrect_address = None
self.parse_notes()
if self.delivery_status == None:
self.update_status(DeliveryStatus.AT_HUB)
def __str__(self):
return f"MailItem({self.id}, {self.address}, {self.city}, {self.state}, {self.zip}, {self.deadline}, {self.weight})"
def __repr__(self):
return f"MailItem({self.id}, {self.address}, {self.city}, {self.state}, {self.zip}, {self.deadline}, {self.weight})"
def parse_notes(self):
if self.notes == None:
return
# Example: "Can only be on truck 2"
truck_restriction = re.search(r"truck\s(\d)+", self.notes)
if truck_restriction:
self.required_truck = int(truck_restriction.group(1))
# Example: "Must be delivered with 15, 19"
co_delivery_string = self.notes.split("Must be delivered with ")
if len(co_delivery_string) == 2:
# TODO: rework one-liner to be more readable
self.co_delivery_restrictions = [int(package_ID) for package_ID in co_delivery_string[1].split(", ") if package_ID != None and package_ID != ""]
# Example: "Delayed on flight---will not arrive to depot until 9:05 am"
delay_list = self.notes.split("Delayed on flight---will not arrive to depot until ")
if len(delay_list) == 2:
self.delayed_until = string_12_to_time(delay_list[1])
self.delayed_until = time_to_minutes(self.delayed_until)
self.update_status(DeliveryStatus.DELAYED)
if "Wrong address listed" in self.notes:
self.has_incorrect_address = True
self.update_status(DeliveryStatus.DELAYED)
def update_status(self, status:DeliveryStatus, time: int=0):
self.status_log.append((time, status))
self.delivery_status = status
match status:
case DeliveryStatus.DELAYED:
pass
case DeliveryStatus.AT_HUB:
pass
case DeliveryStatus.ON_TRUCK:
pass
case DeliveryStatus.DELIVERED:
self.delivery_time = time
def get_status(self, time):
prior_entry = prior_entry_status = prior_entry_time = entry = entry_status = entry_time = None
for entry in self.status_log:
entry_time, entry_status = entry
if entry_time > time:
prior_entry_time, prior_entry_status = prior_entry
return (minutes_to_time(prior_entry_time), prior_entry_status)
prior_entry = entry
return (minutes_to_time(entry_time), entry_status)
# only exists because sometimes we want to check the address at a particular time, and some packages have their incorrect addresses updated
def get_address(self, time):
prior_entry = prior_entry_address = prior_entry_time = entry = entry_address = entry_time = None
for entry in self.address_log:
entry_time, entry_address = entry
if entry_time > time:
prior_entry_time, prior_entry_address = prior_entry
return prior_entry_address
prior_entry = entry
return entry_address
# new_address should be a list in the format ["410 S State St", "Salt Lake City", "UT", 84111]
def update_incorrect_address(self, new_address, time: int=0):
self.address_log.append((time, new_address))
self.has_incorrect_address = False
self.update_status(DeliveryStatus.AT_HUB, time)
def get_delivery_time(self, time):
if self.delivery_time == None:
return "N/A"
if self.delivery_time <= time:
return self.delivery_time
return "N/A"
def can_be_delivered(self):
return not self.delayed_until and not self.has_incorrect_address and self.delivery_status == DeliveryStatus.AT_HUB