admin / Synapse-Cortex
publicSelf Hosted ITSM Tool with RBAC/Tenanting and MFA
Synapse-Cortex / Synapse-Cortexv2 / .venv / Lib / site-packages / anyio / itertools.py
16168 B · main
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 | from __future__ import annotations __all__ = ( "accumulate", "batched", "Chain", "combinations", "combinations_with_replacement", "compress", "count", "cycle", "dropwhile", "filterfalse", "groupby", "islice", "pairwise", "permutations", "product", "repeat", "starmap", "tee", "takewhile", "zip_longest", ) import itertools import operator import sys from collections.abc import ( AsyncGenerator, AsyncIterable, AsyncIterator, Awaitable, Callable, Iterable, Iterator, ) from dataclasses import dataclass, field from typing import Any, Generic, TypeVar, cast, overload from ._core._synchronization import Lock from ._core._tasks import CancelScope from .lowlevel import cancel_shielded_checkpoint, checkpoint, checkpoint_if_cancelled T = TypeVar("T") R = TypeVar("R") _tee_end = object() @dataclass(eq=False) class _IterableAsyncIterator(AsyncIterator[T]): iterator: Iterator[T] async def __anext__(self) -> T: await checkpoint_if_cancelled() try: result = next(self.iterator) except StopIteration: await cancel_shielded_checkpoint() raise StopAsyncIteration from None await cancel_shielded_checkpoint() return result def _iterate(iterable: Iterable[T] | AsyncIterable[T]) -> AsyncIterator[T]: if isinstance(iterable, AsyncIterator): return iterable if isinstance(iterable, AsyncIterable): return iterable.__aiter__() return _IterableAsyncIterator(iter(iterable)) @dataclass(eq=False) class _TeeLink(Generic[T]): value: object | None = None next: _TeeLink[T] | None = None filled: bool = False @dataclass(eq=False) class _TeeState(Generic[T]): iterator: AsyncIterator[T] lock: Lock = field(default_factory=Lock) async def fill(self, link: _TeeLink[T]) -> bool: if link.filled: return False async with self.lock: if link.filled: return True link.value = await anext(self.iterator, _tee_end) if link.value is not _tee_end: link.next = _TeeLink() link.filled = True return True class _TeeAsyncIterator(AsyncIterator[T]): _state: _TeeState[T] _link: _TeeLink[T] _element_yielded: bool def __init__( self, iterable: Iterable[T] | AsyncIterable[T] | _TeeAsyncIterator[T] ) -> None: if isinstance(iterable, _TeeAsyncIterator): self._state = iterable._state self._link = iterable._link else: self._state = _TeeState(_iterate(iterable)) self._link = _TeeLink() self._element_yielded = False async def __anext__(self) -> T: had_yieldpoint = await self._state.fill(self._link) if self._link.value is _tee_end: if not self._element_yielded: await checkpoint() raise StopAsyncIteration if not had_yieldpoint: await checkpoint_if_cancelled() self._element_yielded = True value = cast(T, self._link.value) next_link = self._link.next assert next_link is not None self._link = next_link if not had_yieldpoint: await cancel_shielded_checkpoint() return value async def _operator_add(x: T, y: T) -> T: return operator.add(x, y) async def accumulate( iterable: Iterable[T] | AsyncIterable[T], function: Callable[[T, T], Awaitable[T]] = _operator_add, *, initial: T | None = None, ) -> AsyncGenerator[T, None]: iterator = _iterate(iterable) if initial is None: try: total = await anext(iterator) except StopAsyncIteration: await checkpoint() return else: await checkpoint_if_cancelled() total = initial await cancel_shielded_checkpoint() yield total async for element in iterator: total = await function(total, element) yield total async def batched( iterable: Iterable[T] | AsyncIterable[T], n: int, *, strict: bool = False ) -> AsyncGenerator[tuple[T, ...], None]: if n < 1: raise ValueError("n must be at least one") iterator = _iterate(iterable) while True: batch: list[T] = [] for _ in range(n): try: batch.append(await anext(iterator)) except StopAsyncIteration: if not batch: await checkpoint() return if strict: raise ValueError("batched(): incomplete batch") from None yield tuple(batch) return yield tuple(batch) class Chain: def __call__( self, *iterables: Iterable[T] | AsyncIterable[T] ) -> AsyncGenerator[T, None]: return self.from_iterable(iterables) async def from_iterable( self, iterables: ( Iterable[Iterable[T] | AsyncIterable[T]] | AsyncIterable[Iterable[T] | AsyncIterable[T]] ), ) -> AsyncGenerator[T, None]: element_yielded = False outer_iter = _iterate(iterables) try: async for iterable in outer_iter: async for element in _iterate(iterable): element_yielded = True yield element finally: aclose = getattr(outer_iter, "aclose", None) if aclose is not None: with CancelScope(shield=True): await aclose() if not element_yielded: await checkpoint() chain: Chain = Chain() async def combinations( iterable: Iterable[T] | AsyncIterable[T], r: int ) -> AsyncGenerator[tuple[T, ...], None]: pool: list[T] = [element async for element in _iterate(iterable)] async for combination in _iterate(itertools.combinations(pool, r)): yield combination async def combinations_with_replacement( iterable: Iterable[T] | AsyncIterable[T], r: int ) -> AsyncGenerator[tuple[T, ...], None]: pool: list[T] = [element async for element in _iterate(iterable)] async for combination in _iterate(itertools.combinations_with_replacement(pool, r)): yield combination async def compress( data: Iterable[T] | AsyncIterable[T], selectors: Iterable[object] | AsyncIterable[object], ) -> AsyncGenerator[T, None]: data_iterator = _iterate(data) selector_iterator = _iterate(selectors) element_yielded = False while True: try: datum = await anext(data_iterator) selector = await anext(selector_iterator) except StopAsyncIteration: if not element_yielded: await checkpoint() return if selector: element_yielded = True yield datum async def count(start: int = 0, step: int = 1) -> AsyncGenerator[int, None]: n = start while True: await checkpoint_if_cancelled() value = n n += step await cancel_shielded_checkpoint() yield value async def cycle( iterable: Iterable[T] | AsyncIterable[T], ) -> AsyncGenerator[T, None]: saved: list[T] = [] async for element in _iterate(iterable): saved.append(element) yield element if not saved: await checkpoint() return while True: for element in saved: await checkpoint() yield element async def dropwhile( predicate: Callable[[T], Awaitable[object]], iterable: Iterable[T] | AsyncIterable[T], ) -> AsyncGenerator[T, None]: element_yielded = False dropping = True async for element in _iterate(iterable): if dropping and await predicate(element): continue dropping = False element_yielded = True yield element if not element_yielded: await checkpoint() async def filterfalse( predicate: Callable[[T], Awaitable[object]], iterable: Iterable[T] | AsyncIterable[T], ) -> AsyncGenerator[T, None]: element_yielded = False async for element in _iterate(iterable): if not await predicate(element): element_yielded = True yield element if not element_yielded: await checkpoint() @overload def groupby( iterable: Iterable[T] | AsyncIterable[T], ) -> AsyncGenerator[tuple[T, list[T]], None]: ... @overload def groupby( iterable: Iterable[T] | AsyncIterable[T], key: Callable[[T], Awaitable[R]], ) -> AsyncGenerator[tuple[R, list[T]], None]: ... async def groupby( iterable: Iterable[T] | AsyncIterable[T], key: Callable[[T], Awaitable[object]] | None = None, ) -> AsyncGenerator[tuple[object, list[T]], None]: iterator = _iterate(iterable) try: element = await anext(iterator) except StopAsyncIteration: await checkpoint() return group_key = element if key is None else await key(element) values = [element] async for element in iterator: next_key = element if key is None else await key(element) if next_key != group_key: completed_group = group_key, values group_key = next_key values = [element] yield completed_group else: values.append(element) yield group_key, values @overload def islice( iterable: Iterable[T] | AsyncIterable[T], stop: int | None, /, ) -> AsyncGenerator[T, None]: ... @overload def islice( iterable: Iterable[T] | AsyncIterable[T], start: int | None, stop: int | None, step: int | None = 1, /, ) -> AsyncGenerator[T, None]: ... async def islice( iterable: Iterable[T] | AsyncIterable[T], *args: int | None, ) -> AsyncGenerator[T, None]: if not args: raise TypeError("islice expected at least 2 arguments, got 1") if len(args) > 3: raise TypeError(f"islice expected at most 4 arguments, got {len(args) + 1}") slice_args = slice(*args) start_message = ( "Indices for islice() must be None or an integer: 0 <= x <= sys.maxsize." ) stop_message = ( "Stop argument for islice() must be None or an integer: 0 <= x <= sys.maxsize." ) step_message = "Step for islice() must be a positive integer or None." def normalize_index(value: object, message: str) -> int: try: index = operator.index(cast(Any, value)) except TypeError: raise ValueError(message) from None if index < 0 or index > sys.maxsize: raise ValueError(message) return index start = ( 0 if slice_args.start is None else normalize_index(slice_args.start, start_message) ) stop = ( None if slice_args.stop is None else normalize_index(slice_args.stop, stop_message) ) step = ( 1 if slice_args.step is None else normalize_index(slice_args.step, step_message) ) if step <= 0: raise ValueError(step_message) if stop == 0 or start == stop: await checkpoint() return iterator = _iterate(iterable) index = 0 element_yielded = False while stop is None or index < stop: try: element = await anext(iterator) except StopAsyncIteration: if not element_yielded: await checkpoint() return if index >= start and (index - start) % step == 0: index += 1 element_yielded = True yield element else: index += 1 if not element_yielded: await checkpoint() async def pairwise( iterable: Iterable[T] | AsyncIterable[T], ) -> AsyncGenerator[tuple[T, T], None]: iterator = _iterate(iterable) try: previous = await anext(iterator) except StopAsyncIteration: await checkpoint() return element_yielded = False async for element in iterator: element_yielded = True pair = (previous, element) previous = element yield pair if not element_yielded: await checkpoint() async def permutations( iterable: Iterable[T] | AsyncIterable[T], r: int | None = None ) -> AsyncGenerator[tuple[T, ...], None]: pool: list[T] = [element async for element in _iterate(iterable)] n = len(pool) if r is None: r = n elif not isinstance(r, int): raise TypeError("Expected int as r") elif r < 0: raise ValueError("r must be non-negative") async for permutation in _iterate(itertools.permutations(pool, r)): yield permutation async def product( *iterables: Iterable[T] | AsyncIterable[T], repeat: int = 1 ) -> AsyncGenerator[tuple[T, ...], None]: repeat = operator.index(repeat) if repeat < 0: raise ValueError("repeat argument cannot be negative") pools: list[tuple[T, ...]] = [] for iterable in iterables: pool: list[T] = [element async for element in _iterate(iterable)] pools.append(tuple(pool)) async for value in _iterate(itertools.product(*pools, repeat=repeat)): yield value async def repeat(element: T, times: int | None = None) -> AsyncGenerator[T, None]: if times is None: while True: await checkpoint() yield element remaining = operator.index(cast(Any, times)) if remaining <= 0: await checkpoint() return while remaining > 0: await checkpoint_if_cancelled() remaining -= 1 await cancel_shielded_checkpoint() yield element async def starmap( function: Callable[..., Awaitable[R]], iterable: ( Iterable[Iterable[object] | AsyncIterable[object]] | AsyncIterable[Iterable[object] | AsyncIterable[object]] ), ) -> AsyncGenerator[R, None]: result_yielded = False async for args_iterable in _iterate(iterable): args = [element async for element in _iterate(args_iterable)] result_yielded = True yield await function(*args) if not result_yielded: await checkpoint() def tee( iterable: Iterable[T] | AsyncIterable[T], n: int = 2 ) -> tuple[AsyncIterator[T], ...]: n = operator.index(cast(Any, n)) if n < 0: raise ValueError("n must be >= 0") if n == 0: return () iterator = _TeeAsyncIterator(iterable) iterators: list[AsyncIterator[T]] = [iterator] iterators.extend(_TeeAsyncIterator(iterator) for _ in range(n - 1)) return tuple(iterators) async def takewhile( predicate: Callable[[T], Awaitable[object]], iterable: Iterable[T] | AsyncIterable[T], ) -> AsyncGenerator[T, None]: element_yielded = False async for element in _iterate(iterable): if not await predicate(element): if not element_yielded: await checkpoint() return element_yielded = True yield element if not element_yielded: await checkpoint() async def zip_longest( *iterables: Iterable[object] | AsyncIterable[object], fillvalue: object = None, ) -> AsyncGenerator[tuple[object, ...], None]: iterators = [_iterate(iterable) for iterable in iterables] num_active = len(iterators) if not num_active: await checkpoint() return active = [True] * num_active tuple_yielded = False while True: values: list[object] = [] for index, iterator in enumerate(iterators): if not active[index]: values.append(fillvalue) continue try: value = await anext(iterator) except StopAsyncIteration: active[index] = False num_active -= 1 if not num_active: if not tuple_yielded: await checkpoint() return value = fillvalue values.append(value) tuple_yielded = True yield tuple(values) |