-
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
8 changed files
with
362 additions
and
296 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
name: Test | ||
name: Push | ||
on: [push] | ||
concurrency: | ||
group: ${{ github.head_ref }} | ||
cancel-in-progress: true | ||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
|
@@ -20,5 +23,25 @@ jobs: | |
python-version: ${{ matrix.python-version }} | ||
- run: just deps | ||
- run: just test | ||
- run: just cov | ||
lint: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: extractions/setup-just@v1 | ||
- uses: actions/setup-python@v5 | ||
- run: just deps | ||
- run: just lint | ||
cov: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: extractions/setup-just@v1 | ||
- uses: actions/setup-python@v5 | ||
- run: just deps | ||
- run: just test | ||
- run: just cov xml | ||
- if: always() | ||
uses: orgoro/[email protected] | ||
with: | ||
coverageFile: coverage.xml | ||
token: ${{ secrets.GITHUB_TOKEN }} |
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
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, Tuple | ||
|
||
|
||
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,40 @@ | ||
from typing import Iterator, Tuple, Optional | ||
from unittest.mock import Mock | ||
|
||
from .resources import Resource, Resources, LazyResources | ||
|
||
|
||
class FakeResource(Resource): | ||
pass | ||
|
||
|
||
class FakeResources(Resources[FakeResource]): | ||
def find(self) -> Resources[FakeResource]: | ||
return self | ||
|
||
def find_one(self) -> Optional[FakeResource]: | ||
return Mock(spec=FakeResource) | ||
|
||
def get(self, _: str) -> FakeResource: | ||
return Mock(spec=FakeResource) | ||
|
||
|
||
class TestResources: | ||
def test(self): | ||
resources = FakeResources() | ||
assert resources == resources.find() | ||
assert resources.find_one() | ||
assert resources.get(None) | ||
|
||
|
||
class FakeLazyResources(FakeResources, LazyResources): | ||
def fetch(self, index) -> Tuple[Optional[Iterator[FakeResource]], bool]: | ||
return iter([FakeResource()]), len(self.data) > 0 | ||
|
||
|
||
class TestFakeLazyResources: | ||
def test(self): | ||
resources = FakeLazyResources() | ||
assert resources == resources.find() | ||
assert resources.find_one() | ||
assert resources.get(None) |
Oops, something went wrong.