From d484f83e45a30ebbda746793954245655eb3d987 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Thu, 28 Feb 2019 08:03:02 +0100 Subject: o Better code structure for normalization. --- src/ee/digikey/normalize_facts.py | 100 ++++++++++++++++++++------------------ 1 file changed, 54 insertions(+), 46 deletions(-) (limited to 'src/ee/digikey') diff --git a/src/ee/digikey/normalize_facts.py b/src/ee/digikey/normalize_facts.py index 1ddf78c..62aef0d 100644 --- a/src/ee/digikey/normalize_facts.py +++ b/src/ee/digikey/normalize_facts.py @@ -1,6 +1,6 @@ import re from pathlib import Path -from typing import List, Tuple, Union +from typing import List, Tuple, Union, Mapping, Callable, Any from ee import EeVal, EeException from ee.part import PartDb, load_db, save_db @@ -65,10 +65,52 @@ def handle_tolerance(tolerance: bomFile.Fact, capacitance: bomFile.Fact) -> List return [bomFile.Fact(key=uris.make_fact_key(key), label=label, value=str(value)) for key, label, value in facts] +def re_parser(pattern, kis: List[Tuple[str, int]]): + r = re.compile(pattern) + + def parse(value): + m = r.match(value) + + if not m: + return + + return [(k, m.group(i)) for k, i in kis] + + return parse + + +def no_parser(key): + def parse(value): + return key, value + + return parse + + +def ee_val_parser(key): + def parse(value): + return key, EeVal.parse(value) + + return parse + + _operating_temperature_re = re.compile("-([0-9]+)°C ~ ([0-9])+°C") _height_seated_re = re.compile("([0-9]+\\.[0-9]+)\" \\(([0-9]+\\.[0-9]+)mm\\)") _land_size_re = re.compile("([0-9]+\\.[0-9]+)\" L x ([0-9]+\\.[0-9]+)\" W " "\\(([0-9]+\\.[0-9]+)mm x ([0-9]+\\.[0-9]+)mm\\)") # 0.260" L x 0.260" W (6.60mm x 6.60mm) +parsers: Mapping[int, Callable[[str], Any]] = { + 3: no_parser("tolerance"), + 1471: ee_val_parser("voltage-input-min"), # Voltage - Input (Min) + 573: ee_val_parser("voltage-input-max"), # Voltage - Input (Max) + 1429: ee_val_parser("voltage-output-max"), # Voltage - Output (Max) + 252: re_parser(_operating_temperature_re, [("temperature-min", 1), ("temperature-max", 2)]), + 1686: re_parser(_operating_temperature_re, [("temperature-junction-min", 1), ("temperature-junction-max", 2)]), + + 1500: re_parser(_height_seated_re, [("part-height", 2)]), + 884: re_parser(_land_size_re, [("land-size-x", 3), ("land-size-y", 4)]), + + 2049: ee_val_parser("capacitance"), + 2079: ee_val_parser("voltage-rating"), +} def normalize_facts(in_dir: Path, out_dir: Path): @@ -88,57 +130,23 @@ def normalize_facts(in_dir: Path, out_dir: Path): for f in in_facts: key = uris.parse_digikey_fact_key(f.keyProp) - out_fact = None - value = f.valueProp if value == "-": continue - try: - if key == 3: - out_fact = bomFile.Fact(key=uris.make_fact_key("tolerance"), label="Tolerance", value=value) - elif key == 252: - m = _operating_temperature_re.match(value) - if m: - mn = m.group(1) - mx = m.group(2) - - out_facts.extend([ - bomFile.Fact(key=uris.make_fact_key("temperature-min"), label="Min temperature", value=mn), - bomFile.Fact(key=uris.make_fact_key("temperature-max"), label="Max temperature", value=mx)]) - elif key == 1500: - m = _height_seated_re.match(value) - - if m: - out_fact = bomFile.Fact(key=uris.make_fact_key("part-height"), label="Height", value=m.group(2)) + parser = parsers.get(key) + + if parser is not None: + res = parser(value) + + if res is not None: + if isinstance(res, list): + results = res else: - print("{}: bad str: '{}'".format(key, value)) - elif key == 884: - m = _land_size_re.match(value) - - if m: - out_facts.extend([ - bomFile.Fact(key=uris.make_fact_key("land-size-x"), - label="Land size, X", - value=m.group(3)), - bomFile.Fact(key=uris.make_fact_key("land-size-y"), - label="Land size, Y", - value=m.group(4))]) - elif key == 2049: - val = EeVal.parse(value) - out_fact = bomFile.Fact(key=uris.make_fact_key("capacitance"), label="Capacitance", value=str(val)) - elif key == 2079: - val = EeVal.parse(value) - out_fact = bomFile.Fact(key=uris.make_fact_key("voltage-rating"), label="Voltage rating", - value=str(val)) - else: - pass # fact ignored - except EeException: - pass - - if out_fact: - out_facts.append(out_fact) + results = [res] + facts = [bomFile.Fact(key=uris.make_fact_key(key), value=value) for key, value in results] + out_facts.extend(facts) if len(out_facts) == 0: continue -- cgit v1.2.3