refactor: Implement PR feedback

This commit is contained in:
TrisNol 2023-10-15 12:05:25 +02:00
parent 15ace5382d
commit eba5235dff
2 changed files with 13 additions and 7 deletions

View File

@ -377,19 +377,21 @@ def extract_date_from_string(value: str) -> str | None:
str | None: Date in ISO format, None if not found str | None: Date in ISO format, None if not found
""" """
date_regex = [ # type: ignore date_regex = [ # type: ignore
{"regex": r"\d{1,2}\.\d{1,2}\.(19|20)?\d{2}", "mapper": transform_date_to_iso}, {"regex": r"\d{1,2}\.\d{1,2}\.\d{4}", "mapper": transform_date_to_iso},
{"regex": r"\d{4}-\d{1,2}-\d{1,2}", "mapper": None},
{"regex": r"(20|19)\d{2}-\d{1,2}-\d{1,2}", "mapper": None},
] ]
results = []
for regex in date_regex: for regex in date_regex:
result = re.findall(regex["regex"], value) # type: ignore result = re.findall(regex["regex"], value) # type: ignore
if len(result) == 1: if len(result) == 1:
relevant_data = result[0] relevant_data = result[0]
if regex["mapper"] is not None: # type: ignore if regex["mapper"] is not None: # type: ignore
return regex["mapper"](relevant_data) # type: ignore results.append(regex["mapper"](relevant_data)) # type: ignore
return relevant_data else:
return None results.append(relevant_data)
if len(results) != 1:
return None
return results[0]
def map_founding_date(data: dict) -> str | None: def map_founding_date(data: dict) -> str | None:

View File

@ -612,6 +612,10 @@ def test_map_business_purpose_no_result() -> None:
("Gesellschaftsvertrag vom 06.04.2016 Hallo Welt", "2016-04-06"), ("Gesellschaftsvertrag vom 06.04.2016 Hallo Welt", "2016-04-06"),
("Str. des Tests vom 1999-04-05", "1999-04-05"), ("Str. des Tests vom 1999-04-05", "1999-04-05"),
("Once upon a midnight dreary while I pondered weak and weary...", None), ("Once upon a midnight dreary while I pondered weak and weary...", None),
(
"This company was first founded in 2016-06-10 and then again on 1.5.2004",
None,
),
], ],
) )
def test_extract_date_from_string(value: str, expected_result: str) -> None: def test_extract_date_from_string(value: str, expected_result: str) -> None: