relib 1.2.7__tar.gz → 1.2.9__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.
- relib-1.2.9/.python-version +1 -0
- {relib-1.2.7 → relib-1.2.9}/PKG-INFO +1 -1
- {relib-1.2.7 → relib-1.2.9}/pyproject.toml +1 -1
- relib-1.2.9/relib/__init__.py +4 -0
- {relib-1.2.7 → relib-1.2.9}/relib/system.py +21 -8
- {relib-1.2.7 → relib-1.2.9}/relib/utils.py +48 -0
- {relib-1.2.7 → relib-1.2.9}/uv.lock +1 -1
- relib-1.2.7/relib/__init__.py +0 -45
- {relib-1.2.7 → relib-1.2.9}/.gitignore +0 -0
- {relib-1.2.7 → relib-1.2.9}/LICENSE +0 -0
- {relib-1.2.7 → relib-1.2.9}/README.md +0 -0
- {relib-1.2.7 → relib-1.2.9}/relib/hashing.py +0 -0
- {relib-1.2.7 → relib-1.2.9}/relib/measure_duration.py +0 -0
@@ -0,0 +1 @@
|
|
1
|
+
3.12
|
@@ -8,15 +8,28 @@ from pathlib import Path
|
|
8
8
|
from typing import Any, Awaitable, Callable, Iterable, ParamSpec, TypeVar
|
9
9
|
from .utils import noop
|
10
10
|
|
11
|
+
__all__ = [
|
12
|
+
"read_json",
|
13
|
+
"write_json",
|
14
|
+
"clear_console",
|
15
|
+
"console_link",
|
16
|
+
"roll_tasks",
|
17
|
+
"as_async",
|
18
|
+
"async_limit",
|
19
|
+
]
|
20
|
+
|
11
21
|
P = ParamSpec("P")
|
12
22
|
R = TypeVar("R")
|
13
|
-
|
23
|
+
default_workers = min(32, (os.cpu_count() or 1) + 4)
|
24
|
+
default_sentinel = object()
|
14
25
|
|
15
|
-
def read_json(path: Path) -> Any:
|
26
|
+
def read_json(path: Path, default=default_sentinel) -> Any:
|
27
|
+
if default is not default_sentinel and not path.exists():
|
28
|
+
return default
|
16
29
|
with path.open("r") as f:
|
17
30
|
return json.load(f)
|
18
31
|
|
19
|
-
def write_json(path: Path, obj:
|
32
|
+
def write_json(path: Path, obj: object, indent: None | int = None) -> None:
|
20
33
|
with path.open("w") as f:
|
21
34
|
return json.dump(obj, f, indent=indent)
|
22
35
|
|
@@ -32,18 +45,18 @@ async def worker[T](task: Awaitable[T], semaphore: asyncio.Semaphore, update=noo
|
|
32
45
|
update()
|
33
46
|
return result
|
34
47
|
|
35
|
-
async def roll_tasks[T](tasks: Iterable[Awaitable[T]], workers=
|
48
|
+
async def roll_tasks[T](tasks: Iterable[Awaitable[T]], workers=default_workers, progress=False) -> list[T]:
|
36
49
|
semaphore = asyncio.Semaphore(workers)
|
37
50
|
if not progress:
|
38
|
-
return await asyncio.gather(*
|
51
|
+
return await asyncio.gather(*[worker(task, semaphore) for task in tasks])
|
39
52
|
|
40
53
|
from tqdm import tqdm
|
41
54
|
tasks = tasks if isinstance(tasks, list) else list(tasks)
|
42
55
|
with tqdm(total=len(tasks)) as pbar:
|
43
56
|
update = functools.partial(pbar.update, 1)
|
44
|
-
return await asyncio.gather(*
|
57
|
+
return await asyncio.gather(*[worker(task, semaphore, update) for task in tasks])
|
45
58
|
|
46
|
-
def as_async(workers=
|
59
|
+
def as_async(workers=default_workers) -> Callable[[Callable[P, R]], Callable[P, Awaitable[R]]]:
|
47
60
|
executor = ThreadPoolExecutor(max_workers=workers)
|
48
61
|
|
49
62
|
def on_fn(func: Callable[P, R]) -> Callable[P, Awaitable[R]]:
|
@@ -56,7 +69,7 @@ def as_async(workers=default_num_workers) -> Callable[[Callable[P, R]], Callable
|
|
56
69
|
return wrapper
|
57
70
|
return on_fn
|
58
71
|
|
59
|
-
def async_limit(workers=
|
72
|
+
def async_limit(workers=default_workers) -> Callable[[Callable[P, Awaitable[R]]], Callable[P, Awaitable[R]]]:
|
60
73
|
semaphore = asyncio.Semaphore(workers)
|
61
74
|
|
62
75
|
def on_fn(func: Callable[P, Awaitable[R]]) -> Callable[P, Awaitable[R]]:
|
@@ -2,6 +2,44 @@ import re
|
|
2
2
|
from itertools import chain
|
3
3
|
from typing import Any, Callable, Iterable, overload
|
4
4
|
|
5
|
+
__all__ = [
|
6
|
+
"noop",
|
7
|
+
"non_none",
|
8
|
+
"as_any",
|
9
|
+
"list_split",
|
10
|
+
"drop_none",
|
11
|
+
"distinct",
|
12
|
+
"dict_firsts",
|
13
|
+
"distinct_by",
|
14
|
+
"first",
|
15
|
+
"move_value",
|
16
|
+
"transpose_dict",
|
17
|
+
"make_combinations_by_dict",
|
18
|
+
"merge_dicts",
|
19
|
+
"intersect",
|
20
|
+
"ensure_tuple",
|
21
|
+
"key_of",
|
22
|
+
"omit",
|
23
|
+
"pick",
|
24
|
+
"dict_by",
|
25
|
+
"tuple_by",
|
26
|
+
"flatten",
|
27
|
+
"transpose",
|
28
|
+
"map_dict",
|
29
|
+
"deepen_dict",
|
30
|
+
"flatten_dict_inner",
|
31
|
+
"flatten_dict",
|
32
|
+
"group",
|
33
|
+
"reversed_enumerate",
|
34
|
+
"get_at",
|
35
|
+
"for_each",
|
36
|
+
"sized_partitions",
|
37
|
+
"num_partitions",
|
38
|
+
"df_from_array",
|
39
|
+
"StrFilter",
|
40
|
+
"str_filterer",
|
41
|
+
]
|
42
|
+
|
5
43
|
def noop() -> None:
|
6
44
|
pass
|
7
45
|
|
@@ -27,6 +65,16 @@ def drop_none[T](iterable: Iterable[T | None]) -> list[T]:
|
|
27
65
|
def distinct[T](items: Iterable[T]) -> list[T]:
|
28
66
|
return list(dict.fromkeys(items))
|
29
67
|
|
68
|
+
def dict_firsts[T, K](items: Iterable[tuple[K, T]]) -> dict[K, T]:
|
69
|
+
result: dict[K, T] = {}
|
70
|
+
for key, item in items:
|
71
|
+
if key not in result:
|
72
|
+
result[key] = item
|
73
|
+
return result
|
74
|
+
|
75
|
+
def distinct_by[T](items: Iterable[tuple[object, T]]) -> list[T]:
|
76
|
+
return list(dict_firsts(items).values())
|
77
|
+
|
30
78
|
def first[T](iterable: Iterable[T]) -> T | None:
|
31
79
|
return next(iter(iterable), None)
|
32
80
|
|
relib-1.2.7/relib/__init__.py
DELETED
@@ -1,45 +0,0 @@
|
|
1
|
-
from .utils import (
|
2
|
-
noop,
|
3
|
-
non_none,
|
4
|
-
as_any,
|
5
|
-
list_split,
|
6
|
-
drop_none,
|
7
|
-
distinct,
|
8
|
-
first,
|
9
|
-
move_value,
|
10
|
-
transpose_dict,
|
11
|
-
make_combinations_by_dict,
|
12
|
-
merge_dicts,
|
13
|
-
intersect,
|
14
|
-
ensure_tuple,
|
15
|
-
key_of,
|
16
|
-
omit,
|
17
|
-
pick,
|
18
|
-
dict_by,
|
19
|
-
tuple_by,
|
20
|
-
flatten,
|
21
|
-
transpose,
|
22
|
-
map_dict,
|
23
|
-
deepen_dict,
|
24
|
-
flatten_dict_inner,
|
25
|
-
flatten_dict,
|
26
|
-
group,
|
27
|
-
reversed_enumerate,
|
28
|
-
get_at,
|
29
|
-
for_each,
|
30
|
-
sized_partitions,
|
31
|
-
num_partitions,
|
32
|
-
df_from_array,
|
33
|
-
StrFilter,
|
34
|
-
str_filterer,
|
35
|
-
)
|
36
|
-
from .system import (
|
37
|
-
read_json,
|
38
|
-
write_json,
|
39
|
-
clear_console,
|
40
|
-
console_link,
|
41
|
-
roll_tasks,
|
42
|
-
as_async,
|
43
|
-
)
|
44
|
-
from .hashing import hash, hash_obj
|
45
|
-
from .measure_duration import measure_duration
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|