Skip to content

Commit

Permalink
Only use date units for comparisons
Browse files Browse the repository at this point in the history
  • Loading branch information
Mauko Quiroga committed Sep 16, 2021
1 parent 15644b9 commit 5c19d73
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 65 deletions.
6 changes: 3 additions & 3 deletions openfisca_core/data_storage/in_memory_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def __init__(self, is_eternal = False):

def get(self, period):
if self.is_eternal:
period = periods.period(DateUnit.ETERNITY)
period = periods.period(DateUnit.ETERNITY.value)
period = periods.period(period)

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

def put(self, value, period):
if self.is_eternal:
period = periods.period(DateUnit.ETERNITY)
period = periods.period(DateUnit.ETERNITY.value)
period = periods.period(period)

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

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

self._arrays = {
Expand Down
6 changes: 3 additions & 3 deletions openfisca_core/data_storage/on_disk_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def _decode_file(self, file):

def get(self, period):
if self.is_eternal:
period = periods.period(DateUnit.ETERNITY)
period = periods.period(DateUnit.ETERNITY.value)
period = periods.period(period)

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

def put(self, value, period):
if self.is_eternal:
period = periods.period(DateUnit.ETERNITY)
period = periods.period(DateUnit.ETERNITY.value)
period = periods.period(period)

filename = str(period)
Expand All @@ -56,7 +56,7 @@ def delete(self, period = None):
return

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

if period is not None:
Expand Down
8 changes: 4 additions & 4 deletions openfisca_core/holders/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ def set_input_dispatch_by_period(holder, period, array):
period_unit = period.unit

if holder.variable.definition_period == DateUnit.MONTH:
cached_period_unit = DateUnit.MONTH
cached_period_unit = DateUnit.MONTH.value
elif holder.variable.definition_period == DateUnit.YEAR:
cached_period_unit = DateUnit.YEAR
cached_period_unit = DateUnit.YEAR.value
else:
raise ValueError('set_input_dispatch_by_period can be used only for yearly or monthly variables.')

Expand Down Expand Up @@ -56,9 +56,9 @@ def set_input_divide_by_period(holder, period, array):
period_unit = period.unit

if holder.variable.definition_period == DateUnit.MONTH:
cached_period_unit = DateUnit.MONTH
cached_period_unit = DateUnit.MONTH.value
elif holder.variable.definition_period == DateUnit.YEAR:
cached_period_unit = DateUnit.YEAR
cached_period_unit = DateUnit.YEAR.value
else:
raise ValueError('set_input_divide_by_period can be used only for yearly or monthly variables.')

Expand Down
4 changes: 2 additions & 2 deletions openfisca_core/holders/holder.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def set_input(self, period, array):
'{0} is only defined for {2}s. Please adapt your input.',
]).format(
self.variable.name,
DateUnit.ETERNITY,
DateUnit.ETERNITY.value,
self.variable.definition_period,
)
raise PeriodMismatchError(
Expand Down Expand Up @@ -201,7 +201,7 @@ def _set(self, period, value):
value = self._to_array(value)
if self.variable.definition_period != DateUnit.ETERNITY:
if period is None:
raise ValueError(f'A period must be specified to set values, except for variables with {DateUnit.ETERNITY} as as period_definition.')
raise ValueError(f'A period must be specified to set values, except for variables with {DateUnit.ETERNITY.value} 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
12 changes: 6 additions & 6 deletions openfisca_core/periods/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def instant(instant: Optional[InstantLike] = None) -> Optional[Instant]:
>>> instant(Instant((2021, 9, 16)))
<Instant(2021, 9, 16)>
>>> instant(Period((DateUnit.YEAR, Instant((2021, 9, 16)), 1)))
>>> instant(Period((DateUnit.YEAR.value, Instant((2021, 9, 16)), 1)))
<Instant(2021, 9, 16)>
>>> instant(2021)
Expand Down Expand Up @@ -134,7 +134,7 @@ def period(value):
return value

if isinstance(value, Instant):
return Period((DateUnit.DAY, value, 1))
return Period((DateUnit.DAY.value, value, 1))

def parse_simple_period(value):
"""
Expand All @@ -151,11 +151,11 @@ def parse_simple_period(value):
except ValueError:
return None
else:
return Period((DateUnit.DAY, Instant((date.year, date.month, date.day)), 1))
return Period((DateUnit.DAY.value, Instant((date.year, date.month, date.day)), 1))
else:
return Period((DateUnit.MONTH, Instant((date.year, date.month, 1)), 1))
return Period((DateUnit.MONTH.value, Instant((date.year, date.month, 1)), 1))
else:
return Period((DateUnit.YEAR, Instant((date.year, date.month, 1)), 1))
return Period((DateUnit.YEAR.value, Instant((date.year, date.month, 1)), 1))

def raise_error(value):
message = os.linesep.join([
Expand All @@ -170,7 +170,7 @@ def raise_error(value):

# check the type
if isinstance(value, int):
return Period((DateUnit.YEAR, Instant((value, 1, 1)), 1))
return Period((DateUnit.YEAR.value, Instant((value, 1, 1)), 1))
if not isinstance(value, str):
raise_error(value)

Expand Down
6 changes: 3 additions & 3 deletions openfisca_core/periods/instant_.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def period(self, unit: DateUnit, size: int = 1) -> Timeable:
:exc:`AssertionError`: When ``size`` is not an unsigned :obj:`int`.
Examples:
>>> Instant((2021, 9, 13)).period(DateUnit.YEAR)
>>> Instant((2021, 9, 13)).period(DateUnit.YEAR.value)
Period(('year', <Instant(2021, 9, 13)>, 1))
>>> Instant((2021, 9, 13)).period("month", 2)
Expand Down Expand Up @@ -218,13 +218,13 @@ def offset(self, offset: OffsetBy, unit: DateUnit) -> Instant:
``last-of``, or any :obj:`int`.
Examples:
>>> Instant((2020, 12, 31)).offset("first-of", DateUnit.MONTH)
>>> Instant((2020, 12, 31)).offset("first-of", DateUnit.MONTH.value)
<Instant(2020, 12, 1)>
>>> Instant((2020, 1, 1)).offset("last-of", "year")
<Instant(2020, 12, 31)>
>>> Instant((2020, 1, 1)).offset(1, DateUnit.YEAR)
>>> Instant((2020, 1, 1)).offset(1, DateUnit.YEAR.value)
<Instant(2021, 1, 1)>
>>> Instant((2020, 1, 1)).offset(-3, "day")
Expand Down
10 changes: 5 additions & 5 deletions openfisca_core/periods/period_.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def __str__(self):
return str(year)
else:
# rolling year
return '{}:{}-{:02d}'.format(DateUnit.YEAR, year, month)
return '{}:{}-{:02d}'.format(DateUnit.YEAR.value, year, month)
# simple month
if unit == DateUnit.MONTH and size == 1:
return '{}-{:02d}'.format(year, month)
Expand Down Expand Up @@ -175,13 +175,13 @@ def get_subperiods(self, unit):
raise ValueError('Cannot subdivide {0} into {1}'.format(self.unit, unit))

if unit == DateUnit.YEAR:
return [self.this_year.offset(i, DateUnit.YEAR) for i in range(self.size)]
return [self.this_year.offset(i, DateUnit.YEAR.value) for i in range(self.size)]

if unit == DateUnit.MONTH:
return [self.first_month.offset(i, DateUnit.MONTH) for i in range(self.size_in_months)]
return [self.first_month.offset(i, DateUnit.MONTH.value) for i in range(self.size_in_months)]

if unit == DateUnit.DAY:
return [self.first_day.offset(i, DateUnit.DAY) for i in range(self.size_in_days)]
return [self.first_day.offset(i, DateUnit.DAY.value) for i in range(self.size_in_days)]

def offset(self, offset, unit = None):
"""
Expand Down Expand Up @@ -367,7 +367,7 @@ def size_in_days(self):
if unit == DateUnit.DAY:
return length
if unit in [DateUnit.MONTH, DateUnit.YEAR]:
last_day = self.start.offset(length, unit).offset(-1, DateUnit.DAY)
last_day = self.start.offset(length, unit).offset(-1, DateUnit.DAY.value)
return (last_day.date - self.start.date).days + 1

raise ValueError("Cannot calculate number of days in {0}".format(unit))
Expand Down
76 changes: 38 additions & 38 deletions openfisca_core/periods/tests/test_instant.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,71 +54,71 @@ def test_period_deprecation(instant):
"""Throws a deprecation warning when called."""

with pytest.warns(DeprecationWarning):
instant.period(DateUnit.DAY)
instant.period(DateUnit.DAY.value)


def test_period_for_eternity(instant):
"""Throws an AssertionError when called with the eternity unit."""

with pytest.raises(AssertionError, match = "eternity"):
instant.period(DateUnit.ETERNITY)
instant.period(DateUnit.ETERNITY.value)


def test_period_with_invalid_size(instant):
"""Throws an AssertionError when called with an invalid size."""

with pytest.raises(AssertionError, match = "int >= 1"):
instant.period(DateUnit.DAY, size = 0)
instant.period(DateUnit.DAY.value, size = 0)


def test_offset_for_eternity(instant):
"""Throws an AssertionError when called with the eternity unit."""

with pytest.raises(AssertionError, match = "eternity"):
instant.offset("first-of", DateUnit.ETERNITY)
instant.offset("first-of", DateUnit.ETERNITY.value)


def test_offset_with_invalid_offset(instant):
"""Throws an AssertionError when called with an invalid offset."""

with pytest.raises(AssertionError, match = "any int"):
instant.offset("doomsday", DateUnit.YEAR)
instant.offset("doomsday", DateUnit.YEAR.value)


@pytest.mark.parametrize("actual, offset, expected", [
((2020, 1, 1), (1, DateUnit.DAY), (2020, 1, 2)),
((2020, 1, 1), (1, DateUnit.MONTH), (2020, 2, 1)),
((2020, 1, 1), (1, DateUnit.YEAR), (2021, 1, 1)),
((2020, 1, 31), (1, DateUnit.DAY), (2020, 2, 1)),
((2020, 1, 31), (1, DateUnit.MONTH), (2020, 2, 29)),
((2020, 1, 31), (1, DateUnit.YEAR), (2021, 1, 31)),
((2020, 2, 28), (1, DateUnit.DAY), (2020, 2, 29)),
((2020, 2, 28), (1, DateUnit.MONTH), (2020, 3, 28)),
((2020, 2, 29), (1, DateUnit.YEAR), (2021, 2, 28)),
((2020, 1, 1), (-1, DateUnit.DAY), (2019, 12, 31)),
((2020, 1, 1), (-1, DateUnit.MONTH), (2019, 12, 1)),
((2020, 1, 1), (-1, DateUnit.YEAR), (2019, 1, 1)),
((2020, 3, 1), (-1, DateUnit.DAY), (2020, 2, 29)),
((2020, 3, 31), (-1, DateUnit.MONTH), (2020, 2, 29)),
((2020, 2, 29), (-1, DateUnit.YEAR), (2019, 2, 28)),
((2020, 1, 30), (3, DateUnit.DAY), (2020, 2, 2)),
((2020, 10, 2), (3, DateUnit.MONTH), (2021, 1, 2)),
((2020, 1, 1), (3, DateUnit.YEAR), (2023, 1, 1)),
((2020, 1, 1), (-3, DateUnit.DAY), (2019, 12, 29)),
((2020, 1, 1), (-3, DateUnit.MONTH), (2019, 10, 1)),
((2020, 1, 1), (-3, DateUnit.YEAR), (2017, 1, 1)),
((2020, 1, 1), ("first-of", DateUnit.MONTH), (2020, 1, 1)),
((2020, 2, 1), ("first-of", DateUnit.MONTH), (2020, 2, 1)),
((2020, 2, 3), ("first-of", DateUnit.MONTH), (2020, 2, 1)),
((2020, 1, 1), ("first-of", DateUnit.YEAR), (2020, 1, 1)),
((2020, 2, 1), ("first-of", DateUnit.YEAR), (2020, 1, 1)),
((2020, 2, 3), ("first-of", DateUnit.YEAR), (2020, 1, 1)),
((2020, 1, 1), ("last-of", DateUnit.MONTH), (2020, 1, 31)),
((2020, 2, 1), ("last-of", DateUnit.MONTH), (2020, 2, 29)),
((2020, 2, 3), ("last-of", DateUnit.MONTH), (2020, 2, 29)),
((2020, 1, 1), ("last-of", DateUnit.YEAR), (2020, 12, 31)),
((2020, 2, 1), ("last-of", DateUnit.YEAR), (2020, 12, 31)),
((2020, 2, 3), ("last-of", DateUnit.YEAR), (2020, 12, 31)),
((2020, 1, 1), (1, DateUnit.DAY.value), (2020, 1, 2)),
((2020, 1, 1), (1, DateUnit.MONTH.value), (2020, 2, 1)),
((2020, 1, 1), (1, DateUnit.YEAR.value), (2021, 1, 1)),
((2020, 1, 31), (1, DateUnit.DAY.value), (2020, 2, 1)),
((2020, 1, 31), (1, DateUnit.MONTH.value), (2020, 2, 29)),
((2020, 1, 31), (1, DateUnit.YEAR.value), (2021, 1, 31)),
((2020, 2, 28), (1, DateUnit.DAY.value), (2020, 2, 29)),
((2020, 2, 28), (1, DateUnit.MONTH.value), (2020, 3, 28)),
((2020, 2, 29), (1, DateUnit.YEAR.value), (2021, 2, 28)),
((2020, 1, 1), (-1, DateUnit.DAY.value), (2019, 12, 31)),
((2020, 1, 1), (-1, DateUnit.MONTH.value), (2019, 12, 1)),
((2020, 1, 1), (-1, DateUnit.YEAR.value), (2019, 1, 1)),
((2020, 3, 1), (-1, DateUnit.DAY.value), (2020, 2, 29)),
((2020, 3, 31), (-1, DateUnit.MONTH.value), (2020, 2, 29)),
((2020, 2, 29), (-1, DateUnit.YEAR.value), (2019, 2, 28)),
((2020, 1, 30), (3, DateUnit.DAY.value), (2020, 2, 2)),
((2020, 10, 2), (3, DateUnit.MONTH.value), (2021, 1, 2)),
((2020, 1, 1), (3, DateUnit.YEAR.value), (2023, 1, 1)),
((2020, 1, 1), (-3, DateUnit.DAY.value), (2019, 12, 29)),
((2020, 1, 1), (-3, DateUnit.MONTH.value), (2019, 10, 1)),
((2020, 1, 1), (-3, DateUnit.YEAR.value), (2017, 1, 1)),
((2020, 1, 1), ("first-of", DateUnit.MONTH.value), (2020, 1, 1)),
((2020, 2, 1), ("first-of", DateUnit.MONTH.value), (2020, 2, 1)),
((2020, 2, 3), ("first-of", DateUnit.MONTH.value), (2020, 2, 1)),
((2020, 1, 1), ("first-of", DateUnit.YEAR.value), (2020, 1, 1)),
((2020, 2, 1), ("first-of", DateUnit.YEAR.value), (2020, 1, 1)),
((2020, 2, 3), ("first-of", DateUnit.YEAR.value), (2020, 1, 1)),
((2020, 1, 1), ("last-of", DateUnit.MONTH.value), (2020, 1, 31)),
((2020, 2, 1), ("last-of", DateUnit.MONTH.value), (2020, 2, 29)),
((2020, 2, 3), ("last-of", DateUnit.MONTH.value), (2020, 2, 29)),
((2020, 1, 1), ("last-of", DateUnit.YEAR.value), (2020, 12, 31)),
((2020, 2, 1), ("last-of", DateUnit.YEAR.value), (2020, 12, 31)),
((2020, 2, 3), ("last-of", DateUnit.YEAR.value), (2020, 12, 31)),
])
def test_offset(actual, offset, expected):
"""It works ;)."""
Expand Down
2 changes: 1 addition & 1 deletion openfisca_core/scripts/measure_performances.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class city_code(Variable):
value_type = 'FixedStr'
max_length = 5
entity = Famille
definition_period = DateUnit.ETERNITY
definition_period = DateUnit.ETERNITY.value
label = """Code INSEE "city_code" de la commune de résidence de la famille"""


Expand Down

0 comments on commit 5c19d73

Please sign in to comment.