relib 1.3.1__py3-none-any.whl → 1.3.2__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.
- relib/iter_utils.py +68 -17
- relib/runtime_tools.py +8 -7
- {relib-1.3.1.dist-info → relib-1.3.2.dist-info}/METADATA +1 -1
- {relib-1.3.1.dist-info → relib-1.3.2.dist-info}/RECORD +6 -6
- {relib-1.3.1.dist-info → relib-1.3.2.dist-info}/WHEEL +0 -0
- {relib-1.3.1.dist-info → relib-1.3.2.dist-info}/licenses/LICENSE +0 -0
relib/iter_utils.py
CHANGED
@@ -1,16 +1,17 @@
|
|
1
|
-
from
|
2
|
-
from
|
1
|
+
from contextlib import contextmanager
|
2
|
+
from itertools import chain, islice
|
3
|
+
from typing import Any, Iterable, Literal, Self, overload
|
3
4
|
from .dict_utils import dict_firsts
|
4
5
|
|
5
6
|
__all__ = [
|
7
|
+
"chunked",
|
6
8
|
"distinct_by", "distinct", "drop_none",
|
7
9
|
"first", "flatten",
|
8
10
|
"interleave", "intersect",
|
9
11
|
"list_split",
|
10
12
|
"move_value",
|
11
|
-
"num_partitions",
|
12
13
|
"reversed_enumerate",
|
13
|
-
"
|
14
|
+
"seekable", "sort_by",
|
14
15
|
"transpose",
|
15
16
|
]
|
16
17
|
|
@@ -36,7 +37,7 @@ def move_value[T](iterable: Iterable[T], from_i: int, to_i: int) -> list[T]:
|
|
36
37
|
return values
|
37
38
|
|
38
39
|
def reversed_enumerate[T](values: list[T] | tuple[T, ...]) -> Iterable[tuple[int, T]]:
|
39
|
-
return zip(range(len(values))[
|
40
|
+
return zip(range(len(values))[::-1], reversed(values))
|
40
41
|
|
41
42
|
def intersect[T](*iterables: Iterable[T]) -> list[T]:
|
42
43
|
return list(set.intersection(*map(set, iterables)))
|
@@ -50,18 +51,68 @@ def list_split[T](iterable: Iterable[T], sep: T) -> list[list[T]]:
|
|
50
51
|
ranges = list(zip(split_at[0:-1], split_at[1:]))
|
51
52
|
return [values[start + 1:end] for start, end in ranges]
|
52
53
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
54
|
+
class seekable[T]:
|
55
|
+
def __init__(self, iterable: Iterable[T]):
|
56
|
+
self.index = 0
|
57
|
+
self.source = iter(iterable)
|
58
|
+
self.sink: list[T] = []
|
59
|
+
|
60
|
+
def __iter__(self):
|
61
|
+
return self
|
62
|
+
|
63
|
+
def __next__(self) -> T:
|
64
|
+
if len(self.sink) > self.index:
|
65
|
+
item = self.sink[self.index]
|
66
|
+
else:
|
67
|
+
item = next(self.source)
|
68
|
+
self.sink.append(item)
|
69
|
+
self.index += 1
|
70
|
+
return item
|
71
|
+
|
72
|
+
def __bool__(self):
|
73
|
+
return bool(self.lookahead(1))
|
74
|
+
|
75
|
+
def clear(self):
|
76
|
+
self.sink[:self.index] = []
|
77
|
+
self.index = 0
|
78
|
+
|
79
|
+
def seek(self, index: int) -> Self:
|
80
|
+
remainder = index - len(self.sink)
|
81
|
+
if remainder > 0:
|
82
|
+
next(islice(self, remainder, remainder), None)
|
83
|
+
self.index = max(0, min(index, len(self.sink)))
|
84
|
+
return self
|
85
|
+
|
86
|
+
def step(self, count: int) -> Self:
|
87
|
+
return self.seek(self.index + count)
|
88
|
+
|
89
|
+
@contextmanager
|
90
|
+
def freeze(self):
|
91
|
+
def commit(offset: int = 0):
|
92
|
+
nonlocal initial_index
|
93
|
+
initial_index = self.index + offset
|
94
|
+
initial_index = self.index
|
95
|
+
try:
|
96
|
+
yield commit
|
97
|
+
finally:
|
98
|
+
self.seek(initial_index)
|
99
|
+
|
100
|
+
def lookahead(self, count: int) -> list[T]:
|
101
|
+
with self.freeze():
|
102
|
+
return list(islice(self, count))
|
103
|
+
|
104
|
+
@overload
|
105
|
+
def chunked[T](values: Iterable[T], *, num_chunks: int, chunk_size=None) -> list[list[T]]: ...
|
106
|
+
@overload
|
107
|
+
def chunked[T](values: Iterable[T], *, num_chunks=None, chunk_size: int) -> list[list[T]]: ...
|
108
|
+
def chunked(values, *, num_chunks=None, chunk_size=None):
|
109
|
+
values = values if isinstance(values, list) else list(values)
|
110
|
+
if isinstance(num_chunks, int):
|
111
|
+
chunk_size = (len(values) / num_chunks).__ceil__()
|
112
|
+
elif isinstance(chunk_size, int):
|
113
|
+
num_chunks = (len(values) / chunk_size).__ceil__()
|
114
|
+
assert isinstance(num_chunks, int) and isinstance(chunk_size, int)
|
115
|
+
return [values[i * chunk_size:(i + 1) * chunk_size] for i in range(num_chunks)]
|
65
116
|
|
66
117
|
@overload
|
67
118
|
def flatten[T](iterable: Iterable[T], depth: Literal[0]) -> list[T]: ...
|
relib/runtime_tools.py
CHANGED
@@ -4,7 +4,7 @@ import os
|
|
4
4
|
from concurrent.futures import ThreadPoolExecutor
|
5
5
|
from functools import partial, wraps
|
6
6
|
from time import time
|
7
|
-
from typing import
|
7
|
+
from typing import Callable, Coroutine, Iterable, ParamSpec, TypeVar
|
8
8
|
from .processing_utils import noop
|
9
9
|
|
10
10
|
__all__ = [
|
@@ -17,6 +17,7 @@ __all__ = [
|
|
17
17
|
|
18
18
|
P = ParamSpec("P")
|
19
19
|
R = TypeVar("R")
|
20
|
+
Coro = Coroutine[object, object, R]
|
20
21
|
|
21
22
|
default_workers = min(32, (os.cpu_count() or 1) + 4)
|
22
23
|
default_executor = ThreadPoolExecutor(max_workers=default_workers)
|
@@ -27,13 +28,13 @@ def clear_console() -> None:
|
|
27
28
|
def console_link(text: str, url: str) -> str:
|
28
29
|
return f"\033]8;;{url}\033\\{text}\033]8;;\033\\"
|
29
30
|
|
30
|
-
async def worker[T](task:
|
31
|
+
async def worker[T](task: Coro[T], semaphore: asyncio.Semaphore, update=noop) -> T:
|
31
32
|
async with semaphore:
|
32
33
|
result = await task
|
33
34
|
update()
|
34
35
|
return result
|
35
36
|
|
36
|
-
async def roll_tasks[T](tasks: Iterable[
|
37
|
+
async def roll_tasks[T](tasks: Iterable[Coro[T]], workers=default_workers, progress=False) -> list[T]:
|
37
38
|
semaphore = asyncio.Semaphore(workers)
|
38
39
|
if not progress:
|
39
40
|
return await asyncio.gather(*[worker(task, semaphore) for task in tasks])
|
@@ -44,10 +45,10 @@ async def roll_tasks[T](tasks: Iterable[Awaitable[T]], workers=default_workers,
|
|
44
45
|
update = partial(pbar.update, 1)
|
45
46
|
return await asyncio.gather(*[worker(task, semaphore, update) for task in tasks])
|
46
47
|
|
47
|
-
def as_async(workers: int | ThreadPoolExecutor = default_executor) -> Callable[[Callable[P, R]], Callable[P,
|
48
|
+
def as_async(workers: int | ThreadPoolExecutor = default_executor) -> Callable[[Callable[P, R]], Callable[P, Coro[R]]]:
|
48
49
|
executor = ThreadPoolExecutor(max_workers=workers) if isinstance(workers, int) else workers
|
49
50
|
|
50
|
-
def on_fn(func: Callable[P, R]) -> Callable[P,
|
51
|
+
def on_fn(func: Callable[P, R]) -> Callable[P, Coro[R]]:
|
51
52
|
@wraps(func)
|
52
53
|
async def wrapper(*args: P.args, **kwargs: P.kwargs) -> R:
|
53
54
|
loop = asyncio.get_running_loop()
|
@@ -57,10 +58,10 @@ def as_async(workers: int | ThreadPoolExecutor = default_executor) -> Callable[[
|
|
57
58
|
return wrapper
|
58
59
|
return on_fn
|
59
60
|
|
60
|
-
def async_limit(workers=default_workers) -> Callable[[Callable[P,
|
61
|
+
def async_limit(workers=default_workers) -> Callable[[Callable[P, Coro[R]]], Callable[P, Coro[R]]]:
|
61
62
|
semaphore = asyncio.Semaphore(workers)
|
62
63
|
|
63
|
-
def on_fn(func: Callable[P,
|
64
|
+
def on_fn(func: Callable[P, Coro[R]]) -> Callable[P, Coro[R]]:
|
64
65
|
@wraps(func)
|
65
66
|
async def wrapper(*args: P.args, **kwargs: P.kwargs) -> R:
|
66
67
|
async with semaphore:
|
@@ -1,11 +1,11 @@
|
|
1
1
|
relib/__init__.py,sha256=WerjUaM_sNvudjXFudLRtXB7viZWEW1RSinkDjrh4nE,163
|
2
2
|
relib/dict_utils.py,sha256=jqW6bYSaQMt2AC2KFzDJKyl88idyMttWxXDu3t-fA5I,2980
|
3
3
|
relib/io_utils.py,sha256=EtnIGQmLXjoHUPFteB5yPXDD3wGLvH4O3CahlCebXDQ,555
|
4
|
-
relib/iter_utils.py,sha256=
|
4
|
+
relib/iter_utils.py,sha256=O2sXQ7Old7b9mkRczi-R8Gio92hpzSQl30TXsDS7PD4,5179
|
5
5
|
relib/processing_utils.py,sha256=eMzjlxsEmfvtKafDITBWSp9D5RwegSWsUsvj1FpmBM0,1893
|
6
|
-
relib/runtime_tools.py,sha256=
|
6
|
+
relib/runtime_tools.py,sha256=KNtthODV1DdijABX_VIVbStbgT7Dlubu8oAEGxQsIhQ,2838
|
7
7
|
relib/type_utils.py,sha256=oY96cAAux1JwhXgWFFyqEv_f-wwyPc_Hm6I9Yeisu_M,323
|
8
|
-
relib-1.3.
|
9
|
-
relib-1.3.
|
10
|
-
relib-1.3.
|
11
|
-
relib-1.3.
|
8
|
+
relib-1.3.2.dist-info/METADATA,sha256=X3NdxQpaT122Rd0CM709I-rJUgUlbTbw3F3b0SwZMhQ,1295
|
9
|
+
relib-1.3.2.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
10
|
+
relib-1.3.2.dist-info/licenses/LICENSE,sha256=9xVsdtv_-uSyY9Xl9yujwAPm4-mjcCLeVy-ljwXEWbo,1059
|
11
|
+
relib-1.3.2.dist-info/RECORD,,
|
File without changes
|
File without changes
|