Skip to content

Commit

Permalink
Use abstraction layer for core optimization scalar
Browse files Browse the repository at this point in the history
  • Loading branch information
glatterf42 committed Jul 2, 2024
1 parent 0a2c79f commit 2698897
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 31 deletions.
35 changes: 8 additions & 27 deletions ixmp4/core/optimization/scalar.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
from datetime import datetime
from typing import ClassVar, Iterable
from typing import ClassVar

import pandas as pd

from ixmp4.core.base import BaseFacade, BaseModelFacade
from ixmp4.core.base import BaseModelFacade
from ixmp4.core.unit import Unit
from ixmp4.data.abstract import Docs as DocsModel
from ixmp4.data.abstract import Run
from ixmp4.data.abstract import Scalar as ScalarModel

from .base import Lister, Retriever, Tabulator


class Scalar(BaseModelFacade):
_model: ScalarModel
Expand Down Expand Up @@ -93,12 +92,11 @@ def __str__(self) -> str:
return f"<Scalar {self.id} name={self.name}>"


class ScalarRepository(BaseFacade):
_run: Run

def __init__(self, _run: Run, *args, **kwargs) -> None:
class ScalarRepository(Retriever[Scalar], Lister[Scalar], Tabulator[Scalar]):
def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self._run = _run
self._backend_repository = self.backend.optimization.scalars
self._model_type = Scalar

def create(self, name: str, value: float, unit: str | Unit | None = None) -> Scalar:
if isinstance(unit, Unit):
Expand All @@ -121,20 +119,3 @@ def create(self, name: str, value: float, unit: str | Unit | None = None) -> Sca
"run.optimization.scalars.update()?"
) from e
return Scalar(_backend=self.backend, _model=model)

def get(self, name: str) -> Scalar:
model = self.backend.optimization.scalars.get(run_id=self._run.id, name=name)
return Scalar(_backend=self.backend, _model=model)

def list(self, name: str | None = None) -> Iterable[Scalar]:
scalars = self.backend.optimization.scalars.list(run_id=self._run.id, name=name)
return [
Scalar(
_backend=self.backend,
_model=i,
)
for i in scalars
]

def tabulate(self, name: str | None = None) -> pd.DataFrame:
return self.backend.optimization.scalars.tabulate(name=name)
8 changes: 4 additions & 4 deletions ixmp4/data/abstract/optimization/scalar.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,21 @@ class ScalarRepository(
):
docs: DocsRepository

def create(self, name: str, value: float, unit_name: str, run_id: int) -> Scalar:
def create(self, run_id: int, name: str, value: float, unit_name: str) -> Scalar:
"""Creates a Scalar.
Parameters
----------
run_id : int
The id of the :class:`ixmp4.data.abstract.Run` for which this Scalar is
defined.
name : str
The name of the Scalar.
value : float
The value of the Scalar.
unit_name : str
The name of the :class:`ixmp4.data.abstract.Unit` for which this Scalar is
defined.
run_id : int
The id of the :class:`ixmp4.data.abstract.Run` for which this Scalar is
defined.
Raises
------
Expand Down

0 comments on commit 2698897

Please sign in to comment.