From 55da05b3d9068f6e33542c0534cd390d677fcf0e Mon Sep 17 00:00:00 2001 From: David Michaels Date: Wed, 7 Aug 2024 15:30:20 -0400 Subject: [PATCH] Added to_number function to misc_utils. --- dcicutils/misc_utils.py | 7 ++----- pyproject.toml | 2 +- test/test_misc_utils.py | 5 ++--- 3 files changed, 5 insertions(+), 9 deletions(-) diff --git a/dcicutils/misc_utils.py b/dcicutils/misc_utils.py index f11a1603c..b46b8bede 100644 --- a/dcicutils/misc_utils.py +++ b/dcicutils/misc_utils.py @@ -1005,16 +1005,12 @@ def to_integer(value: str, fallback: Optional[Any] = None, strict: bool = False) _MULTIPLIER_SUFFIXES = { "K": _MULTIPLIER_K, - "Kb": _MULTIPLIER_K, "KB": _MULTIPLIER_K, "M": _MULTIPLIER_M, - "Mb": _MULTIPLIER_M, "MB": _MULTIPLIER_M, "G": _MULTIPLIER_G, - "Gb": _MULTIPLIER_G, "GB": _MULTIPLIER_G, "T": _MULTIPLIER_T, - "Tb": _MULTIPLIER_T, "TB": _MULTIPLIER_T } @@ -1051,8 +1047,9 @@ def to_number(value: str, return fallback if allow_multiplier_suffix is True: + value_upper = value.upper() for suffix in _MULTIPLIER_SUFFIXES: - if value.endswith(suffix): + if value_upper.endswith(suffix): value_multiplier *= _MULTIPLIER_SUFFIXES[suffix] if not (value := value[:-len(suffix)].strip()): return fallback diff --git a/pyproject.toml b/pyproject.toml index b76de206c..6486d1494 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "dcicutils" -version = "8.13.3.1b23" # TODO: To become 8.14.0 +version = "8.13.3.1b24" # TODO: To become 8.14.0 description = "Utility package for interacting with the 4DN Data Portal and other 4DN resources" authors = ["4DN-DCIC Team "] license = "MIT" diff --git a/test/test_misc_utils.py b/test/test_misc_utils.py index 83216da5d..5a4262d47 100644 --- a/test/test_misc_utils.py +++ b/test/test_misc_utils.py @@ -3734,13 +3734,13 @@ def test_to_number(): assert to_number("1,234,567") is None assert to_number("27500") == 27500 assert to_number("789", allow_float=True) == 789.0 - # assert type(to_number("789", allow_float=True)) == float + assert type(to_number("789", allow_float=True)) == int assert to_number("27500", allow_commas=True) == 27500 assert to_number("1,234,567", allow_commas=True) == 1234567 assert to_number("1234.0567", allow_float=True) == 1234.0567 assert to_number("1K", allow_multiplier_suffix=True) == 1000 assert to_number("1Kb", allow_multiplier_suffix=True) == 1000 - assert to_number("1KB", allow_multiplier_suffix=True) == 1000 + assert to_number("1kB", allow_multiplier_suffix=True) == 1000 assert to_number("2M", allow_multiplier_suffix=True) == 2000000 assert to_number("2Mb", allow_multiplier_suffix=True) == 2000000 assert to_number("2MB", allow_multiplier_suffix=True) == 2000000 @@ -3750,7 +3750,6 @@ def test_to_number(): assert to_number("4T", allow_multiplier_suffix=True) == 4000000000000 assert to_number("4Tb", allow_multiplier_suffix=True) == 4000000000000 assert to_number("4TB", allow_multiplier_suffix=True) == 4000000000000 - assert to_number("4k", allow_multiplier_suffix=True) is None assert to_number("1,234,567K", allow_commas=True) is None assert to_number("1,234,567K", allow_commas=True, allow_multiplier_suffix=True) == 1234567000 assert to_number("-1,234,567K", allow_commas=True, allow_multiplier_suffix=True) == -1234567000