Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

duplicate code: get_year() vs clean year() #186

Closed
epogrebnyak opened this issue Jul 6, 2018 · 0 comments
Closed

duplicate code: get_year() vs clean year() #186

epogrebnyak opened this issue Jul 6, 2018 · 0 comments

Comments

@epogrebnyak
Copy link
Collaborator

# EP: get_year - более протестированная и общая функция, но может быть ее
# можно заменить на clean_year
YEAR_CATCHER = re.compile("\D*(\d{4}).*")
def get_year(string: str, rx=YEAR_CATCHER):
"""Extracts year from *string* using *rx* regex.
Returns:
Year as integer
False if year is not valid or not in plausible range."""
match = re.match(rx, string)
if match:
year = int(match.group(1))
if year >= 1991 and year <= 2050:
return year
return False
def is_year(string: str) -> bool:
return get_year(string) is not False
# WONTFIX: use re.match()?
REGEX_Y = re.compile(r'\s*(\d{4})')
def clean_year(year: str) -> int:
found = re.search(REGEX_Y, year)
return int(found.group(0)) if found else None
# TODO: move to tests
assert clean_year('20052),3)') == 2005
assert clean_year('20114)') == 2011
assert clean_year('abc') is None
assert clean_year('') is None
assert clean_year('111') is None
assert clean_year('1001') == 1001

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant