-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
323 additions
and
292 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
from __future__ import annotations | ||
|
||
|
||
from abc import ABC, abstractmethod | ||
from typing import Generic, Iterator, Optional, TypeVar, List, TypedDict | ||
|
||
|
||
class Resource(TypedDict): | ||
pass | ||
|
||
|
||
T = TypeVar("T", bound=Resource) | ||
|
||
|
||
class Resources(ABC, Generic[T], Iterator[T]): | ||
def __init__(self, data: List[T]) -> None: | ||
super().__init__() | ||
self.data = data | ||
|
||
@abstractmethod | ||
def find(self, *args, **kwargs) -> Resources[T]: | ||
raise NotImplementedError() | ||
|
||
@abstractmethod | ||
def find_one(self, *args, **kwargs) -> Optional[T]: | ||
raise NotImplementedError() | ||
|
||
@abstractmethod | ||
def get(self, id: str) -> T: | ||
raise NotImplementedError() | ||
|
||
def __iter__(self) -> Iterator[T]: | ||
self.index = 0 | ||
return self | ||
|
||
def __next__(self) -> T: | ||
if self.index >= len(self.data): | ||
raise StopIteration | ||
|
||
v = self.data[self.index] | ||
self.index += 1 | ||
return v | ||
|
||
def to_pandas(self): | ||
try: | ||
from pandas import DataFrame | ||
|
||
return DataFrame(self) | ||
except ImportError: | ||
return None | ||
|
||
|
||
class LazyResources(Resources[T]): | ||
def __init__(self, data: List[T] = []) -> None: | ||
super().__init__(data) | ||
self.data = data | ||
self.exhausted = False | ||
self.index = 0 | ||
|
||
@abstractmethod | ||
def fetch(self, index) -> tuple[Optional[Iterator[T]], bool]: | ||
raise NotImplementedError() | ||
|
||
def __iter__(self) -> Iterator[T]: | ||
self.index = 0 | ||
return self | ||
|
||
def __next__(self) -> T: | ||
if self.index >= len(self.data): | ||
if self.exhausted: | ||
raise StopIteration | ||
|
||
results, self.exhausted = self.fetch(self.index) | ||
if not results: | ||
raise StopIteration | ||
|
||
self.data += results | ||
|
||
v = self.data[self.index] | ||
self.index += 1 | ||
return v |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from typing import Iterator, List | ||
from .resources import Resource, Resources, LazyResources | ||
|
||
|
||
class FakeResource(Resource): | ||
pass | ||
|
||
|
||
class FakeResources(Resources[FakeResource]): | ||
def __init__(self, data: List[FakeResource] = []) -> None: | ||
super().__init__(data) | ||
|
||
def __next__(self) -> FakeResource: | ||
return super().__next__() | ||
|
||
def find(self) -> Resources[FakeResource]: | ||
return self | ||
|
||
def find_one(self) -> FakeResource | None: | ||
return None | ||
|
||
def get(self, _: str) -> FakeResource: | ||
return | ||
|
||
|
||
class TestResources: | ||
def test(self): | ||
resources = FakeResources() | ||
assert resources == resources.find() | ||
assert resources.find_one() is None | ||
assert resources.get(None) is None | ||
|
||
|
||
class FakeLazyResources(FakeResources, LazyResources): | ||
def fetch(self, index) -> tuple[Iterator | None, bool]: | ||
return [FakeResource], len(self.data) > 0 | ||
|
||
|
||
class TestFakeLazyResources: | ||
def test(self): | ||
resources = FakeLazyResources() | ||
assert resources == resources.find() | ||
assert resources.find_one() is None | ||
assert resources.get(None) is None |
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
Oops, something went wrong.