Skip to content
This repository was archived by the owner on Apr 24, 2025. It is now read-only.

Commit 7888557

Browse files
committed
Fix code style issues with Black
1 parent ac841d4 commit 7888557

File tree

4 files changed

+896
-911
lines changed

4 files changed

+896
-911
lines changed

projects/API Based Weather Report/API Based Weather Report.py

Lines changed: 74 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import requests
22
from datetime import datetime
3-
from Util_Functions import wind_degree_to_direction, unix_timestamp_to_localtime, convert_temperature
3+
from Util_Functions import (
4+
wind_degree_to_direction,
5+
unix_timestamp_to_localtime,
6+
convert_temperature,
7+
)
48

59

610
def fetch_weather(api_key, location):
@@ -49,17 +53,29 @@ def write_to_file(weather_data, temperature_unit):
4953
date_time = datetime.now().strftime("%d %b %Y | %I:%M:%S %p")
5054

5155
# Writing header information to the file
52-
if "name" in weather_data and "sys" in weather_data and "country" in weather_data["sys"]:
53-
f.write("-------------------------------------------------------------\n")
54-
f.write(f"Weather Stats for - {weather_data['name']} | {weather_data['sys']['country']} "
55-
f"| {date_time}\n")
56-
f.write("-------------------------------------------------------------\n")
56+
if (
57+
"name" in weather_data
58+
and "sys" in weather_data
59+
and "country" in weather_data["sys"]
60+
):
61+
f.write(
62+
"-------------------------------------------------------------\n"
63+
)
64+
f.write(
65+
f"Weather Stats for - {weather_data['name']} | {weather_data['sys']['country']} "
66+
f"| {date_time}\n"
67+
)
68+
f.write(
69+
"-------------------------------------------------------------\n"
70+
)
5771

5872
# Writing temperature information to the file
5973
if "main" in weather_data and "temp" in weather_data["main"]:
6074
f.write(
6175
"\tCurrent temperature is : "
62-
+ convert_temperature(weather_data["main"]["temp"], temperature_unit)
76+
+ convert_temperature(
77+
weather_data["main"]["temp"], temperature_unit
78+
)
6379
+ "\n"
6480
)
6581

@@ -90,20 +106,38 @@ def write_to_file(weather_data, temperature_unit):
90106
# Writing wind direction information to the file
91107
if "wind" in weather_data and "deg" in weather_data["wind"]:
92108
f.write(
93-
"\tCurrent wind direction : " +
94-
wind_degree_to_direction(weather_data["wind"]["deg"]) + " \n")
109+
"\tCurrent wind direction : "
110+
+ wind_degree_to_direction(weather_data["wind"]["deg"])
111+
+ " \n"
112+
)
95113

96114
# Writing sunrise local time to the file
97-
if "sys" in weather_data and "sunrise" in weather_data["sys"] and "timezone" in weather_data:
115+
if (
116+
"sys" in weather_data
117+
and "sunrise" in weather_data["sys"]
118+
and "timezone" in weather_data
119+
):
98120
f.write(
99-
"\tToday's sunrise time : " +
100-
unix_timestamp_to_localtime(weather_data["sys"]["sunrise"], weather_data["timezone"]) + " \n")
121+
"\tToday's sunrise time : "
122+
+ unix_timestamp_to_localtime(
123+
weather_data["sys"]["sunrise"], weather_data["timezone"]
124+
)
125+
+ " \n"
126+
)
101127

102128
# Writing sunset local time to the file
103-
if "sys" in weather_data and "sunset" in weather_data["sys"] and "timezone" in weather_data:
129+
if (
130+
"sys" in weather_data
131+
and "sunset" in weather_data["sys"]
132+
and "timezone" in weather_data
133+
):
104134
f.write(
105-
"\tToday's sunset time : " +
106-
unix_timestamp_to_localtime(weather_data["sys"]["sunset"], weather_data["timezone"]) + " \n")
135+
"\tToday's sunset time : "
136+
+ unix_timestamp_to_localtime(
137+
weather_data["sys"]["sunset"], weather_data["timezone"]
138+
)
139+
+ " \n"
140+
)
107141

108142
# Printing confirmation message after writing to file
109143
print("Weather information written to weatherinfo.txt")
@@ -127,7 +161,9 @@ def main():
127161
# Prompting the user to input API key, city name, and temperature unit
128162
api_key = input("Please enter your OpenWeatherMap API key: ")
129163
location = input("Enter the city name: ")
130-
temperature_unit = input("Enter the temperature unit. 'C' for Celsius and 'F' for Fahrenheit: ")
164+
temperature_unit = input(
165+
"Enter the temperature unit. 'C' for Celsius and 'F' for Fahrenheit: "
166+
)
131167

132168
if not (temperature_unit.upper() == "C" or temperature_unit.upper() == "F"):
133169
print("Temperature unit must either be 'C' or be 'F'.")
@@ -152,20 +188,35 @@ def main():
152188
write_to_file(weather_data, temperature_unit)
153189

154190
# Printing weather information to console
155-
print("Current City : " + weather_data['name'] + ', ' +
156-
weather_data['sys']['country'])
191+
print(
192+
"Current City : "
193+
+ weather_data["name"]
194+
+ ", "
195+
+ weather_data["sys"]["country"]
196+
)
157197
print(
158198
"Current temperature is: "
159199
+ convert_temperature(weather_data["main"]["temp"], temperature_unit)
160200
)
161201
print("Current weather desc : " + weather_data["weather"][0]["description"])
162202
print("Current Humidity :", weather_data["main"]["humidity"], "%")
163203
print("Current wind speed :", weather_data["wind"]["speed"], "kmph")
164-
print("Current wind direction:", wind_degree_to_direction(weather_data["wind"]["deg"]))
165-
print("Today's sunrise time :",
166-
unix_timestamp_to_localtime(weather_data["sys"]["sunrise"], weather_data["timezone"]))
167-
print("Today's sunset time :",
168-
unix_timestamp_to_localtime(weather_data["sys"]["sunset"], weather_data["timezone"]))
204+
print(
205+
"Current wind direction:",
206+
wind_degree_to_direction(weather_data["wind"]["deg"]),
207+
)
208+
print(
209+
"Today's sunrise time :",
210+
unix_timestamp_to_localtime(
211+
weather_data["sys"]["sunrise"], weather_data["timezone"]
212+
),
213+
)
214+
print(
215+
"Today's sunset time :",
216+
unix_timestamp_to_localtime(
217+
weather_data["sys"]["sunset"], weather_data["timezone"]
218+
),
219+
)
169220
else:
170221
# Printing error message if weather data fetching fails
171222
print("Failed to fetch weather data. Please check your input and try again.")

projects/API Based Weather Report/Util_Functions.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,22 @@ def wind_degree_to_direction(str_wind_degree):
1919
return "API Wind Degree data format error!"
2020

2121
directions = [
22-
"N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE",
23-
"S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"
22+
"N",
23+
"NNE",
24+
"NE",
25+
"ENE",
26+
"E",
27+
"ESE",
28+
"SE",
29+
"SSE",
30+
"S",
31+
"SSW",
32+
"SW",
33+
"WSW",
34+
"W",
35+
"WNW",
36+
"NW",
37+
"NNW",
2438
]
2539
index = int((wind_degree + 11.25) // 22.5) % 16
2640
return directions[index]
@@ -56,7 +70,7 @@ def unix_timestamp_to_localtime(str_unix_timestamp, str_timezone_offset_seconds)
5670
# Apply timezone offset
5771
local_time = utc_time + timedelta(seconds=timezone_offset_seconds)
5872

59-
return local_time.strftime('%Y-%m-%d %H:%M:%S')
73+
return local_time.strftime("%Y-%m-%d %H:%M:%S")
6074

6175

6276
def convert_temperature(str_temperature_kelvin, temperature_unit):
@@ -84,10 +98,10 @@ def convert_temperature(str_temperature_kelvin, temperature_unit):
8498
return "Temperature unit must either be 'C' or be 'F'!"
8599

86100
# Converting
87-
if unit == 'C':
101+
if unit == "C":
88102
temperature_c = temperature_k - 273.15
89103
return f"{temperature_c:.2f} °C"
90104

91-
if unit == 'F':
105+
if unit == "F":
92106
temperature_f = temperature_k * 9 / 5 - 459.67
93107
return f"{temperature_f:.2f} °F"
Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import unittest
2-
from Util_Functions import wind_degree_to_direction, unix_timestamp_to_localtime, convert_temperature
2+
from Util_Functions import (
3+
wind_degree_to_direction,
4+
unix_timestamp_to_localtime,
5+
convert_temperature,
6+
)
37

48

59
class MyTestCase(unittest.TestCase):
@@ -9,37 +13,44 @@ def test_wind_degree_to_direction(self):
913
self.assertEqual("W", wind_degree_to_direction("280"))
1014

1115
def test_wind_degree_to_direction_parameter_format_error(self):
12-
self.assertEqual("API Wind Degree data format error!",
13-
wind_degree_to_direction("abc"))
16+
self.assertEqual(
17+
"API Wind Degree data format error!", wind_degree_to_direction("abc")
18+
)
1419

1520
def test_unix_timestamp_to_localtime(self):
16-
self.assertEqual("2024-06-07 07:11:56",
17-
unix_timestamp_to_localtime("1717715516", "28800"))
21+
self.assertEqual(
22+
"2024-06-07 07:11:56", unix_timestamp_to_localtime("1717715516", "28800")
23+
)
1824

1925
def test_unix_timestamp_to_localtime_unix_timestamp_format_error(self):
20-
self.assertEqual("API sunset/sunrise data format error!",
21-
unix_timestamp_to_localtime("abc", "28800"))
26+
self.assertEqual(
27+
"API sunset/sunrise data format error!",
28+
unix_timestamp_to_localtime("abc", "28800"),
29+
)
2230

2331
def test_unix_timestamp_to_localtime_timezone_format_error(self):
24-
self.assertEqual("API timezone data format error!",
25-
unix_timestamp_to_localtime("1717715516", "abc"))
32+
self.assertEqual(
33+
"API timezone data format error!",
34+
unix_timestamp_to_localtime("1717715516", "abc"),
35+
)
2636

2737
def test_convert_temperature_to_celsius(self):
28-
self.assertEqual("15.44 °C",
29-
convert_temperature("288.59", "C"))
38+
self.assertEqual("15.44 °C", convert_temperature("288.59", "C"))
3039

3140
def test_convert_temperature_to_fahrenheit(self):
32-
self.assertEqual("59.79 °F",
33-
convert_temperature("288.59", "F"))
41+
self.assertEqual("59.79 °F", convert_temperature("288.59", "F"))
3442

3543
def test_convert_temperature_temperature_format_error(self):
36-
self.assertEqual("API temperature data format error!",
37-
convert_temperature("abc", "F"))
44+
self.assertEqual(
45+
"API temperature data format error!", convert_temperature("abc", "F")
46+
)
3847

3948
def test_convert_temperature_temperature_unit_error(self):
40-
self.assertEqual("Temperature unit must either be 'C' or be 'F'!",
41-
convert_temperature("288.59", "H"))
49+
self.assertEqual(
50+
"Temperature unit must either be 'C' or be 'F'!",
51+
convert_temperature("288.59", "H"),
52+
)
4253

4354

44-
if __name__ == '__main__':
55+
if __name__ == "__main__":
4556
unittest.main()

0 commit comments

Comments
 (0)