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

Commit 6f6f2da

Browse files
committed
doc: update docstrings
1 parent ecddf87 commit 6f6f2da

File tree

2 files changed

+46
-12
lines changed

2 files changed

+46
-12
lines changed

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

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,17 @@
33
from Util_Functions import wind_degree_to_direction, unix_timestamp_to_localtime, convert_temperature
44

55

6-
# Function to fetch weather data from OpenWeatherMap API
76
def fetch_weather(api_key, location):
7+
"""
8+
Function to fetch weather data from OpenWeatherMap API.
9+
10+
Parameters:
11+
api_key (str): API key.
12+
location (str): City name.
13+
14+
Returns:
15+
str: The JSON response string.
16+
"""
817
try:
918
# Constructing the API link with the provided API key and location
1019
complete_api_link = f"https://api.openweathermap.org/data/2.5/weather?q={location}&appid={api_key}"
@@ -24,8 +33,15 @@ def fetch_weather(api_key, location):
2433
return None
2534

2635

27-
# Function to write weather information to a text file
2836
def write_to_file(weather_data, temperature_unit):
37+
"""
38+
Function to write weather information to a text file.
39+
40+
Parameters:
41+
weather_data (str): The JSON API response string.
42+
temperature_unit (str): 'C' for Celsius, 'F' for Fahrenheit.
43+
"""
44+
2945
try:
3046
# Opening the file "weatherinfo.txt" in write mode
3147
with open("weatherinfo.txt", "w+") as f:
@@ -97,16 +113,18 @@ def write_to_file(weather_data, temperature_unit):
97113
print("Error writing to file:", e)
98114

99115

100-
# Main function
101116
def main():
117+
"""
118+
Main function.
119+
"""
102120
# Printing welcome messages and instructions
103121
print("Welcome to the Weather Information App!")
104122
print("You need an API key to access weather data from OpenWeatherMap.")
105123
print(
106124
"You can obtain your API key by signing up at https://home.openweathermap.org/users/sign_up"
107125
)
108126

109-
# Prompting the user to input API key, city name, and
127+
# Prompting the user to input API key, city name, and temperature unit
110128
api_key = input("Please enter your OpenWeatherMap API key: ")
111129
location = input("Enter the city name: ")
112130
temperature_unit = input("Enter the temperature unit. 'C' for Celsius and 'F' for Fahrenheit: ")
@@ -120,10 +138,12 @@ def main():
120138

121139
# Checking if weather data was successfully fetched
122140
if weather_data:
141+
# Checking if the API key is invalid
123142
if weather_data["cod"] == "401":
124143
print("Invalid API key.")
125144
return
126145

146+
# Checking if the city is not found
127147
if weather_data["cod"] == "404":
128148
print("City not found.")
129149
return

projects/API Based Weather Report/Util_Functions.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@ def wind_degree_to_direction(str_wind_degree):
55
"""
66
Convert wind degree to wind direction.
77
8-
:param str_wind_degree: str, Wind degree from API (0 to 360)
9-
:return: Wind direction as a string (e.g., N, NE, E, etc.)
8+
Parameters:
9+
str_wind_degree (str): Wind degree from API (0 to 360)
10+
11+
Returns:
12+
str: Wind direction (e.g., N, NE, E, etc.)
13+
Or message "API Wind Degree data format error!"
1014
"""
1115
# convert wind degree from str to int.
1216
try:
@@ -26,9 +30,14 @@ def unix_timestamp_to_localtime(str_unix_timestamp, str_timezone_offset_seconds)
2630
"""
2731
Convert unix timestamp to localtime (for sunrise and sunset).
2832
29-
:param str_unix_timestamp: str, Unix timestamp (e.g., "1717715516")
30-
:param str_timezone_offset_seconds: str, timezone offset in second (e.g., "28800" represents UTC+8)
31-
:return: local_time (e.g., "2024-06-07 07:11:56")
33+
Parameters:
34+
str_unix_timestamp (str): Unix timestamp (e.g., "1717715516")
35+
str_timezone_offset_seconds (str): timezone offset in second (e.g., "28800" represents UTC+8)
36+
37+
Returns:
38+
str: local_time (e.g., "2024-06-07 07:11:56")
39+
Or message "API sunset/sunrise data format error!"
40+
Or message "API timezone data format error!"
3241
"""
3342
# Convert strings to integers
3443
try:
@@ -54,9 +63,14 @@ def convert_temperature(str_temperature_kelvin, temperature_unit):
5463
"""
5564
Convert temperature in Kelvin degree to Celsius degree or Fahrenheit degree based on second parameter .
5665
57-
:param str_temperature_kelvin: str, temperature in Kelvin degree (e.g., "291.19")
58-
:param temperature_unit: str, "C" for Celsius, "F" for Fahrenheit
59-
:return: temperature (e.g., "21.07 °C" or "67.12 °F")
66+
Parameters:
67+
str_temperature_kelvin (str): temperature in Kelvin degree (e.g., "291.19")
68+
temperature_unit (str): "C" for Celsius, "F" for Fahrenheit
69+
70+
Returns:
71+
str: temperature (e.g., "21.07 °C" or "67.12 °F")
72+
Or message "API temperature data format error!"
73+
Or message "Temperature unit must either be 'C' or be 'F'!"
6074
"""
6175
# Convert strings to integers
6276
try:

0 commit comments

Comments
 (0)