Skip to content

Commit

Permalink
feat(conlist): Implement more elegant way to constrain list length
Browse files Browse the repository at this point in the history
I had been using validators to check that the length of embedded lists were correct but I just realized that conlist can do the same thing but better.
  • Loading branch information
chriswmackey committed Jan 22, 2020
1 parent 0ddc72d commit 7c76616
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 30 deletions.
11 changes: 2 additions & 9 deletions honeybee_schema/energy/schedule.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Schedule Type Limit Schema"""
from pydantic import BaseModel, Field, validator, root_validator, constr
from pydantic import BaseModel, Field, validator, root_validator, constr, conlist
from typing import List
from enum import Enum
import datetime
Expand Down Expand Up @@ -61,7 +61,7 @@ class ScheduleDay(NamedEnergyBaseModel):
'The length of this list must match the length of the times list.'
)

times: List[List[float]] = Field(
times: List[conlist(float, min_items=2, max_items=2)] = Field(
[0, 0],
description='A list of lists with each sub-list possesing 2 values for '
'[hour, minute]. The length of the master list must match the length '
Expand All @@ -73,13 +73,6 @@ class ScheduleDay(NamedEnergyBaseModel):
'of times as the "time of beginning" is a different convention than '
'EnergyPlus, which uses "time until".'
)

@validator('times')
def check_len_times(cls, v):
for i in v:
assert len(i) == 2, \
'Schedule times must each have two values for [hour, minute].'
return v

@root_validator
def check_times_values_match(cls, values):
Expand Down
6 changes: 3 additions & 3 deletions honeybee_schema/energy/simulation.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Simulation Parameter Schema"""
from pydantic import BaseModel, Field, validator, root_validator, constr
from pydantic import BaseModel, Field, validator, root_validator, constr, conlist
from typing import List
from enum import Enum

Expand Down Expand Up @@ -187,7 +187,7 @@ class RunPeriod(DatedBaseModel):
description='Text for the day of the week on which the simulation starts.'
)

holidays: List[List[int]] = Field(
holidays: List[conlist(int, min_items=2, max_items=2)] = Field(
default=None,
description='A list of lists where each sub-list consists of two integers '
'for [month, day], representing a date which is a holiday within the '
Expand Down Expand Up @@ -273,7 +273,7 @@ class SimulationParameter(BaseModel):
)

@validator('timestep')
def check_values(cls, v):
def check_timestep(cls, v):
valid_timesteps = (1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30, 60)
assert v in valid_timesteps, \
'"{}" is not a valid timestep. Choose from {}'.format(v, valid_timesteps)
Expand Down
23 changes: 5 additions & 18 deletions honeybee_schema/model.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Model schema and the 5 geometry objects that define it."""
from pydantic import BaseModel, Field, validator, constr
from pydantic import BaseModel, Field, validator, constr, conlist
from typing import List, Union
from enum import Enum

Expand Down Expand Up @@ -31,7 +31,7 @@ class Plane(BaseModel):

x: List[float] = Field(
default=None,
description="Plane x-axis as 3 (x, y, z) values",
description="Plane x-axis as 3 (x, y, z) values. If None, it is autocalculated.",
min_items=3,
max_items=3
)
Expand All @@ -42,14 +42,15 @@ class Face3D(BaseModel):

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

boundary: List[List[float]] = Field(
boundary: List[conlist(float, min_items=3, max_items=3)] = Field(
...,
min_items=3,
description='A list of points representing the outer boundary vertices of '
'the face. The list should include at least 3 points and each point '
'should be a list of 3 (x, y, z) values.'
)

holes: List[List[List[float]]] = Field(
holes: List[conlist(conlist(float, min_items=3, max_items=3), min_items=3)] = Field(
default=None,
description='Optional list of lists with one list for each hole in the face.'
'Each hole should be a list of at least 3 points and each point a list '
Expand All @@ -63,20 +64,6 @@ class Face3D(BaseModel):
'If None, the plane will usually be derived from the boundary points.'
)

@validator('boundary')
def check_num_items(cls, v):
for i in v:
assert len(i) == 3, 'Number of floats must be 3 for (x, y, z).'
return v

@validator('holes')
def check_num_items_holes(cls, v):
if v is not None:
for pt_list in v:
for pt in pt_list:
assert len(pt) == 3, 'Number of floats must be 3 for (x, y, z).'
return v


class ShadePropertiesAbridged(BaseModel):

Expand Down

0 comments on commit 7c76616

Please sign in to comment.