-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpackage_statuses.py
More file actions
292 lines (267 loc) · 14.2 KB
/
package_statuses.py
File metadata and controls
292 lines (267 loc) · 14.2 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# Author: Matthew Shelbourn | Student ID: 001059665 | mshelbo@wgu.edu | December, 2020
# package_statuses.py includes a function to calculate and return the status for all packages
# based on a given time parameter
from datetime import datetime, timedelta
from durations import calc_dest_transit_time, calc_delivery_time
from truckloads import get_truck_1, get_truck_2, get_truck_3
from distances import calc_distance, get_dest_name
from package_table import get_package_table
# Executes truck routes and returns formatted print statements displaying the status of each package based
# on a time parameter
# Space-time complexity O(N)
def get_package_statuses(time_param):
packages = get_package_table()
truck_1 = get_truck_1()
truck_2 = get_truck_2()
truck_3 = get_truck_3()
current_time_truck_1 = datetime(year=2020, month=12, day=25, hour=8, minute=0) # Tracks current time for truck 1
current_time_truck_2 = datetime(year=2020, month=12, day=25, hour=9, minute=5) # Tracks current time for truck 2
current_time_truck_3 = datetime(year=2020, month=12, day=25, hour=10, minute=20) # Tracks current time for truck 3
current_location_truck_1 = 0 # Current location of package (initializes to 0 which refers to the index of hub)
current_location_name_truck_1 = 'Western Governors University' # Current name of location where package resides
current_location_truck_2 = 0 # Current location of package (initializes to 0 which refers to the index of hub)
current_location_name_truck_2 = 'Western Governors University' # Current name of location where package resides
current_location_truck_3 = 0 # Current location of package (initializes to 0 which refers to the index of hub)
current_location_name_truck_3 = 'Western Governors University' # Current name of location where package resides
final_destination = 0 # Index of hub (for returning trucks to hub)
dest_distance = 0 # Distance between each destination on route
dest_transit_time = 0 # Transit time between each destination on route
delivery_time = '' # Time package was delivered
time_param = datetime.strptime(time_param, '%H:%M')
converted_time_param = datetime(year=2020, month=12, day=25, hour=time_param.hour, minute=time_param.minute)
truck_1_hub_arrival_time = '' # Needed for dispatching truck 3. Driver must return to hub before truck 3 can depart
# Sets the initial status of all packages to 'Out for Delivery'
# Space-time complexity O(N)
for pkg in truck_1:
if current_time_truck_1 > converted_time_param + timedelta(seconds=1):
pkg[8] = 'N/A'
pkg[10] = 'N/A'
packages.update(int(pkg[0]), pkg)
continue
else:
pkg[8] = str(current_time_truck_1)
pkg[10] = 'N/A'
pkg[11] = 'Out for Delivery'
packages.update(int(pkg[0]), pkg)
# Delivers the packages, updates package status and delivery time
# Space-time complexity O(N)
for pkg in truck_1:
dest_distance = calc_distance(current_location_truck_1, pkg)
dest_transit_time = calc_dest_transit_time(dest_distance)
delivery_time = str(calc_delivery_time(current_time_truck_1, dest_transit_time))
current_time_truck_1 = calc_delivery_time(current_time_truck_1, dest_transit_time)
if current_time_truck_1 >= converted_time_param:
pkg[9] = current_location_name_truck_1
continue
else:
current_location_truck_1 = pkg
current_location_name_truck_1 = get_dest_name(pkg)
pkg[9] = current_location_name_truck_1
pkg[10] = delivery_time
pkg[11] = 'Delivered'
packages.update(int(pkg[0]), pkg)
# Returns truck 1 to the hub
dest_distance = calc_distance(current_location_truck_1, final_destination)
dest_transit_time = calc_dest_transit_time(dest_distance)
current_location_name_truck_1 = get_dest_name(final_destination)
truck_1_hub_arrival_time = calc_delivery_time(current_time_truck_1, dest_transit_time)
# Sets the initial status of all packages to 'Out for Delivery'
# Space-time complexity O(N)
for pkg in truck_2:
if current_time_truck_2 > converted_time_param + timedelta(seconds=1):
pkg[8] = 'N/A'
pkg[10] = 'N/A'
packages.update(int(pkg[0]), pkg)
continue
else:
pkg[8] = str(current_time_truck_2)
pkg[10] = 'N/A'
pkg[11] = 'Out for Delivery'
packages.update(int(pkg[0]), pkg)
# Delivers the packages, updates package status and delivery time
# Space-time complexity O(N)
for pkg in truck_2:
dest_distance = calc_distance(current_location_truck_2, pkg)
dest_transit_time = calc_dest_transit_time(dest_distance)
delivery_time = str(calc_delivery_time(current_time_truck_2, dest_transit_time))
current_time_truck_2 = calc_delivery_time(current_time_truck_2, dest_transit_time)
if current_time_truck_2 >= converted_time_param:
pkg[9] = current_location_name_truck_2
continue
else:
current_location_truck_2 = pkg
current_location_name_truck_2 = get_dest_name(pkg)
pkg[9] = current_location_name_truck_2
pkg[10] = delivery_time
pkg[11] = 'Delivered'
packages.update(int(pkg[0]), pkg)
# Sets initial departure time of truck 3 based on the time that truck 1 returns to the hub
current_time_truck_3 = current_time_truck_3 if current_time_truck_3 > truck_1_hub_arrival_time \
else truck_1_hub_arrival_time
# Sets the initial status of all packages to 'Out for Delivery'
# Space-time complexity O(N)
for pkg in truck_3:
if current_time_truck_3 > converted_time_param + timedelta(seconds=1):
pkg[8] = 'N/A'
pkg[10] = 'N/A'
packages.update(int(pkg[0]), pkg)
continue
else:
pkg[8] = str(current_time_truck_3)
pkg[10] = 'N/A'
pkg[11] = 'Out for Delivery'
packages.update(int(pkg[0]), pkg)
# Delivers the packages, updates package status and delivery time
# Space-time complexity O(N)
for pkg in truck_3:
dest_distance = calc_distance(current_location_truck_3, pkg)
dest_transit_time = calc_dest_transit_time(dest_distance)
delivery_time = str(calc_delivery_time(current_time_truck_3, dest_transit_time))
current_time_truck_3 = calc_delivery_time(current_time_truck_3, dest_transit_time)
if current_time_truck_3 >= converted_time_param:
pkg[9] = current_location_name_truck_3
continue
else:
current_location_truck_3 = pkg
current_location_name_truck_3 = get_dest_name(pkg)
pkg[9] = current_location_name_truck_3
pkg[10] = delivery_time
pkg[11] = 'Delivered'
packages.update(int(pkg[0]), pkg)
# Prints the statuses for all packages at a specific time based on a provided time parameter
# Space-time complexity O(N)
for el in range(1, 41):
pkg = packages.read(el)
print('Details for package: ' + pkg[0] + ' -- Status: ' + pkg[11] + ' || Left Hub At: ' + pkg[8] +
' || Current Location: ' + pkg[9] + ' || Delivery Deadline: ' + pkg[5] + ' || Delivery Time: ' + pkg[10])
return ''
# Returns the status of a specified package based on a package_id and time parameter
# Space-time complexity O(N)
def get_package_status(package_id, time_param):
packages = get_package_table()
truck_1 = get_truck_1()
truck_2 = get_truck_2()
truck_3 = get_truck_3()
current_time_truck_1 = datetime(year=2020, month=12, day=25, hour=8, minute=0) # Tracks current time for truck 1
current_time_truck_2 = datetime(year=2020, month=12, day=25, hour=9, minute=5) # Tracks current time for truck 2
current_time_truck_3 = datetime(year=2020, month=12, day=25, hour=10, minute=20) # Tracks current time for truck 3
current_location_truck_1 = 0 # Current location of package (initializes to 0 which refers to the index of hub)
current_location_name_truck_1 = 'Western Governors University' # Current name of location where package resides
current_location_truck_2 = 0 # Current location of package (initializes to 0 which refers to the index of hub)
current_location_name_truck_2 = 'Western Governors University' # Current name of location where package resides
current_location_truck_3 = 0 # Current location of package (initializes to 0 which refers to the index of hub)
current_location_name_truck_3 = 'Western Governors University' # Current name of location where package resides
final_destination = 0 # Index of hub (for returning trucks to hub)
dest_distance = 0 # Distance between each destination on route
dest_transit_time = 0 # Transit time between each destination on route
delivery_time = '' # Time package was delivered
initial_time_param = time_param
time_param = datetime.strptime(time_param, '%H:%M')
converted_time_param = datetime(year=2020, month=12, day=25, hour=time_param.hour, minute=time_param.minute)
truck_1_hub_arrival_time = '' # Needed for dispatching truck 3. Driver must return to hub before truck 3 can depart
# Sets the initial status of all packages to 'Out for Delivery'
# Space-time complexity O(N)
for pkg in truck_1:
if current_time_truck_1 > converted_time_param + timedelta(seconds=1):
pkg[8] = 'N/A'
pkg[10] = 'N/A'
packages.update(int(pkg[0]), pkg)
continue
else:
pkg[8] = str(current_time_truck_1)
pkg[10] = 'N/A'
pkg[11] = 'Out for Delivery'
packages.update(int(pkg[0]), pkg)
# Delivers the packages, updates package status and delivery time
# Space-time complexity O(N)
for pkg in truck_1:
dest_distance = calc_distance(current_location_truck_1, pkg)
dest_transit_time = calc_dest_transit_time(dest_distance)
delivery_time = str(calc_delivery_time(current_time_truck_1, dest_transit_time))
current_time_truck_1 = calc_delivery_time(current_time_truck_1, dest_transit_time)
if current_time_truck_1 >= converted_time_param:
pkg[9] = current_location_name_truck_1
continue
else:
current_location_truck_1 = pkg
current_location_name_truck_1 = get_dest_name(pkg)
pkg[9] = current_location_name_truck_1
pkg[10] = delivery_time
pkg[11] = 'Delivered'
packages.update(int(pkg[0]), pkg)
# Returns truck 1 to the hub
dest_distance = calc_distance(current_location_truck_1, final_destination)
dest_transit_time = calc_dest_transit_time(dest_distance)
current_location_name_truck_1 = get_dest_name(final_destination)
truck_1_hub_arrival_time = calc_delivery_time(current_time_truck_1, dest_transit_time)
# Sets the initial status of all packages to 'Out for Delivery'
# Space-time complexity O(N)
for pkg in truck_2:
if current_time_truck_2 > converted_time_param + timedelta(seconds=1):
pkg[8] = 'N/A'
pkg[10] = 'N/A'
packages.update(int(pkg[0]), pkg)
continue
else:
pkg[8] = str(current_time_truck_2)
pkg[10] = 'N/A'
pkg[11] = 'Out for Delivery'
packages.update(int(pkg[0]), pkg)
# Delivers the packages, updates package status and delivery time
# Space-time complexity O(N)
for pkg in truck_2:
dest_distance = calc_distance(current_location_truck_2, pkg)
dest_transit_time = calc_dest_transit_time(dest_distance)
delivery_time = str(calc_delivery_time(current_time_truck_2, dest_transit_time))
current_time_truck_2 = calc_delivery_time(current_time_truck_2, dest_transit_time)
if current_time_truck_2 >= converted_time_param:
pkg[9] = current_location_name_truck_2
continue
else:
current_location_truck_2 = pkg
current_location_name_truck_2 = get_dest_name(pkg)
pkg[9] = current_location_name_truck_2
pkg[10] = delivery_time
pkg[11] = 'Delivered'
packages.update(int(pkg[0]), pkg)
# Sets initial departure time of truck 3 based on the time that truck 1 returns to the hub
current_time_truck_3 = current_time_truck_3 if current_time_truck_3 > truck_1_hub_arrival_time \
else truck_1_hub_arrival_time
# Sets the initial status of all packages to 'Out for Delivery'
# Space-time complexity O(N)
for pkg in truck_3:
if current_time_truck_3 > converted_time_param + timedelta(seconds=1):
pkg[8] = 'N/A'
pkg[10] = 'N/A'
packages.update(int(pkg[0]), pkg)
continue
else:
pkg[8] = str(current_time_truck_3)
pkg[10] = 'N/A'
pkg[11] = 'Out for Delivery'
packages.update(int(pkg[0]), pkg)
# Delivers the packages, updates package status and delivery time
# Space-time complexity O(N)
for pkg in truck_3:
dest_distance = calc_distance(current_location_truck_3, pkg)
dest_transit_time = calc_dest_transit_time(dest_distance)
delivery_time = str(calc_delivery_time(current_time_truck_3, dest_transit_time))
current_time_truck_3 = calc_delivery_time(current_time_truck_3, dest_transit_time)
if current_time_truck_3 >= converted_time_param:
pkg[9] = current_location_name_truck_3
continue
else:
current_location_truck_3 = pkg
current_location_name_truck_3 = get_dest_name(pkg)
pkg[9] = current_location_name_truck_3
pkg[10] = delivery_time
pkg[11] = 'Delivered'
packages.update(int(pkg[0]), pkg)
# Prints the statuses for an individual package at a specific time based on a provided package ID parameter and
# a provided time parameter
# Space-time complexity O(N)
pkg = packages.read(package_id)
print('\nDetails for package ' + pkg[0] + ' as of ' + initial_time_param + ' -- Status: ' + pkg[11] +
' || Left Hub At: ' + pkg[8] + ' || Current Location: ' + pkg[9] + ' || Delivery Deadline: ' + pkg[5] +
' || Delivery Time: ' + pkg[10])
return ''