Skip to content

Commit 6bd499c

Browse files
author
Mauko Quiroga
committed
Simplify DateUnit
1 parent c6fed0c commit 6bd499c

28 files changed

+341
-477
lines changed

openfisca_core/data_storage/in_memory_storage.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def __init__(self, is_eternal = False):
1515

1616
def get(self, period):
1717
if self.is_eternal:
18-
period = periods.period(DateUnit.Eternity)
18+
period = periods.period(DateUnit.ETERNITY)
1919
period = periods.period(period)
2020

2121
values = self._arrays.get(period)
@@ -25,7 +25,7 @@ def get(self, period):
2525

2626
def put(self, value, period):
2727
if self.is_eternal:
28-
period = periods.period(DateUnit.Eternity)
28+
period = periods.period(DateUnit.ETERNITY)
2929
period = periods.period(period)
3030

3131
self._arrays[period] = value
@@ -36,7 +36,7 @@ def delete(self, period = None):
3636
return
3737

3838
if self.is_eternal:
39-
period = periods.period(DateUnit.Eternity)
39+
period = periods.period(DateUnit.ETERNITY)
4040
period = periods.period(period)
4141

4242
self._arrays = {

openfisca_core/data_storage/on_disk_storage.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def _decode_file(self, file):
2929

3030
def get(self, period):
3131
if self.is_eternal:
32-
period = periods.period(DateUnit.Eternity)
32+
period = periods.period(DateUnit.ETERNITY)
3333
period = periods.period(period)
3434

3535
values = self._files.get(period)
@@ -39,7 +39,7 @@ def get(self, period):
3939

4040
def put(self, value, period):
4141
if self.is_eternal:
42-
period = periods.period(DateUnit.Eternity)
42+
period = periods.period(DateUnit.ETERNITY)
4343
period = periods.period(period)
4444

4545
filename = str(period)
@@ -56,7 +56,7 @@ def delete(self, period = None):
5656
return
5757

5858
if self.is_eternal:
59-
period = periods.period(DateUnit.Eternity)
59+
period = periods.period(DateUnit.ETERNITY)
6060
period = periods.period(period)
6161

6262
if period is not None:

openfisca_core/holders/helpers.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ def set_input_dispatch_by_period(holder, period, array):
2020
period_size = period.size
2121
period_unit = period.unit
2222

23-
if holder.variable.definition_period == DateUnit.Month:
24-
cached_period_unit = DateUnit.Month
25-
elif holder.variable.definition_period == DateUnit.Year:
26-
cached_period_unit = DateUnit.Year
23+
if holder.variable.definition_period == DateUnit.MONTH:
24+
cached_period_unit = DateUnit.MONTH
25+
elif holder.variable.definition_period == DateUnit.YEAR:
26+
cached_period_unit = DateUnit.YEAR
2727
else:
2828
raise ValueError('set_input_dispatch_by_period can be used only for yearly or monthly variables.')
2929

@@ -55,10 +55,10 @@ def set_input_divide_by_period(holder, period, array):
5555
period_size = period.size
5656
period_unit = period.unit
5757

58-
if holder.variable.definition_period == DateUnit.Month:
59-
cached_period_unit = DateUnit.Month
60-
elif holder.variable.definition_period == DateUnit.Year:
61-
cached_period_unit = DateUnit.Year
58+
if holder.variable.definition_period == DateUnit.MONTH:
59+
cached_period_unit = DateUnit.MONTH
60+
elif holder.variable.definition_period == DateUnit.YEAR:
61+
cached_period_unit = DateUnit.YEAR
6262
else:
6363
raise ValueError('set_input_divide_by_period can be used only for yearly or monthly variables.')
6464

openfisca_core/holders/holder.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def __init__(self, variable, population):
2020
self.population = population
2121
self.variable = variable
2222
self.simulation = population.simulation
23-
self._memory_storage = InMemoryStorage(is_eternal = (self.variable.definition_period == DateUnit.Eternity))
23+
self._memory_storage = InMemoryStorage(is_eternal = (self.variable.definition_period == DateUnit.ETERNITY))
2424

2525
# By default, do not activate on-disk storage, or variable dropping
2626
self._disk_storage = None
@@ -57,7 +57,7 @@ def create_disk_storage(self, directory = None, preserve = False):
5757
os.mkdir(storage_dir)
5858
return OnDiskStorage(
5959
storage_dir,
60-
is_eternal = (self.variable.definition_period == DateUnit.Eternity),
60+
is_eternal = (self.variable.definition_period == DateUnit.ETERNITY),
6161
preserve_storage_dir = preserve
6262
)
6363

@@ -149,13 +149,13 @@ def set_input(self, period, array):
149149
"""
150150

151151
period = periods.period(period)
152-
if period.unit == DateUnit.Eternity and self.variable.definition_period != DateUnit.Eternity:
152+
if period.unit == DateUnit.ETERNITY and self.variable.definition_period != DateUnit.ETERNITY:
153153
error_message = os.linesep.join([
154154
'Unable to set a value for variable {0} for {1}.',
155155
'{0} is only defined for {2}s. Please adapt your input.',
156156
]).format(
157157
self.variable.name,
158-
DateUnit.Eternity,
158+
DateUnit.ETERNITY,
159159
self.variable.definition_period,
160160
)
161161
raise PeriodMismatchError(
@@ -199,9 +199,9 @@ def _to_array(self, value):
199199

200200
def _set(self, period, value):
201201
value = self._to_array(value)
202-
if self.variable.definition_period != DateUnit.Eternity:
202+
if self.variable.definition_period != DateUnit.ETERNITY:
203203
if period is None:
204-
raise ValueError(f'A period must be specified to set values, except for variables with {DateUnit.Eternity} as as period_definition.')
204+
raise ValueError(f'A period must be specified to set values, except for variables with {DateUnit.ETERNITY} as as period_definition.')
205205
if (self.variable.definition_period != period.unit or period.size > 1):
206206
name = self.variable.name
207207
period_size_adj = f'{period.unit}' if (period.size == 1) else f'{period.size}-{period.unit}s'

openfisca_core/periods/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
from .helpers import unit_weight, unit_weights # noqa: F401
4242

4343
for item in DateUnit:
44-
globals()[item.name.upper()] = item.key
44+
globals()[item.name] = item.value
4545

4646
str_by_instant_cache: Dict[Any, Any] = {}
4747
"""Cache to store :obj:`str` reprentations of :obj:`.Instant`.

0 commit comments

Comments
 (0)