Skip to content

Commit

Permalink
Fix code style issues with Black
Browse files Browse the repository at this point in the history
  • Loading branch information
lint-action committed Jun 10, 2024
1 parent ac841d4 commit 7888557
Show file tree
Hide file tree
Showing 4 changed files with 896 additions and 911 deletions.
97 changes: 74 additions & 23 deletions projects/API Based Weather Report/API Based Weather Report.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import requests
from datetime import datetime
from Util_Functions import wind_degree_to_direction, unix_timestamp_to_localtime, convert_temperature
from Util_Functions import (
wind_degree_to_direction,
unix_timestamp_to_localtime,
convert_temperature,
)


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

# Writing header information to the file
if "name" in weather_data and "sys" in weather_data and "country" in weather_data["sys"]:
f.write("-------------------------------------------------------------\n")
f.write(f"Weather Stats for - {weather_data['name']} | {weather_data['sys']['country']} "
f"| {date_time}\n")
f.write("-------------------------------------------------------------\n")
if (
"name" in weather_data
and "sys" in weather_data
and "country" in weather_data["sys"]
):
f.write(
"-------------------------------------------------------------\n"
)
f.write(
f"Weather Stats for - {weather_data['name']} | {weather_data['sys']['country']} "
f"| {date_time}\n"
)
f.write(
"-------------------------------------------------------------\n"
)

# Writing temperature information to the file
if "main" in weather_data and "temp" in weather_data["main"]:
f.write(
"\tCurrent temperature is : "
+ convert_temperature(weather_data["main"]["temp"], temperature_unit)
+ convert_temperature(
weather_data["main"]["temp"], temperature_unit
)
+ "\n"
)

Expand Down Expand Up @@ -90,20 +106,38 @@ def write_to_file(weather_data, temperature_unit):
# Writing wind direction information to the file
if "wind" in weather_data and "deg" in weather_data["wind"]:
f.write(
"\tCurrent wind direction : " +
wind_degree_to_direction(weather_data["wind"]["deg"]) + " \n")
"\tCurrent wind direction : "
+ wind_degree_to_direction(weather_data["wind"]["deg"])
+ " \n"
)

# Writing sunrise local time to the file
if "sys" in weather_data and "sunrise" in weather_data["sys"] and "timezone" in weather_data:
if (
"sys" in weather_data
and "sunrise" in weather_data["sys"]
and "timezone" in weather_data
):
f.write(
"\tToday's sunrise time : " +
unix_timestamp_to_localtime(weather_data["sys"]["sunrise"], weather_data["timezone"]) + " \n")
"\tToday's sunrise time : "
+ unix_timestamp_to_localtime(
weather_data["sys"]["sunrise"], weather_data["timezone"]
)
+ " \n"
)

# Writing sunset local time to the file
if "sys" in weather_data and "sunset" in weather_data["sys"] and "timezone" in weather_data:
if (
"sys" in weather_data
and "sunset" in weather_data["sys"]
and "timezone" in weather_data
):
f.write(
"\tToday's sunset time : " +
unix_timestamp_to_localtime(weather_data["sys"]["sunset"], weather_data["timezone"]) + " \n")
"\tToday's sunset time : "
+ unix_timestamp_to_localtime(
weather_data["sys"]["sunset"], weather_data["timezone"]
)
+ " \n"
)

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

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

# Printing weather information to console
print("Current City : " + weather_data['name'] + ', ' +
weather_data['sys']['country'])
print(
"Current City : "
+ weather_data["name"]
+ ", "
+ weather_data["sys"]["country"]
)
print(
"Current temperature is: "
+ convert_temperature(weather_data["main"]["temp"], temperature_unit)
)
print("Current weather desc : " + weather_data["weather"][0]["description"])
print("Current Humidity :", weather_data["main"]["humidity"], "%")
print("Current wind speed :", weather_data["wind"]["speed"], "kmph")
print("Current wind direction:", wind_degree_to_direction(weather_data["wind"]["deg"]))
print("Today's sunrise time :",
unix_timestamp_to_localtime(weather_data["sys"]["sunrise"], weather_data["timezone"]))
print("Today's sunset time :",
unix_timestamp_to_localtime(weather_data["sys"]["sunset"], weather_data["timezone"]))
print(
"Current wind direction:",
wind_degree_to_direction(weather_data["wind"]["deg"]),
)
print(
"Today's sunrise time :",
unix_timestamp_to_localtime(
weather_data["sys"]["sunrise"], weather_data["timezone"]
),
)
print(
"Today's sunset time :",
unix_timestamp_to_localtime(
weather_data["sys"]["sunset"], weather_data["timezone"]
),
)
else:
# Printing error message if weather data fetching fails
print("Failed to fetch weather data. Please check your input and try again.")
Expand Down
24 changes: 19 additions & 5 deletions projects/API Based Weather Report/Util_Functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,22 @@ def wind_degree_to_direction(str_wind_degree):
return "API Wind Degree data format error!"

directions = [
"N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE",
"S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"
"N",
"NNE",
"NE",
"ENE",
"E",
"ESE",
"SE",
"SSE",
"S",
"SSW",
"SW",
"WSW",
"W",
"WNW",
"NW",
"NNW",
]
index = int((wind_degree + 11.25) // 22.5) % 16
return directions[index]
Expand Down Expand Up @@ -56,7 +70,7 @@ def unix_timestamp_to_localtime(str_unix_timestamp, str_timezone_offset_seconds)
# Apply timezone offset
local_time = utc_time + timedelta(seconds=timezone_offset_seconds)

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


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

# Converting
if unit == 'C':
if unit == "C":
temperature_c = temperature_k - 273.15
return f"{temperature_c:.2f} °C"

if unit == 'F':
if unit == "F":
temperature_f = temperature_k * 9 / 5 - 459.67
return f"{temperature_f:.2f} °F"
47 changes: 29 additions & 18 deletions projects/API Based Weather Report/test_Util_Functions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import unittest
from Util_Functions import wind_degree_to_direction, unix_timestamp_to_localtime, convert_temperature
from Util_Functions import (
wind_degree_to_direction,
unix_timestamp_to_localtime,
convert_temperature,
)


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

def test_wind_degree_to_direction_parameter_format_error(self):
self.assertEqual("API Wind Degree data format error!",
wind_degree_to_direction("abc"))
self.assertEqual(
"API Wind Degree data format error!", wind_degree_to_direction("abc")
)

def test_unix_timestamp_to_localtime(self):
self.assertEqual("2024-06-07 07:11:56",
unix_timestamp_to_localtime("1717715516", "28800"))
self.assertEqual(
"2024-06-07 07:11:56", unix_timestamp_to_localtime("1717715516", "28800")
)

def test_unix_timestamp_to_localtime_unix_timestamp_format_error(self):
self.assertEqual("API sunset/sunrise data format error!",
unix_timestamp_to_localtime("abc", "28800"))
self.assertEqual(
"API sunset/sunrise data format error!",
unix_timestamp_to_localtime("abc", "28800"),
)

def test_unix_timestamp_to_localtime_timezone_format_error(self):
self.assertEqual("API timezone data format error!",
unix_timestamp_to_localtime("1717715516", "abc"))
self.assertEqual(
"API timezone data format error!",
unix_timestamp_to_localtime("1717715516", "abc"),
)

def test_convert_temperature_to_celsius(self):
self.assertEqual("15.44 °C",
convert_temperature("288.59", "C"))
self.assertEqual("15.44 °C", convert_temperature("288.59", "C"))

def test_convert_temperature_to_fahrenheit(self):
self.assertEqual("59.79 °F",
convert_temperature("288.59", "F"))
self.assertEqual("59.79 °F", convert_temperature("288.59", "F"))

def test_convert_temperature_temperature_format_error(self):
self.assertEqual("API temperature data format error!",
convert_temperature("abc", "F"))
self.assertEqual(
"API temperature data format error!", convert_temperature("abc", "F")
)

def test_convert_temperature_temperature_unit_error(self):
self.assertEqual("Temperature unit must either be 'C' or be 'F'!",
convert_temperature("288.59", "H"))
self.assertEqual(
"Temperature unit must either be 'C' or be 'F'!",
convert_temperature("288.59", "H"),
)


if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()
Loading

0 comments on commit 7888557

Please sign in to comment.