-
Notifications
You must be signed in to change notification settings - Fork 689
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into dependabot/pip/projects/Diabetes-Monitoring-…
…Dashboard/requests-2.32.2
- Loading branch information
Showing
32 changed files
with
2,884 additions
and
411 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
from datetime import datetime, timedelta | ||
|
||
|
||
def wind_degree_to_direction(str_wind_degree): | ||
""" | ||
Convert wind degree to wind direction. | ||
Parameters: | ||
str_wind_degree (str): Wind degree from API (0 to 360) | ||
Returns: | ||
str: Wind direction (e.g., N, NE, E, etc.) | ||
Or message "API Wind Degree data format error!" | ||
""" | ||
# convert wind degree from str to int. | ||
try: | ||
wind_degree = int(str_wind_degree) | ||
except ValueError: | ||
return "API Wind Degree data format error!" | ||
|
||
directions = [ | ||
"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] | ||
|
||
|
||
def unix_timestamp_to_localtime(str_unix_timestamp, str_timezone_offset_seconds): | ||
""" | ||
Convert unix timestamp to localtime (for sunrise and sunset). | ||
Parameters: | ||
str_unix_timestamp (str): Unix timestamp (e.g., "1717715516") | ||
str_timezone_offset_seconds (str): timezone offset in second (e.g., "28800" represents UTC+8) | ||
Returns: | ||
str: local_time (e.g., "2024-06-07 07:11:56") | ||
Or message "API sunset/sunrise data format error!" | ||
Or message "API timezone data format error!" | ||
""" | ||
# Convert strings to integers | ||
try: | ||
unix_timestamp = int(str_unix_timestamp) | ||
except ValueError: | ||
return "API sunset/sunrise data format error!" | ||
|
||
try: | ||
timezone_offset_seconds = int(str_timezone_offset_seconds) | ||
except ValueError: | ||
return "API timezone data format error!" | ||
|
||
# Convert Unix timestamp to UTC datetime | ||
utc_time = datetime.utcfromtimestamp(unix_timestamp) | ||
|
||
# Apply timezone offset | ||
local_time = utc_time + timedelta(seconds=timezone_offset_seconds) | ||
|
||
return local_time.strftime("%Y-%m-%d %H:%M:%S") | ||
|
||
|
||
def convert_temperature(str_temperature_kelvin, temperature_unit): | ||
""" | ||
Convert temperature in Kelvin degree to Celsius degree or Fahrenheit degree based on second parameter . | ||
Parameters: | ||
str_temperature_kelvin (str): temperature in Kelvin degree (e.g., "291.19") | ||
temperature_unit (str): "C" for Celsius, "F" for Fahrenheit | ||
Returns: | ||
str: temperature (e.g., "21.07 °C" or "67.12 °F") | ||
Or message "API temperature data format error!" | ||
Or message "Temperature unit must either be 'C' or be 'F'!" | ||
""" | ||
# Convert strings to integers | ||
try: | ||
temperature_k = float(str_temperature_kelvin) | ||
except ValueError: | ||
return "API temperature data format error!" | ||
|
||
# Validate temperature_unit | ||
unit = str(temperature_unit).upper() | ||
if not (unit == "C" or unit == "F"): | ||
return "Temperature unit must either be 'C' or be 'F'!" | ||
|
||
# Converting | ||
if unit == "C": | ||
temperature_c = temperature_k - 273.15 | ||
return f"{temperature_c:.2f} °C" | ||
|
||
if unit == "F": | ||
temperature_f = temperature_k * 9 / 5 - 459.67 | ||
return f"{temperature_f:.2f} °F" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import unittest | ||
from Util_Functions import ( | ||
wind_degree_to_direction, | ||
unix_timestamp_to_localtime, | ||
convert_temperature, | ||
) | ||
|
||
|
||
class MyTestCase(unittest.TestCase): | ||
def test_wind_degree_to_direction(self): | ||
self.assertEqual("ENE", wind_degree_to_direction("60")) | ||
self.assertEqual("SE", wind_degree_to_direction("130")) | ||
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") | ||
) | ||
|
||
def test_unix_timestamp_to_localtime(self): | ||
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"), | ||
) | ||
|
||
def test_unix_timestamp_to_localtime_timezone_format_error(self): | ||
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")) | ||
|
||
def test_convert_temperature_to_fahrenheit(self): | ||
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") | ||
) | ||
|
||
def test_convert_temperature_temperature_unit_error(self): | ||
self.assertEqual( | ||
"Temperature unit must either be 'C' or be 'F'!", | ||
convert_temperature("288.59", "H"), | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
Empty file.
Oops, something went wrong.