klaatu-python 2.2.0__py3-none-any.whl → 2.3.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
klaatu_python/__init__.py CHANGED
@@ -1 +0,0 @@
1
- __version__ = "2.2.0"
klaatu_python/utils.py CHANGED
@@ -1,5 +1,6 @@
1
1
  import re
2
2
  from datetime import date, timedelta
3
+ from locale import format_string, localeconv
3
4
  from math import ceil, floor, log10
4
5
  from typing import Any, Callable, Iterable, Iterator, Literal, Sequence, TypeVar, overload
5
6
  from urllib.parse import parse_qs, urlencode, urlsplit, urlunsplit
@@ -69,6 +70,20 @@ def filter_values_not_null(d: dict) -> dict:
69
70
  return {k: v for k, v in d.items() if v is not None}
70
71
 
71
72
 
73
+ def first_not_null(*values: _T | None) -> _T:
74
+ ret = first_not_null_or_null(*values)
75
+ if ret is None:
76
+ raise TypeError("All values are None")
77
+ return ret
78
+
79
+
80
+ def first_not_null_or_null(*values: _T | None) -> _T | None:
81
+ for value in values:
82
+ if value is not None:
83
+ return value
84
+ return None
85
+
86
+
72
87
  def getitem_nullable(seq: Iterable[_T], idx: int, cond: Callable[[_T], bool] | None = None) -> _T | None:
73
88
  """
74
89
  If `seq` has an item at position `idx`, return that item. Otherwise return
@@ -227,6 +242,35 @@ def is_truthy(value: Any) -> bool:
227
242
  return bool(value)
228
243
 
229
244
 
245
+ def localize_float(
246
+ value: float,
247
+ grouping: bool = True,
248
+ min_decimals: int | None = None,
249
+ max_decimals: int | None = None,
250
+ ) -> str:
251
+ if max_decimals is None:
252
+ max_decimals = 15
253
+
254
+ if min_decimals is not None and min_decimals > max_decimals:
255
+ raise ValueError("min_decimals must be <= max_decimals")
256
+ if 15 < (min_decimals or 0) < 0:
257
+ raise ValueError("min_decimals must be >= 0 and <= 15")
258
+ if 15 < (max_decimals or 0) < 0:
259
+ raise ValueError("max_decimals must be >= 0 and <= 15")
260
+
261
+ decimal_separator = localeconv()["decimal_point"]
262
+ ret = format_string("%.*f", (max_decimals, value), grouping=grouping)
263
+
264
+ if decimal_separator in ret:
265
+ base, decimals = ret.split(decimal_separator)
266
+ decimals = decimals.rstrip("0").ljust(min_decimals or 0, "0")
267
+ if not decimals:
268
+ return base
269
+ return base + decimal_separator + decimals
270
+
271
+ return ret
272
+
273
+
230
274
  def nonulls(items: Iterable[_T | None]) -> list[_T]:
231
275
  """Just filters away None values from `items`."""
232
276
  return [item for item in items if item is not None]
@@ -1,11 +1,11 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: klaatu-python
3
- Version: 2.2.0
3
+ Version: 2.3.0
4
4
  Author-email: Robert Huselius <robert@huseli.us>
5
5
  License-Expression: GPL-3.0-only
6
6
  Project-URL: Homepage, https://github.com/Eboreg/klaatu-python
7
- Project-URL: Source, https://github.com/Eboreg/klaatu-python
8
7
  Project-URL: Issues, https://github.com/Eboreg/klaatu-python/issues
8
+ Project-URL: Source, https://github.com/Eboreg/klaatu-python
9
9
  Requires-Python: >=3.7
10
10
  Description-Content-Type: text/markdown
11
11
  License-File: LICENSE
@@ -34,7 +34,7 @@ In `pyproject.toml`:
34
34
  ```
35
35
  [project]
36
36
  dependencies = [
37
- "klaatu_python @ git+https://git@github.com/Eboreg/klaatu-python.git",
37
+ "klaatu-python",
38
38
  ...
39
39
  ]
40
40
  ```
@@ -0,0 +1,9 @@
1
+ klaatu_python/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
+ klaatu_python/case.py,sha256=2Tya4dlBrqkwhQZu9PTLEA0SiWSIoKw12Ar2UJZssd4,1692
3
+ klaatu_python/lock.py,sha256=STJpYdpYxHJDges9DFv08kC4hGa1IPtHMNxanrQlioM,1393
4
+ klaatu_python/utils.py,sha256=4YJGV4gc1svrU_M6TfxsQlDFp9UG_DFCV4LurXqgGdQ,11097
5
+ klaatu_python-2.3.0.dist-info/licenses/LICENSE,sha256=OXLcl0T2SZ8Pmy2_dmlvKuetivmyPd5m1q-Gyd-zaYY,35149
6
+ klaatu_python-2.3.0.dist-info/METADATA,sha256=EDL3degZoyTe-EAKpl79agfpbSoelLHHCLebXlG3BIE,928
7
+ klaatu_python-2.3.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
8
+ klaatu_python-2.3.0.dist-info/top_level.txt,sha256=JxX5CCI_SC5L4jbAG7acjKwIPVWmCl46ofDZNZtnV1o,14
9
+ klaatu_python-2.3.0.dist-info/RECORD,,
@@ -1,9 +0,0 @@
1
- klaatu_python/__init__.py,sha256=DKk-1b-rZsJFxFi1JoJ7TmEvIEQ0rf-C9HAZWwvjuM0,22
2
- klaatu_python/case.py,sha256=2Tya4dlBrqkwhQZu9PTLEA0SiWSIoKw12Ar2UJZssd4,1692
3
- klaatu_python/lock.py,sha256=STJpYdpYxHJDges9DFv08kC4hGa1IPtHMNxanrQlioM,1393
4
- klaatu_python/utils.py,sha256=SffgWZV0AbMe63VfK0REz7bjzWutPCLdin3vBb3xc6o,9779
5
- klaatu_python-2.2.0.dist-info/licenses/LICENSE,sha256=OXLcl0T2SZ8Pmy2_dmlvKuetivmyPd5m1q-Gyd-zaYY,35149
6
- klaatu_python-2.2.0.dist-info/METADATA,sha256=eSqjgxZZ4301cxMuweJJFHWr2nWzvzsGxUwBJvzcdVg,982
7
- klaatu_python-2.2.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
8
- klaatu_python-2.2.0.dist-info/top_level.txt,sha256=JxX5CCI_SC5L4jbAG7acjKwIPVWmCl46ofDZNZtnV1o,14
9
- klaatu_python-2.2.0.dist-info/RECORD,,