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