relib 1.2.6__py3-none-any.whl → 1.2.8__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/__init__.py +1 -0
- relib/system.py +21 -8
- {relib-1.2.6.dist-info → relib-1.2.8.dist-info}/METADATA +1 -1
- relib-1.2.8.dist-info/RECORD +9 -0
- relib-1.2.6.dist-info/RECORD +0 -9
- {relib-1.2.6.dist-info → relib-1.2.8.dist-info}/WHEEL +0 -0
- {relib-1.2.6.dist-info → relib-1.2.8.dist-info}/licenses/LICENSE +0 -0
relib/__init__.py
CHANGED
relib/system.py
CHANGED
@@ -11,14 +11,17 @@ from .utils import noop
|
|
11
11
|
P = ParamSpec("P")
|
12
12
|
R = TypeVar("R")
|
13
13
|
default_num_workers = min(32, (os.cpu_count() or 1) + 4)
|
14
|
+
_default_json: Any = {}
|
14
15
|
|
15
|
-
def read_json(path: Path) -> Any:
|
16
|
+
def read_json(path: Path, default=_default_json) -> Any:
|
17
|
+
if default is not _default_json and not path.exists():
|
18
|
+
return default
|
16
19
|
with path.open("r") as f:
|
17
20
|
return json.load(f)
|
18
21
|
|
19
|
-
def write_json(path: Path, obj: Any) -> None:
|
22
|
+
def write_json(path: Path, obj: Any, indent: None | int = None) -> None:
|
20
23
|
with path.open("w") as f:
|
21
|
-
return json.dump(obj, f)
|
24
|
+
return json.dump(obj, f, indent=indent)
|
22
25
|
|
23
26
|
def clear_console() -> None:
|
24
27
|
os.system("cls" if os.name == "nt" else "clear")
|
@@ -32,19 +35,19 @@ async def worker[T](task: Awaitable[T], semaphore: asyncio.Semaphore, update=noo
|
|
32
35
|
update()
|
33
36
|
return result
|
34
37
|
|
35
|
-
async def roll_tasks[T](tasks: Iterable[Awaitable[T]], workers
|
38
|
+
async def roll_tasks[T](tasks: Iterable[Awaitable[T]], workers=default_num_workers, progress=False) -> list[T]:
|
36
39
|
semaphore = asyncio.Semaphore(workers)
|
37
40
|
if not progress:
|
38
|
-
return await asyncio.gather(*
|
41
|
+
return await asyncio.gather(*[worker(task, semaphore) for task in tasks])
|
39
42
|
|
40
43
|
from tqdm import tqdm
|
41
44
|
tasks = tasks if isinstance(tasks, list) else list(tasks)
|
42
45
|
with tqdm(total=len(tasks)) as pbar:
|
43
46
|
update = functools.partial(pbar.update, 1)
|
44
|
-
return await asyncio.gather(*
|
47
|
+
return await asyncio.gather(*[worker(task, semaphore, update) for task in tasks])
|
45
48
|
|
46
|
-
def as_async(
|
47
|
-
executor = ThreadPoolExecutor(max_workers=
|
49
|
+
def as_async(workers=default_num_workers) -> Callable[[Callable[P, R]], Callable[P, Awaitable[R]]]:
|
50
|
+
executor = ThreadPoolExecutor(max_workers=workers)
|
48
51
|
|
49
52
|
def on_fn(func: Callable[P, R]) -> Callable[P, Awaitable[R]]:
|
50
53
|
@functools.wraps(func)
|
@@ -54,5 +57,15 @@ def as_async(num_workers=default_num_workers) -> Callable[[Callable[P, R]], Call
|
|
54
57
|
fn_call = functools.partial(ctx.run, func, *args, **kwargs)
|
55
58
|
return await loop.run_in_executor(executor, fn_call)
|
56
59
|
return wrapper
|
60
|
+
return on_fn
|
61
|
+
|
62
|
+
def async_limit(workers=default_num_workers) -> Callable[[Callable[P, Awaitable[R]]], Callable[P, Awaitable[R]]]:
|
63
|
+
semaphore = asyncio.Semaphore(workers)
|
57
64
|
|
65
|
+
def on_fn(func: Callable[P, Awaitable[R]]) -> Callable[P, Awaitable[R]]:
|
66
|
+
@functools.wraps(func)
|
67
|
+
async def wrapper(*args: P.args, **kwargs: P.kwargs) -> R:
|
68
|
+
async with semaphore:
|
69
|
+
return await func(*args, **kwargs)
|
70
|
+
return wrapper
|
58
71
|
return on_fn
|
@@ -0,0 +1,9 @@
|
|
1
|
+
relib/__init__.py,sha256=pypVcr0ctaSdFZmwMnZvtioTbms3MlWfAcrnPVdZxog,689
|
2
|
+
relib/hashing.py,sha256=DB_fnkj0ls01FgZbf4nPFHl4EBU8X_0OrmDvty4HlRE,6020
|
3
|
+
relib/measure_duration.py,sha256=LCTo_D_qReNprD3fhtJ0daeWycS6xQE_cwxeg2_h0xo,456
|
4
|
+
relib/system.py,sha256=RO8pd5eUmucbAxgfFi7UWHH5Cdp0aM6O1oe5cylrc4k,2527
|
5
|
+
relib/utils.py,sha256=4fM0zsAdfrM2w9Q1YKcTfFG5kDzoNgD2amkgfTG39OY,6933
|
6
|
+
relib-1.2.8.dist-info/METADATA,sha256=b8RvpgbcT7Y9zq6r-kwnnCZgql_eAdG41Y83-q2YIXg,1295
|
7
|
+
relib-1.2.8.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
8
|
+
relib-1.2.8.dist-info/licenses/LICENSE,sha256=9xVsdtv_-uSyY9Xl9yujwAPm4-mjcCLeVy-ljwXEWbo,1059
|
9
|
+
relib-1.2.8.dist-info/RECORD,,
|
relib-1.2.6.dist-info/RECORD
DELETED
@@ -1,9 +0,0 @@
|
|
1
|
-
relib/__init__.py,sha256=E_ye3ZsGqW3xCibphEWtXhXjIJI58zVxX53VajgvG2Q,674
|
2
|
-
relib/hashing.py,sha256=DB_fnkj0ls01FgZbf4nPFHl4EBU8X_0OrmDvty4HlRE,6020
|
3
|
-
relib/measure_duration.py,sha256=LCTo_D_qReNprD3fhtJ0daeWycS6xQE_cwxeg2_h0xo,456
|
4
|
-
relib/system.py,sha256=snk8Y7ou9zEe6Zmk18qlMZ3mMAb9k4yQDoXOPbeHLNo,1929
|
5
|
-
relib/utils.py,sha256=4fM0zsAdfrM2w9Q1YKcTfFG5kDzoNgD2amkgfTG39OY,6933
|
6
|
-
relib-1.2.6.dist-info/METADATA,sha256=HW81w0srpbZPqeo8qIlOA-DNUqXD7ulPhSuHRCpiFFs,1295
|
7
|
-
relib-1.2.6.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
8
|
-
relib-1.2.6.dist-info/licenses/LICENSE,sha256=9xVsdtv_-uSyY9Xl9yujwAPm4-mjcCLeVy-ljwXEWbo,1059
|
9
|
-
relib-1.2.6.dist-info/RECORD,,
|
File without changes
|
File without changes
|