From ff34feba0e3cb667dedf177553795565b6fee924 Mon Sep 17 00:00:00 2001 From: atarasov Date: Thu, 12 Mar 2026 18:11:26 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9E=D0=B1=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=BA=D0=B0=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BE=D0=BA,=20=D0=BF?= =?UTF-8?q?=D0=BE=D0=BA=D0=B0=D0=B7=D0=B0=D0=BD=D1=8B=20=D0=BF=D1=80=D0=BE?= =?UTF-8?q?=D0=B1=D0=BB=D0=B5=D0=BC=D0=BD=D1=8B=D0=B5=20=D0=B7=D0=B0=D0=BF?= =?UTF-8?q?=D0=B8=D1=81=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lists/01-example-short.txt | 1 + parse.py | 16 ++++++----- res/01-example-short.json | 55 +++++++++++++++++++++++++++++++++++++- 3 files changed, 65 insertions(+), 7 deletions(-) diff --git a/lists/01-example-short.txt b/lists/01-example-short.txt index 98f2988..79d4e86 100644 --- a/lists/01-example-short.txt +++ b/lists/01-example-short.txt @@ -1,3 +1,4 @@ +CHEVROLET Tacuma 1.6 [A16DMS] 09.2000- DAEWOO Rezzo/Tacuma 1.6 [A16DMS] 09.2000- \n DAEWOO Espero 1.5 16V [A15MF] 02.1995- HYUNDAI Accent 1.3 [G4E-A] 08.1999- рандомный \n text HYUNDAI Accent 1.3 [G4EH] 08.2002- вфы вфы HYUNDAI Accent 1.4 GL 11.2005- HYUNDAI Accent 1.5 [G4EB] 08.2002- HYUNDAI Accent 1.6 \ No newline at end of file diff --git a/parse.py b/parse.py index c853ec5..bfa3d67 100644 --- a/parse.py +++ b/parse.py @@ -43,13 +43,17 @@ def prepare_records(content: str) -> list[str]: return [r.strip() for r in content.split(RECORD_DELIMITERS[0]) if r.strip()] -def _normalize_obj(obj: dict) -> dict: - return { +def _normalize_obj(obj: dict, original: str) -> dict: + result = { "brand": obj.get("brand", ""), "model": obj.get("model", ""), "engine_code": obj.get("engine_code", ""), "date_range": obj.get("date_range", ""), } + if not result["engine_code"].strip(): + result["has_error"] = True + result["original"] = original.replace("\t", " ") + return result def extract_record(client: Client, record: str) -> list[dict]: @@ -71,19 +75,19 @@ def extract_record(client: Client, record: str) -> list[dict]: result = result.strip()[4:].strip() parsed = json.loads(result) if isinstance(parsed, list): - return [_normalize_obj(o) for o in parsed] - return [_normalize_obj(parsed)] + return [_normalize_obj(o, record) for o in parsed] + return [_normalize_obj(parsed, record)] except (json.JSONDecodeError, KeyError): if attempt < RETRY_COUNT: time.sleep(RETRY_DELAY) else: - return [{"brand": "", "model": "", "engine_code": "", "date_range": record}] + return [{"brand": "", "model": "", "engine_code": "", "date_range": record, "has_error": True, "original": record.replace("\t", " ")}] except Exception: if attempt < RETRY_COUNT: time.sleep(RETRY_DELAY) else: raise - return [{"brand": "", "model": "", "engine_code": "", "date_range": record}] + return [{"brand": "", "model": "", "engine_code": "", "date_range": record, "has_error": True, "original": record.replace("\t", " ")}] def process_file(client: Client, filepath: pathlib.Path) -> bool: diff --git a/res/01-example-short.json b/res/01-example-short.json index 0637a08..468910f 100644 --- a/res/01-example-short.json +++ b/res/01-example-short.json @@ -1 +1,54 @@ -[] \ No newline at end of file +[ + { + "brand": "CHEVROLET", + "model": "Tacuma 1.6", + "engine_code": "[A16DMS]", + "date_range": "09.2000-" + }, + { + "brand": "DAEWOO", + "model": "Rezzo/Tacuma 1.6", + "engine_code": "[A16DMS]", + "date_range": "09.2000-" + }, + { + "brand": "DAEWOO", + "model": "Espero 1.5 16V", + "engine_code": "[A15MF]", + "date_range": "02.1995-" + }, + { + "brand": "HYUNDAI", + "model": "Accent 1.3", + "engine_code": "[G4E-A]", + "date_range": "08.1999- рандомный" + }, + { + "brand": "HYUNDAI", + "model": "Accent 1.3", + "engine_code": "[G4EH]", + "date_range": "08.2002-" + }, + { + "brand": "HYUNDAI", + "model": "Accent 1.4 GL", + "engine_code": "", + "date_range": "11.2005-", + "has_error": true, + "original": "HYUNDAI Accent 1.3 [G4E-A] 08.1999- рандомный \\n text HYUNDAI Accent 1.3 [G4EH] 08.2002- вфы вфы HYUNDAI Accent 1.4 GL 11.2005-" + }, + { + "brand": "HYUNDAI", + "model": "Accent 1.5", + "engine_code": "[G4EB]", + "date_range": "08.2002-" + }, + { + "brand": "HYUNDAI", + "model": "Accent", + "engine_code": "", + "date_range": "", + "has_error": true, + "original": "HYUNDAI Accent 1.6" + } +] \ No newline at end of file