vortezwohl 0.0.1__tar.gz

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.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Vortez Wohl / 吳子豪
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,132 @@
1
+ Metadata-Version: 2.4
2
+ Name: vortezwohl
3
+ Version: 0.0.1
4
+ Summary: SDK of vortezwohl.
5
+ Requires-Python: >=3.10
6
+ Description-Content-Type: text/markdown
7
+ License-File: LICENSE
8
+ Requires-Dist: psutil
9
+ Requires-Dist: typing-extensions
10
+ Dynamic: license-file
11
+
12
+ # vortezwohl-sdk
13
+
14
+ > Useful Python SDKs
15
+
16
+ ## Installation
17
+
18
+ ```
19
+ pip install -U git+https://github.com/vortezwohl/vortezwohl-sdk.git
20
+ ```
21
+
22
+ ## Quick Start
23
+
24
+ - ### Thread Pool
25
+
26
+ - Import SDKs
27
+
28
+ ```python
29
+ import random
30
+ import time
31
+
32
+ from vortezwohl.concurrent import ThreadPool
33
+ ```
34
+
35
+ - Create callables
36
+
37
+ ```python
38
+ def job1(x: int, y: int) -> int:
39
+ res = x + y
40
+ time.sleep(res / 2)
41
+ return res
42
+
43
+
44
+ def job2() -> int:
45
+ _delay = random.randint(1, 4)
46
+ time.sleep(_delay / 2)
47
+ return _delay
48
+
49
+
50
+ def job3(x) -> int:
51
+ time.sleep(x / 2)
52
+ return x
53
+ ```
54
+
55
+ - Gather jobs (tasks)
56
+
57
+ ```python
58
+ with ThreadPool() as t:
59
+ for fn, param, result in t.gather(jobs=[job1, job1, job1, job2, job2, job3],
60
+ arguments=[(1, 2), (2, 3), {'x': 3, 'y': 4}, None, None, 5]):
61
+ print('fn={}, param={}, result={}'.format(fn, param, result))
62
+ ```
63
+
64
+ stdout:
65
+
66
+ ```
67
+ fn=<function job2 at 0x00000183016C0180>, param=None, result=1
68
+ fn=<function job1 at 0x00000183013B63E0>, param=(1, 2), result=3
69
+ fn=<function job2 at 0x00000183016C0180>, param=None, result=3
70
+ fn=<function job1 at 0x00000183013B63E0>, param=(2, 3), result=5
71
+ fn=<function job3 at 0x00000183016C0220>, param=5, result=5
72
+ fn=<function job1 at 0x00000183013B63E0>, param={'x': 3, 'y': 4}, result=7
73
+ ```
74
+
75
+ - Submit jobs respectively
76
+
77
+
78
+ ```python
79
+ with ThreadPool() as t:
80
+ t.submit(job1, 1, 2)
81
+ t.submit(job1, 2, 3)
82
+ t.submit(job1, x=3, y=4)
83
+ t.submit(job2)
84
+ t.submit(job2)
85
+ t.submit(job3, 5)
86
+ for fn, param, result in t.next_result:
87
+ print('fn={}, param={}, result={}'.format(fn, param, result))
88
+ ```
89
+
90
+ stdout:
91
+
92
+ ```
93
+ fn=<function job2 at 0x000001B82B61C0E0>, param=None, result=2
94
+ fn=<function job1 at 0x000001B82B31A480>, param=(1, 2), result=3
95
+ fn=<function job2 at 0x000001B82B61C0E0>, param=None, result=4
96
+ fn=<function job1 at 0x000001B82B31A480>, param=(2, 3), result=5
97
+ fn=<function job3 at 0x000001B82B61C180>, param=(5,), result=5
98
+ fn=<function job1 at 0x000001B82B31A480>, param={'x': 3, 'y': 4}, result=7
99
+ ```
100
+
101
+ - ### Seed Generator
102
+
103
+ - Import SDKs
104
+
105
+ ```python
106
+ import random
107
+
108
+ from vortezwohl.random import next_seed
109
+ ```
110
+
111
+ - Do random stuff
112
+
113
+ ```python
114
+ for _ in range(10):
115
+ next_seed()
116
+ print(random.randint(1, 10))
117
+ ```
118
+
119
+ stdout:
120
+
121
+ ```
122
+ 3
123
+ 4
124
+ 4
125
+ 8
126
+ 9
127
+ 6
128
+ 8
129
+ 7
130
+ 1
131
+ 10
132
+ ```
@@ -0,0 +1,121 @@
1
+ # vortezwohl-sdk
2
+
3
+ > Useful Python SDKs
4
+
5
+ ## Installation
6
+
7
+ ```
8
+ pip install -U git+https://github.com/vortezwohl/vortezwohl-sdk.git
9
+ ```
10
+
11
+ ## Quick Start
12
+
13
+ - ### Thread Pool
14
+
15
+ - Import SDKs
16
+
17
+ ```python
18
+ import random
19
+ import time
20
+
21
+ from vortezwohl.concurrent import ThreadPool
22
+ ```
23
+
24
+ - Create callables
25
+
26
+ ```python
27
+ def job1(x: int, y: int) -> int:
28
+ res = x + y
29
+ time.sleep(res / 2)
30
+ return res
31
+
32
+
33
+ def job2() -> int:
34
+ _delay = random.randint(1, 4)
35
+ time.sleep(_delay / 2)
36
+ return _delay
37
+
38
+
39
+ def job3(x) -> int:
40
+ time.sleep(x / 2)
41
+ return x
42
+ ```
43
+
44
+ - Gather jobs (tasks)
45
+
46
+ ```python
47
+ with ThreadPool() as t:
48
+ for fn, param, result in t.gather(jobs=[job1, job1, job1, job2, job2, job3],
49
+ arguments=[(1, 2), (2, 3), {'x': 3, 'y': 4}, None, None, 5]):
50
+ print('fn={}, param={}, result={}'.format(fn, param, result))
51
+ ```
52
+
53
+ stdout:
54
+
55
+ ```
56
+ fn=<function job2 at 0x00000183016C0180>, param=None, result=1
57
+ fn=<function job1 at 0x00000183013B63E0>, param=(1, 2), result=3
58
+ fn=<function job2 at 0x00000183016C0180>, param=None, result=3
59
+ fn=<function job1 at 0x00000183013B63E0>, param=(2, 3), result=5
60
+ fn=<function job3 at 0x00000183016C0220>, param=5, result=5
61
+ fn=<function job1 at 0x00000183013B63E0>, param={'x': 3, 'y': 4}, result=7
62
+ ```
63
+
64
+ - Submit jobs respectively
65
+
66
+
67
+ ```python
68
+ with ThreadPool() as t:
69
+ t.submit(job1, 1, 2)
70
+ t.submit(job1, 2, 3)
71
+ t.submit(job1, x=3, y=4)
72
+ t.submit(job2)
73
+ t.submit(job2)
74
+ t.submit(job3, 5)
75
+ for fn, param, result in t.next_result:
76
+ print('fn={}, param={}, result={}'.format(fn, param, result))
77
+ ```
78
+
79
+ stdout:
80
+
81
+ ```
82
+ fn=<function job2 at 0x000001B82B61C0E0>, param=None, result=2
83
+ fn=<function job1 at 0x000001B82B31A480>, param=(1, 2), result=3
84
+ fn=<function job2 at 0x000001B82B61C0E0>, param=None, result=4
85
+ fn=<function job1 at 0x000001B82B31A480>, param=(2, 3), result=5
86
+ fn=<function job3 at 0x000001B82B61C180>, param=(5,), result=5
87
+ fn=<function job1 at 0x000001B82B31A480>, param={'x': 3, 'y': 4}, result=7
88
+ ```
89
+
90
+ - ### Seed Generator
91
+
92
+ - Import SDKs
93
+
94
+ ```python
95
+ import random
96
+
97
+ from vortezwohl.random import next_seed
98
+ ```
99
+
100
+ - Do random stuff
101
+
102
+ ```python
103
+ for _ in range(10):
104
+ next_seed()
105
+ print(random.randint(1, 10))
106
+ ```
107
+
108
+ stdout:
109
+
110
+ ```
111
+ 3
112
+ 4
113
+ 4
114
+ 8
115
+ 9
116
+ 6
117
+ 8
118
+ 7
119
+ 1
120
+ 10
121
+ ```
@@ -0,0 +1,10 @@
1
+ [project]
2
+ name = "vortezwohl"
3
+ version = "0.0.1"
4
+ description = "SDK of vortezwohl."
5
+ readme = "README.md"
6
+ requires-python = ">=3.10"
7
+ dependencies = [
8
+ "psutil",
9
+ "typing-extensions",
10
+ ]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
File without changes
@@ -0,0 +1 @@
1
+ from .thread_pool import ThreadPool
@@ -0,0 +1,76 @@
1
+ import traceback
2
+ from concurrent.futures import ThreadPoolExecutor, Future, as_completed
3
+ from threading import Lock
4
+ from types import NoneType
5
+
6
+ from typing_extensions import Iterable, Any, Callable, Dict, Tuple
7
+
8
+ import psutil
9
+
10
+
11
+ class ThreadPool(object):
12
+ def __init__(self, max_workers: int | None = None):
13
+ if max_workers is None:
14
+ max_workers = psutil.cpu_count(logical=True) + 1
15
+ self._max_workers = max_workers
16
+ self._thread_pool_executor = ThreadPoolExecutor(max_workers=self._max_workers)
17
+ self._futures = list()
18
+ self._futures_lock = Lock()
19
+
20
+ def __enter__(self):
21
+ return self
22
+
23
+ def __exit__(self, exc_type, exc_val, exc_tb):
24
+ return self.shutdown(cancel_futures=False)
25
+
26
+ def submit(self, job: Callable, *args, **kwargs) -> Future:
27
+ _future = None
28
+ if len(args) > 0:
29
+ _future = self._thread_pool_executor.submit(lambda param: (job, param, job(*param)), args)
30
+ elif len(kwargs) > 0:
31
+ _future = self._thread_pool_executor.submit(lambda param: (job, param, job(**param)), kwargs)
32
+ else:
33
+ _future = self._thread_pool_executor.submit(lambda: (job, None, job()))
34
+ with self._futures_lock:
35
+ self._futures.append(_future)
36
+ return _future
37
+
38
+ def gather(self, jobs: Iterable[Callable], arguments: list[Dict] | list[Tuple] | list[NoneType] | None = None) \
39
+ -> tuple[Callable, Any, Any] | Exception:
40
+ futures = []
41
+ for i, job in enumerate(jobs):
42
+ if arguments is None or arguments[i] is None:
43
+ futures.append(self._thread_pool_executor.submit(lambda: (job, None, job())))
44
+ continue
45
+ if isinstance(arguments[i], dict):
46
+ futures.append(self._thread_pool_executor.submit(lambda param: (job, param, job(**param)), arguments[i]))
47
+ elif isinstance(arguments[i], Iterable):
48
+ futures.append(self._thread_pool_executor.submit(lambda param: (job, param, job(*param)), arguments[i]))
49
+ else:
50
+ futures.append(self._thread_pool_executor.submit(lambda param: (job, param, job(param)), arguments[i]))
51
+ for future in as_completed(futures):
52
+ try:
53
+ yield future.result()
54
+ except Exception as __e:
55
+ yield __e, traceback.format_exc()
56
+
57
+ def shutdown(self, cancel_futures: bool = True):
58
+ self._thread_pool_executor.shutdown(wait=True, cancel_futures=cancel_futures)
59
+ return self
60
+
61
+ @property
62
+ def next_result(self) -> Any:
63
+ for f in as_completed(self._futures):
64
+ try:
65
+ yield f.result()
66
+ finally:
67
+ with self._futures_lock:
68
+ self._futures.remove(f)
69
+
70
+ @property
71
+ def thread_pool_executor(self) -> ThreadPoolExecutor:
72
+ return self._thread_pool_executor
73
+
74
+ @property
75
+ def max_workers(self) -> int:
76
+ return self._max_workers
@@ -0,0 +1 @@
1
+ from .seed_generator import next_seed
@@ -0,0 +1,16 @@
1
+ import threading
2
+ import random
3
+
4
+ SEED_LOCK = threading.Lock()
5
+ SEED_INCREMENT = 0
6
+ SEED_MULTIPLIER = 1
7
+ MOD = int(1e16)
8
+
9
+
10
+ def next_seed() -> int:
11
+ global SEED_INCREMENT, SEED_MULTIPLIER
12
+ with SEED_LOCK:
13
+ SEED_INCREMENT = ((SEED_INCREMENT + 1) * SEED_MULTIPLIER * max((SEED_INCREMENT // MOD), 1)) % MOD
14
+ SEED_MULTIPLIER += 1
15
+ random.seed(SEED_INCREMENT)
16
+ return SEED_INCREMENT
@@ -0,0 +1,132 @@
1
+ Metadata-Version: 2.4
2
+ Name: vortezwohl
3
+ Version: 0.0.1
4
+ Summary: SDK of vortezwohl.
5
+ Requires-Python: >=3.10
6
+ Description-Content-Type: text/markdown
7
+ License-File: LICENSE
8
+ Requires-Dist: psutil
9
+ Requires-Dist: typing-extensions
10
+ Dynamic: license-file
11
+
12
+ # vortezwohl-sdk
13
+
14
+ > Useful Python SDKs
15
+
16
+ ## Installation
17
+
18
+ ```
19
+ pip install -U git+https://github.com/vortezwohl/vortezwohl-sdk.git
20
+ ```
21
+
22
+ ## Quick Start
23
+
24
+ - ### Thread Pool
25
+
26
+ - Import SDKs
27
+
28
+ ```python
29
+ import random
30
+ import time
31
+
32
+ from vortezwohl.concurrent import ThreadPool
33
+ ```
34
+
35
+ - Create callables
36
+
37
+ ```python
38
+ def job1(x: int, y: int) -> int:
39
+ res = x + y
40
+ time.sleep(res / 2)
41
+ return res
42
+
43
+
44
+ def job2() -> int:
45
+ _delay = random.randint(1, 4)
46
+ time.sleep(_delay / 2)
47
+ return _delay
48
+
49
+
50
+ def job3(x) -> int:
51
+ time.sleep(x / 2)
52
+ return x
53
+ ```
54
+
55
+ - Gather jobs (tasks)
56
+
57
+ ```python
58
+ with ThreadPool() as t:
59
+ for fn, param, result in t.gather(jobs=[job1, job1, job1, job2, job2, job3],
60
+ arguments=[(1, 2), (2, 3), {'x': 3, 'y': 4}, None, None, 5]):
61
+ print('fn={}, param={}, result={}'.format(fn, param, result))
62
+ ```
63
+
64
+ stdout:
65
+
66
+ ```
67
+ fn=<function job2 at 0x00000183016C0180>, param=None, result=1
68
+ fn=<function job1 at 0x00000183013B63E0>, param=(1, 2), result=3
69
+ fn=<function job2 at 0x00000183016C0180>, param=None, result=3
70
+ fn=<function job1 at 0x00000183013B63E0>, param=(2, 3), result=5
71
+ fn=<function job3 at 0x00000183016C0220>, param=5, result=5
72
+ fn=<function job1 at 0x00000183013B63E0>, param={'x': 3, 'y': 4}, result=7
73
+ ```
74
+
75
+ - Submit jobs respectively
76
+
77
+
78
+ ```python
79
+ with ThreadPool() as t:
80
+ t.submit(job1, 1, 2)
81
+ t.submit(job1, 2, 3)
82
+ t.submit(job1, x=3, y=4)
83
+ t.submit(job2)
84
+ t.submit(job2)
85
+ t.submit(job3, 5)
86
+ for fn, param, result in t.next_result:
87
+ print('fn={}, param={}, result={}'.format(fn, param, result))
88
+ ```
89
+
90
+ stdout:
91
+
92
+ ```
93
+ fn=<function job2 at 0x000001B82B61C0E0>, param=None, result=2
94
+ fn=<function job1 at 0x000001B82B31A480>, param=(1, 2), result=3
95
+ fn=<function job2 at 0x000001B82B61C0E0>, param=None, result=4
96
+ fn=<function job1 at 0x000001B82B31A480>, param=(2, 3), result=5
97
+ fn=<function job3 at 0x000001B82B61C180>, param=(5,), result=5
98
+ fn=<function job1 at 0x000001B82B31A480>, param={'x': 3, 'y': 4}, result=7
99
+ ```
100
+
101
+ - ### Seed Generator
102
+
103
+ - Import SDKs
104
+
105
+ ```python
106
+ import random
107
+
108
+ from vortezwohl.random import next_seed
109
+ ```
110
+
111
+ - Do random stuff
112
+
113
+ ```python
114
+ for _ in range(10):
115
+ next_seed()
116
+ print(random.randint(1, 10))
117
+ ```
118
+
119
+ stdout:
120
+
121
+ ```
122
+ 3
123
+ 4
124
+ 4
125
+ 8
126
+ 9
127
+ 6
128
+ 8
129
+ 7
130
+ 1
131
+ 10
132
+ ```
@@ -0,0 +1,13 @@
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
4
+ vortezwohl/__init__.py
5
+ vortezwohl.egg-info/PKG-INFO
6
+ vortezwohl.egg-info/SOURCES.txt
7
+ vortezwohl.egg-info/dependency_links.txt
8
+ vortezwohl.egg-info/requires.txt
9
+ vortezwohl.egg-info/top_level.txt
10
+ vortezwohl/concurrent/__init__.py
11
+ vortezwohl/concurrent/thread_pool.py
12
+ vortezwohl/random/__init__.py
13
+ vortezwohl/random/seed_generator.py
@@ -0,0 +1,2 @@
1
+ psutil
2
+ typing-extensions
@@ -0,0 +1 @@
1
+ vortezwohl