From cbb6978c5f12f11c03df52fe53779b90cce4d3e5 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sun, 1 Oct 2023 16:36:44 +0530 Subject: [PATCH 1/4] Add time_conversions.py for time unit conversion --- conversions/time_conversions.py | 139 ++++++++++++++++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 conversions/time_conversions.py diff --git a/conversions/time_conversions.py b/conversions/time_conversions.py new file mode 100644 index 000000000000..d5220b7ff99f --- /dev/null +++ b/conversions/time_conversions.py @@ -0,0 +1,139 @@ +from typing import Union + + +def seconds_to_minutes(seconds: Union[int, float], ndigits: int = 2) -> float: + """ + Convert seconds to minutes and round it to the specified number of decimal places. + + >>> seconds_to_minutes(60) + 1.0 + >>> seconds_to_minutes(150) + 2.5 + >>> seconds_to_minutes(3600) + 60.0 + >>> seconds_to_minutes(65.4321, 3) + 1.091 + >>> seconds_to_minutes("120") + 2.0 + >>> seconds_to_minutes("seconds") + Traceback (most recent call last): + ... + ValueError: could not convert string to float: 'seconds' + """ + return round(float(seconds) / 60, ndigits) + + +def minutes_to_seconds(minutes: Union[int, float], ndigits: int = 2) -> float: + """ + Convert minutes to seconds and round it to the specified number of decimal places. + + >>> minutes_to_seconds(1) + 60.0 + >>> minutes_to_seconds(2.5) + 150.0 + >>> minutes_to_seconds(60) + 3600.0 + >>> minutes_to_seconds(1.09, 3) + 65.4 + >>> minutes_to_seconds("2") + 120.0 + >>> minutes_to_seconds("minutes") + Traceback (most recent call last): + ... + ValueError: could not convert string to float: 'minutes' + """ + return round(float(minutes) * 60, ndigits) + + +def hours_to_minutes(hours: Union[int, float], ndigits: int = 2) -> float: + """ + Convert hours to minutes and round it to the specified number of decimal places. + + >>> hours_to_minutes(1) + 60.0 + >>> hours_to_minutes(2.5) + 150.0 + >>> hours_to_minutes(24) + 1440.0 + >>> hours_to_minutes(1.5, 3) + 90.0 + >>> hours_to_minutes("2") + 120.0 + >>> hours_to_minutes("hours") + Traceback (most recent call last): + ... + ValueError: could not convert string to float: 'hours' + """ + return round(float(hours) * 60, ndigits) + + +def minutes_to_hours(minutes: Union[int, float], ndigits: int = 2) -> float: + """ + Convert minutes to hours and round it to the specified number of decimal places. + + >>> minutes_to_hours(60) + 1.0 + >>> minutes_to_hours(150) + 2.5 + >>> minutes_to_hours(1440) + 24.0 + >>> minutes_to_hours(90, 3) + 1.5 + >>> minutes_to_hours("120") + 2.0 + >>> minutes_to_hours("minutes") + Traceback (most recent call last): + ... + ValueError: could not convert string to float: 'minutes' + """ + return round(float(minutes) / 60, ndigits) + + +def days_to_hours(days: Union[int, float], ndigits: int = 2) -> float: + """ + Convert days to hours and round it to the specified number of decimal places. + + >>> days_to_hours(1) + 24.0 + >>> days_to_hours(0.5) + 12.0 + >>> days_to_hours(7) + 168.0 + >>> days_to_hours(3.5, 3) + 84.0 + >>> days_to_hours("2") + 48.0 + >>> days_to_hours("days") + Traceback (most recent call last): + ... + ValueError: could not convert string to float: 'days' + """ + return round(float(days) * 24, ndigits) + + +def hours_to_days(hours: Union[int, float], ndigits: int = 2) -> float: + """ + Convert hours to days and round it to the specified number of decimal places. + + >>> hours_to_days(24) + 1.0 + >>> hours_to_days(12) + 0.5 + >>> hours_to_days(168) + 7.0 + >>> hours_to_days(84, 3) + 3.5 + >>> hours_to_days("48") + 2.0 + >>> hours_to_days("hours") + Traceback (most recent call last): + ... + ValueError: could not convert string to float: 'hours' + """ + return round(float(hours) / 24, ndigits) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From fbca2936115066941c59b977c05147c35f614f5c Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sun, 1 Oct 2023 16:41:47 +0530 Subject: [PATCH 2/4] Correct type annotations --- conversions/time_conversions.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/conversions/time_conversions.py b/conversions/time_conversions.py index d5220b7ff99f..c93fe6cbad11 100644 --- a/conversions/time_conversions.py +++ b/conversions/time_conversions.py @@ -1,7 +1,7 @@ from typing import Union -def seconds_to_minutes(seconds: Union[int, float], ndigits: int = 2) -> float: +def seconds_to_minutes(seconds: float, ndigits: int = 2) -> float: """ Convert seconds to minutes and round it to the specified number of decimal places. @@ -23,7 +23,7 @@ def seconds_to_minutes(seconds: Union[int, float], ndigits: int = 2) -> float: return round(float(seconds) / 60, ndigits) -def minutes_to_seconds(minutes: Union[int, float], ndigits: int = 2) -> float: +def minutes_to_seconds(minutes: float, ndigits: int = 2) -> float: """ Convert minutes to seconds and round it to the specified number of decimal places. @@ -45,7 +45,7 @@ def minutes_to_seconds(minutes: Union[int, float], ndigits: int = 2) -> float: return round(float(minutes) * 60, ndigits) -def hours_to_minutes(hours: Union[int, float], ndigits: int = 2) -> float: +def hours_to_minutes(hours: float, ndigits: int = 2) -> float: """ Convert hours to minutes and round it to the specified number of decimal places. @@ -67,7 +67,7 @@ def hours_to_minutes(hours: Union[int, float], ndigits: int = 2) -> float: return round(float(hours) * 60, ndigits) -def minutes_to_hours(minutes: Union[int, float], ndigits: int = 2) -> float: +def minutes_to_hours(minutes: float, ndigits: int = 2) -> float: """ Convert minutes to hours and round it to the specified number of decimal places. @@ -89,7 +89,7 @@ def minutes_to_hours(minutes: Union[int, float], ndigits: int = 2) -> float: return round(float(minutes) / 60, ndigits) -def days_to_hours(days: Union[int, float], ndigits: int = 2) -> float: +def days_to_hours(days: float, ndigits: int = 2) -> float: """ Convert days to hours and round it to the specified number of decimal places. @@ -111,7 +111,7 @@ def days_to_hours(days: Union[int, float], ndigits: int = 2) -> float: return round(float(days) * 24, ndigits) -def hours_to_days(hours: Union[int, float], ndigits: int = 2) -> float: +def hours_to_days(hours: float, ndigits: int = 2) -> float: """ Convert hours to days and round it to the specified number of decimal places. @@ -135,5 +135,4 @@ def hours_to_days(hours: Union[int, float], ndigits: int = 2) -> float: if __name__ == "__main__": import doctest - doctest.testmod() From 7e6bf9199370567c3c6bfcc8ea8ec67893300057 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 1 Oct 2023 11:13:08 +0000 Subject: [PATCH 3/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- conversions/time_conversions.py | 1 + 1 file changed, 1 insertion(+) diff --git a/conversions/time_conversions.py b/conversions/time_conversions.py index c93fe6cbad11..89842c6ccb1d 100644 --- a/conversions/time_conversions.py +++ b/conversions/time_conversions.py @@ -135,4 +135,5 @@ def hours_to_days(hours: float, ndigits: int = 2) -> float: if __name__ == "__main__": import doctest + doctest.testmod() From 66db143ae8474959a68672e8376572bbc250352d Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sun, 1 Oct 2023 16:49:29 +0530 Subject: [PATCH 4/4] Remove ununsed package --- conversions/time_conversions.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/conversions/time_conversions.py b/conversions/time_conversions.py index c93fe6cbad11..5ddff1ab9186 100644 --- a/conversions/time_conversions.py +++ b/conversions/time_conversions.py @@ -1,6 +1,3 @@ -from typing import Union - - def seconds_to_minutes(seconds: float, ndigits: int = 2) -> float: """ Convert seconds to minutes and round it to the specified number of decimal places.