def validateParameters(part, resolved): for field in fields_to_check: if field in resolved: if field in ['Capacitance']: if capacitor.convert_capacitance_co_farads( part[field] ) != capacitor.convert_capacitance_co_farads( resolved[field]): print(resolved) print( str(field) + ' ' + str(part[field]) + ' ' + resolved[field]) return False elif field in ['Resistance']: if resistor.convert_resistance_to_ohms( part[field] ) != resistor.convert_resistance_to_ohms(resolved[field]): print(resolved) print( str(field) + ' ' + str(part[field]) + ' ' + resolved[field]) return False elif field in ['Voltage']: if part[field].replace( 'VDC', 'V') != resolved[field].replace('VDC', 'V'): print(resolved) print( str(field) + ' ' + str(part[field]) + ' ' + resolved[field]) return False elif part[field] != resolved[field]: print(resolved) print(str(field)) return False return True
def isSame(resA, resB): if resistor.convert_resistance_to_ohms(resA['Resistance']) != resistor.convert_resistance_to_ohms(resB['Resistance']): return False if resA['Case'] != resB['Case']: return False if resA['Manufacturer'] != resB['Manufacturer']: return False if resA['Manufacturer Part Number'] != resB['Manufacturer Part Number']: return False return True
def build_products_data(self, product_list): def get_component_by_symbol(components, symbol): for component in components: if component['Symbol']['SymbolTME'] == symbol: return component print("Unable to found component: " + symbol) components = [] tme_symbols = [] for product in product_list: part = decode_product_dictionary(product) tme_symbols.append(part['Symbol']['SymbolTME']) components.append(part) tme_symbols_chunks = list(split_list(tme_symbols, 50)) for tme_symbols in tme_symbols_chunks: print('requesting stock and price: ' + str(tme_symbols)) stock = self.get_stock_and_prices(tme_symbols) for stock_data in stock: component = get_component_by_symbol(components, stock_data['Symbol']) component['OrderInfo']['StockCount'] = stock_data['StockCount'] component['PriceRanges'] = stock_data['PriceList'] for tme_symbols in tme_symbols_chunks: print('requesting parameters for components: ' + str(tme_symbols)) parameters = self.tme.get_parameters(tme_symbols) for product in parameters: component = get_component_by_symbol(components, product['Symbol']) for parameter in product['ParameterList']: if parameter['ParameterName'] == 'Voltage': component['Parameters'][parameter[ 'ParameterName']] = voltage.string_to_voltage( parameter["ParameterValue"]) elif parameter['ParameterName'] == 'Capacitance': component['Parameters'][parameter[ 'ParameterName']] = capacitor.convert_capacitance_co_farads( parameter["ParameterValue"]) elif parameter['ParameterName'] == 'Resistance': component['Parameters'][parameter[ 'ParameterName']] = resistor.convert_resistance_to_ohms( parameter["ParameterValue"]) elif parameter['ParameterName'] == 'Tolerance': component['Parameters'][parameter[ 'ParameterName']] = tolerance.string_to_tolerance( parameter["ParameterValue"]) else: component['Parameters'][parameter[ 'ParameterName']] = parameter["ParameterValue"] return components
def get_resistance_id(resistance, parameters): """ :param resistance: resistance in Ohms, type: Decimal :param parameters: :return: """ for parameter in parameters: if parameter['Name'] == 'Resistance': for value in parameter['Values']: param = resistor.convert_resistance_to_ohms(value['ValueName']) expected = resistance if param == None or expected == None: print(value['ValueName']) print(resistance) raise RuntimeError("Unable to convert: " + value['ValueName']) if param == expected: return { 'parameter_id': parameter['ParameterId'], 'value_ids': value['ValueId'] }
def resistors_key(x): if x['Resistance'] == 'DNR': return -1 if x['Resistance'] is not None: return resistor.convert_resistance_to_ohms(x['Resistance']) return 0
def find_component(components_group, group, tme_config): def to_string(case): if case: return case return 'None' from distributor_connector import tme from distributor_connector import partkeepr distributors = [ tme.TME(tme_config['token'], tme_config['app_secret']), partkeepr.Partkeepr("https://partkeepr.locallan", "auto", "auto") ] for shop in distributors: for component in components_group: if 'Manufacturer Part Number' in component and component[ 'Manufacturer Part Number'] != "": print("Request for " + component['Manufacturer Part Number']) found = shop.find_component( component['Manufacturer Part Number']) else: if group == "Capacitors": print("Request for " + to_string(component['Capacitance']) + " " + to_string(component['Voltage']) + ' ' + to_string(component['Case'])) try: capacitor_parameters = { 'Capacitance': capacitor.convert_capacitance_co_farads( component['Capacitance']), 'Case': to_string(component['Case']), 'Voltage': voltage.string_to_voltage(component['Voltage']), 'Dielectric Type': component['Dielectric Type'] } found = shop.find_capacitor_by_parameters( capacitor_parameters) if found: for part in found: if 'Voltage' in part['Parameters']: part['Parameters'][ 'Voltage'] = voltage.volts_to_string( part['Parameters']['Voltage']) if 'Capacitance' in part['Parameters']: part['Parameters'][ 'Capacitance'] = capacitor.farads_to_string( part['Parameters']['Capacitance']) except InvalidOperation: print(component) raise elif group == "Resistors": print("Request for " + to_string(component['Resistance']) + ' ' + to_string(component['Case']) + ' ' + to_string(component['Tolerance'])) if component['Resistance'] is None: print("Skipping...") component["Distributors"] = [] continue try: resistor_parameters = { 'Resistance': resistor.convert_resistance_to_ohms( component['Resistance']), 'Case': component['Case'], 'Tolerance': tolerance.string_to_tolerance( component['Tolerance']) } found = shop.find_resistor_by_parameters( resistor_parameters) if found: for part in found: if 'Resistance' in part['Parameters']: part['Parameters'][ 'Resistance'] = resistor.ohms_to_string( part['Parameters']['Resistance']) if 'Tolerance' in part['Parameters']: part['Parameters'][ 'Tolerance'] = tolerance.tolerance_to_string( part['Parameters']['Tolerance']) except TypeError: print(component) raise elif group in ["IntegratedCircuits" ] and component['Comment'] != '': print("Request for " + to_string(component['Comment'])) found = shop.find_component(component['Comment']) else: found = None if "Distributors" not in component: component["Distributors"] = [] if found: component["Distributors"].append({ "Name": shop.name, "Components": found })
def valueForCompare(k): return resistor.convert_resistance_to_ohms(k['Resistance'])
def decodeResistor(component): def getResistance(component): for field in ['Resistance', 'resistance', 'Value']: if field in component: if component[field] != '': return component[field].replace(" ", "").replace( ",", ".").replace('?', 'R') for field in ['Comment', 'Description']: fieldContent = component[field].replace(",", ".") match = re.search('(\d+(M|k|R|m)\d+)', fieldContent) # try to match 4k7 4R7 4M1 etc. if match: return match.group(1).replace(" ", "").replace( "M", ".").replace("k", ".").replace("R", ".").replace( "m", ".") + match.group(2) match = re.search(r'(\d+(\.\d+)?[kRM])', fieldContent) # try to match 4.7k 4,7k 4.7M etc. if match: return match.group(1).replace(" ", "").replace(",", ".") match = re.search(r'(\d+(\.\d+)? ?((m|k|M|G)?(O|o)hm))', fieldContent) if match: if match.group(4) != "": return match.group(1).replace(" ", "").replace( ",", ".").replace("Ohm", "").replace("ohm", "").replace('?', 'R') else: return match.group(1).replace(" ", "").replace( ",", ".").replace("Ohm", "R").replace("ohm", "R").replace('?', 'R') def getManufacturer(component): manufacturerList = ['Vishay', 'AVX', 'Kemet'] if 'Comment' in component: for word in component['Comment'].split(): for manufacturer in manufacturerList: if manufacturer.lower() == word.lower(): return manufacturer return '' def getManufacturerPartNumber(component): resistorFamilyList = [r'(CRCW[A-Za-z0-9]+)'] for field in ['Comment', 'Description']: if field in component: for word in component[field].split(): for resistorRegexpr in resistorFamilyList: if re.compile(resistorRegexpr).match(word): return word return '' part = {} part['Resistance'] = resistor.ohms_to_string( resistor.convert_resistance_to_ohms(getResistance(component))) part['Tolerance'] = getTolerance(component) part['Case'] = getCase(component) part['Manufacturer'] = getManufacturer(component) part['Manufacturer Part Number'] = getManufacturerPartNumber(component) part['Quantity'] = getQuantity(component) part['Designator'] = component['Designator'] return part