diff --git a/satella/__init__.py b/satella/__init__.py index e4b1c62145f41e5e37fad3e392eb06a2188cbd91..0522a19c3eae0152657cd48e273fd12350d284ae 100644 --- a/satella/__init__.py +++ b/satella/__init__.py @@ -1 +1 @@ -__version__ = '2.25.6a3' +__version__ = '2.25.6a4' diff --git a/satella/coding/structures/dictionaries/cache_dict.py b/satella/coding/structures/dictionaries/cache_dict.py index 89cedc6809754dad305e74dd1d40e92e57db1974..12e1e89e92509f1f99a64bb82f8c49be2f195c9c 100644 --- a/satella/coding/structures/dictionaries/cache_dict.py +++ b/satella/coding/structures/dictionaries/cache_dict.py @@ -18,7 +18,8 @@ _TIME_MODIFIERS = [ ('d', 24 * 60 * 60), ('w', 7 * 24 * 60 * 60) ] -def _parse_time_string(s: tp.Union[int, float, str]) -> float: + +def _parse_time_string(s: tp.Union[int, float, str]) -> tp.Union[int, float]: """ Parse a time string into seconds, so eg. '30m' will be equal to 1800, and so will be '30 min'. @@ -42,7 +43,7 @@ def _parse_time_string(s: tp.Union[int, float, str]) -> float: if modifier in s: return float(s[:s.index(modifier)]) * multiple - return float(s) + return float(s) if float(int(s)) != int(s) else int(s) class CacheDict(tp.Mapping[K, V]): diff --git a/satella/time/parse.py b/satella/time/parse.py index 329ac8636abaace5a8b519ed4abf8dad7a2397c9..f9d54161b8219376746d4e0993d2b23b46b18b36 100644 --- a/satella/time/parse.py +++ b/satella/time/parse.py @@ -9,7 +9,7 @@ TIME_MODIFIERS = [ ] -def parse_time_string(s: tp.Union[int, float, str]) -> float: +def parse_time_string(s: tp.Union[int, float, str]) -> tp.Union[int, float]: """ Parse a time string into seconds, so eg. '30m' will be equal to 1800, and so will be '30 min'. @@ -24,13 +24,14 @@ def parse_time_string(s: tp.Union[int, float, str]) -> float: .. warning:: This does not handle fractions of a second! :param s: time string or time value in seconds - :return: value in seconds + :return: value in seconds (an int) """ if isinstance(s, (int, float)): - return s + return int(s) for modifier, multiple in TIME_MODIFIERS: if modifier in s: return float(s[:s.index(modifier)]) * multiple - return float(s) + return float(s) if float(int(s)) != int(s) else int(s) + diff --git a/tests/test_time.py b/tests/test_time.py index 550b2bc6e693420883f3997c16d8454ef457c39e..b93843e97a68d0a588e49061665c7397af87452a 100644 --- a/tests/test_time.py +++ b/tests/test_time.py @@ -14,6 +14,12 @@ from satella.warnings import mark_temporarily_disabled class TestTime(unittest.TestCase): + def test_parse_time(self): + p = parse_time_string('1800s') + self.assertEqual(p, 1800) + p = parse_time_string('1.5s') + self.assertEqual(p, 1.5) + def test_mark_temp_disabled(self): @mark_temporarily_disabled('Skipped due to skip')