Skip to content

Commit 2698897

Browse files
committed
Use abstraction layer for core optimization scalar
1 parent 0a2c79f commit 2698897

File tree

2 files changed

+12
-31
lines changed

2 files changed

+12
-31
lines changed

ixmp4/core/optimization/scalar.py

+8-27
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
from datetime import datetime
2-
from typing import ClassVar, Iterable
2+
from typing import ClassVar
33

4-
import pandas as pd
5-
6-
from ixmp4.core.base import BaseFacade, BaseModelFacade
4+
from ixmp4.core.base import BaseModelFacade
75
from ixmp4.core.unit import Unit
86
from ixmp4.data.abstract import Docs as DocsModel
9-
from ixmp4.data.abstract import Run
107
from ixmp4.data.abstract import Scalar as ScalarModel
118

9+
from .base import Lister, Retriever, Tabulator
10+
1211

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

9594

96-
class ScalarRepository(BaseFacade):
97-
_run: Run
98-
99-
def __init__(self, _run: Run, *args, **kwargs) -> None:
95+
class ScalarRepository(Retriever[Scalar], Lister[Scalar], Tabulator[Scalar]):
96+
def __init__(self, *args, **kwargs) -> None:
10097
super().__init__(*args, **kwargs)
101-
self._run = _run
98+
self._backend_repository = self.backend.optimization.scalars
99+
self._model_type = Scalar
102100

103101
def create(self, name: str, value: float, unit: str | Unit | None = None) -> Scalar:
104102
if isinstance(unit, Unit):
@@ -121,20 +119,3 @@ def create(self, name: str, value: float, unit: str | Unit | None = None) -> Sca
121119
"run.optimization.scalars.update()?"
122120
) from e
123121
return Scalar(_backend=self.backend, _model=model)
124-
125-
def get(self, name: str) -> Scalar:
126-
model = self.backend.optimization.scalars.get(run_id=self._run.id, name=name)
127-
return Scalar(_backend=self.backend, _model=model)
128-
129-
def list(self, name: str | None = None) -> Iterable[Scalar]:
130-
scalars = self.backend.optimization.scalars.list(run_id=self._run.id, name=name)
131-
return [
132-
Scalar(
133-
_backend=self.backend,
134-
_model=i,
135-
)
136-
for i in scalars
137-
]
138-
139-
def tabulate(self, name: str | None = None) -> pd.DataFrame:
140-
return self.backend.optimization.scalars.tabulate(name=name)

ixmp4/data/abstract/optimization/scalar.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,21 @@ class ScalarRepository(
3939
):
4040
docs: DocsRepository
4141

42-
def create(self, name: str, value: float, unit_name: str, run_id: int) -> Scalar:
42+
def create(self, run_id: int, name: str, value: float, unit_name: str) -> Scalar:
4343
"""Creates a Scalar.
4444
4545
Parameters
4646
----------
47+
run_id : int
48+
The id of the :class:`ixmp4.data.abstract.Run` for which this Scalar is
49+
defined.
4750
name : str
4851
The name of the Scalar.
4952
value : float
5053
The value of the Scalar.
5154
unit_name : str
5255
The name of the :class:`ixmp4.data.abstract.Unit` for which this Scalar is
5356
defined.
54-
run_id : int
55-
The id of the :class:`ixmp4.data.abstract.Run` for which this Scalar is
56-
defined.
5757
5858
Raises
5959
------

0 commit comments

Comments
 (0)