Skip to content

Commit 9a96aed

Browse files
committed
Refactor as
1 parent 31c2d32 commit 9a96aed

5 files changed

Lines changed: 27 additions & 23 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- Rename `instant` to `build_instant`.
1111
- Rename `period` to `build_period`.
1212
- Move `instant_date` to `Instant.to_date`.
13+
- Refactor `Period.contains` as `Period.__contains__`.
1314
- Make `periods.parse_period` stricter (for example `2022-1` now fails).
1415

1516
#### Technical changes

openfisca_core/data_storage/in_memory_storage.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def delete(self, period = None):
4141
self._arrays = {
4242
period_item: value
4343
for period_item, value in self._arrays.items()
44-
if not period.contains(period_item)
44+
if period_item not in period
4545
}
4646

4747
def get_known_periods(self):

openfisca_core/data_storage/on_disk_storage.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def delete(self, period = None):
6262
self._files = {
6363
period_item: value
6464
for period_item, value in self._files.items()
65-
if not period.contains(period_item)
65+
if period_item not in period
6666
}
6767

6868
def get_known_periods(self):

openfisca_core/periods/period_.py

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,29 @@ def __str__(self) -> str:
144144
# complex period
145145
return '{}:{}-{:02d}:{}'.format(unit, year, month, size)
146146

147+
def __contains__(self, other: object) -> bool:
148+
"""Checks if a ``period`` contains another one.
149+
150+
Args:
151+
other (object): The other ``Period``.
152+
153+
Returns:
154+
True if ``other`` is contained, otherwise False.
155+
156+
Example:
157+
>>> period = Period((YEAR, Instant((2021, 1, 1)), 1))
158+
>>> sub_period = Period((MONTH, Instant((2021, 1, 1)), 3))
159+
160+
>>> sub_period in period
161+
True
162+
163+
"""
164+
165+
if isinstance(other, types.Period):
166+
return self.start <= other.start and self.stop >= other.stop
167+
168+
return super().__contains__(other)
169+
147170
@property
148171
def date(self) -> datetime.date:
149172
"""The date representation of the ``period``'s' start date.
@@ -512,23 +535,3 @@ def offset(
512535
"""
513536

514537
return self.__class__((self[0], self[1].offset(offset, self[0] if unit is None else unit), self[2]))
515-
516-
def contains(self, other: types.Period) -> bool:
517-
"""Checks if a ``period`` contains another one.
518-
519-
Args:
520-
other (:obj:`.Period`): The other ``Period``.
521-
522-
Returns:
523-
True if ``other`` is contained, otherwise False.
524-
525-
Example:
526-
>>> period = Period((YEAR, Instant((2021, 1, 1)), 1))
527-
>>> sub_period = Period((MONTH, Instant((2021, 1, 1)), 3))
528-
529-
>>> period.contains(sub_period)
530-
True
531-
532-
"""
533-
534-
return self.start <= other.start and self.stop >= other.stop

openfisca_core/variables/helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def get_annualized_variable(variable: variables.Variable, annualization_period:
1717
def make_annual_formula(original_formula, annualization_period = None):
1818

1919
def annual_formula(population, period, parameters):
20-
if period.start.month != 1 and (annualization_period is None or annualization_period.contains(period)):
20+
if period.start.month != 1 and (annualization_period is None or period not in annualization_period):
2121
return population(variable.name, period.this_year.first_month)
2222
if original_formula.__code__.co_argcount == 2:
2323
return original_formula(population, period)

0 commit comments

Comments
 (0)