relib 1.3.1__tar.gz → 1.3.2__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.3.1
3
+ Version: 1.3.2
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.3.1"
3
+ version = "1.3.2"
4
4
  requires-python = ">=3.12"
5
5
  dependencies = []
6
6
  authors = [
@@ -1,16 +1,17 @@
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",
12
13
  "reversed_enumerate",
13
- "sized_partitions", "sort_by",
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))[::1], reversed(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
- 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)]
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]: ...
@@ -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 Awaitable, Callable, Iterable, ParamSpec, TypeVar
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: Awaitable[T], semaphore: asyncio.Semaphore, update=noop) -> T:
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[Awaitable[T]], workers=default_workers, progress=False) -> list[T]:
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, Awaitable[R]]]:
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, Awaitable[R]]:
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, Awaitable[R]]], Callable[P, Awaitable[R]]]:
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, Awaitable[R]]) -> Callable[P, Awaitable[R]]:
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:
@@ -106,7 +106,7 @@ wheels = [
106
106
 
107
107
  [[package]]
108
108
  name = "relib"
109
- version = "1.3.1"
109
+ version = "1.3.2"
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
File without changes
File without changes