Skip to content

Commit 73c984c

Browse files
authored
Make code easier to read
1 parent 6b5a1f6 commit 73c984c

File tree

1 file changed

+71
-73
lines changed

1 file changed

+71
-73
lines changed

NextTrainDisplay.py

Lines changed: 71 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,59 @@
99
"""
1010

1111

12-
def main():
13-
"""Make lists with the data, update the window with the data, then loop.
12+
def get_data_from_api():
13+
"""Get data from SEPTA's API and format the data.
14+
Return the API data.
15+
"""
16+
url = ("https://www3.septa.org/api/Arrivals/index.php?station=" + station
17+
+ "&direction=" + direction)
18+
url = url.replace(" ", "%20")
19+
with urllib.request.urlopen(url) as site:
20+
raw_data = str(site.read())
21+
return str(raw_data)
22+
23+
24+
def status_color(status):
25+
"""Determine the text color of the status message.
26+
Return the hexadecimal value for the color.
1427
"""
28+
if status == "On Time":
29+
return "#00FF00" # Lime
30+
elif status == "Suspended":
31+
return "#FF6347" # Tomato
32+
elif status == "Not Tracked":
33+
return "#00FFFF" # Cyan
34+
elif int(status.replace(" min late", "")) < 3:
35+
return "#00FF00" # Lime
36+
elif int(status.replace(" min late", "")) < 6:
37+
return "#FFFF00" # Yellow
38+
elif int(status.replace(" min late", "")) < 9:
39+
return "#FFA500" # Orange
40+
else:
41+
return "#FF6347" # Tomato
42+
43+
44+
def time_until_next_update(min_until_depart_int):
45+
"""Determine amount of time until the data gets updated,
46+
which is lower when a train is close to depart the station.
47+
Return the time until the data gets updated in milliseconds.
48+
"""
49+
if len(min_until_depart_int) > 0:
50+
next_departure_time = min(min_until_depart_int)
51+
if next_departure_time > 20:
52+
return 60000
53+
elif next_departure_time > 10:
54+
return 45000
55+
elif next_departure_time > 5:
56+
return 30000
57+
elif next_departure_time > 2:
58+
return 15000
59+
else:
60+
return 10000
61+
62+
63+
def main():
64+
"""Make lists with the data, update the window with the data, then loop."""
1565
data = get_data_from_api()
1666

1767
# Get the scheduled times and format the data
@@ -41,27 +91,27 @@ def main():
4191
min_until_depart_int = []
4292
min_until_depart = []
4393
for index, item in enumerate(data):
44-
if (item == "\"train_id\""):
94+
if item == "\"train_id\"":
4595
train_id.append(
4696
str(data[index + 1]).replace("\"", "").replace(".", ""))
47-
elif (item == "\"destination\""):
97+
elif item == "\"destination\"":
4898
destination.append(
4999
"To " + str(data[index + 1]).replace("\"", "")
50100
.replace("_", " "))
51-
elif (item == "\"line\""):
101+
elif item == "\"line\"":
52102
line.append(
53103
str(data[index + 1]).replace("\"", "").replace("_", " ")
54104
+ " Line")
55-
elif (item == "\"status\""):
105+
elif item == "\"status\"":
56106
status.append(
57107
str(data[index + 1]).replace("\"", "").replace("_", " "))
58-
elif (item == "\"service_type\""):
59-
if ((str(data[index + 1])[1:4] == "EXP")):
108+
elif item == "\"service_type\"":
109+
if str(data[index + 1])[1:4] == "EXP":
60110
service_type.append("EXPRESS")
61111
else:
62112
service_type.append(str(data[index + 1]).replace("\"", ""))
63-
elif (item == "\"next_station\""):
64-
if (str(data[index + 1]) == "null"):
113+
elif item == "\"next_station\"":
114+
if str(data[index + 1]) == "null":
65115
next_station.append("")
66116
else:
67117
next_station.append(
@@ -75,20 +125,20 @@ def main():
75125
for index in range(len(depart_timestamps)):
76126
# Create a status message and an integer for the late time
77127
late = "0"
78-
if (status[index] == "On Time"):
128+
if status[index] == "On Time":
79129
pass
80130
else:
81131
for second_index in range(0, 3):
82-
if (status[index][second_index] in ["0", "1", "2", "3", "4",
83-
"5", "6", "7", "8", "9"]):
132+
if status[index][second_index] in ["0", "1", "2", "3", "4",
133+
"5", "6", "7", "8", "9"]:
84134
late += status[index][second_index]
85135
status[index] = status[index] + " late"
86136
late = int(late)
87-
if (str(next_station[index]) == ""):
137+
if str(next_station[index]) == "":
88138
status[index] = "Not Tracked"
89139

90140
# Make the minutes until the train departs message
91-
if (status[index] != "Suspended"):
141+
if status[index] != "Suspended":
92142
if (int((depart_timestamps[index] - current_time) / 60)
93143
+ late != 0):
94144
min_until_depart.append(
@@ -105,19 +155,19 @@ def main():
105155
min_until_depart.append("")
106156
min_until_depart_int.append(999)
107157

108-
# Sort trains by time until departure.
158+
# Sort trains by time until departure
109159
train_order = []
110160
departure_sorter = []
111161
for index in range(0, len(min_until_depart_int)):
112162
departure_sorter.append(min_until_depart_int[index])
113163
number_to_sort = len(train_id)
114-
if (number_to_sort > maximum_results):
164+
if number_to_sort > maximum_results:
115165
number_to_sort = maximum_results
116166
for index in range(0, number_to_sort):
117167
minimum = 2000
118168
min_index = -1
119169
for index, item in enumerate(departure_sorter):
120-
if (item < minimum):
170+
if item < minimum:
121171
minimum = departure_sorter[index]
122172
min_index = index
123173
train_order.append(min_index)
@@ -139,7 +189,7 @@ def main():
139189
text=next_station[train_order[index]])
140190

141191
# Remove data from rows that should be empty
142-
if (len(train_order) < maximum_results):
192+
if len(train_order) < maximum_results:
143193
for index in range(len(train_order), maximum_results):
144194
display_train_id[index].config(text="")
145195
display_service_type[index].config(text="")
@@ -153,60 +203,8 @@ def main():
153203
root.after(update_frequency, main)
154204

155205

156-
def get_data_from_api():
157-
"""Get data from SEPTA's API and format the data
158-
Return the API data
159-
"""
160-
url = ("https://www3.septa.org/api/Arrivals/index.php?station=" + station
161-
+ "&direction=" + direction)
162-
url = url.replace(" ", "%20")
163-
with urllib.request.urlopen(url) as site:
164-
raw_data = str(site.read())
165-
return str(raw_data)
166-
167-
168-
def status_color(status):
169-
"""Determine the text color of the status message
170-
Return the hexadecimal value for the color
171-
"""
172-
if (status == "On Time"):
173-
return "#00FF00" # Lime
174-
elif (status == "Suspended"):
175-
return "#FF6347" # Tomato
176-
elif (status == "Not Tracked"):
177-
return "#00FFFF" # Cyan
178-
elif (int(status.replace(" min late", "")) < 3):
179-
return "#00FF00" # Lime
180-
elif (int(status.replace(" min late", "")) < 6):
181-
return "#FFFF00" # Yellow
182-
elif (int(status.replace(" min late", "")) < 9):
183-
return "#FFA500" # Orange
184-
else:
185-
return "#FF6347" # Tomato
186-
187-
188-
def time_until_next_update(min_until_depart_int):
189-
"""Determine amount of time until the data gets updated,
190-
which is lower when a train is close to depart the station
191-
Return the time until the data gets updated in milliseconds
192-
"""
193-
if (len(min_until_depart_int) > 0):
194-
next_departure_time = min(min_until_depart_int)
195-
if (next_departure_time > 20):
196-
return 60000
197-
elif (next_departure_time > 10):
198-
return 45000
199-
elif (next_departure_time > 5):
200-
return 30000
201-
elif (next_departure_time > 2):
202-
return 15000
203-
else:
204-
return 10000
205-
206-
207206
if __name__ == "__main__":
208-
"""Get the settings and create the display
209-
"""
207+
"""Get the settings and create the display."""
210208
# Get the settings
211209
with open("settings.txt", "r") as settings:
212210
fullscreen = settings.readline().strip("\n")
@@ -219,7 +217,7 @@ def time_until_next_update(min_until_depart_int):
219217
BG_COLOR = "#000000" # Black
220218
FG_COLOR = "#FFFFFF" # White
221219
root.configure(bg=BG_COLOR)
222-
if (fullscreen[0].upper() == "T"):
220+
if fullscreen[0].upper() == "T":
223221
fullscreen = True
224222
else:
225223
fullscreen = False

0 commit comments

Comments
 (0)