Skip to content

Commit

Permalink
chore: remove pydantic from common
Browse files Browse the repository at this point in the history
  • Loading branch information
frodehk committed Dec 17, 2024
1 parent 5371ecd commit 8937692
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
from abc import ABC, abstractmethod
from typing import Optional, Union

from pydantic_core.core_schema import ValidationInfo

from libecalc.application.energy.emitter import Emitter
from libecalc.application.energy.energy_component import EnergyComponent
from libecalc.common.component_type import ComponentType
Expand Down Expand Up @@ -60,7 +58,7 @@ def __init__(
max_usage_from_shore: Optional[Expression] = None,
):
super().__init__(name, regularity)
self.user_defined_category = self.check_user_defined_category(user_defined_category)
self.user_defined_category = self.check_user_defined_category(user_defined_category, name)
self.energy_usage_model = energy_usage_model
self.component_type = component_type
self.fuel = fuel
Expand All @@ -74,20 +72,15 @@ def id(self) -> str:
return generate_id(self.name)

@classmethod
def check_user_defined_category(cls, user_defined_category, info: ValidationInfo = None):
def check_user_defined_category(cls, user_defined_category, name: str):
"""Provide which value and context to make it easier for user to correct wrt mandatory changes."""
if isinstance(user_defined_category, dict) and len(user_defined_category.values()) > 0:
user_defined_category = _convert_keys_in_dictionary_from_str_to_periods(user_defined_category)
for user_category in user_defined_category.values():
if user_category not in list(ConsumerUserDefinedCategoryType):
name_context_str = ""
if (name := info.data.get("name")) is not None:
name_context_str = f"with the name {name}"

raise ValueError(
f"CATEGORY: {user_category} is not allowed for {cls.__name__} {name_context_str}. Valid categories are: {[(consumer_user_defined_category.value) for consumer_user_defined_category in ConsumerUserDefinedCategoryType]}"
f"CATEGORY: {user_category} is not allowed for {cls.__name__} with name {name}. Valid categories are: {[(consumer_user_defined_category.value) for consumer_user_defined_category in ConsumerUserDefinedCategoryType]}"
)

return user_defined_category


Expand Down
63 changes: 35 additions & 28 deletions src/libecalc/domain/infrastructure/energy_components/common.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
from typing import Annotated, Literal, Optional, TypeVar, Union

from pydantic import ConfigDict, Field
from typing import Literal, Optional, TypeVar, Union

from libecalc.common.component_type import ComponentType
from libecalc.domain.infrastructure.energy_components.asset.asset import Asset
Expand All @@ -14,10 +12,9 @@
from libecalc.domain.infrastructure.energy_components.generator_set.generator_set_dto import GeneratorSet
from libecalc.domain.infrastructure.energy_components.installation.installation import Installation
from libecalc.domain.infrastructure.energy_components.pump.component_dto import PumpComponent
from libecalc.dto.base import EcalcBaseModel
from libecalc.expression import Expression

Consumer = Annotated[Union[FuelConsumer, ElectricityConsumer], Field(discriminator="consumes")]
Consumer = Union[FuelConsumer, ElectricityConsumer]

ComponentDTO = Union[
Asset,
Expand All @@ -31,36 +28,46 @@
]


class CompressorOperationalSettings(EcalcBaseModel):
rate: Expression
inlet_pressure: Expression
outlet_pressure: Expression

class CompressorOperationalSettings:
def __init__(self, rate: Expression, inlet_pressure: Expression, outlet_pressure: Expression):
self.rate = rate
self.inlet_pressure = inlet_pressure
self.outlet_pressure = outlet_pressure

class PumpOperationalSettings(EcalcBaseModel):
rate: Expression
inlet_pressure: Expression
outlet_pressure: Expression
fluid_density: Expression

class PumpOperationalSettings:
def __init__(
self, rate: Expression, inlet_pressure: Expression, outlet_pressure: Expression, fluid_density: Expression
):
self.rate = rate
self.inlet_pressure = inlet_pressure
self.outlet_pressure = outlet_pressure
self.fluid_density = fluid_density

class Stream(EcalcBaseModel):
model_config = ConfigDict(populate_by_name=True)

stream_name: Optional[str] = Field(None)
from_component_id: str
to_component_id: str
class Stream:
def __init__(self, from_component_id: str, to_component_id: str, stream_name: Optional[str] = None):
self.stream_name = stream_name
self.from_component_id = from_component_id
self.to_component_id = to_component_id


ConsumerComponent = TypeVar("ConsumerComponent", bound=Union[CompressorComponent, PumpComponent])


class TrainComponent(BaseConsumer):
component_type: Literal[ComponentType.TRAIN_V2] = Field(
ComponentType.TRAIN_V2,
title="TYPE",
description="The type of the component",
alias="TYPE",
)
stages: list[ConsumerComponent]
streams: list[Stream]
component_type: Literal[ComponentType.TRAIN_V2] = ComponentType.TRAIN_V2

def __init__(
self,
name: str,
regularity: dict,
consumes,
user_defined_category: dict,
component_type: ComponentType,
stages: list,
streams: list,
):
super().__init__(name, regularity, consumes, user_defined_category, component_type)
self.stages = stages
self.streams = streams
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from typing import Optional, Union

from pydantic_core.core_schema import ValidationInfo

from libecalc.application.energy.energy_component import EnergyComponent
from libecalc.common.component_type import ComponentType
from libecalc.common.string.string_utils import generate_id
Expand Down Expand Up @@ -75,16 +73,12 @@ def convert_expression_installation(self, data):
# Implement the conversion logic here
return convert_expression(data)

def check_user_defined_category(cls, user_defined_category, info: ValidationInfo = None):
def check_user_defined_category(self, user_defined_category):
# Provide which value and context to make it easier for user to correct wrt mandatory changes.
if user_defined_category is not None:
if user_defined_category not in list(InstallationUserDefinedCategoryType):
name_context_str = ""
if (name := info.data.get("name")) is not None:
name_context_str = f"with the name {name}"

raise ValueError(
f"CATEGORY: {user_defined_category} is not allowed for {cls.__name__} {name_context_str}. Valid categories are: {[str(installation_user_defined_category.value) for installation_user_defined_category in InstallationUserDefinedCategoryType]}"
f"CATEGORY: {user_defined_category} is not allowed for Installation with name {self.name}. Valid categories are: {[str(installation_user_defined_category.value) for installation_user_defined_category in InstallationUserDefinedCategoryType]}"
)

return user_defined_category
Expand Down

0 comments on commit 8937692

Please sign in to comment.