xloft 0.4.5__py3-none-any.whl → 0.4.7__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.

Potentially problematic release.


This version of xloft might be problematic. Click here for more details.

xloft/human.py CHANGED
@@ -7,10 +7,32 @@ The module contains the following functions:
7
7
 
8
8
  from __future__ import annotations
9
9
 
10
+ __all__ = (
11
+ "to_human_size",
12
+ "get_cache_human_size",
13
+ )
14
+
10
15
  import math
11
16
 
17
+ # To caching the results from to_human_size method.
18
+ _cache_human_size: dict[int, str] = {}
19
+
20
+
21
+ def get_cache_human_size() -> dict[int, str]:
22
+ """Get a copy of variable _cach_human_size.
23
+
24
+ Hint: To tests.
25
+ """
26
+ return _cache_human_size.copy()
27
+
28
+
29
+ def clean_cache_human_size() -> None:
30
+ """Reset of variable _cach_human_size."""
31
+ global _cache_human_size # noqa: PLW0603
32
+ _cache_human_size = {}
33
+
12
34
 
13
- def to_human_size(size: int) -> str:
35
+ def to_human_size(n_bytes: int) -> str:
14
36
  """Convert number of bytes to readable format.
15
37
 
16
38
  Examples:
@@ -23,15 +45,20 @@ def to_human_size(size: int) -> str:
23
45
  1023.999 KB
24
46
 
25
47
  Args:
26
- size: The number of bytes.
48
+ n_bytes: The number of bytes.
27
49
 
28
50
  Returns:
29
51
  Returns a humanized string: 200 bytes | 1 KB | 1.5 MB etc.
30
52
  """
31
- idx: int = math.floor(math.log(size) / math.log(1024))
53
+ result: str | None = _cache_human_size.get(n_bytes)
54
+ if result is not None:
55
+ return result
56
+ idx: int = math.floor(math.log(n_bytes) / math.log(1024))
32
57
  ndigits: int = [0, 3, 6, 9, 12][idx]
33
- human_size: int | float = size if size < 1024 else abs(round(size / pow(1024, idx), ndigits))
58
+ human_size: int | float = n_bytes if n_bytes < 1024 else abs(round(n_bytes / pow(1024, idx), ndigits))
34
59
  order = ["bytes", "KB", "MB", "GB", "TB"][idx]
35
60
  if math.modf(human_size)[0] == 0.0:
36
61
  human_size = int(human_size)
37
- return f"{human_size} {order}"
62
+ result = f"{human_size} {order}"
63
+ _cache_human_size[n_bytes] = result
64
+ return result
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: xloft
3
- Version: 0.4.5
3
+ Version: 0.4.7
4
4
  Summary: (XLOFT) X-Library of tools
5
5
  Project-URL: Homepage, https://github.com/kebasyaty/xloft
6
6
  Project-URL: Repository, https://github.com/kebasyaty/xloft
@@ -1,10 +1,10 @@
1
1
  xloft/__init__.py,sha256=UL4IR8EcFjgWoQFayl6FrK2u8QmZytGCwa_E1jDiCmE,475
2
2
  xloft/errors.py,sha256=hZcmF0QVVdvE5oM1jsXymRk_pPGgDSnUDM9wx9zJAYQ,895
3
- xloft/human.py,sha256=odRbUF_58YuFWC_VQeRDZ-6ifHDiiAH2HNb5G5K6JIM,1106
3
+ xloft/human.py,sha256=WQbVOKpOvzGot-c7f3xitbS05JUIQmj3dreGtRV6GA0,1792
4
4
  xloft/namedtuple.py,sha256=a_l3bZF-L2I7MGxuF2CXzAHgNai-Vyj6SY1ODwxs7TU,6856
5
5
  xloft/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
6
6
  xloft/quantum.py,sha256=ZkFqqFROC9j5JbwJmej3yL9VJcqElPrrimFTz0Pe3Mk,4679
7
- xloft-0.4.5.dist-info/METADATA,sha256=0cAqqMXQdh5I_DMw9Rm5UU6agFU13OlYlyOpNpYZIgc,7241
8
- xloft-0.4.5.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
9
- xloft-0.4.5.dist-info/licenses/LICENSE,sha256=2zZINd6m_jNYlowdQImlEizyhSui5cBAJZRhWQURcEc,1095
10
- xloft-0.4.5.dist-info/RECORD,,
7
+ xloft-0.4.7.dist-info/METADATA,sha256=n2eO_R6lJ7VedihIUh4TiLYdDS9CaJR36hQxZyxpYw4,7241
8
+ xloft-0.4.7.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
9
+ xloft-0.4.7.dist-info/licenses/LICENSE,sha256=2zZINd6m_jNYlowdQImlEizyhSui5cBAJZRhWQURcEc,1095
10
+ xloft-0.4.7.dist-info/RECORD,,
File without changes