Skip to content

Commit

Permalink
Merge pull request #191 from Deltares/feat/GEOLIB-220-remove-pydantic…
Browse files Browse the repository at this point in the history
…-v2-checks

feat/GEOLIB-220 remove pydantic v2 checks
  • Loading branch information
wfaustmann authored Aug 12, 2024
2 parents 607caf3 + c719158 commit 6bd75fc
Show file tree
Hide file tree
Showing 65 changed files with 1,242 additions and 3,106 deletions.
3 changes: 0 additions & 3 deletions geolib/_compat.py

This file was deleted.

27 changes: 7 additions & 20 deletions geolib/models/base_data_class.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
from pydantic import BaseModel

from geolib._compat import IS_PYDANTIC_V2

if IS_PYDANTIC_V2:
from pydantic import ConfigDict
from pydantic import BaseModel, ConfigDict

from .meta import MetaData

Expand All @@ -13,17 +8,9 @@
class BaseDataClass(BaseModel):
"""Base class for *all* pydantic classes in GEOLib."""

if IS_PYDANTIC_V2:
model_config = ConfigDict(
validate_assignment=True,
arbitrary_types_allowed=True,
validate_default=True,
extra=settings.extra_fields,
)
else:

class Config:
validate_assignment = True
arbitrary_types_allowed = True
validate_all = True
extra = settings.extra_fields
model_config = ConfigDict(
validate_assignment=True,
arbitrary_types_allowed=True,
validate_default=True,
extra=settings.extra_fields,
)
13 changes: 2 additions & 11 deletions geolib/models/base_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,7 @@
from typing import List, Optional, Type, Union

import requests
from pydantic import DirectoryPath, FilePath, HttpUrl, ValidationError, conlist

from geolib._compat import IS_PYDANTIC_V2

if IS_PYDANTIC_V2:
from pydantic import SerializeAsAny

from pydantic import DirectoryPath, FilePath, HttpUrl, SerializeAsAny, ValidationError
from requests.auth import HTTPBasicAuth

from geolib.errors import CalculationError
Expand All @@ -32,10 +26,7 @@

class BaseModel(BaseDataClass, abc.ABC):
filename: Optional[Path] = None
if IS_PYDANTIC_V2:
datastructure: Optional[SerializeAsAny[BaseModelStructure]] = None
else:
datastructure: Optional[BaseModelStructure] = None
datastructure: Optional[SerializeAsAny[BaseModelStructure]] = None
"""
This is the base class for all models in GEOLib.
Expand Down
103 changes: 26 additions & 77 deletions geolib/models/dfoundations/dfoundations_model.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
import logging
from enum import Enum
from pathlib import Path
from subprocess import CompletedProcess, run
from typing import BinaryIO, List, Optional, Type, Union

from pydantic import FilePath
from pydantic import Field, FilePath
from typing_extensions import Annotated

from geolib._compat import IS_PYDANTIC_V2

if IS_PYDANTIC_V2:
from pydantic import Field
from typing_extensions import Annotated
else:
from pydantic import confloat, conint

from geolib.geometry import Point
from geolib.models import BaseDataClass, BaseModel, BaseModelStructure
from geolib.models import BaseDataClass, BaseModel
from geolib.models.internal import Bool
from geolib.models.meta import CONSOLE_RUN_BATCH_FLAG
from geolib.soils import Soil
Expand Down Expand Up @@ -55,30 +45,14 @@ class ModelOptions(BaseDataClass):
is_rigid: Bool = True

# Transformation
if IS_PYDANTIC_V2:
max_allowed_settlement_lim_state_str: Annotated[float, Field(ge=0, le=100000)] = 0
max_allowed_rel_rotation_lim_state_str: Annotated[
int, Field(ge=1, le=10000)
] = 100
max_allowed_settlement_lim_state_serv: Annotated[
float, Field(ge=0, le=100000)
] = 0
max_allowed_rel_rotation_lim_state_serv: Annotated[
int, Field(ge=1, le=10000)
] = 300
# Factors
factor_xi3: Optional[Annotated[float, Field(ge=0.01, le=10)]] = None
factor_xi4: Optional[Annotated[float, Field(ge=0.01, le=10)]] = None
ea_gem: Optional[Annotated[float, Field(ge=1)]] = None
else:
max_allowed_settlement_lim_state_str: confloat(ge=0, le=100000) = 0
max_allowed_rel_rotation_lim_state_str: conint(ge=1, le=10000) = 100
max_allowed_settlement_lim_state_serv: confloat(ge=0, le=100000) = 0
max_allowed_rel_rotation_lim_state_serv: conint(ge=1, le=10000) = 300
# Factors
factor_xi3: Optional[confloat(ge=0.01, le=10)] = None
factor_xi4: Optional[confloat(ge=0.01, le=10)] = None
ea_gem: Optional[confloat(ge=1)] = None
max_allowed_settlement_lim_state_str: Annotated[float, Field(ge=0, le=100000)] = 0
max_allowed_rel_rotation_lim_state_str: Annotated[int, Field(ge=1, le=10000)] = 100
max_allowed_settlement_lim_state_serv: Annotated[float, Field(ge=0, le=100000)] = 0
max_allowed_rel_rotation_lim_state_serv: Annotated[int, Field(ge=1, le=10000)] = 300
# Factors
factor_xi3: Optional[Annotated[float, Field(ge=0.01, le=10)]] = None
factor_xi4: Optional[Annotated[float, Field(ge=0.01, le=10)]] = None
ea_gem: Optional[Annotated[float, Field(ge=1)]] = None

# Combined Model Options
is_suppress_qc_reduction: Bool = False
Expand All @@ -90,52 +64,33 @@ class ModelOptions(BaseDataClass):
use_extra_almere_rules: Bool = False

def _to_internal(self):
if IS_PYDANTIC_V2:
return InternalModelOptions(**self.model_dump())
else:
return InternalModelOptions(**self.dict())
return InternalModelOptions(**self.model_dump())

@classmethod
def model_type(cls):
raise NotImplementedError("Implement in concrete classes.")


class BearingPilesModel(ModelOptions):
if IS_PYDANTIC_V2:
factor_gamma_b: Optional[Annotated[float, Field(ge=1, le=100)]] = None
factor_gamma_s: Optional[Annotated[float, Field(ge=1, le=100)]] = None
factor_gamma_fnk: Optional[Annotated[float, Field(ge=-100, le=100)]] = None
area: Optional[Annotated[float, Field(ge=0, le=100000)]] = None
else:
factor_gamma_b: Optional[confloat(ge=1, le=100)] = None
factor_gamma_s: Optional[confloat(ge=1, le=100)] = None
factor_gamma_fnk: Optional[confloat(ge=-100, le=100)] = None
area: Optional[confloat(ge=0, le=100000)] = None
factor_gamma_b: Optional[Annotated[float, Field(ge=1, le=100)]] = None
factor_gamma_s: Optional[Annotated[float, Field(ge=1, le=100)]] = None
factor_gamma_fnk: Optional[Annotated[float, Field(ge=-100, le=100)]] = None
area: Optional[Annotated[float, Field(ge=0, le=100000)]] = None

@classmethod
def model_type(cls):
return ModelTypeEnum.BEARING_PILES


class TensionPilesModel(ModelOptions):
if IS_PYDANTIC_V2:
unit_weight_water: Annotated[float, Field(ge=0.01, le=20)] = 9.81
use_compaction: Bool = False
surcharge: Annotated[float, Field(ge=0, le=1e7)] = 0
use_piezometric_levels: Bool = True

factor_gamma_var: Optional[Annotated[float, Field(ge=0.01, le=100)]] = None
factor_gamma_st: Optional[Annotated[float, Field(ge=0.01, le=100)]] = None
factor_gamma_gamma: Optional[Annotated[float, Field(ge=0.01, le=100)]] = None
else:
unit_weight_water: confloat(ge=0.01, le=20) = 9.81
use_compaction: Bool = False
surcharge: confloat(ge=0, le=1e7) = 0
use_piezometric_levels: Bool = True

factor_gamma_var: Optional[confloat(ge=0.01, le=100)] = None
factor_gamma_st: Optional[confloat(ge=0.01, le=100)] = None
factor_gamma_gamma: Optional[confloat(ge=0.01, le=100)] = None
unit_weight_water: Annotated[float, Field(ge=0.01, le=20)] = 9.81
use_compaction: Bool = False
surcharge: Annotated[float, Field(ge=0, le=1e7)] = 0
use_piezometric_levels: Bool = True

factor_gamma_var: Optional[Annotated[float, Field(ge=0.01, le=100)]] = None
factor_gamma_st: Optional[Annotated[float, Field(ge=0.01, le=100)]] = None
factor_gamma_gamma: Optional[Annotated[float, Field(ge=0.01, le=100)]] = None

@classmethod
def model_type(cls):
Expand Down Expand Up @@ -170,10 +125,7 @@ class CalculationOptions(BaseDataClass):
trajectory_interval: float = 0.50

def _to_internal(self):
if IS_PYDANTIC_V2:
fields = self.model_dump(exclude=["calculationtype"])
else:
fields = self.dict(exclude={"calculationtype"})
fields = self.model_dump(exclude=["calculationtype"])
return PreliminaryDesign(**fields)


Expand Down Expand Up @@ -214,10 +166,7 @@ def input(self):
return self.datastructure.input_data

def serialize(self, filename: Union[FilePath, BinaryIO]):
if IS_PYDANTIC_V2:
serializer = DFoundationsInputSerializer(ds=self.datastructure.model_dump())
else:
serializer = DFoundationsInputSerializer(ds=self.datastructure.dict())
serializer = DFoundationsInputSerializer(ds=self.datastructure.model_dump())
serializer.write(filename)

if isinstance(filename, Path):
Expand Down
44 changes: 15 additions & 29 deletions geolib/models/dfoundations/dfoundations_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import re
from typing import Any, Dict, List, Tuple

from geolib._compat import IS_PYDANTIC_V2
from geolib.models.dseries_parser import (
DSerieListGroupNextStructure,
DSerieParser,
Expand Down Expand Up @@ -76,36 +75,26 @@ def parse_text(cls, text):
Returns:
DSerieStructure: Parsed structure.
"""
if IS_PYDANTIC_V2:

def split_line(text: str) -> List[str]:
parts = []
for part in re.split(" |\t", text.strip()):
if part == "":
continue
if part.isdigit():
parts.append(int(part))
else:
try:
parts.append(float(part))
except ValueError:
parts.append(part)
return parts

else:

def split_line(text: str) -> List[str]:
parts = re.split(" |\t", text.strip())
return list(filter(lambda part: part != "", parts))
def split_line(text: str) -> List[str]:
parts = []
for part in re.split(" |\t", text.strip()):
if part == "":
continue
if part.isdigit():
parts.append(int(part))
else:
try:
parts.append(float(part))
except ValueError:
parts.append(part)
return parts

table_text = list(DSerieParser.parse_list_group(text).values())[0]
table_data = list(DSerieParser.parse_list_group(table_text).values())
if len(table_data) == 0:
values_dict_list = table_data
if IS_PYDANTIC_V2:
collection_property_name = list(cls.model_fields.items())[0][0]
else:
collection_property_name = list(cls.__fields__.items())[0][0]
collection_property_name = list(cls.model_fields.items())[0][0]
return cls(**{collection_property_name: values_dict_list})
else:
# Expected two groups (column_indication and data)
Expand All @@ -114,10 +103,7 @@ def split_line(text: str) -> List[str]:
dict(zip(keys, values))
for values in map(split_line, table_data[1].split("\n"))
]
if IS_PYDANTIC_V2:
collection_property_name = list(cls.model_fields.items())[0][0]
else:
collection_property_name = list(cls.__fields__.items())[0][0]
collection_property_name = list(cls.model_fields.items())[0][0]

return cls(**{collection_property_name: values_dict_list})

Expand Down
Loading

0 comments on commit 6bd75fc

Please sign in to comment.