xloft 0.4.2__py3-none-any.whl → 0.4.4__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,133 @@
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 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 quantum import QuantumLoop
58
+ >>> def task(item):
59
+ ... return item * item
60
+ >>> data = range(10)
61
+ >>> qloop = QuantumLoop(task, data)
62
+ >>> qloop.run()
63
+ [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
64
+
65
+ Args:
66
+ task: Function with a task algorithm.
67
+ data: The data that needs to be processed.
68
+ max_workers: The maximum number of processes that can be used to
69
+ execute the given calls. If None or not given then as many
70
+ worker processes will be created as the machine has processors.
71
+ timeout: The maximum number of seconds to wait. If None, then there
72
+ is no limit on the wait time.
73
+ chunksize: The size of the chunks the iterable will be broken into
74
+ before being passed to a child process. This argument is only
75
+ used by ProcessPoolExecutor; it is ignored by ThreadPoolExecutor.
76
+ mode: The operating mode for a quantum loop: LoopMode.PROCESS_POOL | LoopMode.THREAD_POOL.
77
+ """
78
+
79
+ def __init__( # noqa: D107
80
+ self,
81
+ task: Callable,
82
+ data: Iterable[Any],
83
+ max_workers: int | None = None,
84
+ timeout: float | None = None,
85
+ chunksize: int = 1,
86
+ mode: LoopMode = LoopMode.PROCESS_POOL,
87
+ ) -> None:
88
+ self.quantum = task
89
+ self.data = data
90
+ self.max_workers = max_workers
91
+ self.timeout = timeout
92
+ self.chunksize = chunksize
93
+ self.mode = mode
94
+
95
+ def process_pool(self) -> list[Any]:
96
+ """Better suitable for operations for which large processor resources are required."""
97
+ with concurrent.futures.ProcessPoolExecutor(self.max_workers) as executor:
98
+ results = list(
99
+ executor.map(
100
+ self.quantum,
101
+ self.data,
102
+ timeout=self.timeout,
103
+ chunksize=self.chunksize,
104
+ ),
105
+ )
106
+ return results # noqa: RET504
107
+
108
+ def thread_pool(self) -> list[Any]:
109
+ """More suitable for tasks related to input-output
110
+ (for example, network queries, file operations),
111
+ where GIL is freed during input-output operations.""" # noqa: D205, D209
112
+ with concurrent.futures.ThreadPoolExecutor(self.max_workers) as executor:
113
+ results = list(
114
+ executor.map(
115
+ self.quantum,
116
+ self.data,
117
+ timeout=self.timeout,
118
+ chunksize=self.chunksize,
119
+ ),
120
+ )
121
+ return results # noqa: RET504
122
+
123
+ def run(self) -> list[Any]:
124
+ """Run the quantum loop."""
125
+ results: list[Any] = []
126
+ match self.mode.value:
127
+ case 1:
128
+ results = self.process_pool()
129
+ case 2:
130
+ results = self.thread_pool()
131
+ case _ as unreachable:
132
+ assert_never(Never(unreachable))
133
+ return results
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: xloft
3
- Version: 0.4.2
3
+ Version: 0.4.4
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,8 +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
- Provides-Extra: quantum
29
- Requires-Dist: quantum; extra == 'quantum'
30
28
  Description-Content-Type: text/markdown
31
29
 
32
30
  <div align="center">
@@ -82,8 +80,6 @@ Online browsable documentation is available at [https://kebasyaty.github.io/xlof
82
80
 
83
81
  ```shell
84
82
  uv add xloft
85
- # For a Quantum module:
86
- uv add xloft[quantum]
87
83
  ```
88
84
 
89
85
  ## Usage
@@ -177,7 +173,7 @@ print(s) # => 1023.999 KB
177
173
  - **Quantum**
178
174
 
179
175
  ```python
180
- from quantum import LoopMode, QuantumLoop, count_qubits
176
+ from xloft.quantum import LoopMode, QuantumLoop, count_qubits
181
177
 
182
178
  # Counting the number of conceptual qubits of your computer.
183
179
  num = count_qubits()
@@ -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=-if8m-gX-lydiFpGgg6dXj3RBijpP2dcXbBfPmJ3N_E,4694
7
+ xloft-0.4.4.dist-info/METADATA,sha256=n8av4ZnoCfSsd3PCFu74hFvnNwb1VYws6eLHKFqHG9c,7255
8
+ xloft-0.4.4.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
9
+ xloft-0.4.4.dist-info/licenses/LICENSE,sha256=2zZINd6m_jNYlowdQImlEizyhSui5cBAJZRhWQURcEc,1095
10
+ xloft-0.4.4.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.2.dist-info/METADATA,sha256=kdesOGV-c5WOIxgDJg5YyEWLhS7i30hvlqgUuctDjDQ,7362
7
- xloft-0.4.2.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
8
- xloft-0.4.2.dist-info/licenses/LICENSE,sha256=2zZINd6m_jNYlowdQImlEizyhSui5cBAJZRhWQURcEc,1095
9
- xloft-0.4.2.dist-info/RECORD,,
File without changes