Skip to content

Commit

Permalink
feat(pydantic): Upgrade to pydantic 1.3
Browse files Browse the repository at this point in the history
This commit upgrades this repo to use pydantic 1.3. This also includes the switching over of several validators to be root_validators and adds a few root_validators in places where they really should exist.
  • Loading branch information
chriswmackey authored and Chris Mackey committed Jan 9, 2020
1 parent 20e6be9 commit 5a91c17
Show file tree
Hide file tree
Showing 19 changed files with 423 additions and 410 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import honeybee_model_schema
```

## API Documentation

[Model Schema](https://ladybug-tools-in2.github.io/honeybee-model-schema/model.html)

[Energy Simulation Parameter Schema](https://ladybug-tools-in2.github.io/honeybee-model-schema/simulation-parameter.html)

## Local Development
Expand Down
6 changes: 3 additions & 3 deletions honeybee_model_schema/_base.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"""Base class for all objects requiring a valid names for all engines."""
from pydantic import BaseModel, Schema, validator
from pydantic import BaseModel, Field


class NamedBaseModel(BaseModel):
"""Base class for all objects requiring a valid names for all engines."""

name: str = Schema(
name: str = Field(
...,
regex=r'[A-Za-z0-9_-]',
min_length=1,
Expand All @@ -15,7 +15,7 @@ class NamedBaseModel(BaseModel):
'It cannot be longer than 100 characters.'
)

display_name: str = Schema(
display_name: str = Field(
default=None,
description='Display name of the object with no restrictions.'
)
14 changes: 7 additions & 7 deletions honeybee_model_schema/bc.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Boundary condition schemas."""
from pydantic import BaseModel, Schema
from pydantic import BaseModel, Field
from typing import List, Union
from enum import Enum

Expand All @@ -8,17 +8,17 @@ class Outdoors(BaseModel):

type: Enum('Outdoors', {'type': 'Outdoors'})

sun_exposure: bool = Schema(
sun_exposure: bool = Field(
True,
description='A boolean noting whether the boundary is exposed to sun.'
)

wind_exposure: bool = Schema(
wind_exposure: bool = Field(
True,
description='A boolean noting whether the boundary is exposed to wind.'
)

view_factor: Union[str, float] = Schema(
view_factor: Union[str, float] = Field(
'autocalculate',
ge=0,
le=1,
Expand All @@ -30,10 +30,10 @@ class Surface(BaseModel):

type: Enum('Surface', {'type': 'Surface'})

boundary_condition_objects: List[str] = Schema(
boundary_condition_objects: List[str] = Field(
...,
minItems=2,
maxItems=3,
min_tems=2,
max_items=3,
description='A list of up to 3 object names that are adjacent to this one. '
'The first object is always the one that is immediately adjacet and is of '
'the same object type (Face, Aperture, Door). When this boundary condition '
Expand Down
12 changes: 6 additions & 6 deletions honeybee_model_schema/datetime.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""DateTime Schema"""
from pydantic import BaseModel, Schema, validator, ValidationError, UrlStr, ConstrainedStr
from pydantic import BaseModel, Field, validator
from typing import List, Union
from enum import Enum
from uuid import UUID, uuid4
Expand All @@ -9,21 +9,21 @@
class Date(BaseModel):
"""Date."""

month: int = Schema(
month: int = Field(
1,
ge=1,
le=12,
description='An integer for the month between [1-12]. Default is 1.'
)

day: int = Schema(
day: int = Field(
1,
ge=1,
le=31,
description='An integer for day of the month [1-31]. Default is 1.'
)

is_leap_year: bool = Schema(
is_leap_year: bool = Field(
False,
description='Optional boolean to note whether the date is for a leap year.'
)
Expand All @@ -48,14 +48,14 @@ def check_date(cls, v, values):
class Time(BaseModel):
"""Time."""

hour: int = Schema(
hour: int = Field(
0,
ge=0,
le=23,
description='An integer for the hour between [0-23]. Default is 0.'
)

minute: int = Schema(
minute: int = Field(
0,
ge=0,
le=59,
Expand Down
6 changes: 2 additions & 4 deletions honeybee_model_schema/energy/_base.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"""Base class for all objects requiring a valid EnergyPlus name."""
from pydantic import BaseModel, Schema, validator
from pydantic import BaseModel, Field, validator


class NamedEnergyBaseModel(BaseModel):
"""Base class for all objects requiring a valid EnergyPlus name."""

name: str = Schema(
name: str = Field(
...,
min_length=1,
max_length=100,
Expand All @@ -18,6 +18,4 @@ def check_name(cls, v):
assert all(ord(i) < 128 for i in v), 'Name contains non ASCII characters.'
assert all(char not in v for char in (',', ';', '!', '\n', '\t')), \
'Name contains invalid character for EnergyPlus (, ; ! \\n \\t).'
assert len(v) > 0, 'Name is an empty string.'
assert len(v) <= 100, 'Number of characters must be less than 100.'
return v
61 changes: 14 additions & 47 deletions honeybee_model_schema/energy/construction.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Construction Schema"""
from pydantic import BaseModel, Schema, validator, ValidationError, constr
from pydantic import BaseModel, Field, constr
from typing import List, Union
from enum import Enum

Expand All @@ -16,29 +16,14 @@ class WindowConstructionAbridged(NamedEnergyBaseModel):
type: Enum('WindowConstructionAbridged', {
'type': 'WindowConstructionAbridged'})

layers: List[constr(min_length=1, max_length=100)] = Schema(
layers: List[constr(min_length=1, max_length=100)] = Field(
...,
description='List of strings for material names. The order of the materials '
'is from exterior to interior.',
minItems=1,
maxItems=8
min_items=1,
max_items=8
)

@validator('layers', whole=True)
def check_num_items(cls, layers):
"Ensure length of material is at least 1 and not more than 8."
if len(layers) == 0:
raise ValidationError(
'Window construction should at least have one material.'
)

elif len(layers) > 8:
raise ValidationError(
'Window construction cannot have more than 8 layers.'
)
else:
return layers


class WindowConstruction(WindowConstructionAbridged):
"""Construction for window objects (Aperture, Door)."""
Expand All @@ -52,12 +37,12 @@ class WindowConstruction(WindowConstructionAbridged):
EnergyWindowMaterialSimpleGlazSys, EnergyWindowMaterialBlind,
EnergyWindowMaterialGlazing, EnergyWindowMaterialShade
]
] = Schema(
] = Field(
...,
description='List of materials. The order of the materials is from outside '
'to inside.',
minItems=1,
maxItems=8
min_items=1,
max_items=8
)


Expand All @@ -67,45 +52,27 @@ class OpaqueConstructionAbridged(NamedEnergyBaseModel):
type: Enum('OpaqueConstructionAbridged', {
'type': 'OpaqueConstructionAbridged'})

layers: List[constr(min_length=1, max_length=100)] = Schema(
layers: List[constr(min_length=1, max_length=100)] = Field(
...,
description='List of strings for material names. The order of the materials '
'is from exterior to interior.',
min_items=1,
max_items=10
)

@validator('layers', whole=True)
def check_num_items(cls, layers):
"Ensure length of material is at least 1 and not more than 10."
if len(layers) == 0:
raise ValidationError(
'Opaque construction should at least have one material.'
)
elif len(layers) > 10:
raise ValidationError(
'Opaque construction cannot have more than 10 layers.'
)
else:
return layers


class OpaqueConstruction(OpaqueConstructionAbridged):
"""Construction for opaque objects (Face, Shade, Door)."""

type: Enum('OpaqueConstruction', {
'type': 'OpaqueConstruction'})

materials: List[
Union[
EnergyMaterial, EnergyMaterialNoMass
]
] = Schema(
materials: List[Union[EnergyMaterial, EnergyMaterialNoMass]] = Field(
...,
description='List of materials. The order of the materials is from outside to'
' inside.',
minItems=1,
maxItems=10
min_items=1,
max_items=10
)


Expand All @@ -115,21 +82,21 @@ class ShadeConstruction(NamedEnergyBaseModel):
type: Enum('ShadeConstruction', {
'type': 'ShadeConstruction'})

solar_reflectance: float = Schema(
solar_reflectance: float = Field(
0.2,
ge=0,
le=1,
description=' A number for the solar reflectance of the construction.'
)

visible_reflectance: float = Schema(
visible_reflectance: float = Field(
0.2,
ge=0,
le=1,
description=' A number for the visible reflectance of the construction.'
)

is_specular: bool = Schema(
is_specular: bool = Field(
default=False,
description='Boolean to note whether the reflection off the shade is diffuse '
'(False) or specular (True). Set to True if the construction is '
Expand Down
Loading

0 comments on commit 5a91c17

Please sign in to comment.