Skip to content

Commit

Permalink
Merge pull request #44 from szegedai/exception_safety
Browse files Browse the repository at this point in the history
Make end points exception-safe
  • Loading branch information
nsomabalint authored May 21, 2024
2 parents dfe55f2 + 9571752 commit 5fbd132
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/datetime-parser-cicd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
python -m mypy --ignore-missing-imports hun_date_parser
- name: Static analysis with flake8
run: |
python -m flake8 --max-line-length=120 --per-file-ignores='patterns.py:E501' hun_date_parser
python -m flake8 --max-line-length=120 --per-file-ignores='patterns.py:E501,datetime_extractor.py:E722' hun_date_parser
- name: Test with pytest
run: |
pytest --cov hun_date_parser
Expand Down
2 changes: 1 addition & 1 deletion hun_date_parser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

__all__ = ["DatetimeTextualizer", "DatetimeExtractor", "datetime2text", "text2datetime", "text2date", "text2time"]

__version__ = "0.2.6"
__version__ = "0.2.7"
3 changes: 3 additions & 0 deletions hun_date_parser/date_parser/date_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ def match_iso_date(s: str,
for group in match:
group = [int(m.lstrip('0')) for m in group if m.lstrip('0')]

if not group:
continue

if realistic_year_restriction and not is_year_realistic(group[0]):
continue

Expand Down
15 changes: 13 additions & 2 deletions hun_date_parser/date_parser/datetime_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ def text2date(input_sentence: str, now: datetime = datetime.now(),
:return: list of date interval dictionaries
"""
datetime_extractor = DatetimeExtractor(now=now, output_container='date',
search_scope=search_scope, realistic_year_required=realistic_year_required)
search_scope=search_scope,
realistic_year_required=realistic_year_required)
return datetime_extractor.parse_datetime(sentence=input_sentence)


Expand All @@ -77,7 +78,8 @@ def text2time(input_sentence: str, now: datetime = datetime.now(),
:return: list of time interval dictionaries
"""
datetime_extractor = DatetimeExtractor(now=now, output_container='time',
search_scope=search_scope, realistic_year_required=realistic_year_required)
search_scope=search_scope,
realistic_year_required=realistic_year_required)
return datetime_extractor.parse_datetime(sentence=input_sentence)


Expand Down Expand Up @@ -345,6 +347,15 @@ def assemble_datetime(self, now: datetime,
return None

def parse_datetime(self, sentence: str) -> List[Dict[str, datelike]]:
"""
Fail-safe wrapper around _parse_datetime. All possible exceptions will be caught and an empty list is returned.
"""
try:
return self._parse_datetime(sentence)
except:
return []

def _parse_datetime(self, sentence: str) -> List[Dict[str, datelike]]:
"""
Extracts list of datetime intervals from input sentence.
:param sentence: Input sentence string.
Expand Down
2 changes: 2 additions & 0 deletions test/test_date_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@
('8000 forint', [], SearchScopes.NOT_RESTRICTED, True),
('8000', [[Year(8000, 'match_iso_date')]], SearchScopes.PAST_SEARCH, False),
('8000 forint', [], SearchScopes.PAST_SEARCH, False),
('0000', [], SearchScopes.NOT_RESTRICTED, False),
('0000', [], SearchScopes.NOT_RESTRICTED, True),
]

tf_weekday = [
Expand Down

0 comments on commit 5fbd132

Please sign in to comment.