Skip to content

Commit

Permalink
Fix str doctests
Browse files Browse the repository at this point in the history
  • Loading branch information
Mauko Quiroga committed Sep 21, 2021
1 parent a7af216 commit 0193f93
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 23 deletions.
5 changes: 4 additions & 1 deletion openfisca_core/periods/date_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class DateUnit(Enum, metaclass = DateUnitMeta):
'<DateUnit.DAY(day)>'
>>> str(DateUnit.DAY)
'DateUnit.DAY'
'day'
>>> dict([(DateUnit.DAY, DateUnit.DAY.value)])
{<DateUnit.DAY(day)>: 'day'}
Expand Down Expand Up @@ -212,6 +212,9 @@ class DateUnit(Enum, metaclass = DateUnitMeta):

__hash__ = object.__hash__

def __str__(self) -> str:
return self.value

def __eq__(self, other):
if isinstance(other, str):
return self.value == other.lower()
Expand Down
54 changes: 32 additions & 22 deletions openfisca_core/periods/period_.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,32 +38,40 @@ def __str__(self):
"""
Transform period to a string.
>>> str(period(YEAR, 2014))
'2014'
>>> str(period(YEAR, '2014-2'))
'year:2014-02'
>>> str(period(MONTH, '2014-2'))
'2014-02'
>>> str(period(YEAR, 2012, size = 2))
'year:2012:2'
>>> str(period(MONTH, 2012, size = 2))
'month:2012-01:2'
>>> str(period(MONTH, 2012, size = 12))
'2012'
>>> str(period(YEAR, '2012-3', size = 2))
'year:2012-03:2'
>>> str(period(MONTH, '2012-3', size = 2))
'month:2012-03:2'
>>> str(period(MONTH, '2012-3', size = 12))
'year:2012-03'
>>> str(Period((DateUnit.YEAR, Instant((2021, 1, 1)), 1)))
'2021'
>>> str(Period((DateUnit.YEAR, Instant((2021, 2, 1)), 1)))
'year:2021-02'
>>> str(Period((DateUnit.MONTH, Instant((2021, 2, 1)), 1)))
'2021-02'
>>> str(Period((DateUnit.YEAR, Instant((2021, 1, 1)), 2)))
'year:2021:2'
>>> str(Period((DateUnit.MONTH, Instant((2021, 1, 1)), 2)))
'month:2021-01:2'
>>> str(Period((DateUnit.MONTH, Instant((2021, 1, 1)), 12)))
'2021'
>>> str(Period((DateUnit.YEAR, Instant((2021, 3, 1)), 2)))
'year:2021-03:2'
>>> str(Period((DateUnit.MONTH, Instant((2021, 3, 1)), 2)))
'month:2021-03:2'
>>> str(Period((DateUnit.MONTH, Instant((2021, 3, 1)), 12)))
'year:2021-03'
"""

unit, start_instant, size = self

if unit == DateUnit.ETERNITY:
return 'ETERNITY'

year, month, day = start_instant

# 1 year long period
Expand All @@ -73,10 +81,12 @@ def __str__(self):
return str(year)
else:
# rolling year
return '{}:{}-{:02d}'.format(DateUnit.YEAR.value, year, month)
return '{}:{}-{:02d}'.format(DateUnit.YEAR, year, month)

# simple month
if unit == DateUnit.MONTH and size == 1:
return '{}-{:02d}'.format(year, month)

# several civil years
if unit == DateUnit.YEAR and month == 1:
return '{}:{}:{}'.format(unit, year, size)
Expand Down

0 comments on commit 0193f93

Please sign in to comment.