-
Notifications
You must be signed in to change notification settings - Fork 6
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
1 parent
9132e59
commit 632c385
Showing
8 changed files
with
115 additions
and
81 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,61 +1,94 @@ | ||
from typing import Generic, TypeVar | ||
from typing import TYPE_CHECKING, Generic, TypeVar | ||
|
||
if TYPE_CHECKING: | ||
from . import InitKwargs | ||
|
||
|
||
import pandas as pd | ||
|
||
# TODO Import this from typing when dropping Python 3.11 | ||
from typing_extensions import TypedDict, Unpack | ||
|
||
from ixmp4.core.base import BaseFacade, BaseModelFacade | ||
from ixmp4.data import abstract | ||
|
||
OptimizationModelType = TypeVar("OptimizationModelType", bound=BaseModelFacade) | ||
FacadeOptimizationModelType = TypeVar( | ||
"FacadeOptimizationModelType", bound=BaseModelFacade | ||
) | ||
AbstractOptimizationModelType = TypeVar( | ||
"AbstractOptimizationModelType", bound=abstract.BaseModel | ||
) | ||
|
||
|
||
class OptimizationBaseRepository(BaseFacade, Generic[OptimizationModelType]): | ||
class OptimizationBaseRepository( | ||
BaseFacade, Generic[FacadeOptimizationModelType, AbstractOptimizationModelType] | ||
): | ||
_run: abstract.Run | ||
_backend_repository: abstract.BackendBaseRepository | ||
_model_type: type[OptimizationModelType] | ||
_backend_repository: abstract.BackendBaseRepository[AbstractOptimizationModelType] | ||
_model_type: type[FacadeOptimizationModelType] | ||
|
||
def __init__(self, _run: abstract.Run, *args, **kwargs) -> None: | ||
super().__init__(*args, **kwargs) | ||
def __init__(self, _run: abstract.Run, **kwargs: Unpack["InitKwargs"]) -> None: | ||
super().__init__(**kwargs) | ||
self._run = _run | ||
|
||
|
||
class Creator(OptimizationBaseRepository[OptimizationModelType], abstract.Creator): | ||
class Creator( | ||
OptimizationBaseRepository[ | ||
FacadeOptimizationModelType, AbstractOptimizationModelType | ||
], | ||
abstract.Creator, | ||
): | ||
def create( | ||
self, | ||
name: str, | ||
# TODO But how do we now show in core layer that e.g. Table needs these? | ||
# constrained_to_indexsets: list[str], | ||
# column_names: list[str] | None = None, | ||
*args, | ||
**kwargs, | ||
) -> OptimizationModelType: | ||
self, name: str, **kwargs: Unpack["abstract.optimization.base.CreateKwargs"] | ||
) -> FacadeOptimizationModelType: | ||
model = self._backend_repository.create( | ||
*args, | ||
**dict(kwargs, name=name, run_id=self._run.id), | ||
run_id=self._run.id, name=name, **kwargs | ||
) | ||
return self._model_type(_backend=self.backend, _model=model) | ||
|
||
|
||
class Retriever(OptimizationBaseRepository[OptimizationModelType], abstract.Retriever): | ||
def get(self, name: str, *args, **kwargs) -> OptimizationModelType: | ||
class Retriever( | ||
OptimizationBaseRepository[ | ||
FacadeOptimizationModelType, AbstractOptimizationModelType | ||
], | ||
abstract.Retriever, | ||
): | ||
def get(self, name: str) -> FacadeOptimizationModelType: | ||
model = self._backend_repository.get(run_id=self._run.id, name=name) | ||
return self._model_type(_backend=self.backend, _model=model) | ||
|
||
|
||
class Lister(OptimizationBaseRepository[OptimizationModelType], abstract.Lister): | ||
def list(self, name: str | None = None) -> list[OptimizationModelType]: | ||
class Lister( | ||
OptimizationBaseRepository[ | ||
FacadeOptimizationModelType, AbstractOptimizationModelType | ||
], | ||
abstract.Lister, | ||
): | ||
def list(self, name: str | None = None) -> list[FacadeOptimizationModelType]: | ||
models = self._backend_repository.list(run_id=self._run.id, name=name) | ||
return [self._model_type(_backend=self.backend, _model=m) for m in models] | ||
|
||
|
||
class Tabulator(OptimizationBaseRepository[OptimizationModelType], abstract.Tabulator): | ||
class Tabulator( | ||
OptimizationBaseRepository[ | ||
FacadeOptimizationModelType, AbstractOptimizationModelType | ||
], | ||
abstract.Tabulator, | ||
): | ||
def tabulate(self, name: str | None = None) -> pd.DataFrame: | ||
return self._backend_repository.tabulate(run_id=self._run.id, name=name) | ||
|
||
|
||
class EnumerateKwargs(TypedDict, total=False): | ||
name: str | None | ||
|
||
|
||
class Enumerator( | ||
Lister[OptimizationModelType], Tabulator[OptimizationModelType], abstract.Enumerator | ||
Lister[FacadeOptimizationModelType, AbstractOptimizationModelType], | ||
Tabulator[FacadeOptimizationModelType, AbstractOptimizationModelType], | ||
abstract.Enumerator, | ||
): | ||
def enumerate( | ||
self, *args, table: bool = False, **kwargs | ||
) -> list[OptimizationModelType] | pd.DataFrame: | ||
return self.tabulate(*args, **kwargs) if table else self.list(*args, **kwargs) | ||
self, table: bool = False, **kwargs: Unpack[EnumerateKwargs] | ||
) -> list[FacadeOptimizationModelType] | pd.DataFrame: | ||
return self.tabulate(**kwargs) if table else self.list(**kwargs) |
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
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
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