xloft 0.1.19__py3-none-any.whl → 0.1.21__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/__init__.py CHANGED
@@ -2,13 +2,11 @@
2
2
 
3
3
  Modules exported by this package:
4
4
 
5
- - `NamedTuple`: Class imitates the behavior of the _named tuple_.
5
+ - `namedtuple`: Class imitates the behavior of the _named tuple_.
6
+ - `humanism` - A collection of instruments for converting data to format is convenient for humans.
7
+ - `quantum` - A set of tools for quantum calculations.
6
8
  """
7
9
 
8
- __all__ = (
9
- "to_human_size",
10
- "NamedTuple",
11
- )
10
+ __all__ = ("NamedTuple",)
12
11
 
13
- from xloft.humanism import to_human_size
14
12
  from xloft.namedtuple import NamedTuple
xloft/humanism.py CHANGED
@@ -1,4 +1,4 @@
1
- """Humanism.
1
+ """A collection of instruments for converting data to format is convenient for humans.
2
2
 
3
3
  The module contains the following functions:
4
4
 
@@ -17,6 +17,8 @@ def to_human_size(size: int) -> str:
17
17
  200 bytes
18
18
  >>> to_human_size(1048576)
19
19
  1 MB
20
+ >>> to_human_size(1048575)
21
+ 1023.999 KB
20
22
 
21
23
  Args:
22
24
  size: The number of bytes.
@@ -25,6 +27,9 @@ def to_human_size(size: int) -> str:
25
27
  Returns a humanized string: 200 bytes | 1 KB | 1.5 MB etc.
26
28
  """
27
29
  idx: int = math.floor(math.log(size) / math.log(1024))
28
- human_size: int | float = size if size < 1024 else abs(round(size / pow(1024, idx), 2))
30
+ ndigits: int = [0, 3, 6, 9, 12][idx]
31
+ human_size: int | float = size if size < 1024 else abs(round(size / pow(1024, idx), ndigits))
29
32
  order = ["bytes", "KB", "MB", "GB", "TB"][idx]
30
- return "{:g} {}".format(human_size, order)
33
+ if math.modf(human_size)[0] == 0.0:
34
+ human_size = int(human_size)
35
+ return f"{human_size} {order}"
xloft/quantum.py ADDED
@@ -0,0 +1,116 @@
1
+ """A set of tools for quantum calculations.
2
+
3
+ The module contains the following tools:
4
+
5
+ - `count_qubits()` - Counting the number of qubits of your computer.
6
+ - `LoopMode` - Quantum loop mode.
7
+ - `QuantumLoop` - Divide the cycle into quantums.
8
+ """
9
+
10
+ import concurrent.futures
11
+ import multiprocessing
12
+ from enum import Enum
13
+ from typing import Any, Callable, Iterable, Never, assert_never
14
+
15
+
16
+ def count_qubits() -> int:
17
+ """Counting the number of qubits of your computer.
18
+
19
+ A qubit in a regular computer is some algorithm that is executed in
20
+ one iteration of a cycle in a separate processor thread.
21
+
22
+ Examples:
23
+ >>> from xloft.quantum import count_qubits
24
+ >>> count_qubits()
25
+ 16
26
+
27
+ Returns:
28
+ The number of qubits.
29
+ """
30
+ return multiprocessing.cpu_count()
31
+
32
+
33
+ class LoopMode(Enum):
34
+ """Quantum loop mode."""
35
+
36
+ PROCESS_POOL = 1
37
+ THREAD_POOL = 2
38
+
39
+
40
+ class QuantumLoop:
41
+ """Divide the cycle into quantums.
42
+
43
+ Args:
44
+ quantum: A function that describes the task.
45
+ data: The data that needs to be processed.
46
+ timeout: The maximum number of seconds to wait. If None, then there
47
+ is no limit on the wait time.
48
+ chunksize: The size of the chunks the iterable will be broken into
49
+ before being passed to a child process. This argument is only
50
+ used by ProcessPoolExecutor; it is ignored by
51
+ ThreadPoolExecutor.
52
+ mode: The operating mode for a quantum loop: LoopMode.PROCESS_POOL | LoopMode.THREAD_POOL.
53
+
54
+ Examples:
55
+ >>> from xloft.quantum import LoopMode, QuantumLoop, count_qubits
56
+ >>> def quantum(self, item):
57
+ ... return item * item
58
+ >>> data = range(10)
59
+ >>> qloop = QuantumLoop(quantum, data, mode=LoopMode.PROCESS_POOL)
60
+ >>> qloop.run()
61
+ [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
62
+ """
63
+
64
+ def __init__( # noqa: D107
65
+ self,
66
+ quantum: Callable,
67
+ data: Iterable[Any],
68
+ timeout: float | None = None,
69
+ chunksize: int = 1,
70
+ mode: LoopMode = LoopMode.PROCESS_POOL,
71
+ ) -> None:
72
+ self.quantum = quantum
73
+ self.data = data
74
+ self.timeout = timeout
75
+ self.chunksize = chunksize
76
+ self.mode = mode
77
+
78
+ def process_pool(self) -> list[Any]:
79
+ """Better suitable for operations for which large processor resources are required."""
80
+ with concurrent.futures.ProcessPoolExecutor() as executor:
81
+ results = list(
82
+ executor.map(
83
+ self.quantum,
84
+ self.data,
85
+ timeout=self.timeout,
86
+ chunksize=self.chunksize,
87
+ ),
88
+ )
89
+ return results
90
+
91
+ def thread_pool(self) -> list[Any]:
92
+ """More suitable for tasks related to input-output
93
+ (for example, network queries, file operations),
94
+ where GIL is freed during input-output operations.""" # noqa: D205, D209
95
+ with concurrent.futures.ThreadPoolExecutor() as executor:
96
+ results = list(
97
+ executor.map(
98
+ self.quantum,
99
+ self.data,
100
+ timeout=self.timeout,
101
+ chunksize=self.chunksize,
102
+ ),
103
+ )
104
+ return results
105
+
106
+ def run(self) -> list[Any]:
107
+ """Run the quantum loop."""
108
+ results: list[Any] = []
109
+ match self.mode.value:
110
+ case 1:
111
+ results = self.process_pool()
112
+ case 2:
113
+ results = self.thread_pool()
114
+ case _ as unreachable:
115
+ assert_never(Never(unreachable))
116
+ return results
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: xloft
3
- Version: 0.1.19
3
+ Version: 0.1.21
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
@@ -60,7 +60,7 @@ Description-Content-Type: text/markdown
60
60
  <a href="https://github.com/kebasyaty/xloft/releases/" alt="GitHub release"><img src="https://img.shields.io/github/release/kebasyaty/xloft" alt="GitHub release"></a>
61
61
  </p>
62
62
  <p align="center">
63
- Currently, the collection is represented by two elements of `NamedTuple` and `to_human_size`.
63
+ Currently, the collection is represented by three modules of `NamedTuple`, `Humanism` and `Quantum`.
64
64
  </p>
65
65
  </p>
66
66
  </div>
@@ -153,10 +153,10 @@ del nt.y # => raise: AttributeCannotBeDelete
153
153
  del nt._id # => raise: AttributeCannotBeDelete
154
154
  ```
155
155
 
156
- - **to_human_size**
156
+ - **Humanism**
157
157
 
158
158
  ```python
159
- from xloft import to_human_size
159
+ from xloft.humanism import to_human_size
160
160
 
161
161
 
162
162
  s = to_human_size(200)
@@ -164,6 +164,27 @@ print(s) # => 200 bytes
164
164
 
165
165
  s = to_human_size(1048576)
166
166
  print(s) # => 1 MB
167
+
168
+ s = to_human_size(1048575)
169
+ print(s) # => 1023.999 KB
170
+ ```
171
+
172
+ - **Quantum**
173
+
174
+ ```python
175
+ from xloft.quantum import LoopMode, QuantumLoop, count_qubits
176
+
177
+ num = count_qubits()
178
+ print(num) # => 16
179
+
180
+ def quantum(self, item):
181
+ return item * item
182
+
183
+ data = range(10)
184
+
185
+ qloop = QuantumLoop(quantum, data, mode=LoopMode.PROCESS_POOL)
186
+ results = qloop.run()
187
+ print(results) # => [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
167
188
  ```
168
189
 
169
190
  ## Changelog
@@ -0,0 +1,10 @@
1
+ xloft/__init__.py,sha256=FZ2m329mXkIm4ETgl0ERwEzRzsFvd-_1ug2Eo1kSVo0,371
2
+ xloft/errors.py,sha256=GYXvi2l01VUDQSs6skiOfQsKLF6tFuUhJMqNkL7BJNI,857
3
+ xloft/humanism.py,sha256=I6q56MhviapBSWF4B_1xd77TDwQMM1jrkbl1N-fOURc,1068
4
+ xloft/namedtuple.py,sha256=OkAHqMaV4hN6Qj_oaOYQ9-y9x4Muv4mNrBn48T6RpiI,6818
5
+ xloft/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
6
+ xloft/quantum.py,sha256=u55hMPPveuVTS8EPADB8Ep3hHI4b13hUXVKtdS2xsmQ,3713
7
+ xloft-0.1.21.dist-info/METADATA,sha256=NI-s2RczH7kQS7hoWStLKxIhHlOsgbedFYDcmMtAUIg,7106
8
+ xloft-0.1.21.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
9
+ xloft-0.1.21.dist-info/licenses/LICENSE,sha256=2zZINd6m_jNYlowdQImlEizyhSui5cBAJZRhWQURcEc,1095
10
+ xloft-0.1.21.dist-info/RECORD,,
@@ -1,9 +0,0 @@
1
- xloft/__init__.py,sha256=zvpDtBWkgaj3oTMsDSWw2SLl7LMy7zO2SyvEhiKwzmg,287
2
- xloft/errors.py,sha256=GYXvi2l01VUDQSs6skiOfQsKLF6tFuUhJMqNkL7BJNI,857
3
- xloft/humanism.py,sha256=KQ-bIQJrVUZ3Xc5mVRQ07bvjq1LJzROr8tTV3RJWC0o,822
4
- xloft/namedtuple.py,sha256=OkAHqMaV4hN6Qj_oaOYQ9-y9x4Muv4mNrBn48T6RpiI,6818
5
- xloft/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
6
- xloft-0.1.19.dist-info/METADATA,sha256=da1niy9aKDAlolNSGNvI2Z9vzhbHkgBs7MJgPMRZsVM,6695
7
- xloft-0.1.19.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
8
- xloft-0.1.19.dist-info/licenses/LICENSE,sha256=2zZINd6m_jNYlowdQImlEizyhSui5cBAJZRhWQURcEc,1095
9
- xloft-0.1.19.dist-info/RECORD,,
File without changes