Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def __set_constant_values__(self):
out = []
for cv in self.constant_values:
if isinstance(cv, ProbabilisticForecastConstantValue):
out.append(cv)
elif isinstance(cv, (float, int)):
cv_dict = self.to_dict()
cv_dict.pop('forecast_id', None)
cv_dict['constant_value'] = cv
out.append(
ProbabilisticForecastConstantValue.from_dict(cv_dict))
else:
raise TypeError(
f'Invalid type for a constant value {cv}. '
'Must be int, float, or ProbablisticConstantValue')
object.__setattr__(self, 'constant_values', tuple(out))
if fx_dict['site_id'] is not None:
if fx_dict['site_id'] in sites:
fx_dict['site'] = sites[fx_dict['site_id']]
else:
fx_dict['site'] = self.get_site(fx_dict['site_id'])
elif fx_dict['aggregate_id'] is not None:
fx_dict['aggregate'] = self.get_aggregate(fx_dict['aggregate_id'])
cvs = []
for constant_value_dict in fx_dict['constant_values']:
# the API just gets the groups attributes for the
# single constant value forecasts, so avoid
# those excess calls
cv_dict = fx_dict.copy()
cv_dict.update(constant_value_dict)
cvs.append(
datamodel.ProbabilisticForecastConstantValue.from_dict(
cv_dict))
fx_dict['constant_values'] = cvs
return datamodel.ProbabilisticForecast.from_dict(fx_dict)
def from_dict(model, input_dict, raise_on_extra=False):
dict_ = input_dict.copy()
if model != Forecast:
return super().from_dict(dict_, raise_on_extra)
if dict_.get('constant_value', None) is not None:
return ProbabilisticForecastConstantValue.from_dict(
dict_, raise_on_extra)
elif dict_.get('constant_values', None) is not None:
return ProbabilisticForecast.from_dict(dict_, raise_on_extra)
elif dict_.get('variable') == 'event':
return EventForecast.from_dict(dict_, raise_on_extra)
else:
return super().from_dict(dict_, raise_on_extra)
elif site.site_id != site_id:
raise ValueError('Supplied site.site_id does not match site_id'
f'from database. site.site_id: {site.site_id}'
f' database site_id: {site_id}')
fx_dict['site'] = site
elif agg_id is not None:
if aggregate is None:
aggregate = self.get_aggregate(agg_id)
elif aggregate.aggregate_id != agg_id:
raise ValueError(
'Supplied aggregate.aggregate_id does not match '
'aggregate from database. aggregate.aggregate_id: '
f'{aggregate.aggregate_id}'
f' database aggregate_id: {agg_id}')
fx_dict['aggregate'] = aggregate
return datamodel.ProbabilisticForecastConstantValue.from_dict(fx_dict)