-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdistance.py
137 lines (118 loc) · 4.95 KB
/
distance.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
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
#Katherine Caudill
#Student #: 005252042
#WGU: Data Structures and Algorithms II - C950
import csv
import datetime
# Distance class to get distance data and time data for trucks and packages
# from csv files and compare them to each other to get the correct data
# for the user to see on the console output screen
class Distance:
# get distance name data
with open('distance_name_data.csv') as distance_name_file:
name_reader = list(csv.reader(distance_name_file, delimiter=','))
# get distance data
with open('distance_data.csv') as distance_file:
distance_reader = list(csv.reader(distance_file, delimiter=','))
# check distance from current location to another location
def check_distance(self, current_location, from_location):
row = int(current_location)
col = int(from_location)
distance = self.distance_reader[row][col]
if distance is None or distance == '':
distance = self.distance_reader[col][row]
return distance
# get number from location address
def check_location_num_from_address(self, location):
for name in self.name_reader:
if location == name[2]:
return name[0]
# get simulated truck time
def check_time(self, truck, name):
truck_timeline = []
if name == 'truck1':
truck_time = ['8:00:00']
if name == 'truck2':
truck_time = ['9:05:00']
if name == 'truck3':
truck_time = ['10:00:00']
for t in truck:
if truck[0] == t:
pl = 0
address = t.get_address()
cl = self.check_location_num_from_address(address)
d = self.check_distance(cl, pl)
pl = cl
time = float(d) / 18
time_format = '{0:02.0f}:{1:02.0f}:00'.format(*divmod(time * 60, 60))
truck_time.append(time_format)
total_time = datetime.timedelta()
for t in truck_time:
total_time += self.get_delta_time(t)
truck_timeline.append(total_time)
return truck_timeline
# convert string to time delta
def get_delta_time(self, s):
try:
if len(s.split(':')) == 2: # If the string is in the format "HH:MM"
(h, m) = s.split(':')
dt = datetime.timedelta(hours=int(h), minutes=int(m))
else: # If the string is in the format "HH:MM:SS"
(h, m, s) = s.split(':')
dt = datetime.timedelta(hours=int(h), minutes=int(m), seconds=int(s))
return dt
except Exception as e:
print(f"Error converting {s} to Time Delta")
raise e
# get specific time
def check_delivery_time(self, truck, truck_time, id):
for i in truck:
if i.get_package_id() == id:
index = truck.index(i)
return truck_time[index+1]
# Check delivery status and any given time
def check_delivery_status(self, t, truck, t_time, name, id):
delivery_time = self.check_delivery_time(truck, t_time, id)
timestamp = self.get_delta_time(t)
if name == 'TRUCK 1':
truck_time = self.get_delta_time('8:00:00')
if name == 'TRUCK 2':
truck_time = self.get_delta_time('9:05:00')
if name == 'TRUCK 3':
truck_time = self.get_delta_time('10:00:00')
if timestamp > delivery_time:
return 'DELIVERED'
if timestamp <= delivery_time:
if timestamp < truck_time:
return 'AT_HUB'
else:
return 'ON_TRUCK'
# calc the total distance traveled by the trucks
def get_total_distance_traveled(self, truck):
total_miles = 0
for t in truck:
if truck[0] == t:
pl = 0
cl = self.check_location_num_from_address(t.get_address())
d = self.check_distance(cl, pl)
pl = cl
total_miles += float(d)
# calc trip back to main hub
d = self.check_distance(cl, 0)
total_miles += float(d)
return round(total_miles, 2)
# display data based off time input
def display_data_from_time(self, name, truck, truck_timeline, time, distance):
print('ID Deadline Status Expected Delivery')
print('\n{} || # of packages: {} || Total Distance: {} miles\n'.format(name, len(truck), distance))
if time == '':
timestamp = '23:00:00'
else:
timestamp = time
print('ID Deadline Status Expected Delivery')
for t in truck:
i = truck.index(t)
id = t.get_package_id()
ds = self.check_delivery_status(timestamp, truck, truck_timeline, name, id)
s = 'ID: {:>2} -- Deadline: {:>6} -- Status: {:>6} -- Expected Delivery: {}'
formatted_string = s.format(id, t.get_deadline(), ds, truck_timeline[i + 1])
print(formatted_string)