Skip to content

Commit

Permalink
adding for pydantic 2 changes
Browse files Browse the repository at this point in the history
  • Loading branch information
tareknrel committed Dec 9, 2023
1 parent 673a41e commit 45176a7
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 52 deletions.
54 changes: 23 additions & 31 deletions ditto/models/base.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function
from builtins import super, range, zip, round, map
from pydantic import BaseModel, Field, ValidationError
from pydantic.json import isoformat, timedelta_isoformat
from pydantic import BaseModel, Field, ValidationError, ConfigDict
from typing_extensions import Annotated
from typing import Optional, List

import warnings
import logging

from .units import Voltage, VoltageUnit, Phase
from .position import Position
from .units import Voltage, Phase

logger = logging.getLogger(__name__)

Expand All @@ -18,37 +18,29 @@ class DiTToBaseModel(BaseModel):
A name is required for all DiTTo models.
"""

class Config:
""" Base pydantic configuration for all DiTTo models.
"""
title = "DiTToBaseModel"
validate_assignment = True
validate_all = True
extra = "forbid"
use_enum_values = True
arbitrary_types_allowed = True
allow_population_by_field_name = True

name: str = Field(
model_config = ConfigDict(
validate_assignment = True,
validate_default = True,
use_enum_values = False,
arbitrary_types_allowed = True,
populate_by_name = True
)

name: Annotated[str,Field(
description="Name of the element in the DiTTo model",
title="name",
cim_value="name"
)
json_schema_extra = {"cim_value":"name"}
)]

substation_name: Optional[str] = Field(
substation_name: Annotated[Optional[str], Field(
description="Name of the substation the element is under",
title="substation_name"
cim_value="EquipmentContainer.Substation.name"
)
title="substation_name",
json_schema_extra = {"cim_value":"EquipmentContainer.Substation.name"}
)]

feeder_name: Optional[str] = Field(
feeder_name: Annotated[Optional[str],Field(
description="Name of the feeder the element is on",
title="feeder_name"
cim_value="EquipmentContainer.name"
)
title="feeder_name",
json_schema_extra = {"cim_value":"EquipmentContainer.name"}
)]

positions: Optional[List[Position]] = Field(
description="A list of positions of the element. For single point elements, this list should have a length of one. For lines, this list may contain intermediate points.",
title="positions"
cim_value="Location.PositionPoints"
)
33 changes: 18 additions & 15 deletions ditto/models/node.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,38 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function
from builtins import super, range, zip, round, map
from pydantic import BaseModel, Field

from .base import DiTToBaseModel
from .units import Voltage, VoltageUnit, Phase
from .units import Voltage, Phase, Distance
from .position import Position
from typing_extensions import Annotated
from typing import Optional, List


class Node(DiTToHasTraits):
class Node(DiTToBaseModel):

nominal_voltage: Optional[Voltage] = Field(
nominal_voltage: Annotated[Optional[Voltage], Field(
description = "The nominal voltage at the node",
title = "Nominal voltage",
cim_value="nomU"
)
json_schema_extra = {"cim_value":"nomU"}
)]

phases: Optional[List[Phase]] = Field(
phases: Annotated[Optional[List[Phase]], Field(
description="Phases at the node",
title="phases"
cim_value="phases"
)
title="phases",
json_schema_extra = {"cim_value":"phases"}
)]

is_substation_connection: Optional[bool] = Field(
is_substation_connection: Annotated[Optional[bool], Field(
description="1 if the node connects from inside a substation to outside, 0 otherwise. These indicate if a node connects a substation to a feeder or high voltage source",
title="is_substation_connection",
default=False,
cim_value="NA"
)
json_schema_extra = {"cim_value":"NA"}
)]

setpoint: Optional[Voltage] = Field(
setpoint: Annotated[Optional[Voltage], Field(
description="Value that the node must be set to. This is typically used for feeder head points",
title="setpoint",
cim_value="NA"
)
json_schema_extra={"cim_value": "NA"},
)]
32 changes: 26 additions & 6 deletions ditto/models/position.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,33 @@
from __future__ import absolute_import, division, print_function
from builtins import super, range, zip, round, map
from pydantic import BaseModel, Field
from .base import DiTToBaseModel
from .units import Voltage, Phase, Distance
import pint
import logging
from typing_extensions import Annotated
from typing import Optional, List

from .base import DiTToHasTraits, Float, Unicode, Any, Int, List, observe, Instance
class Position(DiTToBaseModel):
x_position: Annotated[float, Field(
description="X coordinate with raw float. Projection is defined in the json_schema_extra. Default is longitude value",
title="x_position",
alias="long",
json_schema_extra = {"cim_value":"Location.PositionPoint.xPosition", "projection": "longitude"}
)]
y_position: Annotated[float, Field(
description="Y coordinate with raw float. Projection is defined in the json_schema_extra. Default is latitude value",
title="y_position",
alias="lat",
json_schema_extra = {"cim_value":"Location.PositionPoint.yPosition", "projection": "latitude"}
)]
z_position: Annotated[Distance, Field(
description="Z coordinate in distance units. Default is meters",
title="z_position",
alias="elevation",
json_schema_extra = {"cim_value":"Location.PositionPoint.zPosition"}
)]


class Position(DiTToHasTraits):
long = Float(help="""Decimal Longitude""")
lat = Float(help="""Decimal Latitude""")
elevation = Float(help="""Decimal elevation (meters)""")

def build(self, model):
self._model = model

0 comments on commit 45176a7

Please sign in to comment.