Esempio n. 1
0
    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
Esempio n. 2
0
def test_schedule_fixedinterval_dict_methods():
    """Test the ScheduleFixedInterval to/from dict methods."""
    trans_sched = ScheduleFixedInterval('Custom Transmittance',
                                        [x / 8760 for x in range(8760)],
                                        schedule_types.fractional)

    sch_dict = trans_sched.to_dict()
    new_schedule = ScheduleFixedInterval.from_dict(sch_dict)
    assert new_schedule == trans_sched
    assert sch_dict == new_schedule.to_dict()
Esempio n. 3
0
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)
Esempio n. 4
0
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))