Exemplo n.º 1
0
    def from_dict(cls, data):
        """Create a RunPeriod object from a dictionary.

        Args:
            data: A RunPeriod dictionary in following the format below.

        .. code-block:: python

            {
            "type": "RunPeriod",
            "start_date": {month: 1, day: 1},
            "end_date": {month: 12, day: 31},
            "start_day_of_week": 'Monday',
            "holidays": [{month: 1, day: 1}, {month: 7, day: 4}],
            "daylight_saving_time": {} // DaylightSavingTime dictionary representation
            }
        """
        assert data['type'] == 'RunPeriod', \
            'Expected RunPeriod dictionary. Got {}.'.format(data['type'])
        start_date = Date.from_dict(data['start_date']) if \
            'start_date' in data else Date(1, 1)
        end_date = Date.from_dict(data['end_date']) if \
            'end_date' in data else Date(12, 31)
        start_day_of_week = data['start_day_of_week'] if \
            'start_day_of_week' in data else 'Sunday'
        holidays = None
        if 'holidays' in data and data['holidays'] is not None:
            holidays = tuple(Date.from_dict(hol) for hol in data['holidays'])
        daylight_saving = None
        if 'daylight_saving_time' in data and data['daylight_saving_time'] is not None:
            daylight_saving = DaylightSavingTime.from_dict(data['daylight_saving_time'])
        return cls(start_date, end_date, start_day_of_week, holidays, daylight_saving)
Exemplo n.º 2
0
def test_date_to_from_dict():
    """Test the dict methods for Date."""
    dt1 = Date(6, 21)
    dt_dict = dt1.to_dict()
    rebuilt_dt = Date.from_dict(dt_dict)
    assert dt1 == rebuilt_dt
    assert rebuilt_dt.to_dict() == dt_dict
Exemplo n.º 3
0
    def from_dict(cls, data):
        """Create a DaylightSavingTime object from a dictionary.

        Args:
            data: A DaylightSavingTime dictionary in following the format below.

        .. code-block:: python

            {
            "type": "DaylightSavingTime",
            "start_date": {month: 1, day: 1},
            "end_date": {month: 12, day: 31}
            }
        """
        assert data['type'] == 'DaylightSavingTime', \
            'Expected DaylightSavingTime dictionary. Got {}.'.format(data['type'])
        start_date = Date.from_dict(data['start_date']) if \
            'start_date' in data else Date(1, 1)
        end_date = Date.from_dict(data['end_date']) if \
            'end_date' in data else Date(12, 31)
        return cls(start_date, end_date)
Exemplo n.º 4
0
    def from_dict(cls, data):
        """Create a ScheduleFixedInterval from a dictionary.

        Note that the dictionary must be a non-abridged version for this
        classmethod to work.

        Args:
            data: ScheduleFixedInterval dictionary following the format below.

        .. code-block:: json

            {
            "type": 'ScheduleFixedInterval',
            "name": 'Automated Awning Transmittance',
            "values": [], // list of numbers for the values of the schedule
            "schedule_type_limit": {}, // ScheduleTypeLimit dictionary representation
            "timestep": 1, // Integer for the timestep of the schedule
            "start_date": {}, // Date dictionary representation
            "placeholder_value": 0, // Number for the values out of range
            "interpolate": False // Boolean noting whether to interpolate between values
            }
        """
        assert data['type'] == 'ScheduleFixedInterval', \
            'Expected ScheduleFixedInterval. Got {}.'.format(data['type'])

        sched_type = None
        if 'schedule_type_limit' in data and data[
                'schedule_type_limit'] is not None:
            sched_type = ScheduleTypeLimit.from_dict(
                data['schedule_type_limit'])
        timestep = 1
        if 'timestep' in data and data['timestep'] is not None:
            timestep = data['timestep']
        start_date = Date(1, 1)
        if 'start_date' in data and data['start_date'] is not None:
            start_date = Date.from_dict(data['start_date'])
        placeholder_value = 0
        if 'placeholder_value' in data and data[
                'placeholder_value'] is not None:
            placeholder_value = data['placeholder_value']
        interpolate = False
        if 'interpolate' in data and data['interpolate'] is not None:
            interpolate = data['interpolate']

        return cls(data['name'], data['values'], sched_type, timestep,
                   start_date, placeholder_value, interpolate)