xloft 0.4.3__py3-none-any.whl → 0.4.5__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
@@ -5,7 +5,6 @@ Modules exported by this package:
5
5
  - `namedtuple`: Class imitates the behavior of the _named tuple_.
6
6
  - `human` - A collection of instruments for converting data to format is convenient for humans.
7
7
  - `quantum` - A set of tools for quantum calculations.
8
- Hint: uv add xloft[quantum]
9
8
  """
10
9
 
11
10
  from __future__ import annotations
xloft/quantum.py ADDED
@@ -0,0 +1,132 @@
1
+ """A set of tools for quantum calculations.
2
+
3
+ A Qubit in a regular computer is quantum of algorithm that is executed in
4
+ one iteration of a cycle in a separate processor thread.
5
+
6
+ Quantum is a function with an algorithm of task for data processing.
7
+
8
+ In this case, the Qubit is not a single information,
9
+ but it is a concept of the principle of operation of quantum calculations on a regular computer.
10
+
11
+ The module contains the following tools:
12
+
13
+ - `LoopMode` - Quantum loop mode.
14
+ - `count_qubits()` - Counting the number of conceptual qubits of your computer.
15
+ - `QuantumLoop` - Separation of the cycle into quantum algorithms for multiprocessing data processing.
16
+ """
17
+
18
+ from __future__ import annotations
19
+
20
+ import concurrent.futures
21
+ import multiprocessing
22
+ from collections.abc import Callable, Iterable
23
+ from enum import Enum
24
+ from typing import Any, Never, assert_never
25
+
26
+
27
+ class LoopMode(Enum):
28
+ """Quantum loop mode."""
29
+
30
+ PROCESS_POOL = 1
31
+ THREAD_POOL = 2
32
+
33
+
34
+ def count_qubits() -> int:
35
+ """Counting the number of conceptual qubits of your computer.
36
+
37
+ Conceptual qubit is quantum of algorithm (task) that is executed in
38
+ iterations of a cycle in a separate processor thread.
39
+
40
+ Quantum of algorithm is a function for data processing.
41
+
42
+ Examples:
43
+ >>> from xloft.quantum import count_qubits
44
+ >>> count_qubits()
45
+ 16
46
+
47
+ Returns:
48
+ The number of conceptual qubits.
49
+ """
50
+ return multiprocessing.cpu_count()
51
+
52
+
53
+ class QuantumLoop:
54
+ """Separation of the cycle into quantum algorithms for multiprocessing data processing.
55
+
56
+ Examples:
57
+ >>> from xloft.quantum import QuantumLoop
58
+ >>> def task(item):
59
+ ... return item * item
60
+ >>> data = range(10)
61
+ >>> QuantumLoop(task, data).run()
62
+ [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
63
+
64
+ Args:
65
+ task: Function with a task algorithm.
66
+ data: The data that needs to be processed.
67
+ max_workers: The maximum number of processes that can be used to
68
+ execute the given calls. If None or not given then as many
69
+ worker processes will be created as the machine has processors.
70
+ timeout: The maximum number of seconds to wait. If None, then there
71
+ is no limit on the wait time.
72
+ chunksize: The size of the chunks the iterable will be broken into
73
+ before being passed to a child process. This argument is only
74
+ used by ProcessPoolExecutor; it is ignored by ThreadPoolExecutor.
75
+ mode: The operating mode for a quantum loop: LoopMode.PROCESS_POOL | LoopMode.THREAD_POOL.
76
+ """
77
+
78
+ def __init__( # noqa: D107
79
+ self,
80
+ task: Callable,
81
+ data: Iterable[Any],
82
+ max_workers: int | None = None,
83
+ timeout: float | None = None,
84
+ chunksize: int = 1,
85
+ mode: LoopMode = LoopMode.PROCESS_POOL,
86
+ ) -> None:
87
+ self.quantum = task
88
+ self.data = data
89
+ self.max_workers = max_workers
90
+ self.timeout = timeout
91
+ self.chunksize = chunksize
92
+ self.mode = mode
93
+
94
+ def process_pool(self) -> list[Any]:
95
+ """Better suitable for operations for which large processor resources are required."""
96
+ with concurrent.futures.ProcessPoolExecutor(self.max_workers) 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 # noqa: RET504
106
+
107
+ def thread_pool(self) -> list[Any]:
108
+ """More suitable for tasks related to input-output
109
+ (for example, network queries, file operations),
110
+ where GIL is freed during input-output operations.""" # noqa: D205, D209
111
+ with concurrent.futures.ThreadPoolExecutor(self.max_workers) as executor:
112
+ results = list(
113
+ executor.map(
114
+ self.quantum,
115
+ self.data,
116
+ timeout=self.timeout,
117
+ chunksize=self.chunksize,
118
+ ),
119
+ )
120
+ return results # noqa: RET504
121
+
122
+ def run(self) -> list[Any]:
123
+ """Run the quantum loop."""
124
+ results: list[Any] = []
125
+ match self.mode.value:
126
+ case 1:
127
+ results = self.process_pool()
128
+ case 2:
129
+ results = self.thread_pool()
130
+ case _ as unreachable:
131
+ assert_never(Never(unreachable))
132
+ return results
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: xloft
3
- Version: 0.4.3
3
+ Version: 0.4.5
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
@@ -25,9 +25,6 @@ Classifier: Programming Language :: Python :: Implementation :: CPython
25
25
  Classifier: Topic :: Utilities
26
26
  Classifier: Typing :: Typed
27
27
  Requires-Python: <4.0,>=3.12
28
- Requires-Dist: quantum
29
- Provides-Extra: quantum
30
- Requires-Dist: quantum; extra == 'quantum'
31
28
  Description-Content-Type: text/markdown
32
29
 
33
30
  <div align="center">
@@ -83,8 +80,6 @@ Online browsable documentation is available at [https://kebasyaty.github.io/xlof
83
80
 
84
81
  ```shell
85
82
  uv add xloft
86
- # For a Quantum module:
87
- uv add xloft[quantum]
88
83
  ```
89
84
 
90
85
  ## Usage
@@ -178,7 +173,7 @@ print(s) # => 1023.999 KB
178
173
  - **Quantum**
179
174
 
180
175
  ```python
181
- from quantum import LoopMode, QuantumLoop, count_qubits
176
+ from xloft.quantum import LoopMode, QuantumLoop, count_qubits
182
177
 
183
178
  # Counting the number of conceptual qubits of your computer.
184
179
  num = count_qubits()
@@ -191,8 +186,7 @@ def task(item):
191
186
  data = range(10)
192
187
 
193
188
  # Separation of the cycle into quantum algorithms for multiprocessing data processing.
194
- qloop = QuantumLoop(task, data)
195
- results = qloop.run()
189
+ results = QuantumLoop(task, data).run()
196
190
  print(results) # => [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
197
191
  ```
198
192
 
@@ -0,0 +1,10 @@
1
+ xloft/__init__.py,sha256=UL4IR8EcFjgWoQFayl6FrK2u8QmZytGCwa_E1jDiCmE,475
2
+ xloft/errors.py,sha256=hZcmF0QVVdvE5oM1jsXymRk_pPGgDSnUDM9wx9zJAYQ,895
3
+ xloft/human.py,sha256=odRbUF_58YuFWC_VQeRDZ-6ifHDiiAH2HNb5G5K6JIM,1106
4
+ xloft/namedtuple.py,sha256=a_l3bZF-L2I7MGxuF2CXzAHgNai-Vyj6SY1ODwxs7TU,6856
5
+ xloft/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
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,,
@@ -1,9 +0,0 @@
1
- xloft/__init__.py,sha256=1bdoDR6AzzL_Rhpp3LLbt6swYlZoZjmfnpJ4sGRr3Ug,518
2
- xloft/errors.py,sha256=hZcmF0QVVdvE5oM1jsXymRk_pPGgDSnUDM9wx9zJAYQ,895
3
- xloft/human.py,sha256=odRbUF_58YuFWC_VQeRDZ-6ifHDiiAH2HNb5G5K6JIM,1106
4
- xloft/namedtuple.py,sha256=a_l3bZF-L2I7MGxuF2CXzAHgNai-Vyj6SY1ODwxs7TU,6856
5
- xloft/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
6
- xloft-0.4.3.dist-info/METADATA,sha256=Wwp_dseakyHPsBDi6lFaSvLd36lJ9phIFrMO-dZS_HI,7385
7
- xloft-0.4.3.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
8
- xloft-0.4.3.dist-info/licenses/LICENSE,sha256=2zZINd6m_jNYlowdQImlEizyhSui5cBAJZRhWQURcEc,1095
9
- xloft-0.4.3.dist-info/RECORD,,
File without changes