Skip to content

Commit d9198e4

Browse files
authored
Create backend-ex_1.py
1 parent d21125d commit d9198e4

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

Section #4/backend-ex_1.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import datetime
2+
import pytz
3+
4+
README_PATH = "README.md"
5+
6+
def get_traffic_light_state():
7+
cst = pytz.timezone('US/Central')
8+
now = datetime.datetime.now(cst)
9+
if 10 <= now.hour < 11:
10+
return "GREEN"
11+
elif 11 <= now.hour < 12:
12+
return "YELLOW"
13+
else:
14+
return "RED"
15+
16+
def badge_url(state):
17+
colors = {
18+
"GREEN": "brightgreen",
19+
"YELLOW": "yellow",
20+
"RED": "red"
21+
}
22+
return f"https://img.shields.io/badge/Traffic_Light-{state}-{colors[state]}"
23+
24+
def update_readme():
25+
state = get_traffic_light_state()
26+
badge = badge_url(state)
27+
with open(README_PATH, "r") as f:
28+
lines = f.readlines()
29+
30+
# Find and replace the badge line between the markers.
31+
with open(README_PATH, "w") as f:
32+
for line in lines:
33+
if line.strip().startswith("![Traffic Light]("):
34+
f.write(f"![Traffic Light]({badge})\n")
35+
else:
36+
f.write(line)
37+
38+
if __name__ == "__main__":
39+
update_readme()

0 commit comments

Comments
 (0)