-
Notifications
You must be signed in to change notification settings - Fork 0
/
commutes.py
131 lines (101 loc) · 4.39 KB
/
commutes.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
131
# Existing Module
import requests
import requests_cache
import csv
# Calculcates the commute details from one location to any number of other locations
def commute_calc(file_name):
# Caching turned on
requests_cache.install_cache("locations_cache", expire_after=None)
# API Prep
with open("google-maps-api-key.txt","r") as api_file:
api_key = api_file.read()
url = "https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&"
# Access - Read and Write
with open(file_name, "r") as file:
reader = csv.reader(file)
rows = list(reader)
length = len(rows)
# Re-used variables pulled
try:
base = rows[11][1]
gas_price = float(rows[8][1])
fuel_efficiency = float(rows[9][1])
except IndexError:
print("Index Error. Fix File or Code.")
# Summation Variables started at 0
# Total Weekly Drive Time (hours)
twdt = 0
# Total Weekly Commute Distance (miles)
twcd = 0
# Total Weekly Cost ($)
twc = 0
# For Loop calculating the result for each destination
for i in range(15,length):
try:
destination = rows[i][0]
frequency = float(rows[i][1])
except IndexError:
print("Index Error. Fix File or Code.")
r = requests.get(url + "origins=" + base + "&destinations=" + destination + "&key=" + api_key)
# API Results
seconds = r.json()["rows"][0]["elements"][0]["duration"]["value"]
minutes = round(seconds / 60, 2)
hours = round(seconds / 3600, 2)
distance = round(r.json()["rows"][0]["elements"][0]["distance"]["value"] / 1609, 2)
# Print Results
print(f"Traveling from {base} to {destination}:")
if hours >= 1:
print(f"The total travel time in hours is {hours} hours.")
else:
print(f"The total travel time in minutes is {minutes} minutes.")
print(f"The total travel distance is {distance} miles.")
# Additional Calculations
# Weekly Round Trip Commute Time (hrs)
wrtcth = round(frequency * (2* hours), 2)
# Weekly Round Trip Commute Distance (miles)
wrtcdm = round(frequency * (2* distance), 2)
# Weekly Round Trip Fuel Cost (miles)
wrtcc = round(wrtcdm * (gas_price / fuel_efficiency), 2)
# Printing Additional Results
print(f"The weekly round trip commute time is {wrtcth} hrs.")
print(f"The weekly round trip commute distance is {wrtcdm} miles.")
print(f"The weekly round trip fuel cost is ${wrtcc}.\n")
# Updating the results for that entry
rows[i][2] = hours
rows[i][3] = minutes
rows[i][4] = wrtcth
rows[i][5] = distance
rows[i][6] = wrtcdm
rows[i][7] = wrtcc
# Updating the summations
twdt += wrtcth
twcd += wrtcdm
twc += wrtcc
# Cleaning up the data
twdt = round(twdt, 2)
twcd = round(twcd, 2)
twc = round(twc, 2)
# Updating all the summation variables
# Weekly Totals
rows[4][1] = twdt
rows[5][1] = twcd
rows[6][1] = twc
# Yearly Totals
rows[0][1] = tydt = round(twdt * 52, 2)
rows[1][1] = tycd = round(twcd * 52, 2)
rows[2][1] = tyc = round(twc * 52, 2)
# Printing the Overall Values
print("Weekly Commute Totals")
print(f"The Total Weekly Drive time of this commute schedule is {twdt} hours.")
print(f"The Total Weekly Commute distance of this commute schedule is {twcd} miles.")
print(f"The Total Weekly Fuel Cost of this commute schedule is ${twc} cost.\n")
print("Yearly Commute Totals")
print(f"The Total Yearly Drive time of this commute schedule is {tydt} hours.")
print(f"The Total Yearly Commute distance of this commute schedule is {tycd} miles.")
print(f"The Total Yearly Fuel Cost of this commute schedule is ${tyc} cost.\n")
# Writing all the results back to the file
with open(file_name, mode="w",newline="") as file:
writer = csv.writer(file)
writer.writerows(rows)
print(f"The file {file_name} has been updated.")
return base, twdt, twcd, twc