xloft 0.1.20__py3-none-any.whl → 0.1.22__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
@@ -4,12 +4,9 @@ Modules exported by this package:
4
4
 
5
5
  - `namedtuple`: Class imitates the behavior of the _named tuple_.
6
6
  - `humanism` - A collection of instruments for converting data to format is convenient for humans.
7
+ - `quantum` - A set of tools for quantum calculations.
7
8
  """
8
9
 
9
- __all__ = (
10
- "to_human_size",
11
- "NamedTuple",
12
- )
10
+ __all__ = ("NamedTuple",)
13
11
 
14
- from xloft.humanism import to_human_size
15
12
  from xloft.namedtuple import NamedTuple
xloft/quantum.py ADDED
@@ -0,0 +1,117 @@
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` - Separation of 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 quantum of algorithm that is executed in
20
+ one iteration of a cycle in a separate processor thread.
21
+
22
+ Quantum is a function with an algorithm of task for data processing.
23
+
24
+ Examples:
25
+ >>> from xloft.quantum import count_qubits
26
+ >>> count_qubits()
27
+ 16
28
+
29
+ Returns:
30
+ The number of qubits.
31
+ """
32
+ return multiprocessing.cpu_count()
33
+
34
+
35
+ class LoopMode(Enum):
36
+ """Quantum loop mode."""
37
+
38
+ PROCESS_POOL = 1
39
+ THREAD_POOL = 2
40
+
41
+
42
+ class QuantumLoop:
43
+ """Separation of the cycle into quantums.
44
+
45
+ Args:
46
+ quantum: A function that describes the task.
47
+ data: The data that needs to be processed.
48
+ timeout: The maximum number of seconds to wait. If None, then there
49
+ is no limit on the wait time.
50
+ chunksize: The size of the chunks the iterable will be broken into
51
+ before being passed to a child process. This argument is only
52
+ used by ProcessPoolExecutor; it is ignored by ThreadPoolExecutor.
53
+ mode: The operating mode for a quantum loop: LoopMode.PROCESS_POOL | LoopMode.THREAD_POOL.
54
+
55
+ Examples:
56
+ >>> from xloft.quantum import LoopMode, QuantumLoop, count_qubits
57
+ >>> def quantum(self, item):
58
+ ... return item * item
59
+ >>> data = range(10)
60
+ >>> qloop = QuantumLoop(quantum, data, mode=LoopMode.PROCESS_POOL)
61
+ >>> qloop.run()
62
+ [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
63
+ """
64
+
65
+ def __init__( # noqa: D107
66
+ self,
67
+ quantum: Callable,
68
+ data: Iterable[Any],
69
+ timeout: float | None = None,
70
+ chunksize: int = 1,
71
+ mode: LoopMode = LoopMode.PROCESS_POOL,
72
+ ) -> None:
73
+ self.quantum = quantum
74
+ self.data = data
75
+ self.timeout = timeout
76
+ self.chunksize = chunksize
77
+ self.mode = mode
78
+
79
+ def process_pool(self) -> list[Any]:
80
+ """Better suitable for operations for which large processor resources are required."""
81
+ with concurrent.futures.ProcessPoolExecutor() as executor:
82
+ results = list(
83
+ executor.map(
84
+ self.quantum,
85
+ self.data,
86
+ timeout=self.timeout,
87
+ chunksize=self.chunksize,
88
+ ),
89
+ )
90
+ return results
91
+
92
+ def thread_pool(self) -> list[Any]:
93
+ """More suitable for tasks related to input-output
94
+ (for example, network queries, file operations),
95
+ where GIL is freed during input-output operations.""" # noqa: D205, D209
96
+ with concurrent.futures.ThreadPoolExecutor() as executor:
97
+ results = list(
98
+ executor.map(
99
+ self.quantum,
100
+ self.data,
101
+ timeout=self.timeout,
102
+ chunksize=self.chunksize,
103
+ ),
104
+ )
105
+ return results
106
+
107
+ def run(self) -> list[Any]:
108
+ """Run the quantum loop."""
109
+ results: list[Any] = []
110
+ match self.mode.value:
111
+ case 1:
112
+ results = self.process_pool()
113
+ case 2:
114
+ results = self.thread_pool()
115
+ case _ as unreachable:
116
+ assert_never(Never(unreachable))
117
+ return results
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: xloft
3
- Version: 0.1.20
3
+ Version: 0.1.22
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)
@@ -169,6 +169,25 @@ s = to_human_size(1048575)
169
169
  print(s) # => 1023.999 KB
170
170
  ```
171
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(item):
181
+ """Task."""
182
+ return item * item
183
+
184
+ data = range(10)
185
+
186
+ qloop = QuantumLoop(quantum, data, mode=LoopMode.PROCESS_POOL)
187
+ results = qloop.run()
188
+ print(results) # => [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
189
+ ```
190
+
172
191
  ## Changelog
173
192
 
174
193
  [View the change history.](https://github.com/kebasyaty/xloft/blob/main/CHANGELOG.md "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=jkDo4mMTy2FXzeYWRDMG_iQ6DjixdeO1ujPNUIZF688,3867
7
+ xloft-0.1.22.dist-info/METADATA,sha256=6qB7pAWa-hf7XygBqewmZZERodNsQXYQbCVzFcyz-Xs,7116
8
+ xloft-0.1.22.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
9
+ xloft-0.1.22.dist-info/licenses/LICENSE,sha256=2zZINd6m_jNYlowdQImlEizyhSui5cBAJZRhWQURcEc,1095
10
+ xloft-0.1.22.dist-info/RECORD,,
@@ -1,9 +0,0 @@
1
- xloft/__init__.py,sha256=1gISK3F9XZoQH08YjP0YN5HPUxRgpEe8qih1To2cLT8,387
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-0.1.20.dist-info/METADATA,sha256=mQrIKVa1QnRhYXDyzaOnAEl6gryQHEOIgzFtx58GbBQ,6750
7
- xloft-0.1.20.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
8
- xloft-0.1.20.dist-info/licenses/LICENSE,sha256=2zZINd6m_jNYlowdQImlEizyhSui5cBAJZRhWQURcEc,1095
9
- xloft-0.1.20.dist-info/RECORD,,
File without changes