Skip to content

Commit 6b5a1f6

Browse files
authored
Put file writing code inside 'with' statements
1 parent 7543a97 commit 6b5a1f6

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

NextTrainDisplay.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import urllib.request
55
import time
66
import datetime
7-
"""Make a visual display that contains live information about the next trains
7+
"""Make a visual display that contains live information about the next trains
88
to depart a given SEPTA Regional Rail station using SEPTA's API.
99
"""
1010

@@ -73,15 +73,15 @@ def main():
7373

7474
# Find the amount of minutes until the train departs
7575
for index in range(len(depart_timestamps)):
76-
# Create an int for the late time
76+
# Create a status message and an integer for the late time
7777
late = "0"
7878
if (status[index] == "On Time"):
7979
pass
8080
else:
8181
for second_index in range(0, 3):
8282
if (status[index][second_index] in ["0", "1", "2", "3", "4",
8383
"5", "6", "7", "8", "9"]):
84-
late = late + status[index][second_index]
84+
late += status[index][second_index]
8585
status[index] = status[index] + " late"
8686
late = int(late)
8787
if (str(next_station[index]) == ""):
@@ -160,9 +160,8 @@ def get_data_from_api():
160160
url = ("https://www3.septa.org/api/Arrivals/index.php?station=" + station
161161
+ "&direction=" + direction)
162162
url = url.replace(" ", "%20")
163-
site = urllib.request.urlopen(url)
164-
raw_data = site.read()
165-
site.close()
163+
with urllib.request.urlopen(url) as site:
164+
raw_data = str(site.read())
166165
return str(raw_data)
167166

168167

@@ -209,12 +208,11 @@ def time_until_next_update(min_until_depart_int):
209208
"""Get the settings and create the display
210209
"""
211210
# Get the settings
212-
settings = open("settings.txt", "r")
213-
fullscreen = settings.readline().strip("\n")
214-
maximum_results = int(settings.readline().strip("\n"))
215-
station = settings.readline().strip("\n")
216-
direction = settings.readline().strip("\n")
217-
settings.close()
211+
with open("settings.txt", "r") as settings:
212+
fullscreen = settings.readline().strip("\n")
213+
maximum_results = int(settings.readline().strip("\n"))
214+
station = settings.readline().strip("\n")
215+
direction = settings.readline().strip("\n")
218216

219217
# Create the display
220218
root = Tk()
@@ -274,8 +272,13 @@ def time_until_next_update(min_until_depart_int):
274272

275273
# Make copyright disclaimer
276274
copyright.append(Label(root))
277-
copyright[0].config(bg=BG_COLOR, fg=FG_COLOR, font=("Arial", int(root.winfo_screenwidth() / 130)),
278-
text="This is an unofficial real-time information display that uses SEPTA's API. Visit septa.org for more information. The displayed data is Copyright Southeastern Pennsylvania Transportation Authority. All Rights Reserved.")
275+
copyright[0].config(bg=BG_COLOR, fg=FG_COLOR,
276+
font=("Arial", int(root.winfo_screenwidth() / 130)),
277+
text="This is an unofficial real-time information "
278+
"display that uses SEPTA's API. Visit septa.org for "
279+
"more information. The displayed data is Copyright "
280+
"Southeastern Pennsylvania Transportation Authority. "
281+
"All Rights Reserved.")
279282
copyright[0].grid(row=(maximum_results - 1) *
280283
3 + 3, column=0, columnspan=4)
281284
root.grid_rowconfigure((maximum_results - 1) * 3 + 3, weight=1)

0 commit comments

Comments
 (0)