Skip to content

Commit

Permalink
Added to_number function to misc_utils.
Browse files Browse the repository at this point in the history
  • Loading branch information
dmichaels-harvard committed Aug 7, 2024
1 parent f0aa788 commit 55da05b
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 9 deletions.
7 changes: 2 additions & 5 deletions dcicutils/misc_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"]
license = "MIT"
Expand Down
5 changes: 2 additions & 3 deletions test/test_misc_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 55da05b

Please sign in to comment.