def from_dict(cls, data): """Create a ScheduleRule from a dictionary. Args: data: ScheduleRule dictionary following the format below. .. code-block:: json { "type": 'ScheduleRule' "schedule_day": { "type": 'ScheduleDay', "name": 'Office Occupancy', "values": [0, 1, 0], "times": [(0, 0), (9, 0), (17, 0)], "interpolate": False } "apply_sunday": False, "apply_monday": True, "apply_tuesday": True, "apply_wednesday": True, "apply_thursday": True, "apply_friday": True, "apply_saturday": False, "apply_holiday": False, "start_date": (1, 1), "end_date": (12, 31) } """ assert data['type'] == 'ScheduleRule', \ 'Expected ScheduleRule. Got {}.'.format(data['type']) schedule_day = ScheduleDay.from_dict(data['schedule_day']) apply_sunday = data['apply_sunday'] if 'apply_sunday' in data else False apply_monday = data['apply_monday'] if 'apply_monday' in data else False apply_tuesday = data['apply_tuesday'] if 'apply_tuesday' in data else False apply_wednesday = data['apply_wednesday'] if 'apply_wednesday' in data else False apply_thursday = data['apply_thursday'] if 'apply_thursday' in data else False apply_friday = data['apply_friday'] if 'apply_friday' in data else False apply_saturday = data['apply_saturday'] if 'apply_saturday' in data else False apply_holiday = data['apply_holiday'] if 'apply_holiday' in data else False start_date = Date.from_array(data['start_date']) if \ 'start_date' in data else cls._year_start end_date = Date.from_array(data['end_date']) if \ 'end_date' in data else cls._year_end return cls(schedule_day, apply_sunday, apply_monday, apply_tuesday, apply_wednesday, apply_thursday, apply_friday, apply_saturday, apply_holiday, start_date, end_date)
def _extract_apply_from_dict(data): """Extract the apply values from a dictionary.""" apply_sunday = data['apply_sunday'] if 'apply_sunday' in data else False apply_monday = data['apply_monday'] if 'apply_monday' in data else False apply_tuesday = data['apply_tuesday'] if 'apply_tuesday' in data else False apply_wednesday = data['apply_wednesday'] if 'apply_wednesday' in data else False apply_thursday = data['apply_thursday'] if 'apply_thursday' in data else False apply_friday = data['apply_friday'] if 'apply_friday' in data else False apply_saturday = data['apply_saturday'] if 'apply_saturday' in data else False start_date = Date.from_array(data['start_date']) if \ 'start_date' in data else ScheduleRule._year_start end_date = Date.from_array(data['end_date']) if \ 'end_date' in data else ScheduleRule._year_end return apply_sunday, apply_monday, apply_tuesday, apply_wednesday, \ apply_thursday, apply_friday, apply_saturday, start_date, end_date
def test_date_to_from_array(): """Test the from_array method for Date.""" dt1 = Date(6, 21) dt_arr = dt1.to_array() rebuilt_dt = Date.from_array(dt_arr) assert rebuilt_dt == dt1 assert rebuilt_dt.to_array() == dt_arr
def from_dict(cls, data): """Create a UWGRunPeriod object from a dictionary. Args: data: A UWGRunPeriod dictionary in following the format below. .. code-block:: python { "type": "UWGRunPeriod", "start_date": [3, 12], "end_date": [11, 5] } """ assert data['type'] == 'UWGRunPeriod', \ 'Expected UWGRunPeriod dictionary. Got {}.'.format(data['type']) start_date = Date.from_array(data['start_date']) if 'start_date' in data and \ data['start_date'] is not None else Date(1, 1) end_date = Date.from_array(data['end_date']) if 'end_date' in data and \ data['end_date'] is not None else Date(12, 31) return cls(start_date, end_date)
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:: python { "type": 'ScheduleFixedInterval', "identifier": 'Awning_Transmittance_X45NF23U', "display_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": (1, 1), # 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_array(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'] new_obj = cls(data['identifier'], data['values'], sched_type, timestep, start_date, placeholder_value, interpolate) if 'display_name' in data and data['display_name'] is not None: new_obj.display_name = data['display_name'] return new_obj