Skip to content

Commit 5c19d73

Browse files
author
Mauko Quiroga
committed
Only use date units for comparisons
1 parent 15644b9 commit 5c19d73

File tree

9 files changed

+65
-65
lines changed

9 files changed

+65
-65
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.value)
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.value)
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.value)
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.value)
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.value)
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.value)
6060
period = periods.period(period)
6161

6262
if period is not None:

openfisca_core/holders/helpers.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ def set_input_dispatch_by_period(holder, period, array):
2121
period_unit = period.unit
2222

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

@@ -56,9 +56,9 @@ def set_input_divide_by_period(holder, period, array):
5656
period_unit = period.unit
5757

5858
if holder.variable.definition_period == DateUnit.MONTH:
59-
cached_period_unit = DateUnit.MONTH
59+
cached_period_unit = DateUnit.MONTH.value
6060
elif holder.variable.definition_period == DateUnit.YEAR:
61-
cached_period_unit = DateUnit.YEAR
61+
cached_period_unit = DateUnit.YEAR.value
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def set_input(self, period, array):
155155
'{0} is only defined for {2}s. Please adapt your input.',
156156
]).format(
157157
self.variable.name,
158-
DateUnit.ETERNITY,
158+
DateUnit.ETERNITY.value,
159159
self.variable.definition_period,
160160
)
161161
raise PeriodMismatchError(
@@ -201,7 +201,7 @@ def _set(self, period, value):
201201
value = self._to_array(value)
202202
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.value} 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/helpers.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def instant(instant: Optional[InstantLike] = None) -> Optional[Instant]:
4747
>>> instant(Instant((2021, 9, 16)))
4848
<Instant(2021, 9, 16)>
4949
50-
>>> instant(Period((DateUnit.YEAR, Instant((2021, 9, 16)), 1)))
50+
>>> instant(Period((DateUnit.YEAR.value, Instant((2021, 9, 16)), 1)))
5151
<Instant(2021, 9, 16)>
5252
5353
>>> instant(2021)
@@ -134,7 +134,7 @@ def period(value):
134134
return value
135135

136136
if isinstance(value, Instant):
137-
return Period((DateUnit.DAY, value, 1))
137+
return Period((DateUnit.DAY.value, value, 1))
138138

139139
def parse_simple_period(value):
140140
"""
@@ -151,11 +151,11 @@ def parse_simple_period(value):
151151
except ValueError:
152152
return None
153153
else:
154-
return Period((DateUnit.DAY, Instant((date.year, date.month, date.day)), 1))
154+
return Period((DateUnit.DAY.value, Instant((date.year, date.month, date.day)), 1))
155155
else:
156-
return Period((DateUnit.MONTH, Instant((date.year, date.month, 1)), 1))
156+
return Period((DateUnit.MONTH.value, Instant((date.year, date.month, 1)), 1))
157157
else:
158-
return Period((DateUnit.YEAR, Instant((date.year, date.month, 1)), 1))
158+
return Period((DateUnit.YEAR.value, Instant((date.year, date.month, 1)), 1))
159159

160160
def raise_error(value):
161161
message = os.linesep.join([
@@ -170,7 +170,7 @@ def raise_error(value):
170170

171171
# check the type
172172
if isinstance(value, int):
173-
return Period((DateUnit.YEAR, Instant((value, 1, 1)), 1))
173+
return Period((DateUnit.YEAR.value, Instant((value, 1, 1)), 1))
174174
if not isinstance(value, str):
175175
raise_error(value)
176176

openfisca_core/periods/instant_.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ def period(self, unit: DateUnit, size: int = 1) -> Timeable:
179179
:exc:`AssertionError`: When ``size`` is not an unsigned :obj:`int`.
180180
181181
Examples:
182-
>>> Instant((2021, 9, 13)).period(DateUnit.YEAR)
182+
>>> Instant((2021, 9, 13)).period(DateUnit.YEAR.value)
183183
Period(('year', <Instant(2021, 9, 13)>, 1))
184184
185185
>>> Instant((2021, 9, 13)).period("month", 2)
@@ -218,13 +218,13 @@ def offset(self, offset: OffsetBy, unit: DateUnit) -> Instant:
218218
``last-of``, or any :obj:`int`.
219219
220220
Examples:
221-
>>> Instant((2020, 12, 31)).offset("first-of", DateUnit.MONTH)
221+
>>> Instant((2020, 12, 31)).offset("first-of", DateUnit.MONTH.value)
222222
<Instant(2020, 12, 1)>
223223
224224
>>> Instant((2020, 1, 1)).offset("last-of", "year")
225225
<Instant(2020, 12, 31)>
226226
227-
>>> Instant((2020, 1, 1)).offset(1, DateUnit.YEAR)
227+
>>> Instant((2020, 1, 1)).offset(1, DateUnit.YEAR.value)
228228
<Instant(2021, 1, 1)>
229229
230230
>>> Instant((2020, 1, 1)).offset(-3, "day")

openfisca_core/periods/period_.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def __str__(self):
6969
return str(year)
7070
else:
7171
# rolling year
72-
return '{}:{}-{:02d}'.format(DateUnit.YEAR, year, month)
72+
return '{}:{}-{:02d}'.format(DateUnit.YEAR.value, year, month)
7373
# simple month
7474
if unit == DateUnit.MONTH and size == 1:
7575
return '{}-{:02d}'.format(year, month)
@@ -175,13 +175,13 @@ def get_subperiods(self, unit):
175175
raise ValueError('Cannot subdivide {0} into {1}'.format(self.unit, unit))
176176

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

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

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

186186
def offset(self, offset, unit = None):
187187
"""
@@ -367,7 +367,7 @@ def size_in_days(self):
367367
if unit == DateUnit.DAY:
368368
return length
369369
if unit in [DateUnit.MONTH, DateUnit.YEAR]:
370-
last_day = self.start.offset(length, unit).offset(-1, DateUnit.DAY)
370+
last_day = self.start.offset(length, unit).offset(-1, DateUnit.DAY.value)
371371
return (last_day.date - self.start.date).days + 1
372372

373373
raise ValueError("Cannot calculate number of days in {0}".format(unit))

openfisca_core/periods/tests/test_instant.py

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -54,71 +54,71 @@ def test_period_deprecation(instant):
5454
"""Throws a deprecation warning when called."""
5555

5656
with pytest.warns(DeprecationWarning):
57-
instant.period(DateUnit.DAY)
57+
instant.period(DateUnit.DAY.value)
5858

5959

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

6363
with pytest.raises(AssertionError, match = "eternity"):
64-
instant.period(DateUnit.ETERNITY)
64+
instant.period(DateUnit.ETERNITY.value)
6565

6666

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

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

7373

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

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

8080

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

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

8787

8888
@pytest.mark.parametrize("actual, offset, expected", [
89-
((2020, 1, 1), (1, DateUnit.DAY), (2020, 1, 2)),
90-
((2020, 1, 1), (1, DateUnit.MONTH), (2020, 2, 1)),
91-
((2020, 1, 1), (1, DateUnit.YEAR), (2021, 1, 1)),
92-
((2020, 1, 31), (1, DateUnit.DAY), (2020, 2, 1)),
93-
((2020, 1, 31), (1, DateUnit.MONTH), (2020, 2, 29)),
94-
((2020, 1, 31), (1, DateUnit.YEAR), (2021, 1, 31)),
95-
((2020, 2, 28), (1, DateUnit.DAY), (2020, 2, 29)),
96-
((2020, 2, 28), (1, DateUnit.MONTH), (2020, 3, 28)),
97-
((2020, 2, 29), (1, DateUnit.YEAR), (2021, 2, 28)),
98-
((2020, 1, 1), (-1, DateUnit.DAY), (2019, 12, 31)),
99-
((2020, 1, 1), (-1, DateUnit.MONTH), (2019, 12, 1)),
100-
((2020, 1, 1), (-1, DateUnit.YEAR), (2019, 1, 1)),
101-
((2020, 3, 1), (-1, DateUnit.DAY), (2020, 2, 29)),
102-
((2020, 3, 31), (-1, DateUnit.MONTH), (2020, 2, 29)),
103-
((2020, 2, 29), (-1, DateUnit.YEAR), (2019, 2, 28)),
104-
((2020, 1, 30), (3, DateUnit.DAY), (2020, 2, 2)),
105-
((2020, 10, 2), (3, DateUnit.MONTH), (2021, 1, 2)),
106-
((2020, 1, 1), (3, DateUnit.YEAR), (2023, 1, 1)),
107-
((2020, 1, 1), (-3, DateUnit.DAY), (2019, 12, 29)),
108-
((2020, 1, 1), (-3, DateUnit.MONTH), (2019, 10, 1)),
109-
((2020, 1, 1), (-3, DateUnit.YEAR), (2017, 1, 1)),
110-
((2020, 1, 1), ("first-of", DateUnit.MONTH), (2020, 1, 1)),
111-
((2020, 2, 1), ("first-of", DateUnit.MONTH), (2020, 2, 1)),
112-
((2020, 2, 3), ("first-of", DateUnit.MONTH), (2020, 2, 1)),
113-
((2020, 1, 1), ("first-of", DateUnit.YEAR), (2020, 1, 1)),
114-
((2020, 2, 1), ("first-of", DateUnit.YEAR), (2020, 1, 1)),
115-
((2020, 2, 3), ("first-of", DateUnit.YEAR), (2020, 1, 1)),
116-
((2020, 1, 1), ("last-of", DateUnit.MONTH), (2020, 1, 31)),
117-
((2020, 2, 1), ("last-of", DateUnit.MONTH), (2020, 2, 29)),
118-
((2020, 2, 3), ("last-of", DateUnit.MONTH), (2020, 2, 29)),
119-
((2020, 1, 1), ("last-of", DateUnit.YEAR), (2020, 12, 31)),
120-
((2020, 2, 1), ("last-of", DateUnit.YEAR), (2020, 12, 31)),
121-
((2020, 2, 3), ("last-of", DateUnit.YEAR), (2020, 12, 31)),
89+
((2020, 1, 1), (1, DateUnit.DAY.value), (2020, 1, 2)),
90+
((2020, 1, 1), (1, DateUnit.MONTH.value), (2020, 2, 1)),
91+
((2020, 1, 1), (1, DateUnit.YEAR.value), (2021, 1, 1)),
92+
((2020, 1, 31), (1, DateUnit.DAY.value), (2020, 2, 1)),
93+
((2020, 1, 31), (1, DateUnit.MONTH.value), (2020, 2, 29)),
94+
((2020, 1, 31), (1, DateUnit.YEAR.value), (2021, 1, 31)),
95+
((2020, 2, 28), (1, DateUnit.DAY.value), (2020, 2, 29)),
96+
((2020, 2, 28), (1, DateUnit.MONTH.value), (2020, 3, 28)),
97+
((2020, 2, 29), (1, DateUnit.YEAR.value), (2021, 2, 28)),
98+
((2020, 1, 1), (-1, DateUnit.DAY.value), (2019, 12, 31)),
99+
((2020, 1, 1), (-1, DateUnit.MONTH.value), (2019, 12, 1)),
100+
((2020, 1, 1), (-1, DateUnit.YEAR.value), (2019, 1, 1)),
101+
((2020, 3, 1), (-1, DateUnit.DAY.value), (2020, 2, 29)),
102+
((2020, 3, 31), (-1, DateUnit.MONTH.value), (2020, 2, 29)),
103+
((2020, 2, 29), (-1, DateUnit.YEAR.value), (2019, 2, 28)),
104+
((2020, 1, 30), (3, DateUnit.DAY.value), (2020, 2, 2)),
105+
((2020, 10, 2), (3, DateUnit.MONTH.value), (2021, 1, 2)),
106+
((2020, 1, 1), (3, DateUnit.YEAR.value), (2023, 1, 1)),
107+
((2020, 1, 1), (-3, DateUnit.DAY.value), (2019, 12, 29)),
108+
((2020, 1, 1), (-3, DateUnit.MONTH.value), (2019, 10, 1)),
109+
((2020, 1, 1), (-3, DateUnit.YEAR.value), (2017, 1, 1)),
110+
((2020, 1, 1), ("first-of", DateUnit.MONTH.value), (2020, 1, 1)),
111+
((2020, 2, 1), ("first-of", DateUnit.MONTH.value), (2020, 2, 1)),
112+
((2020, 2, 3), ("first-of", DateUnit.MONTH.value), (2020, 2, 1)),
113+
((2020, 1, 1), ("first-of", DateUnit.YEAR.value), (2020, 1, 1)),
114+
((2020, 2, 1), ("first-of", DateUnit.YEAR.value), (2020, 1, 1)),
115+
((2020, 2, 3), ("first-of", DateUnit.YEAR.value), (2020, 1, 1)),
116+
((2020, 1, 1), ("last-of", DateUnit.MONTH.value), (2020, 1, 31)),
117+
((2020, 2, 1), ("last-of", DateUnit.MONTH.value), (2020, 2, 29)),
118+
((2020, 2, 3), ("last-of", DateUnit.MONTH.value), (2020, 2, 29)),
119+
((2020, 1, 1), ("last-of", DateUnit.YEAR.value), (2020, 12, 31)),
120+
((2020, 2, 1), ("last-of", DateUnit.YEAR.value), (2020, 12, 31)),
121+
((2020, 2, 3), ("last-of", DateUnit.YEAR.value), (2020, 12, 31)),
122122
])
123123
def test_offset(actual, offset, expected):
124124
"""It works ;)."""

openfisca_core/scripts/measure_performances.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class city_code(Variable):
8282
value_type = 'FixedStr'
8383
max_length = 5
8484
entity = Famille
85-
definition_period = DateUnit.ETERNITY
85+
definition_period = DateUnit.ETERNITY.value
8686
label = """Code INSEE "city_code" de la commune de résidence de la famille"""
8787

8888

0 commit comments

Comments
 (0)