From 5c19d7354b23e6818bb8fe275ced988ca051a179 Mon Sep 17 00:00:00 2001 From: Mauko Quiroga Date: Thu, 16 Sep 2021 18:13:13 +0200 Subject: [PATCH] Only use date units for comparisons --- .../data_storage/in_memory_storage.py | 6 +- .../data_storage/on_disk_storage.py | 6 +- openfisca_core/holders/helpers.py | 8 +- openfisca_core/holders/holder.py | 4 +- openfisca_core/periods/helpers.py | 12 +-- openfisca_core/periods/instant_.py | 6 +- openfisca_core/periods/period_.py | 10 +-- openfisca_core/periods/tests/test_instant.py | 76 +++++++++---------- .../scripts/measure_performances.py | 2 +- 9 files changed, 65 insertions(+), 65 deletions(-) diff --git a/openfisca_core/data_storage/in_memory_storage.py b/openfisca_core/data_storage/in_memory_storage.py index a82977c9d0..162972164b 100644 --- a/openfisca_core/data_storage/in_memory_storage.py +++ b/openfisca_core/data_storage/in_memory_storage.py @@ -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) @@ -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 @@ -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 = { diff --git a/openfisca_core/data_storage/on_disk_storage.py b/openfisca_core/data_storage/on_disk_storage.py index 23f9b19d6e..ec996b7d98 100644 --- a/openfisca_core/data_storage/on_disk_storage.py +++ b/openfisca_core/data_storage/on_disk_storage.py @@ -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) @@ -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) @@ -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: diff --git a/openfisca_core/holders/helpers.py b/openfisca_core/holders/helpers.py index 96c6d964fa..86046d3bc6 100644 --- a/openfisca_core/holders/helpers.py +++ b/openfisca_core/holders/helpers.py @@ -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.') @@ -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.') diff --git a/openfisca_core/holders/holder.py b/openfisca_core/holders/holder.py index 9e6b46008c..eaadf578d9 100644 --- a/openfisca_core/holders/holder.py +++ b/openfisca_core/holders/holder.py @@ -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( @@ -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' diff --git a/openfisca_core/periods/helpers.py b/openfisca_core/periods/helpers.py index cc88b3afcf..ea549c1642 100644 --- a/openfisca_core/periods/helpers.py +++ b/openfisca_core/periods/helpers.py @@ -47,7 +47,7 @@ def instant(instant: Optional[InstantLike] = None) -> Optional[Instant]: >>> instant(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) @@ -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): """ @@ -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([ @@ -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) diff --git a/openfisca_core/periods/instant_.py b/openfisca_core/periods/instant_.py index 464d194bf8..75fa6ba078 100644 --- a/openfisca_core/periods/instant_.py +++ b/openfisca_core/periods/instant_.py @@ -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', , 1)) >>> Instant((2021, 9, 13)).period("month", 2) @@ -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, 1, 1)).offset("last-of", "year") - >>> Instant((2020, 1, 1)).offset(1, DateUnit.YEAR) + >>> Instant((2020, 1, 1)).offset(1, DateUnit.YEAR.value) >>> Instant((2020, 1, 1)).offset(-3, "day") diff --git a/openfisca_core/periods/period_.py b/openfisca_core/periods/period_.py index 1c1721d63e..b2a49bbdbf 100644 --- a/openfisca_core/periods/period_.py +++ b/openfisca_core/periods/period_.py @@ -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) @@ -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): """ @@ -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)) diff --git a/openfisca_core/periods/tests/test_instant.py b/openfisca_core/periods/tests/test_instant.py index caf25b8642..3751ca6587 100644 --- a/openfisca_core/periods/tests/test_instant.py +++ b/openfisca_core/periods/tests/test_instant.py @@ -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 ;).""" diff --git a/openfisca_core/scripts/measure_performances.py b/openfisca_core/scripts/measure_performances.py index 689340daa2..7413156296 100644 --- a/openfisca_core/scripts/measure_performances.py +++ b/openfisca_core/scripts/measure_performances.py @@ -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"""