Esempio n. 1
0
    def put(self, value, period, extra_params=None):
        if self.is_eternal:
            period = periods.period(ETERNITY)
        period = periods.period(period)

        if extra_params is None:
            self._arrays[period] = value
        else:
            if self._arrays.get(period) is None:
                self._arrays[period] = {}
            self._arrays[period][tuple(extra_params)] = value
Esempio n. 2
0
    def get(self, period, extra_params=None):
        if self.is_eternal:
            period = periods.period(ETERNITY)
        period = periods.period(period)

        values = self._arrays.get(period)
        if values is None:
            return None
        if extra_params:
            return values.get(tuple(extra_params))
        if isinstance(values, dict):
            return values.values()[0]
        return values
Esempio n. 3
0
    def delete(self, period=None):
        if period is None:
            self._arrays = {}
            return

        if self.is_eternal:
            period = periods.period(ETERNITY)
        period = periods.period(period)

        self._arrays = {
            period_item: value
            for period_item, value in self._arrays.iteritems()
            if not period.contains(period_item)
        }
 def compute(self, variable_name, period, **parameters):
     if period is not None and not isinstance(period, periods.Period):
         period = periods.period(period)
     holder = self.get_variable_entity(variable_name).get_holder(
         variable_name)
     result = holder.compute(period=period, **parameters)
     return result
 def calculate_output(self, variable_name, period):
     """Calculate the value using calculate_output hooks in formula classes."""
     if period is not None and not isinstance(period, periods.Period):
         period = periods.period(period)
     holder = self.get_variable_entity(variable_name).get_holder(
         variable_name)
     return holder.calculate_output(period)
Esempio n. 6
0
    def get_formula(self, period = None):
        """
        Returns the formula used to compute the variable at the given period.

        If no period is given and the variable has several formula, return the oldest formula.

        Note: variable.get_formula() returns a simple function, while variable.formula is the subclass of Formula associated to the Variable, a more complex OpenFisca object.

        :returns: Formula used to compute the variable
        :rtype: function
        """

        if period is None:
            return self.formula.dated_formulas_class[0]['formula_class'].formula.im_func

        if not isinstance(period, periods.Period):
            period = periods.period(period)

        if self.end and period.start.date > self.end:
            return None

        for dated_formula in reversed(self.formula.dated_formulas_class):
            start = dated_formula['start_instant'].date

            if period.start.date >= start:
                return dated_formula['formula_class'].formula.im_func

        return None
Esempio n. 7
0
    def put(self, value, period, extra_params=None):
        if self.is_eternal:
            period = periods.period(ETERNITY)
        period = periods.period(period)

        filename = str(period)
        if extra_params:
            filename = '{}_{}'.format(
                filename, '_'.join([str(param) for param in extra_params]))
        path = os.path.join(self.storage_dir, filename) + '.npy'
        if isinstance(value, EnumArray):
            self._enums[path] = value.possible_values
            value = value.view(np.ndarray)
        np.save(path, value)
        if extra_params is None:
            self._files[period] = path
        else:
            if self._files.get(period) is None:
                self._files[period] = {}
            self._files[period][tuple(extra_params)] = path
 def get_array(self, variable_name, period):
     if period is not None and not isinstance(period, periods.Period):
         period = periods.period(period)
     return self.get_variable_entity(variable_name).get_holder(
         variable_name).get_array(period)