Skip to content

Commit 0193f93

Browse files
author
Mauko Quiroga
committed
Fix str doctests
1 parent a7af216 commit 0193f93

File tree

2 files changed

+36
-23
lines changed

2 files changed

+36
-23
lines changed

openfisca_core/periods/date_unit.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class DateUnit(Enum, metaclass = DateUnitMeta):
9999
'<DateUnit.DAY(day)>'
100100
101101
>>> str(DateUnit.DAY)
102-
'DateUnit.DAY'
102+
'day'
103103
104104
>>> dict([(DateUnit.DAY, DateUnit.DAY.value)])
105105
{<DateUnit.DAY(day)>: 'day'}
@@ -212,6 +212,9 @@ class DateUnit(Enum, metaclass = DateUnitMeta):
212212

213213
__hash__ = object.__hash__
214214

215+
def __str__(self) -> str:
216+
return self.value
217+
215218
def __eq__(self, other):
216219
if isinstance(other, str):
217220
return self.value == other.lower()

openfisca_core/periods/period_.py

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,32 +38,40 @@ def __str__(self):
3838
"""
3939
Transform period to a string.
4040
41-
>>> str(period(YEAR, 2014))
42-
'2014'
43-
44-
>>> str(period(YEAR, '2014-2'))
45-
'year:2014-02'
46-
>>> str(period(MONTH, '2014-2'))
47-
'2014-02'
48-
49-
>>> str(period(YEAR, 2012, size = 2))
50-
'year:2012:2'
51-
>>> str(period(MONTH, 2012, size = 2))
52-
'month:2012-01:2'
53-
>>> str(period(MONTH, 2012, size = 12))
54-
'2012'
55-
56-
>>> str(period(YEAR, '2012-3', size = 2))
57-
'year:2012-03:2'
58-
>>> str(period(MONTH, '2012-3', size = 2))
59-
'month:2012-03:2'
60-
>>> str(period(MONTH, '2012-3', size = 12))
61-
'year:2012-03'
41+
>>> str(Period((DateUnit.YEAR, Instant((2021, 1, 1)), 1)))
42+
'2021'
43+
44+
>>> str(Period((DateUnit.YEAR, Instant((2021, 2, 1)), 1)))
45+
'year:2021-02'
46+
47+
>>> str(Period((DateUnit.MONTH, Instant((2021, 2, 1)), 1)))
48+
'2021-02'
49+
50+
>>> str(Period((DateUnit.YEAR, Instant((2021, 1, 1)), 2)))
51+
'year:2021:2'
52+
53+
>>> str(Period((DateUnit.MONTH, Instant((2021, 1, 1)), 2)))
54+
'month:2021-01:2'
55+
56+
>>> str(Period((DateUnit.MONTH, Instant((2021, 1, 1)), 12)))
57+
'2021'
58+
59+
>>> str(Period((DateUnit.YEAR, Instant((2021, 3, 1)), 2)))
60+
'year:2021-03:2'
61+
62+
>>> str(Period((DateUnit.MONTH, Instant((2021, 3, 1)), 2)))
63+
'month:2021-03:2'
64+
65+
>>> str(Period((DateUnit.MONTH, Instant((2021, 3, 1)), 12)))
66+
'year:2021-03'
67+
6268
"""
6369

6470
unit, start_instant, size = self
71+
6572
if unit == DateUnit.ETERNITY:
6673
return 'ETERNITY'
74+
6775
year, month, day = start_instant
6876

6977
# 1 year long period
@@ -73,10 +81,12 @@ def __str__(self):
7381
return str(year)
7482
else:
7583
# rolling year
76-
return '{}:{}-{:02d}'.format(DateUnit.YEAR.value, year, month)
84+
return '{}:{}-{:02d}'.format(DateUnit.YEAR, year, month)
85+
7786
# simple month
7887
if unit == DateUnit.MONTH and size == 1:
7988
return '{}-{:02d}'.format(year, month)
89+
8090
# several civil years
8191
if unit == DateUnit.YEAR and month == 1:
8292
return '{}:{}:{}'.format(unit, year, size)

0 commit comments

Comments
 (0)