def merge(self): self.group() self.count() for category in self.categories: if category in self.table: for n, item in enumerate(self.table[category]): self.table[category][n][self.TABLE_DESIGNATOR] = \ lib.order_designator(item[self.TABLE_DESIGNATOR], self.logger) # Convert all designator in a number to be ordered if category in ["R", "C", "L", "Y"]: for m in self.table[category]: m[self.TABLE_COMMENT] = lib.value_toFloat( m[self.TABLE_COMMENT], category, self.logger) # print m[COMMENT], key self.table[category] = sorted( self.table[category], key=lambda x: x[ self.TABLE_COMMENT]) # Convert all ORDERED designator in a numeric format if category in ["R", "C", "L", "Y"]: for m in self.table[category]: m[self.TABLE_COMMENT] = lib.value_toStr( m[self.TABLE_COMMENT], self.logger) # print m[self.TABLE_COMMENT], category return self.table
def test_valueToFloat(self): test = [ ("C", ("100pF", )), ("R", ("1R1", "1R2", "0R3", "1R8")), ("R", ("1.1R", "1.2R", "0.3R", "1.8R")), ("R", ("1k", "1k5", "1", "10R", "1R2", "2.2k", "0.3")), ("C", ("0.1uF", "100nF", "1F", "10pF", "2.2uF", "47uF", "1uF")), ("L", ("1nH", "1H", "10pH", "2.2uH", "47uH")), ("R", ("68ohm", "1.2R", "3.33R", "0.12R", "1.234R", "0.33R", "33nohm")), ("Y", ("32.768kHz", "1MHz", "12.134kHz")), ("L", ("4mH", "4.7mH", "100uH")), ("R", ("33nohm", )), ("R", ("2k31", "10k12", "5K421", "4R123", "1M12")), ] checkv1 = [ ((1e-10, "F"), ), ((0.3, "ohm"), (1.1, "ohm"), (1.2, "ohm"), (1.8, "ohm")), ((0.3, "ohm"), (1.1, "ohm"), (1.2, "ohm"), (1.8, "ohm")), ((0.3, "ohm"), (1.0, "ohm"), (1.2, "ohm"), (10.0, "ohm"), (1000.0, "ohm"), (1500.0, "ohm"), (2200.0, "ohm")), ((10e-12, "F"), (100e-9, "F"), (0.1e-6, "F"), (1e-6, "F"), (2.2e-6, "F"), (47e-6, "F"), (1.0, "F")), ((10e-12, "H"), (1e-9, "H"), (2.2e-6, "H"), (47e-6, "H"), (1.0, "H")), ((3.3e-8, "ohm"), (0.12, "ohm"), (0.33, "ohm"), (1.2, "ohm"), (1.234, "ohm"), (3.33, "ohm"), (68, "ohm")), ((12134.0, "Hz"), (32768.0, "Hz"), (1e6, "Hz")), ((100e-6, "H"), (4e-3, "H"), (0.0047, "H")), ((33e-9, "ohm"), ), ((4.123, "ohm"), (2310.0, "ohm"), (5421.0, "ohm"), (10120.0, "ohm"), (1120000.0, "ohm")), ] print for k, m in enumerate(test): l = [] for mm in m[1]: a, b, _ = lib.value_toFloat(mm, m[0], self.logger) l.append((a, b)) l = sorted(l, key=lambda x: x[0]) for n, i in enumerate(l): print i, "->", checkv1[k][n] self.assertTrue(abs(i[0] - checkv1[k][n][0]) < 10e-9) self.assertEqual(i[1], checkv1[k][n][1]) print "-" * 80
def test_valueToFloat(self): test = [ ("C", ("100pF", )), ("R", ("1R1", "1R2", "0R3", "1R8")), ("R", ("1.1R", "1.2R", "0.3R", "1.8R")), ("R", ("1k", "1k5", "1", "10R", "1R2", "2.2k", "0.3")), ("C", ("0.1uF", "100nF", "1F", "10pF", "2.2uF", "47uF", "1uF")), ("L", ("1nH", "1H", "10pH", "2.2uH", "47uH")), ("R", ("68ohm", "1.2R", "3.33R", "0.12R", "1.234R", "0.33R", "33nohm")), ("Y", ("32.768kHz", "1MHz", "12.134kHz")), ("L", ("4mH", "4.7mH", "100uH")), ("R", ("33nohm",)), ("R", ("2k31", "10k12", "5K421", "4R123", "1M12")), ] checkv1 = [ ((1e-10, "F"),), ((0.3, "ohm"), (1.1, "ohm"), (1.2, "ohm"), (1.8, "ohm")), ((0.3, "ohm"), (1.1, "ohm"), (1.2, "ohm"), (1.8, "ohm")), ((0.3, "ohm"), (1.0, "ohm"), (1.2, "ohm"), (10.0, "ohm"), (1000.0, "ohm"), (1500.0, "ohm"), (2200.0, "ohm")), ((10e-12, "F"), (100e-9, "F"), (0.1e-6, "F"), (1e-6, "F"), (2.2e-6, "F"), (47e-6, "F"), (1.0, "F")), ((10e-12, "H"), (1e-9, "H"), (2.2e-6, "H"), (47e-6, "H"), (1.0, "H")), ((3.3e-8, "ohm"), (0.12, "ohm"), (0.33, "ohm"), (1.2, "ohm"), (1.234, "ohm"), (3.33, "ohm"), (68, "ohm")), ((12134.0, "Hz"), (32768.0, "Hz"), (1e6, "Hz")), ((100e-6, "H"), (4e-3, "H"), (0.0047, "H")), ((33e-9, "ohm"),), ((4.123, "ohm"), (2310.0, "ohm"), (5421.0, "ohm"), (10120.0, "ohm"), (1120000.0, "ohm")), ] print for k, m in enumerate(test): l = [] for mm in m[1]: a, b, _ = lib.value_toFloat(mm, m[0], self.logger) l.append((a, b)) l = sorted(l, key=lambda x: x[0]) for n, i in enumerate(l): print i, "->", checkv1[k][n] self.assertTrue(abs(i[0] - checkv1[k][n][0]) < 10e-9) self.assertEqual(i[1], checkv1[k][n][1]) print "-" * 80
def merge(self): self.group() self.count() for category in self.categories: if category in self.table: for n, item in enumerate(self.table[category]): self.table[category][n][self.TABLE_DESIGNATOR] = \ lib.order_designator(item[self.TABLE_DESIGNATOR], self.logger) # Convert all designator in a number to be ordered if category in ["R", "C", "L", "Y"]: d = {} for item in self.table[category]: if item[self.TABLE_FOOTPRINT] in d: d[item[self.TABLE_FOOTPRINT]].append(item) else: d[item[self.TABLE_FOOTPRINT]] = [item] for x in d.values(): self.table[category] = x for m in self.table[category]: m[self.TABLE_COMMENT] = lib.value_toFloat( m[self.TABLE_COMMENT], category, self.logger) self.table[category] = sorted( self.table[category], key=lambda x: x[self.TABLE_COMMENT]) # Convert all ORDERED designator in a numeric format for m in self.table[category]: m[self.TABLE_COMMENT] = lib.value_toStr( m[self.TABLE_COMMENT], self.logger) x = self.table[category] self.table[category] = [] for x in d.values(): for y in x: self.table[category].append(y) return self.table
def count(self): """ grouped items format 'U': ['bom_due.xls', 1, u'U3000', u'Lan transformer', u'LAN TRANFORMER WE 7490100111A', u'LAN_TR_1.27MM_SMD'] 'U': ['test.xlsx', 1, u'U2015', u'DC/DC switch converter', u'LM22671MR-ADJ', u'SOIC8_PAD'] 'U': ['test.xlsx', 1, u'U2002', u'Dual Low-Power Operational Amplifier', u'LM2902', u'SOIC14'] 'U': ['bom_uno.xls', 1, u'U7', u'Temperature sensor', u'LM75BIM', u'SOIC8'] """ self.table = {} # Finally Table layout self.TABLE_TOTALQTY = 0 self.TABLE_DESIGNATOR = len(self.files) + 1 self.TABLE_COMMENT = self.TABLE_DESIGNATOR + 1 self.TABLE_FOOTPRINT = self.TABLE_COMMENT + 1 self.TABLE_DESCRIPTION = self.TABLE_FOOTPRINT + 1 self.stats['total'] = 0 for category in self.categories: if category in self.grouped_items: tmp = {} self.stats[category] = 0 for item in self.grouped_items[category]: # Fix Designator if category in ["R", "C", "L", "Y"]: tmp_comment = lib.value_toFloat(item[COMMENT], category, self.logger) item[COMMENT] = lib.value_toStr(tmp_comment, self.logger) # Fix Not poluate string in list for rexp in cfg.NOT_POPULATE_KEY: item[COMMENT] = re.sub(rexp, 'NP ', item[COMMENT]) if category == 'J': # Avoid merging for NP componets skip_merge = False m = re.findall(cfg.NP_REGEXP, item[COMMENT]) if m: skip_merge = True self.logger.error( "Not Populate connector, leave unmerged..[%s] [%s] match%s\n" % (item[COMMENT], item[DESIGNATOR], m)) item[COMMENT] = "NP Connector" if skip_merge: key = item[DESCRIPTION] + \ item[COMMENT] + item[FOOTPRINT] else: key = item[DESCRIPTION] + item[FOOTPRINT] item[COMMENT] = "Connector" self.logger.warning("Merged key: %s (%s)\n" % (key, item[COMMENT])) if category == 'D' and "LED" in item[FOOTPRINT]: key = item[DESCRIPTION] + item[FOOTPRINT] item[COMMENT] = "LED" self.logger.warning("Merged key: %s (%s)\n" % (key, item[COMMENT])) if category == 'S' and "TACTILE" in item[FOOTPRINT]: key = item[DESCRIPTION] + item[FOOTPRINT] item[COMMENT] = "Tactile Switch" self.logger.warning("Merged key: %s (%s)\n" % (key, item[COMMENT])) elif category == 'U' and re.findall("rele|relay", item[DESCRIPTION].lower()): key = item[DESCRIPTION] + item[FOOTPRINT] item[COMMENT] = u"Relay, Rele'" self.logger.warning("Merged key: %s (%s)\n" % (key, item[COMMENT])) else: key = item[DESCRIPTION] + \ item[COMMENT] + item[FOOTPRINT] # print key # print "<<", item[DESIGNATOR] self.stats[category] += 1 self.stats['total'] += 1 # First colum is total Qty curr_file_index = self.files[ item[FILENAME]] + self.TABLE_TOTALQTY + 1 if key in tmp: tmp[key][self.TABLE_TOTALQTY] += item[QUANTITY] tmp[key][curr_file_index] += item[QUANTITY] tmp[key][ self.TABLE_DESIGNATOR] += ", " + item[DESIGNATOR] tmp[key][self.TABLE_DESIGNATOR] = lib.order_designator( tmp[key][self.TABLE_DESIGNATOR], self.logger) for ex in self.extra_column: # We add 1 because the table now contain also the # file name, see init function in code that import # data. col_id = ex[1] + 1 try: raw_value = item[col_id].strip() if tmp[key][col_id] == "": tmp[key][col_id] = raw_value else: if raw_value != "": words = re.findall(r'\b\S+\b', tmp[key][col_id]) if not raw_value in words: tmp[key][col_id] += "; " + raw_value except IndexError: print "Error! Impossible to update extra column. This as bug!" sys.exit(1) else: row = [item[QUANTITY]] + \ [0] * len(self.files) + \ [ item[DESIGNATOR], item[COMMENT], item[FOOTPRINT], item[DESCRIPTION] ] row[curr_file_index] = item[QUANTITY] # Add extra column to row if they are present in source # file for ex in self.extra_column: # We add 1 because the table now contain also the # file name, see init function in code that import # data. row.append(item[ex[1] + 1]) tmp[key] = row # print "NEW", tmp[key], curr_file_index, # item[FILENAME] self.table[category] = tmp.values()