-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add typing to & do maintenance of periods (#1223)
- Loading branch information
Showing
22 changed files
with
935 additions
and
464 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from pendulum.parsing.exceptions import ParserError | ||
|
||
|
||
class InstantError(ValueError): | ||
"""Raised when an invalid instant-like is provided.""" | ||
|
||
def __init__(self, value: str) -> None: | ||
msg = ( | ||
f"'{value}' is not a valid instant string. Instants are described " | ||
"using either the 'YYYY-MM-DD' format, for instance '2015-06-15', " | ||
"or the 'YYYY-Www-D' format, for instance '2015-W24-1'." | ||
) | ||
super().__init__(msg) | ||
|
||
|
||
class PeriodError(ValueError): | ||
"""Raised when an invalid period-like is provided.""" | ||
|
||
def __init__(self, value: str) -> None: | ||
msg = ( | ||
"Expected a period (eg. '2017', 'month:2017-01', 'week:2017-W01-1:3', " | ||
f"...); got: '{value}'. Learn more about legal period formats in " | ||
"OpenFisca: <https://openfisca.org/doc/coding-the-legislation/35_periods.html#periods-in-simulations>." | ||
) | ||
super().__init__(msg) | ||
|
||
|
||
__all__ = ["InstantError", "ParserError", "PeriodError"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,20 @@ | ||
import re | ||
|
||
from .date_unit import DateUnit | ||
import pendulum | ||
|
||
WEEKDAY = DateUnit.WEEKDAY | ||
WEEK = DateUnit.WEEK | ||
DAY = DateUnit.DAY | ||
MONTH = DateUnit.MONTH | ||
YEAR = DateUnit.YEAR | ||
ETERNITY = DateUnit.ETERNITY | ||
from . import types as t | ||
|
||
# Matches "2015", "2015-01", "2015-01-01" | ||
# Does not match "2015-13", "2015-12-32" | ||
INSTANT_PATTERN = re.compile( | ||
r"^\d{4}(-(0[1-9]|1[012]))?(-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01]))?$", | ||
) | ||
|
||
date_by_instant_cache: dict = {} | ||
str_by_instant_cache: dict = {} | ||
date_by_instant_cache: dict[t.Instant, pendulum.Date] = {} | ||
str_by_instant_cache: dict[t.Instant, t.InstantStr] = {} | ||
year_or_month_or_day_re = re.compile( | ||
r"(18|19|20)\d{2}(-(0?[1-9]|1[0-2])(-([0-2]?\d|3[0-1]))?)?$", | ||
) | ||
|
||
|
||
__all__ = ["INSTANT_PATTERN", "date_by_instant_cache", "str_by_instant_cache"] |
Oops, something went wrong.