def validate_program_type(program_type_json): """Validate all properties of a ProgramType or ProgramTypeAbridged JSON. \b Args: program_type_json: Full path to a ProgramType or ProgramTypeAbridged JSON file. """ try: # first check the JSON against the OpenAPI specification with open(program_type_json) as json_file: data = json.load(json_file) if data['type'] == 'ProgramType': click.echo('Validating ProgramType JSON ...') schema_programtype.ProgramType.parse_file(program_type_json) click.echo('Pydantic validation passed.') ProgramType.from_dict(data) click.echo('Python re-serialization passed.') else: # assume it's a ProgramTypeAbridged schema click.echo('Validating ProgramTypeAbridged JSON ...') schema_programtype.ProgramTypeAbridged.parse_file(program_type_json) click.echo('Pydantic validation passed.') # if we made it to this point, report that the object is valid click.echo('Congratulations! Your Program JSON is valid!') except Exception as e: _logger.exception('ProgramType validation failed.\n{}'.format(e)) sys.exit(1) else: sys.exit(0)
def from_dict(cls, data, host): """Create Room2DEnergyProperties from a dictionary. Note that the dictionary must be a non-abridged version for this classmethod to work. Args: data: A dictionary representation of Room2DEnergyProperties. host: A Room2D object that hosts these properties. """ assert data['type'] == 'Room2DEnergyProperties', \ 'Expected Room2DEnergyProperties. Got {}.'.format(data['type']) new_prop = cls(host) if 'construction_set' in data and data['construction_set'] is not None: new_prop.construction_set = \ ConstructionSet.from_dict(data['construction_set']) if 'program_type' in data and data['program_type'] is not None: new_prop.program_type = ProgramType.from_dict(data['program_type']) if 'hvac' in data and data['hvac'] is not None: hvac_class = HVAC_TYPES_DICT[data['hvac']['type']] new_prop.hvac = hvac_class.from_dict(data['hvac']) cls._deserialize_window_vent(new_prop, data) return new_prop
def test_program_type_dict_methods(): """Test the to/from dict methods.""" simple_office = ScheduleDay( 'Simple Weekday Occupancy', [0, 1, 0], [Time(0, 0), Time(9, 0), Time(17, 0)]) occ_schedule = ScheduleRuleset('Office Occupancy Schedule', simple_office, None, schedule_types.fractional) light_schedule = occ_schedule.duplicate() light_schedule.identifier = 'Office Lighting-Equip Schedule' light_schedule.default_day_schedule.values = [0.25, 1, 0.25] equip_schedule = light_schedule.duplicate() inf_schedule = ScheduleRuleset.from_constant_value( 'Infiltration Schedule', 1, schedule_types.fractional) heat_setpt = ScheduleRuleset.from_constant_value( 'Office Heating Schedule', 21, schedule_types.temperature) cool_setpt = ScheduleRuleset.from_constant_value( 'Office Cooling Schedule', 24, schedule_types.temperature) people = People('Open Office People', 0.05, occ_schedule) lighting = Lighting('Open Office Lighting', 10, light_schedule) equipment = ElectricEquipment('Open Office Equipment', 10, equip_schedule) infiltration = Infiltration('Office Infiltration', 0.00015, inf_schedule) ventilation = Ventilation('Office Ventilation', 0.0025, 0.0003) setpoint = Setpoint('Office Setpoints', heat_setpt, cool_setpt) office_program = ProgramType('Open Office Program', people, lighting, equipment, None, None, infiltration, ventilation, setpoint) prog_dict = office_program.to_dict() new_office_program = ProgramType.from_dict(prog_dict) assert new_office_program == office_program assert prog_dict == new_office_program.to_dict()
def load_program_object(pro_dict): """Load a program object from a dictionary and add it to the _program_types dict.""" try: if pro_dict['type'] == 'ProgramTypeAbridged': program = ProgramType.from_dict_abridged(pro_dict, _schedules) else: program = ProgramType.from_dict(pro_dict) program.lock() assert pro_dict['identifier'] not in _default_programs, 'Cannot overwrite ' \ 'default program type "{}".'.format(pro_dict['identifier']) _program_types[pro_dict['identifier']] = program except (TypeError, KeyError, ValueError): pass # not a Honeybee ProgramType JSON; possibly a comment
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 dict_to_object(honeybee_energy_dict, raise_exception=True): """Re-serialize a dictionary of almost any object within honeybee_energy. This includes any Material, Construction, ConstructionSet, Schedule, Load, ProgramType, or Simulation object. Args: honeybee_energy_dict: A dictionary of any Honeybee energy object. 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 part of honeybee_energy. Default: True. Returns: A Python object derived from the input honeybee_energy_dict. """ try: # get the type key from the dictionary obj_type = honeybee_energy_dict['type'] except KeyError: raise ValueError( 'Honeybee_energy dictionary lacks required "type" key.') if obj_type == 'ProgramType': return ProgramType.from_dict(honeybee_energy_dict) elif obj_type == 'ConstructionSet': return ConstructionSet.from_dict(honeybee_energy_dict) elif obj_type in SCHEDULE_TYPES: return dict_to_schedule(honeybee_energy_dict) elif obj_type in CONSTRUCTION_TYPES: return dict_to_construction(honeybee_energy_dict) elif obj_type in MATERIAL_TYPES: return dict_to_material(honeybee_energy_dict) elif obj_type in LOAD_TYPES: return dict_to_load(honeybee_energy_dict) elif obj_type in SIMULATION_TYPES: return dict_to_simulation(honeybee_energy_dict) elif raise_exception: raise ValueError( '{} is not a recognized honeybee energy object'.format(obj_type))