Last active
January 14, 2022 23:25
-
-
Save circuitsacul/86a0102b34ad32e6a1f122ceb64337ec to your computer and use it in GitHub Desktop.
Quickly convert values of large lists.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# example: | |
range_of_ints = range(0, 500_000_000_000_000) | |
squared_ints = LazyList(range_of_ints, lambda v: v**2) | |
print(len(squared_ints) == len(range_of_ints)) # -> True | |
print(squared_ints) # -> LazyList([0, 1, 4, 9, 16, ..., 249999999999999000000000000001]) | |
print(squared_ints[55:100]) # -> LazyList([3025, 3136, 3249, 3364, 3481, ..., 9801]) | |
print(squared_ints[500]) # -> 250000 | |
# code: | |
# Note: The following code was taken from https://github.com/TrigonDev/apgorm, which is licensed under the MIT license. | |
# https://github.com/TrigonDev/apgorm/blob/main/apgorm/lazy_list.py | |
from __future__ import annotations | |
from typing import ( | |
Any, | |
Callable, | |
Generator, | |
Generic, | |
Sequence, | |
TypeVar, | |
overload, | |
) | |
_IN = TypeVar("_IN") | |
_OUT = TypeVar("_OUT") | |
class LazyList(Generic[_IN, _OUT]): | |
def __init__( | |
self, | |
data: Sequence[_IN] | LazyList[Any, _IN], | |
converter: Callable[[_IN], _OUT], | |
) -> None: | |
self._data = data | |
self._converter = converter | |
def _convert(self, values: Sequence[_IN] | None = None) -> list[_OUT]: | |
_values = self._data if not values else values | |
return [self._converter(v) for v in _values] | |
@overload | |
def __getitem__(self, index: int) -> _OUT: | |
... | |
@overload | |
def __getitem__(self, index: slice) -> LazyList[_IN, _OUT]: | |
... | |
def __getitem__(self, index: int | slice) -> LazyList[_IN, _OUT] | _OUT: | |
if isinstance(index, int): | |
return self._convert([self._data[index]])[0] | |
return LazyList(self._data[index], self._converter) | |
def __iter__(self) -> Generator[_OUT, None, None]: | |
for r in self._data: | |
yield self._converter(r) | |
def __list__(self) -> list[_OUT]: | |
return self._convert() | |
def __len__(self) -> int: | |
return len(self._data) | |
def __repr__(self) -> str: | |
if len(self) > 5: | |
ddd = ", ..., {}".format(repr(self[-1])) | |
else: | |
ddd = "" | |
return "LazyList([{}{}])".format( | |
", ".join([repr(c) for c in list(self[0:5])]), | |
ddd, | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment