Skip to content

Commit

Permalink
Some dirty hacks 🐷
Browse files Browse the repository at this point in the history
  • Loading branch information
cmutel committed Oct 9, 2024
1 parent 9012810 commit 96c7166
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
6 changes: 5 additions & 1 deletion sentier_data_tools/example/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class WaterElectrolysisModel(SentierModel):
provides = {
ProductIRI(
"http://openenergy-platform.org/ontology/oeo/OEO_00010379"
): "Hydrogen",
): "hydrogen",
}
needs = {
ModelTermIRI(
Expand All @@ -28,6 +28,10 @@ class WaterElectrolysisModel(SentierModel):
): "elec_energy_serv_dem",
}

def get_electrolysis_inventory(self) -> None:
...
# bom_electrolysis = self.get_model_data(self, product: VocabIRI, kind: DatasetKind) -> dict:

def run(self) -> tuple[list[Demand], list[Flow]]:
self.prepare()

Expand Down
3 changes: 3 additions & 0 deletions sentier_data_tools/iri/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ def triples(self, subject: bool = True, limit: Optional[int] = 25) -> List[tuple
for line in results
]

def __repr__(self) -> str:
return self.display()

def display(self) -> str:
return display_value_for_uri(str(self), self.kind, self.graph_url)

Expand Down
3 changes: 2 additions & 1 deletion sentier_data_tools/model/arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
from pydantic import BaseModel, ConfigDict

# from sentier_data_tools.data_source_base import DataSourceBase
from sentier_data_tools.iri import FlowIRI, GeonamesIRI, ProductIRI
from sentier_data_tools.iri import FlowIRI, GeonamesIRI, ProductIRI, UnitIRI

# from sentier_data_tools.local_storage import DefaultDataSource


class Demand(BaseModel):
product_iri: ProductIRI
unit_iri: UnitIRI
amount: float
spatial_context: GeonamesIRI = GeonamesIRI("https://sws.geonames.org/6295630/")
properties: Optional[list] = None
Expand Down
24 changes: 18 additions & 6 deletions sentier_data_tools/model/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from sentier_data_tools.local_storage.db import Dataset
from sentier_data_tools.local_storage.fields import DatasetKind
from sentier_data_tools.model.arguments import Demand, Flow, RunConfig
from sentier_data_tools.logs import stdout_feedback_logger as logger


class SentierModel(ABC):
Expand All @@ -18,8 +19,13 @@ def __init__(self, demand: Demand, run_config: RunConfig):
if self.demand.end_date is None:
self.demand.end_date = date(date.today().year + 4, 1, 1)
self.validate_needs_provides()
self.inject_needs_provides_into_class()

def validate_needs_provides(self):
if not isinstance(self.needs, dict):
raise ValueError(f"`needs` must be a `dict`; got {type(self.needs)}")
if not isinstance(self.provides, dict):
raise ValueError(f"`provides` must be a `dict`; got {type(self.provides)}")
for elem in self.needs:
if not isinstance(elem, VocabIRI):
raise ValueError(
Expand All @@ -30,15 +36,21 @@ def validate_needs_provides(self):
raise ValueError(
f"Every term in `provides` must be an instance of `VocabIRI`; got {type(elem)}"
)
if isinstance(self.needs, dict) and len(set(self.needs.values())) != len(
self.needs
):
if len(set(self.needs.values())) != len(self.needs):
raise ValueError("Duplicates alias labels in `needs`")
if isinstance(self.provides, dict) and len(set(self.provides.values())) != len(
self.provides
):
if len(set(self.provides.values())) != len(self.provides):
raise ValueError("Duplicates alias labels in `provides`")

def inject_needs_provides_into_class(self) -> None:
for key, value in self.needs.items():
if hasattr(self, value):
if hasattr(self, f"var_{value}"):
raise ValueError(f"Alias `{value}` conflicts with existing attribute")
logger.info(f"Changing alias `{value}` to `var_{value}`")
setattr(self, f"var_{value}", key)
else:
setattr(self, value, key)

# def prepare(self) -> None:
# self.get_model_data()
# self.data_validity_checks()
Expand Down

0 comments on commit 96c7166

Please sign in to comment.