Skip to content

Commit

Permalink
fix(pydantic): replace enum values with regex validation
Browse files Browse the repository at this point in the history
This is shorter to write and means than when serialized to terminal/file workflow class instances.
  • Loading branch information
chriswmackey committed Jan 10, 2020
1 parent 5a91c17 commit b8870ff
Show file tree
Hide file tree
Showing 14 changed files with 391 additions and 395 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -10,7 +10,7 @@ jobs:
- pip install -r dev-requirements.txt
- pip install -r requirements.txt
script:
- python -m pytest tests/
- python -m pytest --cov=. tests/
after_success:
- coverage report
- coveralls
Expand Down
11 changes: 5 additions & 6 deletions honeybee_model_schema/bc.py
@@ -1,12 +1,11 @@
"""Boundary condition schemas."""
from pydantic import BaseModel, Field
from pydantic import BaseModel, Field, constr
from typing import List, Union
from enum import Enum


class Outdoors(BaseModel):

type: Enum('Outdoors', {'type': 'Outdoors'})
type: constr(regex='^Outdoors$') = 'Outdoors'

sun_exposure: bool = Field(
True,
Expand All @@ -28,7 +27,7 @@ class Outdoors(BaseModel):

class Surface(BaseModel):

type: Enum('Surface', {'type': 'Surface'})
type: constr(regex='^Surface$') = 'Surface'

boundary_condition_objects: List[str] = Field(
...,
Expand All @@ -47,9 +46,9 @@ class Surface(BaseModel):

class Ground(BaseModel):

type: Enum('Ground', {'type': 'Ground'})
type: constr(regex='^Ground$') = 'Ground'


class Adiabatic(BaseModel):

type: Enum('Adiabatic', {'type': 'Adiabatic'})
type: constr(regex='^Adiabatic$') = 'Adiabatic'
1 change: 0 additions & 1 deletion honeybee_model_schema/datetime.py
@@ -1,7 +1,6 @@
"""DateTime Schema"""
from pydantic import BaseModel, Field, validator
from typing import List, Union
from enum import Enum
from uuid import UUID, uuid4
import datetime

Expand Down
16 changes: 5 additions & 11 deletions honeybee_model_schema/energy/construction.py
@@ -1,7 +1,6 @@
"""Construction Schema"""
from pydantic import BaseModel, Field, constr
from typing import List, Union
from enum import Enum

from ._base import NamedEnergyBaseModel
from .material import EnergyMaterial, EnergyMaterialNoMass, \
Expand All @@ -13,8 +12,7 @@
class WindowConstructionAbridged(NamedEnergyBaseModel):
"""Construction for window objects (Aperture, Door)."""

type: Enum('WindowConstructionAbridged', {
'type': 'WindowConstructionAbridged'})
type: constr(regex='^WindowConstructionAbridged$') = 'WindowConstructionAbridged'

layers: List[constr(min_length=1, max_length=100)] = Field(
...,
Expand All @@ -28,8 +26,7 @@ class WindowConstructionAbridged(NamedEnergyBaseModel):
class WindowConstruction(WindowConstructionAbridged):
"""Construction for window objects (Aperture, Door)."""

type: Enum('WindowConstruction', {
'type': 'WindowConstruction'})
type: constr(regex='^WindowConstruction$') = 'WindowConstruction'

materials: List[
Union[
Expand All @@ -49,8 +46,7 @@ class WindowConstruction(WindowConstructionAbridged):
class OpaqueConstructionAbridged(NamedEnergyBaseModel):
"""Construction for opaque objects (Face, Shade, Door)."""

type: Enum('OpaqueConstructionAbridged', {
'type': 'OpaqueConstructionAbridged'})
type: constr(regex='^OpaqueConstructionAbridged$') = 'OpaqueConstructionAbridged'

layers: List[constr(min_length=1, max_length=100)] = Field(
...,
Expand All @@ -64,8 +60,7 @@ class OpaqueConstructionAbridged(NamedEnergyBaseModel):
class OpaqueConstruction(OpaqueConstructionAbridged):
"""Construction for opaque objects (Face, Shade, Door)."""

type: Enum('OpaqueConstruction', {
'type': 'OpaqueConstruction'})
type: constr(regex='^OpaqueConstruction$') = 'OpaqueConstruction'

materials: List[Union[EnergyMaterial, EnergyMaterialNoMass]] = Field(
...,
Expand All @@ -79,8 +74,7 @@ class OpaqueConstruction(OpaqueConstructionAbridged):
class ShadeConstruction(NamedEnergyBaseModel):
"""Construction for Shade objects."""

type: Enum('ShadeConstruction', {
'type': 'ShadeConstruction'})
type: constr(regex='^ShadeConstruction$') = 'ShadeConstruction'

solar_reflectance: float = Field(
0.2,
Expand Down
15 changes: 7 additions & 8 deletions honeybee_model_schema/energy/constructionset.py
@@ -1,14 +1,13 @@
"""ConstructionSet Schema"""
from pydantic import BaseModel, Field
from enum import Enum
from pydantic import BaseModel, Field, constr

from ._base import NamedEnergyBaseModel


class WallSetAbridged(BaseModel):
"""A set of constructions for wall assemblies."""

type: Enum('WallSetAbridged', {'type': 'WallSetAbridged'})
type: constr(regex='^WallSetAbridged$') = 'WallSetAbridged'

interior_construction: str = Field(
default=None,
Expand Down Expand Up @@ -38,7 +37,7 @@ class WallSetAbridged(BaseModel):
class FloorSetAbridged(BaseModel):
"""A set of constructions for floor assemblies."""

type: Enum('FloorSetAbridged', {'type': 'FloorSetAbridged'})
type: constr(regex='^FloorSetAbridged$') = 'FloorSetAbridged'

interior_construction: str = Field(
default=None,
Expand Down Expand Up @@ -68,7 +67,7 @@ class FloorSetAbridged(BaseModel):
class RoofCeilingSetAbridged(BaseModel):
"""A set of constructions for roof and ceiling assemblies."""

type: Enum('RoofCeilingSetAbridged', {'type': 'RoofCeilingSetAbridged'})
type: constr(regex='^RoofCeilingSetAbridged$') = 'RoofCeilingSetAbridged'

interior_construction: str = Field(
default=None,
Expand Down Expand Up @@ -98,7 +97,7 @@ class RoofCeilingSetAbridged(BaseModel):
class ApertureSetAbridged(BaseModel):
"""A set of constructions for aperture assemblies."""

type: Enum('ApertureSetAbridged', {'type': 'ApertureSetAbridged'})
type: constr(regex='^ApertureSetAbridged$') = 'ApertureSetAbridged'

interior_construction: str = Field(
default=None,
Expand Down Expand Up @@ -138,7 +137,7 @@ class ApertureSetAbridged(BaseModel):
class DoorSetAbridged(BaseModel):
"""A set of constructions for door assemblies."""

type: Enum('DoorSetAbridged', {'type': 'DoorSetAbridged'})
type: constr(regex='^DoorSetAbridged$') = 'DoorSetAbridged'

interior_construction: str = Field(
default=None,
Expand Down Expand Up @@ -184,7 +183,7 @@ class DoorSetAbridged(BaseModel):
class ConstructionSetAbridged(NamedEnergyBaseModel):
"""A set of constructions for different surface types and boundary conditions."""

type: Enum('ConstructionSetAbridged', {'type': 'ConstructionSetAbridged'})
type: constr(regex='^ConstructionSetAbridged$') = 'ConstructionSetAbridged'

wall_set: WallSetAbridged = Field(
default=None,
Expand Down
14 changes: 7 additions & 7 deletions honeybee_model_schema/energy/designday.py
@@ -1,5 +1,5 @@
"""Simulation Parameter Schema"""
from pydantic import BaseModel, Field
from pydantic import BaseModel, Field, constr
from typing import Union
from enum import Enum
from ..datetime import Date
Expand All @@ -10,7 +10,7 @@
class DryBulbCondition(BaseModel):
"""Used to specify dry bulb conditions on a design day."""

type: Enum('DryBulbCondition', {'type': 'DryBulbCondition'})
type: constr(regex='^DryBulbCondition$') = 'DryBulbCondition'

dry_bulb_max: float = Field(
...,
Expand All @@ -37,7 +37,7 @@ class HumidityTypes(str, Enum):
class HumidityCondition(BaseModel):
"""Used to specify humidity conditions on a design day."""

type: Enum('HumidityCondition', {'type': 'HumidityCondition'})
type: constr(regex='^HumidityCondition$') = 'HumidityCondition'

humidity_type: HumidityTypes

Expand Down Expand Up @@ -67,7 +67,7 @@ class HumidityCondition(BaseModel):
class WindCondition(BaseModel):
"""Used to specify wind conditions on a design day."""

type: Enum('WindCondition', {'type': 'WindCondition'})
type: constr(regex='^WindCondition$') = 'WindCondition'

wind_speed: float = Field(
...,
Expand All @@ -87,7 +87,7 @@ class WindCondition(BaseModel):
class ASHRAEClearSky(BaseModel):
"""Used to specify sky conditions on a design day."""

type: Enum('ASHRAEClearSky', {'type': 'ASHRAEClearSky'})
type: constr(regex='^ASHRAEClearSky$') = 'ASHRAEClearSky'

date: Date = Field(
...,
Expand All @@ -112,7 +112,7 @@ class ASHRAEClearSky(BaseModel):
class ASHRAETau(BaseModel):
"""Used to specify sky conditions on a design day."""

type: Enum('ASHRAETau', {'type': 'ASHRAETau'})
type: constr(regex='^ASHRAETau$') = 'ASHRAETau'

date: Date = Field(
...,
Expand Down Expand Up @@ -157,7 +157,7 @@ class DesignDayTypes(str, Enum):
class DesignDay(NamedEnergyBaseModel):
"""An object representing design day conditions."""

type: Enum('DesignDay', {'type': 'DesignDay'})
type: constr(regex='^DesignDay$') = 'DesignDay'

day_type: DesignDayTypes

Expand Down
5 changes: 3 additions & 2 deletions honeybee_model_schema/energy/hvac.py
@@ -1,5 +1,5 @@
"""Ideal Air Schema"""
from pydantic import BaseModel, Field, validator
from pydantic import BaseModel, Field, validator, constr
from typing import Union
from enum import Enum

Expand All @@ -11,7 +11,8 @@ class EconomizerType(str, Enum):

class IdealAirSystem(BaseModel):
""" Provides a model for an ideal HVAC system."""
type: Enum('IdealAirSystem', {'type': 'IdealAirSystem'})

type: constr(regex='^IdealAirSystem$') = 'IdealAirSystem'

heating_limit: Union[float, str] = Field(
'autosize',
Expand Down
17 changes: 8 additions & 9 deletions honeybee_model_schema/energy/load.py
@@ -1,14 +1,13 @@
"""Programtype Schema"""
from pydantic import BaseModel, Field, validator, root_validator
from pydantic import BaseModel, Field, validator, root_validator, constr
from typing import Union
from enum import Enum

from ._base import NamedEnergyBaseModel


class PeopleAbridged(NamedEnergyBaseModel):

type: Enum('PeopleAbridged', {'type': 'PeopleAbridged'})
type: constr(regex='^PeopleAbridged$') = 'PeopleAbridged'

people_per_area: float = Field(
...,
Expand Down Expand Up @@ -70,7 +69,7 @@ def check_sum_fractions(cls, values):

class LightingAbridged(NamedEnergyBaseModel):

type: Enum('LightingAbridged', {'type': 'LightingAbridged'})
type: constr(regex='^LightingAbridged$') = 'LightingAbridged'

watts_per_area: float = Field(
...,
Expand Down Expand Up @@ -178,17 +177,17 @@ def check_sum_fractions(cls, values):

class ElectricEquipmentAbridged(_EquipmentBase):

type: Enum('ElectricEquipmentAbridged', {'type': 'ElectricEquipmentAbridged'})
type: constr(regex='^ElectricEquipmentAbridged$') = 'ElectricEquipmentAbridged'


class GasEquipmentAbridged(_EquipmentBase):

type: Enum('GasEquipmentAbridged', {'type': 'GasEquipmentAbridged'})
type: constr(regex='^GasEquipmentAbridged$') = 'GasEquipmentAbridged'


class InfiltrationAbridged(NamedEnergyBaseModel):

type: Enum('InfiltrationAbridged', {'type': 'InfiltrationAbridged'})
type: constr(regex='^InfiltrationAbridged$') = 'InfiltrationAbridged'

flow_per_exterior_area: float = Field(
...,
Expand Down Expand Up @@ -224,7 +223,7 @@ class InfiltrationAbridged(NamedEnergyBaseModel):

class VentilationAbridged(NamedEnergyBaseModel):

type: Enum('VentilationAbridged', {'type': 'VentilationAbridged'})
type: constr(regex='^VentilationAbridged$') = 'VentilationAbridged'

flow_per_person: float = Field(
0,
Expand Down Expand Up @@ -268,7 +267,7 @@ class VentilationAbridged(NamedEnergyBaseModel):
class SetpointAbridged(NamedEnergyBaseModel):
"""Used to specify information about the setpoint schedule."""

type: Enum('SetpointAbridged', {'type': 'SetpointAbridged'})
type: constr(regex='^SetpointAbridged$') = 'SetpointAbridged'

cooling_schedule: str = Field(
...,
Expand Down

0 comments on commit b8870ff

Please sign in to comment.