From 14f3272864ab90d7f843581d05b42bd2e5a90112 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Ma=C5=9Blanka?= <piotr.maslanka@ericsson.com> Date: Wed, 18 Sep 2024 12:04:19 +0200 Subject: [PATCH] fixed circular import --- satella/__init__.py | 2 +- satella/coding/structures/dictionaries/cache_dict.py | 5 +++-- satella/time/parse.py | 9 +++++---- tests/test_time.py | 6 ++++++ 4 files changed, 15 insertions(+), 7 deletions(-) diff --git a/satella/__init__.py b/satella/__init__.py index e4b1c621..0522a19c 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 89cedc68..12e1e89e 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 329ac863..f9d54161 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 550b2bc6..b93843e9 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') -- GitLab