dcicutils 8.13.3.1b26__py3-none-any.whl → 8.13.3.1b27__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
dcicutils/misc_utils.py CHANGED
@@ -1027,24 +1027,32 @@ def to_number(value: str,
1027
1027
  allow_multiplier_suffix: bool = False,
1028
1028
  fallback: Optional[Union[int, float]] = None) -> Optional[Union[int, float]]:
1029
1029
  """
1030
- Converts the given string value to an int, or float if as_float is True, or None if malformed.
1031
- If allow_commas is True then allow commas (only) every three digits. If allow_multiplier_suffix
1032
- is True allow any of K, KB; or M, MB; or G, or GB; or T, TB (case-insensitive), to mean multiply
1033
- value by one thousand; one million; one billion; or one trillion; respectively. If as_float is True
1034
- then value is parsed and returned as a float rather than int. Note that even if as_float is False,
1035
- values that might look like a float, can be an int, for example, "1.5K", returns 1500 as an int;
1036
- but "1.5002K" returns None, i.e. since 1.5002K is 1500.2 which is not an int.
1030
+ Converts the given string value to an int, or float if as_float is True,
1031
+ or None or the give fallback value if malformed.
1032
+
1033
+ If allow_commas is True then allows appropriately placed commas (i.e. every three digits).
1034
+
1035
+ If allow_multiplier_suffix is True allows any of the multiplier suffixes K, KB; M, MB;
1036
+ or G, GB; or T, TB; all case-insensitive; which will multiply the value by a thousand;
1037
+ a million; a billion; or a trillion; respectively.
1038
+
1039
+ If as_float is True then value is parsed and returned as a float rather than int.
1040
+
1041
+ Note that even if as_float is False, values that might LOOK like a float, can be
1042
+ an int, for example, "1.5K", returns 1500 as an int since it is; but "1.5002K"
1043
+ is malformed, since 1.5002K is equivalent to 1500.2 which is not an int.
1037
1044
  """
1038
1045
 
1039
1046
  if not (isinstance(value, str) and (value := value.strip())):
1047
+ # Just in case the value is already the actual/desired return type.
1040
1048
  if as_float is True:
1041
1049
  return float(value) if isinstance(value, (float, int)) else fallback
1042
1050
  else:
1043
1051
  return value if isinstance(value, int) else fallback
1044
1052
 
1045
1053
  value_multiplier = 1
1046
- value_negative = False
1047
1054
  value_fraction = None
1055
+ value_negative = False
1048
1056
 
1049
1057
  if value.startswith("-"):
1050
1058
  if not (value := value[1:].strip()):
@@ -1080,6 +1088,7 @@ def to_number(value: str,
1080
1088
  value = value[:value_dot_zero_suffix.start()]
1081
1089
 
1082
1090
  if (allow_commas is True) and ("," in value):
1091
+ # Make sure that any commas are properly placed.
1083
1092
  if not re.fullmatch(r"(-?\d{1,3}(,\d{3})*)", value):
1084
1093
  return fallback
1085
1094
  value = value.replace(",", "")
@@ -1090,7 +1099,15 @@ def to_number(value: str,
1090
1099
  value = float(value) if as_float is True else int(value)
1091
1100
 
1092
1101
  if value_fraction:
1093
- value_float = (float(value) + value_fraction) * float(value_multiplier)
1102
+ value_float = float(value) + value_fraction
1103
+ # Here we do NOT simply do: value_float *= float(value_multiplier);
1104
+ # because it introduces obvious FLOATing point precision ERRORs; for example,
1105
+ # to_integer("1.5678K", allow_multiplier_suffix=True) would yield 1567.8000000000002
1106
+ # if we simply did 1.5678 * 1000.0; but doing the multiplication 10 at a time obviates this
1107
+ # idiosyncracy yielding 1567.8; this ASSUMES that the multipliers are simple multiples of 10.
1108
+ while value_multiplier > 1:
1109
+ value_float *= 10
1110
+ value_multiplier /= 10
1094
1111
  if as_float is True:
1095
1112
  value = value_float
1096
1113
  else:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: dcicutils
3
- Version: 8.13.3.1b26
3
+ Version: 8.13.3.1b27
4
4
  Summary: Utility package for interacting with the 4DN Data Portal and other 4DN resources
5
5
  Home-page: https://github.com/4dn-dcic/utils
6
6
  License: MIT
@@ -45,7 +45,7 @@ dcicutils/license_policies/park-lab-gpl-pipeline.jsonc,sha256=vLZkwm3Js-kjV44nug
45
45
  dcicutils/license_policies/park-lab-pipeline.jsonc,sha256=9qlY0ASy3iUMQlr3gorVcXrSfRHnVGbLhkS427UaRy4,283
46
46
  dcicutils/license_utils.py,sha256=2Yxnh1T1iuMe6wluwbvpFO_zYSGPxB4-STAMc-vz-YM,47202
47
47
  dcicutils/log_utils.py,sha256=7pWMc6vyrorUZQf-V-M3YC6zrPgNhuV_fzm9xqTPph0,10883
48
- dcicutils/misc_utils.py,sha256=zkzCwtuqdq90HoOP8kLuJw4ldeE8AdRNBduW2KzEBfQ,114108
48
+ dcicutils/misc_utils.py,sha256=R3nUtmx4nynmdTfc98w3CV2JKYCPZPRBijets1nWM20,114877
49
49
  dcicutils/obfuscation_utils.py,sha256=fo2jOmDRC6xWpYX49u80bVNisqRRoPskFNX3ymFAmjw,5963
50
50
  dcicutils/opensearch_utils.py,sha256=V2exmFYW8Xl2_pGFixF4I2Cc549Opwe4PhFi5twC0M8,1017
51
51
  dcicutils/portal_object_utils.py,sha256=Az3n1aL-PQkN5gOFE6ZqC2XkYsqiwKlq7-tZggs1QN4,11062
@@ -75,8 +75,8 @@ dcicutils/trace_utils.py,sha256=g8kwV4ebEy5kXW6oOrEAUsurBcCROvwtZqz9fczsGRE,1769
75
75
  dcicutils/validation_utils.py,sha256=cMZIU2cY98FYtzK52z5WUYck7urH6JcqOuz9jkXpqzg,14797
76
76
  dcicutils/variant_utils.py,sha256=2H9azNx3xAj-MySg-uZ2SFqbWs4kZvf61JnK6b-h4Qw,4343
77
77
  dcicutils/zip_utils.py,sha256=_Y9EmL3D2dUZhxucxHvrtmmlbZmK4FpSsHEb7rGSJLU,3265
78
- dcicutils-8.13.3.1b26.dist-info/LICENSE.txt,sha256=qnwSmfnEWMl5l78VPDEzAmEbLVrRqQvfUQiHT0ehrOo,1102
79
- dcicutils-8.13.3.1b26.dist-info/METADATA,sha256=jQjbtF24ujN4fAqJqGPzqMmdDZMI78IAeZlpBs3WAWc,3440
80
- dcicutils-8.13.3.1b26.dist-info/WHEEL,sha256=7Z8_27uaHI_UZAc4Uox4PpBhQ9Y5_modZXWMxtUi4NU,88
81
- dcicutils-8.13.3.1b26.dist-info/entry_points.txt,sha256=W6kEWdUJk9tQ4myAgpehPdebcwvCAZ7UgB-wyPgDUMg,335
82
- dcicutils-8.13.3.1b26.dist-info/RECORD,,
78
+ dcicutils-8.13.3.1b27.dist-info/LICENSE.txt,sha256=qnwSmfnEWMl5l78VPDEzAmEbLVrRqQvfUQiHT0ehrOo,1102
79
+ dcicutils-8.13.3.1b27.dist-info/METADATA,sha256=w4K_ZFbf-pBM62p35V5NoZS4HQWx0jlimtBQ2z73pQE,3440
80
+ dcicutils-8.13.3.1b27.dist-info/WHEEL,sha256=7Z8_27uaHI_UZAc4Uox4PpBhQ9Y5_modZXWMxtUi4NU,88
81
+ dcicutils-8.13.3.1b27.dist-info/entry_points.txt,sha256=W6kEWdUJk9tQ4myAgpehPdebcwvCAZ7UgB-wyPgDUMg,335
82
+ dcicutils-8.13.3.1b27.dist-info/RECORD,,