def check_set_value(self, value): if self.value_type == Enum and isinstance(value, str): try: value = self.possible_values[value].index except KeyError: possible_values = [item.name for item in self.possible_values] raise ValueError( "'{}' is not a known value for '{}'. Possible values are ['{}']." .format(value, self.name, "', '".join(possible_values))) if self.value_type in (float, int) and isinstance(value, str): try: value = tools.eval_expression(value) except SyntaxError: raise ValueError( "I couldn't understand '{}' as a value for '{}'".format( value, self.name)) try: value = numpy.array([value], dtype=self.dtype)[0] except (TypeError, ValueError): if (self.value_type == datetime.date): error_message = "Can't deal with date: '{}'.".format(value) else: error_message = "Can't deal with value: expected type {}, received '{}'.".format( self.json_type, value) raise ValueError(error_message) except (OverflowError): error_message = "Can't deal with value: '{}', it's too large for type '{}'.".format( value, self.json_type) raise ValueError(error_message) return value
def set_input(self, period, array): """ Set a variable's value (``array``) for a given period (``period``) :param array: the input value for the variable :param period: the period at which the value is setted Example : >>> holder.set_input([12, 14], '2018-04') >>> holder.get_array('2018-04') >>> [12, 14] If a ``set_input`` property has been set for the variable, this method may accept inputs for periods not matching the ``definition_period`` of the variable. To read more about this, check the `documentation <https://openfisca.org/doc/coding-the-legislation/35_periods.html#set-input-automatically-process-variable-inputs-defined-for-periods-not-matching-the-definition-period>`_. """ period = periods.period(period) if period.unit == periods.ETERNITY and self.variable.definition_period != periods.ETERNITY: error_message = os.linesep.join([ 'Unable to set a value for variable {0} for periods.ETERNITY.', '{0} is only defined for {1}s. Please adapt your input.', ]).format(self.variable.name, self.variable.definition_period) raise PeriodMismatchError(self.variable.name, period, self.variable.definition_period, error_message) if self.variable.is_neutralized: warning_message = "You cannot set a value for the variable {}, as it has been neutralized. The value you provided ({}) will be ignored.".format( self.variable.name, array) return warnings.warn(warning_message, Warning) if self.variable.value_type in (float, int) and isinstance(array, str): array = tools.eval_expression(array) if self.variable.set_input: return self.variable.set_input(self, period, array) return self._set(period, array)
def init_variable_value(self, entity, entity_id, variable_name, period_str, value): path_in_json = [entity.plural, entity_id, variable_name, period_str] entity_index = entity.ids.index(entity_id) holder = entity.get_holder(variable_name) try: period = make_period(period_str) except ValueError as e: raise SituationParsingError(path_in_json, e.args[0]) if value is None: return array = holder.buffer.get(period) if array is None: array = holder.default_array() if holder.variable.value_type == Enum and isinstance( value, basestring_type): try: value = holder.variable.possible_values[value].index except KeyError: possible_values = [ item.name for item in holder.variable.possible_values ] raise SituationParsingError( path_in_json, "'{}' is not a known value for '{}'. Possible values are ['{}']." .format(value, variable_name, "', '".join(possible_values))) if holder.variable.value_type in (float, int) and isinstance( value, basestring_type): value = eval_expression(value) try: array[entity_index] = value except (OverflowError): error_message = "Can't deal with value: '{}', it's too large for type '{}'.".format( value, holder.variable.json_type) raise SituationParsingError(path_in_json, error_message) except (ValueError, TypeError): if holder.variable.value_type == date: error_message = "Can't deal with date: '{}'.".format(value) else: error_message = "Can't deal with value: expected type {}, received '{}'.".format( holder.variable.json_type, value) raise SituationParsingError(path_in_json, error_message) if not variable_name in self.input: self.input[variable_name] = {} self.input[variable_name][period_str] = array holder.buffer[period] = array