def from_dict(cls, data, host): """Create ContextShadeEnergyProperties from a dictionary. Note that the dictionary must be a non-abridged version for this classmethod to work. Args: data: A dictionary representation of ContextShadeEnergyProperties. host: A ContextShade object that hosts these properties. """ assert data['type'] == 'ContextShadeEnergyProperties', \ 'Expected ContextShadeEnergyProperties. Got {}.'.format(data['type']) new_prop = cls(host) if 'construction' in data and data['construction'] is not None: new_prop.construction = ShadeConstruction.from_dict( data['construction']) if 'transmittance_schedule' in data and \ data['transmittance_schedule'] is not None: sch_dict = data['transmittance_schedule'] if sch_dict['type'] == 'ScheduleRuleset': new_prop.transmittance_schedule = \ ScheduleRuleset.from_dict(data['transmittance_schedule']) elif sch_dict['type'] == 'ScheduleFixedInterval': new_prop.transmittance_schedule = \ ScheduleFixedInterval.from_dict(data['transmittance_schedule']) else: raise ValueError( 'Expected non-abridged Schedule dictionary for ContextShade ' 'transmittance_schedule. Got {}.'.format(sch_dict['type'])) return new_prop
def validate_schedule(schedule_json): """Validate all properties of a schedule or abridged schedule JSON. \b Args: schedule_json: Full path to a either ScheduleRuleset, ScheduleRulesetAbridged ScheduleFixedInterval, or ScheduleFixedIntervalAbridged JSON file. """ try: # first check the JSON against the OpenAPI specification with open(schedule_json) as json_file: data = json.load(json_file) if data['type'] == 'ScheduleRuleset': click.echo('Validating ScheduleRuleset JSON ...') schema_schedule.ScheduleRuleset.parse_file(schedule_json) click.echo('Pydantic validation passed.') ScheduleRuleset.from_dict(data) click.echo('Python re-serialization passed.') elif data['type'] == 'ScheduleFixedInterval': click.echo('Validating ScheduleFixedInterval JSON ...') schema_schedule.ScheduleFixedInterval.parse_file(schedule_json) click.echo('Pydantic validation passed.') ScheduleFixedInterval.from_dict(data) click.echo('Python re-serialization passed.') elif data['type'] == 'ScheduleRulesetAbridged': click.echo('Validating ScheduleRulesetAbridged JSON ...') schema_schedule.ScheduleRulesetAbridged.parse_file(schedule_json) click.echo('Pydantic validation passed.') else: # assume it's a ScheduleFixedIntervalAbridged schema click.echo('Validating ScheduleFixedIntervalAbridged JSON ...') schema_schedule.ScheduleFixedIntervalAbridged.parse_file( schedule_json) click.echo('Pydantic validation passed.') # if we made it to this point, report that the object is valid click.echo('Congratulations! Your Schedule JSON is valid!') except Exception as e: _logger.exception('Schedule validation failed.\n{}'.format(e)) sys.exit(1) else: sys.exit(0)
def test_schedule_from_lib(): """Test the existence of schedule objects in the library.""" runner = CliRunner() result = runner.invoke(schedule_type_limit_by_id, ['Fractional']) assert result.exit_code == 0 sch_dict = json.loads(result.output) assert isinstance(ScheduleTypeLimit.from_dict(sch_dict), ScheduleTypeLimit) result = runner.invoke(schedule_by_id, ['Generic Office Occupancy']) assert result.exit_code == 0 sch_dict = json.loads(result.output) assert isinstance(ScheduleRuleset.from_dict(sch_dict), ScheduleRuleset) result = runner.invoke(program_type_by_id, ['Generic Office Program']) assert result.exit_code == 0 prog_dict = json.loads(result.output) assert isinstance(ProgramType.from_dict(prog_dict), ProgramType)
def test_schedule_ruleset_dict_methods(): """Test the ScheduleRuleset to/from dict methods.""" weekday_office = ScheduleDay( 'Weekday Office Occupancy', [0, 1, 0], [Time(0, 0), Time(9, 0), Time(17, 0)]) saturday_office = ScheduleDay( 'Saturday Office Occupancy', [0, 0.25, 0], [Time(0, 0), Time(9, 0), Time(17, 0)]) sunday_office = ScheduleDay('Sunday Office Occupancy', [0]) sat_rule = ScheduleRule(saturday_office, apply_saturday=True) sun_rule = ScheduleRule(sunday_office, apply_sunday=True) summer_office = ScheduleDay( 'Summer Office Occupancy', [0, 1, 0.25], [Time(0, 0), Time(6, 0), Time(22, 0)]) winter_office = ScheduleDay('Winter Office Occupancy', [0]) schedule = ScheduleRuleset('Office Occupancy', weekday_office, [sat_rule, sun_rule], schedule_types.fractional, summer_office, winter_office) sch_dict = schedule.to_dict() new_schedule = ScheduleRuleset.from_dict(sch_dict) assert new_schedule == schedule assert sch_dict == new_schedule.to_dict()
def dict_to_schedule(sch_dict, raise_exception=True): """Get a Python object of any Schedule from a dictionary. Args: sch_dict: A dictionary of any Honeybee energy schedules. Note that this should be a non-abridged dictionary to be valid. raise_exception: Boolean to note whether an excpetion should be raised if the object is not identified as a schedule. Default: True. Returns: A Python object derived from the input sch_dict. """ try: # get the type key from the dictionary sch_type = sch_dict['type'] except KeyError: raise ValueError('Schedule dictionary lacks required "type" key.') if sch_type == 'ScheduleRuleset': return ScheduleRuleset.from_dict(sch_dict) elif sch_type == 'ScheduleFixedInterval': return ScheduleFixedInterval.from_dict(sch_dict) elif raise_exception: raise ValueError('{} is not a recognized energy Schedule type'.format(sch_type))