relib 1.2.5__tar.gz → 1.2.7__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: relib
3
- Version: 1.2.5
3
+ Version: 1.2.7
4
4
  Project-URL: Repository, https://github.com/Reddan/relib.git
5
5
  Author: Hampus Hallman
6
6
  License: Copyright 2018-2025 Hampus Hallman
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "relib"
3
- version = "1.2.5"
3
+ version = "1.2.7"
4
4
  requires-python = ">=3.12"
5
5
  dependencies = []
6
6
  authors = [
@@ -16,9 +16,9 @@ def read_json(path: Path) -> Any:
16
16
  with path.open("r") as f:
17
17
  return json.load(f)
18
18
 
19
- def write_json(path: Path, obj: Any) -> None:
19
+ def write_json(path: Path, obj: Any, indent: None | int = None) -> None:
20
20
  with path.open("w") as f:
21
- return json.dump(obj, f)
21
+ return json.dump(obj, f, indent=indent)
22
22
 
23
23
  def clear_console() -> None:
24
24
  os.system("cls" if os.name == "nt" else "clear")
@@ -32,18 +32,19 @@ async def worker[T](task: Awaitable[T], semaphore: asyncio.Semaphore, update=noo
32
32
  update()
33
33
  return result
34
34
 
35
- async def roll_tasks[T](tasks: Iterable[Awaitable[T]], workers: int, progress=False) -> list[T]:
35
+ async def roll_tasks[T](tasks: Iterable[Awaitable[T]], workers=default_num_workers, progress=False) -> list[T]:
36
36
  semaphore = asyncio.Semaphore(workers)
37
37
  if not progress:
38
38
  return await asyncio.gather(*(worker(task, semaphore) for task in tasks))
39
+
39
40
  from tqdm import tqdm
40
- assert isinstance(tasks, (list, tuple, set, frozenset, dict))
41
+ tasks = tasks if isinstance(tasks, list) else list(tasks)
41
42
  with tqdm(total=len(tasks)) as pbar:
42
- update = lambda: pbar.update(1)
43
+ update = functools.partial(pbar.update, 1)
43
44
  return await asyncio.gather(*(worker(task, semaphore, update) for task in tasks))
44
45
 
45
- def as_async(num_workers=default_num_workers) -> Callable[[Callable[P, R]], Callable[P, Awaitable[R]]]:
46
- executor = ThreadPoolExecutor(max_workers=num_workers)
46
+ def as_async(workers=default_num_workers) -> Callable[[Callable[P, R]], Callable[P, Awaitable[R]]]:
47
+ executor = ThreadPoolExecutor(max_workers=workers)
47
48
 
48
49
  def on_fn(func: Callable[P, R]) -> Callable[P, Awaitable[R]]:
49
50
  @functools.wraps(func)
@@ -53,5 +54,15 @@ def as_async(num_workers=default_num_workers) -> Callable[[Callable[P, R]], Call
53
54
  fn_call = functools.partial(ctx.run, func, *args, **kwargs)
54
55
  return await loop.run_in_executor(executor, fn_call)
55
56
  return wrapper
57
+ return on_fn
58
+
59
+ def async_limit(workers=default_num_workers) -> Callable[[Callable[P, Awaitable[R]]], Callable[P, Awaitable[R]]]:
60
+ semaphore = asyncio.Semaphore(workers)
56
61
 
62
+ def on_fn(func: Callable[P, Awaitable[R]]) -> Callable[P, Awaitable[R]]:
63
+ @functools.wraps(func)
64
+ async def wrapper(*args: P.args, **kwargs: P.kwargs) -> R:
65
+ async with semaphore:
66
+ return await func(*args, **kwargs)
67
+ return wrapper
57
68
  return on_fn
@@ -106,7 +106,7 @@ wheels = [
106
106
 
107
107
  [[package]]
108
108
  name = "relib"
109
- version = "1.2.5"
109
+ version = "1.2.7"
110
110
  source = { editable = "." }
111
111
 
112
112
  [package.dev-dependencies]
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes