From 624cf5fce2332811afe8e57df2090b3791b71775 Mon Sep 17 00:00:00 2001 From: Brendan Haines Date: Mon, 20 Sep 2021 21:49:05 -0600 Subject: [PATCH] clean up and avoid setting a copy warnings --- scripting/plugins/BomGeneration/jlcparts.py | 94 +++++++++++++++------ 1 file changed, 68 insertions(+), 26 deletions(-) diff --git a/scripting/plugins/BomGeneration/jlcparts.py b/scripting/plugins/BomGeneration/jlcparts.py index b755fcf..d339da1 100644 --- a/scripting/plugins/BomGeneration/jlcparts.py +++ b/scripting/plugins/BomGeneration/jlcparts.py @@ -11,6 +11,7 @@ parser = argparse.ArgumentParser() parser.add_argument("input", nargs="?", default=None) args = parser.parse_args() + def mlcc_code2specs(code): specs = {} code = code.lower() @@ -21,9 +22,30 @@ def mlcc_code2specs(code): specs["tolerance"] = 0 else: # type 2 - specs["temperature_min"] = {"x": -55, "y": -30, "z": +10}[code[0]] - specs["temperature_max"] = {2: +45, 4: +65, 5: +85, 6: +105, 7: +125}[int(code[1])] - specs["tolerance"] = {"d":0.033, "e":0.047, "f":0.075, "p":0.10, "r":0.15, "s":0.22, "t":0.33, "u":0.56, "v":0.82}[code[2]] + specs["temperature_min"] = { + "x": -55, + "y": -30, + "z": +10, + }[code[0]] + specs["temperature_max"] = { + 2: +45, + 4: +65, + 5: +85, + 6: +105, + 7: +125, + }[int(code[1])] + specs["tolerance"] = { + "d": 0.033, + "e": 0.047, + "f": 0.075, + "p": 0.10, + "r": 0.15, + "s": 0.22, + "t": 0.33, + "u": 0.56, + "v": 0.82, + }[code[2]] + footprints = { "0402": (2, ["0402", "R0402", "C0402"]), @@ -37,47 +59,67 @@ footprints = { # TODO: remove hardcoded csv -args.input = "/home/brendan/Documents/tempsync/projects/0034_UniversalController/jlcparts.csv" +args.input = ( + "/home/brendan/Documents/tempsync/projects/0034_UniversalController/jlcparts.csv" +) if args.input: - parts_csv = pd.read_csv(args.input, encoding_errors="replace", delimiter=",", index_col=False) + parts_csv = pd.read_csv( + args.input, encoding_errors="replace", delimiter=",", index_col=False + ) else: raise NotImplementedError() logger.info("Parsing JLCPCB stock...") -stock = parts_csv[parts_csv["Stock"]>0] +stock = parts_csv[parts_csv["Stock"] > 0] # NOTES: # - if something has tolerance specified as absolute, "Value" may be wrong but "Tolerance" will be NA so it will be dropped # resistors -resistors = stock[stock["First Category"] == "Resistors"] -resistors.loc[:, "Value"] = resistors["Description"].str.extract(r"(\d+(\.\d+)?)��").astype(float).iloc[:,0] -resistors.loc[:, "Tolerance"] = resistors["Description"].str.extract(r"(\d+)%").astype(float) / 100 -resistors.loc[:, "TempCoef"] = resistors["Description"].str.extract(r"(\d+)ppm/��").astype(float) / 1e6 +resistors = stock[stock["First Category"] == "Resistors"].copy() +resistors["Value"] = ( + resistors["Description"].str.extract(r"(\d+(\.\d+)?)��").astype(float).iloc[:, 0] +) +resistors["Tolerance"] = ( + resistors["Description"].str.extract(r"(\d+)%").astype(float) / 100 +) +resistors["TempCoef"] = ( + resistors["Description"].str.extract(r"(\d+)ppm/��").astype(float) / 1e6 +) power_frac = resistors["Description"].str.extract(r"(\d+)/(\d+)W").astype(float) -power_frac = power_frac.iloc[:,0] / power_frac.iloc[:,1] -power_dec = resistors["Description"].str.extract(r"(\d+(\.\d+))W").astype(float).iloc[:,0] +power_frac = power_frac.iloc[:, 0] / power_frac.iloc[:, 1] +power_dec = ( + resistors["Description"].str.extract(r"(\d+(\.\d+))W").astype(float).iloc[:, 0] +) power = power_frac.add(power_dec, fill_value=0) power[power_frac.notnull() & power_dec.notnull()] = pd.NA -resistors.loc[:, "Power"] = power -resistors.loc[:, "Footprint"] = pd.NA -for fp, (pins, name) in footprints.items(): +resistors["Power"] = power +resistors["Footprint"] = pd.NA +for fp, (pins, names) in footprints.items(): matches = False - for n in name: - matches = matches | (resistors["Package"] == n) + for name in names: + matches = matches | (resistors["Package"] == name) matches = matches & (resistors["Solder Joint"] == pins) - resistors["Footprint"][matches] = fp + resistors.loc[matches, "Footprint"] = fp # resistors.drop("Package", inplace=True) resistors.dropna(inplace=True) +print(len(resistors)) + # capacitors -capacitors = stock[stock["First Category"] == "Capacitors"] +capacitors = stock[stock["First Category"] == "Capacitors"].copy() value = capacitors["Description"].str.extract(r"(\d+(\.\d+)?)([pnum]?)F") -value = value.iloc[:,0].astype(float) * value.iloc[:,2].replace(["p", "n", "u", "m", ""], [1e-12, 1e-9, 1e-6, 1e-3, 1]).astype(float) -capacitors.loc[:, "Value"] = value -capacitors.loc[:, "Tolerance"] = capacitors["Description"].str.extract(r"(\d+)%").astype(float) / 100 -capacitors.loc[:, "Voltage"] = capacitors["Description"].str.extract(r"(\d+(\.\d+)?)V").astype(float).iloc[:,0] +value = value.iloc[:, 0].astype(float) * value.iloc[:, 2].replace( + ["p", "n", "u", "m", ""], [1e-12, 1e-9, 1e-6, 1e-3, 1] +).astype(float) +capacitors["Value"] = value +capacitors["Tolerance"] = ( + capacitors["Description"].str.extract(r"(\d+)%").astype(float) / 100 +) +capacitors["Voltage"] = ( + capacitors["Description"].str.extract(r"(\d+(\.\d+)?)V").astype(float).iloc[:, 0] +) # TODO: "MLCC"/"Tantalum" code_np0 = capacitors["Description"].str.contains(r"NP0") code_t1 = capacitors["Description"].str.extract(r"([CBLAMPRSTVU][01234678][GHJKLMN])") @@ -92,9 +134,9 @@ for fp, (pins, name) in footprints.items(): for n in name: matches = matches | (capacitors["Package"] == n) matches = matches & (capacitors["Solder Joint"] == pins) - capacitors["Footprint"][matches] = fp + capacitors.loc[matches, "Footprint"] = fp # capacitors.drop("Package", inplace=True) capacitors.dropna(inplace=True) - -pass \ No newline at end of file +print(len(capacitors)) +pass