Skip to content

Commit

Permalink
Add weeks to holders
Browse files Browse the repository at this point in the history
  • Loading branch information
Mauko Quiroga committed Mar 11, 2021
1 parent c557909 commit 2b992ec
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion openfisca_core/holders.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from openfisca_core.data_storage import InMemoryStorage, OnDiskStorage
from openfisca_core.errors import PeriodMismatchError
from openfisca_core.indexed_enums import Enum
from openfisca_core.periods import MONTH, YEAR, ETERNITY
from openfisca_core.periods import WEEK, MONTH, YEAR, ETERNITY
from openfisca_core.tools import eval_expression

log = logging.getLogger(__name__)
Expand Down Expand Up @@ -157,6 +157,7 @@ def set_input(self, period, array):
"""

period = periods.period(period)

if period.unit == ETERNITY and self.variable.definition_period != ETERNITY:
error_message = os.linesep.join([
'Unable to set a value for variable {0} for ETERNITY.',
Expand All @@ -171,16 +172,20 @@ def set_input(self, period, array):
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 = eval_expression(array)

if self.variable.set_input:
return self.variable.set_input(self, period, array)

return self._set(period, array)

def _to_array(self, value):
Expand Down Expand Up @@ -209,6 +214,7 @@ def _set(self, period, value):
if self.variable.definition_period != ETERNITY:
if period is None:
raise ValueError('A period must be specified to set values, except for variables with ETERNITY as as period_definition.')

if (self.variable.definition_period != period.unit or period.size > 1):
name = self.variable.name
period_size_adj = f'{period.unit}' if (period.size == 1) else f'{period.size}-{period.unit}s'
Expand Down Expand Up @@ -300,13 +306,19 @@ def set_input_divide_by_period(holder, period, array):
"""
if not isinstance(array, np.ndarray):
array = np.array(array)

period_size = period.size
period_unit = period.unit

if holder.variable.definition_period == WEEK:
cached_period_unit = periods.WEEK

if holder.variable.definition_period == MONTH:
cached_period_unit = periods.MONTH

elif holder.variable.definition_period == YEAR:
cached_period_unit = periods.YEAR

else:
raise ValueError('set_input_divide_by_period can be used only for yearly or monthly variables.')

Expand Down

0 comments on commit 2b992ec

Please sign in to comment.