def test_check_digit(): assert GTIN(raw=890123456789).check_digit == '0' assert GTIN(raw=10101).check_digit == '1' assert GTIN(raw=567898901234).check_digit == '2' assert GTIN(raw=82957399425).check_digit == '3' assert GTIN(raw=5936663101).check_digit == '4' assert GTIN(raw=15059928976).check_digit == '5' assert GTIN(raw=901234567890).check_digit == '6' assert GTIN(raw=36013101).check_digit == '7' assert GTIN(raw=123456789012).check_digit == '8' assert GTIN(raw=208957399425).check_digit == '9'
def test_gcp(): # GS1 US assert GCP_PREFIXES['03321'] == 6 assert GTIN('00332100000001').gcp == '033210' assert GCP_PREFIXES['0810000'] == 9 # Restricted distribution assert '2' not in GCP_PREFIXES assert '02' not in GCP_PREFIXES # Test restricted distribution GCPs assert GTIN('02345678901289').gcp == '' assert GTIN('00234567890129').gcp == '' # Test a missing GCP assert GTIN('01345678901280').gcp == '' # Test refreshing XML GCP_PREFIXES.refresh()
def test_gcp(): gcp_prefixes_lengths = prefixes_lengths(local=True) # GS1 US assert gcp_prefixes_lengths['03321'] == 6 assert GTIN('00332100000001').gcp == '033210' assert gcp_prefixes_lengths['081'] == 9 # Restricted distribution assert gcp_prefixes_lengths['02'] == 0 assert GTIN('02345678901289').gcp == '' assert gcp_prefixes_lengths['2'] == 0 assert GTIN('00234567890129').gcp == '' # Exception handling gcp_not_found_error = None try: g = GTIN('01345678901280').gcp except Exception as e: gcp_not_found_error = e assert isinstance(gcp_not_found_error, GCPNotFoundError)
def convert_upc_to_gtin(): if not request.json or not 'upc' in request.json: abort(400) #print (request.json) upc = request.json['upc'] length = request.json['length'] if 'length' in request.json else 12 res = str(GTIN(raw=request.json['upc'], length=length)) return jsonify({'gtin': res}), 201
def is_code_supported_by_gs1_api(code): code = code.lstrip('0') try: _ = GTIN(code) except CheckDigitError: return False if code.isdigit() \ and len(code) in [8, 11, 12, 13, 14] \ and not code.startswith(('190', '967', '977', '978', '979', '99', '150', '169', '2', '922', '178', '161')): return True return False
def check_gtin(product_code, length=13): ''' Verifica se um codigo gtim esta correto ''' if len(product_code) == 13: try: #biblioteca GTIN com problemas return True GTIN(product_code, length) except Exception: return False return True else: return False
def convert_to_ean(number): return str(GTIN(raw='{}'.format(number)))
data = { "contentFormat": "quantity", "transactionId": "searchnpick-%s" % uuid.uuid4(), "bizStep": "urn:epcglobal:cbv:bizstep:packing", "bizLocation": "FRA082" } max_elements = int(sys.argv[2]) elements = json.loads('[]'); # [max_elements] for x in range(0, max_elements): elements.append({ "format": "quantity", "gtin": "%s" % str(GTIN(raw=("%014d" % (x + 1)))), "quantity": 1 }) content = { "content": elements } data["containers"] = [content] for x in range(1, 2): data["transactionId"] = "searchnpick-%s" % uuid.uuid4() start = time.time() print(data)
targetfolder = "C:\\Temp\\test5000\\" activeUPCListFile = "C:\\Users\\Richard.Wang\\Documents\\My_DailyWork\\GS1-REST-API\\ActiveUPCList.txt" #Get all active UPC codes into a list with open(activeUPCListFile) as file_in: activeUPCList = [] for line in file_in: activeUPCList.append(line.rstrip()) # List all files in a folder targetFiles = [] for file in glob.glob(targetfolder + "productKeys-17.json"): targetFiles.append(file) for targetUPC in activeUPCList: targetGTIN = str(GTIN(raw=int(targetUPC))) for oneFile in targetFiles: # print(oneFile) with open(oneFile, 'r') as f: key_dict = json.load(f) allKeys = key_dict["keys"] exist = False for key in allKeys: #print("-targetGTIN-GTIN14=" + targetGTIN + '-' + key['gtin14']) if key['gtin14'] == targetGTIN: exist = True print("Target UPC-GTIN=" + targetUPC + '-' + key['gtin14']) print(key['baseGln']) print(key['gln'])
def epcDLTranslator(urn): if match( r'^urn:epc:id:sgtin:((\d{6}\.\d{7})|(\d{7}\.\d{6})|(\d{8}\.\d{5})|(\d{9}\.\d{4})|(\d{10}\.\d{3})|(\d{11}\.\d{2})|(\d{12}\.\d{1}))\.(\%2[125-9A-Fa-f]|\%3[0-9A-Fa-f]|\%4[1-9A-Fa-f]|\%5[0-9AaFf]|\%6[1-9A-Fa-f]|\%7[0-9Aa]|[!\')(*+,.0-9:;=A-Za-z_-]){1,20}$', urn) is not None: partition = urn.index('.') gs1companyprefix = urn[17:partition] itemref = urn[(partition + 1):(partition + 1 + (13 - len(gs1companyprefix)))] rawGTIN = itemref[0:1] + gs1companyprefix + itemref[1:] serial = urn[32:] return ('https://id.gs1.org/01/' + str(GTIN(raw=rawGTIN)) + '/21/' + serial) elif match( r'^urn:epc:id:sscc:((\d{6}\.\d{11}$)|(\d{7}\.\d{10}$)|(\d{8}\.\d{9}$)|(\d{9}\.\d{8}$)|(\d{10}\.\d{7}$)|(\d{11}\.\d{6}$)|(\d{12}\.\d{5}$))', urn) is not None: partition = urn.index('.') gs1companyprefix = urn[16:partition] serialref = urn[(partition + 1):] rawSSCC = urn[(partition + 1):(partition + 2)] + gs1companyprefix + serialref return ('https://id.gs1.org/00/' + str(GTIN(raw=rawSSCC))) elif match( r'^urn:epc:id:sgln:((\d{6}\.\d{6})|(\d{7}\.\d{5})|(\d{8}\.\d{4})|(\d{9}\.\d{3})|(\d{10}\.\d{2})|(\d{11}\.\d{1})|(\d{12}\.))\.(\%2[125-9A-Fa-f]|\%3[0-9A-Fa-f]|\%4[1-9A-Fa-f]|\%5[0-9AaFf]|\%6[1-9A-Fa-f]|\%7[0-9Aa]|[!\')(*+,.0-9:;=A-Za-z_-]){1,20}$', urn) is not None: partition = urn.index('.') gs1companyprefix = urn[16:partition] locationref = urn[(partition + 1):(partition + 1 + (12 - len(gs1companyprefix)))] rawGLN = gs1companyprefix + locationref extension = urn[30:] if extension == '0': return ('https://id.gs1.org/414/' + str(GTIN(raw=rawGLN))) else: return ('https://id.gs1.org/414/' + str(GTIN(raw=rawGLN)) + '/254/' + extension) elif match( r'^urn:epc:id:grai:(([\d]{6}\.[\d]{6})|([\d]{7}\.[\d]{5})|([\d]{8}\.[\d]{4})|([\d]{9}\.[\d]{3})|([\d]{10}\.[\d]{2})|([\d]{11}\.[\d]{1})|([\d]{12}\.))\.(\%2[125-9A-Fa-f]|\%3[0-9A-Fa-f]|\%4[1-9A-Fa-f]|\%5[0-9AaFf]|\%6[1-9A-Fa-f]|\%7[0-9Aa]|[!\')(*+,.0-9:;=A-Za-z_-]){1,16}$', urn) is not None: partition = urn.index('.') gs1companyprefix = urn[16:partition] assetref = urn[(partition + 1):(partition + 1 + (12 - len(gs1companyprefix)))] rawGRAI = '0' + gs1companyprefix + assetref serial = urn[30:] return ('https://id.gs1.org/8003/' + str(GTIN(raw=rawGRAI)) + serial) elif match( r'^urn:epc:id:giai:(([\d]{6}\.(\%2[125-9A-Fa-f]|\%3[0-9A-Fa-f]|\%4[1-9A-Fa-f]|\%5[0-9AaFf]|\%6[1-9A-Fa-f]|\%7[0-9Aa]|[!\')(*+,.0-9:;=A-Za-z_-]){1,24})|([\d]{7}\.(\%2[125-9A-Fa-f]|\%3[0-9A-Fa-f]|\%4[1-9A-Fa-f]|\%5[0-9AaFf]|\%6[1-9A-Fa-f]|\%7[0-9Aa]|[!\')(*+,.0-9:;=A-Za-z_-]){1,23})|([\d]{8}\.(\%2[125-9A-Fa-f]|\%3[0-9A-Fa-f]|\%4[1-9A-Fa-f]|\%5[0-9AaFf]|\%6[1-9A-Fa-f]|\%7[0-9Aa]|[!\')(*+,.0-9:;=A-Za-z_-]){1,22})|([\d]{9}\.(\%2[125-9A-Fa-f]|\%3[0-9A-Fa-f]|\%4[1-9A-Fa-f]|\%5[0-9AaFf]|\%6[1-9A-Fa-f]|\%7[0-9Aa]|[!\')(*+,.0-9:;=A-Za-z_-]){1,21})|([\d]{10}\.(\%2[125-9A-Fa-f]|\%3[0-9A-Fa-f]|\%4[1-9A-Fa-f]|\%5[0-9AaFf]|\%6[1-9A-Fa-f]|\%7[0-9Aa]|[!\')(*+,.0-9:;=A-Za-z_-]){1,20})|([\d]{11}\.(\%2[125-9A-Fa-f]|\%3[0-9A-Fa-f]|\%4[1-9A-Fa-f]|\%5[0-9AaFf]|\%6[1-9A-Fa-f]|\%7[0-9Aa]|[!\')(*+,.0-9:;=A-Za-z_-]){1,19})|([\d]{12}\.(\%2[125-9A-Fa-f]|\%3[0-9A-Fa-f]|\%4[1-9A-Fa-f]|\%5[0-9AaFf]|\%6[1-9A-Fa-f]|\%7[0-9Aa]|[!\')(*+,.0-9:;=A-Za-z_-]){1,18}))$', urn) is not None: partition = urn.index('.') gs1companyprefix = urn[16:partition] assetref = urn[(partition + 1):] return ('https://id.gs1.org/8004/' + gs1companyprefix + assetref) elif match( r'^urn:epc:id:gsrn:(([\d]{6}\.[\d]{11}$)|([\d]{7}\.[\d]{10}$)|([\d]{8}\.[\d]{9}$)|([\d]{9}\.[\d]{8}$)|([\d]{10}\.[\d]{7}$)|([\d]{11}\.[\d]{6}$)|([\d]{12}\.[\d]{5}$))', urn) is not None: partition = urn.index('.') gs1companyprefix = urn[16:partition] serviceref = urn[(partition + 1):] rawGSRN = gs1companyprefix + serviceref return ('https://id.gs1.org/8018/' + str(GTIN(raw=rawGSRN))) elif match( r'^urn:epc:id:gsrnp:(([\d]{6}\.[\d]{11}$)|([\d]{7}\.[\d]{10}$)|([\d]{8}\.[\d]{9}$)|([\d]{9}\.[\d]{8}$)|([\d]{10}\.[\d]{7}$)|([\d]{11}\.[\d]{6}$)|([\d]{12}\.[\d]{5}$))', urn) is not None: partition = urn.index('.') gs1companyprefix = urn[17:partition] serviceref = urn[(partition + 1):] rawGSRNP = gs1companyprefix + serviceref return ('https://id.gs1.org/8017/' + str(GTIN(raw=rawGSRNP))) elif match( r'^urn:epc:id:gdti:(([\d]{6}\.[\d]{6})|([\d]{7}\.[\d]{5})|([\d]{8}\.[\d]{4})|([\d]{9}\.[\d]{3})|([\d]{10}\.[\d]{2})|([\d]{11}\.[\d]{1})|([\d]{12}\.\.))(\%2[125-9A-Fa-f]|\%3[0-9A-Fa-f]|\%4[1-9A-Fa-f]|\%5[0-9AaFf]|\%6[1-9A-Fa-f]|\%7[0-9Aa]|[!\')(*+,.0-9:;=A-Za-z_-]){1,20}$', urn) is not None: partition = urn.index('.') gs1companyprefix = urn[16:partition] documenttype = urn[(partition + 1):(partition + 1 + (12 - len(gs1companyprefix)))] rawGDTI = gs1companyprefix + documenttype serial = urn[30:] return ('https://id.gs1.org/253/' + str(GTIN(raw=rawGDTI)) + serial) elif match( r'^urn:epc:id:cpi:((\d{6}\.(\%2[3dfDF]|\%3[0-9]|\%4[1-9A-Fa-f]|\%5[0-9Aa]|[0-9A-Z-]){1,24})|(\d{7}\.(\%2[3dfDF]|\%3[0-9]|\%4[1-9A-Fa-f]|\%5[0-9Aa]|[0-9A-Z-]){1,23})|(\d{8}\.(\%2[3dfDF]|\%3[0-9]|\%4[1-9A-Fa-f]|\%5[0-9Aa]|[0-9A-Z-]){1,22})|(\d{9}\.(\%2[3dfDF]|\%3[0-9]|\%4[1-9A-Fa-f]|\%5[0-9Aa]|[0-9A-Z-]){1,21})|(\d{10}\.(\%2[3dfDF]|\%3[0-9]|\%4[1-9A-Fa-f]|\%5[0-9Aa]|[0-9A-Z-]){1,20})|(\d{11}\.(\%2[3dfDF]|\%3[0-9]|\%4[1-9A-Fa-f]|\%5[0-9Aa]|[0-9A-Z-]){1,19})|(\d{12}\.(\%2[3dfDF]|\%3[0-9]|\%4[1-9A-Fa-f]|\%5[0-9Aa]|[0-9A-Z-]){1,18}))\.[\d]{1,12}$', urn) is not None: partition = urn.index('.') gs1companyprefix = urn[15:partition] separator = urn.rfind('.') cpref = urn[(partition + 1):(separator)] rawCPI = gs1companyprefix + cpref serial = urn[(separator + 1):] return ('https://id.gs1.org/8010/' + rawCPI + '/8011/' + serial) elif match( r'^urn:epc:id:sgcn:(([\d]{6}\.[\d]{6})|([\d]{7}\.[\d]{5})|([\d]{8}\.[\d]{4})|([\d]{9}\.[\d]{3})|([\d]{10}\.[\d]{2})|([\d]{11}\.[\d]{1})|([\d]{12}\.))\.[\d]{1,12}$', urn) is not None: partition = urn.index('.') gs1companyprefix = urn[16:partition] couponref = urn[(partition + 1):(partition + 1 + (12 - len(gs1companyprefix)))] rawSGCN = gs1companyprefix + couponref serial = urn[30:] return ('https://id.gs1.org/255/' + str(GTIN(raw=rawSGCN)) + serial) elif match( r'^urn:epc:id:ginc:([\d]{6}\.(\%2[125-9A-Fa-f]|\%3[0-9A-Fa-f]|\%4[1-9A-Fa-f]|\%5[0-9AaFf]|\%6[1-9A-Fa-f]|\%7[0-9Aa]|[!\')(*+,.0-9:;=A-Za-z_-]){1,24}|[\d]{7}\.(\%2[125-9A-Fa-f]|\%3[0-9A-Fa-f]|\%4[1-9A-Fa-f]|\%5[0-9AaFf]|\%6[1-9A-Fa-f]|\%7[0-9Aa]|[!\')(*+,.0-9:;=A-Za-z_-]){1,23}|[\d]{8}\.(\%2[125-9A-Fa-f]|\%3[0-9A-Fa-f]|\%4[1-9A-Fa-f]|\%5[0-9AaFf]|\%6[1-9A-Fa-f]|\%7[0-9Aa]|[!\')(*+,.0-9:;=A-Za-z_-]){1,22}|[\d]{9}\.(\%2[125-9A-Fa-f]|\%3[0-9A-Fa-f]|\%4[1-9A-Fa-f]|\%5[0-9AaFf]|\%6[1-9A-Fa-f]|\%7[0-9Aa]|[!\')(*+,.0-9:;=A-Za-z_-]){1,21}|[\d]{10}\.(\%2[125-9A-Fa-f]|\%3[0-9A-Fa-f]|\%4[1-9A-Fa-f]|\%5[0-9AaFf]|\%6[1-9A-Fa-f]|\%7[0-9Aa]|[!\')(*+,.0-9:;=A-Za-z_-]){1,20}|[\d]{11}\.(\%2[125-9A-Fa-f]|\%3[0-9A-Fa-f]|\%4[1-9A-Fa-f]|\%5[0-9AaFf]|\%6[1-9A-Fa-f]|\%7[0-9Aa]|[!\')(*+,.0-9:;=A-Za-z_-]){1,19}|[\d]{12}\.(\%2[125-9A-Fa-f]|\%3[0-9A-Fa-f]|\%4[1-9A-Fa-f]|\%5[0-9AaFf]|\%6[1-9A-Fa-f]|\%7[0-9Aa]|[!\')(*+,.0-9:;=A-Za-z_-]){1,18})$', urn) is not None: partition = urn.index('.') gs1companyprefix = urn[16:partition] consignmentref = urn[(partition + 1):] return ('https://id.gs1.org/401/' + gs1companyprefix + consignmentref) elif match( r'^urn:epc:id:gsin:(([\d]{6}\.[\d]{10}$)|([\d]{7}\.[\d]{9}$)|([\d]{8}\.[\d]{8}$)|([\d]{9}\.[\d]{7}$)|([\d]{10}\.[\d]{6}$)|([\d]{11}\.[\d]{5}$)|([\d]{12}\.[\d]{4}$))', urn) is not None: partition = urn.index('.') gs1companyprefix = urn[16:partition] shipperref = urn[(partition + 1):] rawGSIN = gs1companyprefix + shipperref return ('https://id.gs1.org/402/' + str(GTIN(raw=rawGSIN))) elif match( r'^urn:epc:id:itip:(([\d]{6}\.[\d]{7})|([\d]{7}\.[\d]{6})|([\d]{8}\.[\d]{5})|([\d]{9}\.[\d]{4})|([\d]{10}\.[\d]{3})|([\d]{11}\.[\d]{2})|([\d]{12}\.[\d]{1}))\.[\d]{2}\.[\d]{2}\.(\%2[125-9A-Fa-f]|\%3[0-9A-Fa-f]|\%4[1-9A-Fa-f]|\%5[0-9AaFf]|\%6[1-9A-Fa-f]|\%7[0-9Aa]|[!\')(*+,.0-9:;=A-Za-z_-]){1,20}$', urn) is not None: partition = urn.index('.') gs1companyprefix = urn[16:partition] itemref = urn[(partition + 1):(partition + 1 + (13 - len(gs1companyprefix)))] rawGTIN = itemref[0:1] + gs1companyprefix + itemref[1:] piece = urn[31:33] total = urn[34:36] serial = urn[37:] return ('https://id.gs1.org/8006/' + str(GTIN(raw=rawGTIN)) + piece + total + '/21/' + serial) elif match( r'^urn:epc:id:upui:((\d{6}\.\d{7})|(\d{7}\.\d{6})|(\d{8}\.\d{5})|(\d{9}\.\d{4})|(\d{10}\.\d{3})|(\d{11}\.\d{2})|(\d{12}\.\d{1}))\.(\%2[125-9A-Fa-f]|\%3[0-9A-Fa-f]|\%4[1-9A-Fa-f]|\%5[0-9AaFf]|\%6[1-9A-Fa-f]|\%7[0-9Aa]|[!\')(*+,.0-9:;=A-Za-z_-]){1,28}$', urn) is not None: partition = urn.index('.') gs1companyprefix = urn[16:partition] itemref = urn[(partition + 1):(partition + 1 + (13 - len(gs1companyprefix)))] rawGTIN = itemref[0:1] + gs1companyprefix + itemref[1:] serial = urn[31:] return ('https://id.gs1.org/01/' + str(GTIN(raw=rawGTIN)) + '/235/' + serial) elif match( r'^urn:epc:id:pgln:(([\d]{6}\.[\d]{6})|([\d]{7}\.[\d]{5})|([\d]{8}\.[\d]{4})|([\d]{9}\.[\d]{3})|([\d]{10}\.[\d]{2})|([\d]{11}\.[\d]{1})|([\d]{12}\.))$', urn) is not None: partition = urn.index('.') gs1companyprefix = urn[16:partition] partyref = urn[(partition + 1):(partition + 1 + (12 - len(gs1companyprefix)))] rawGLN = gs1companyprefix + partyref return ('https://id.gs1.org/417/' + str(GTIN(raw=rawGLN))) elif match( r'^urn:epc:class:lgtin:(([\d]{6}\.[\d]{7})|([\d]{7}\.[\d]{6})|([\d]{8}\.[\d]{5})|([\d]{9}\.[\d]{4})|([\d]{10}\.[\d]{3})|([\d]{11}\.[\d]{2})|([\d]{12}\.[\d]{1}))\.(\%2[125-9A-Fa-f]|\%3[0-9A-Fa-f]|\%4[1-9A-Fa-f]|\%5[0-9AaFf]|\%6[1-9A-Fa-f]|\%7[0-9Aa]|[!\')(*+,.0-9:;=A-Za-z_-]){1,20}$', urn) is not None: partition = urn.index('.') gs1companyprefix = urn[20:partition] itemref = urn[(partition + 1):(partition + 1 + (13 - len(gs1companyprefix)))] rawGTIN = itemref[0:1] + gs1companyprefix + itemref[1:] lot = urn[35:] return ('https://id.gs1.org/01/' + str(GTIN(raw=rawGTIN)) + '/10/' + lot) else: return ('This does not seem to be a valid EPC URI/EPC Class URI.')
''' Created on Aug 17, 2020 @author: Richard.Wang ''' from gtin import GTIN print(str(GTIN(raw=500))) print(str(GTIN(raw=501))) print(str(GTIN(raw=504))) print(str(GTIN(raw=505))) print(str(GTIN(raw=507))) print(str(GTIN(raw=508))) print(str(GTIN(raw=509))) print(str(GTIN(raw=8804433000864))) print(str(GTIN(raw=8809201000077))) print(str(GTIN(raw=8809201000091))) print(str(GTIN(raw=8856966007063))) print(str(GTIN(raw=8858754200083)))
df = df.rename( columns={ 'product_id': 'id', 'product_name': 'title', 'description': 'description', 'deeplink': 'link', 'image_url': 'image_link', 'in_stock': 'availability', 'brand': 'brand', 'european_article_number': 'EAN' }) df.sort_values(["price"], ascending=False) #check and change GTIN df['GTIN'] = df.EAN.apply(lambda x: (str(GTIN(int(x), length=13)))) # if the GTIN is change, return true df['GTIN_change'] = (df.GTIN != df.EAN.apply(lambda x: (str(x)))) # create new df to check a = df[['EAN', 'GTIN', 'GTIN_change']] #------------------------------------- #plot graph le = preprocessing.LabelEncoder() df['brand_encoded'] = le.fit_transform(df.brand.values) brandlist = sorted(list(dict.fromkeys(df.brand))) df.plot.scatter(x='price', y='delivery_cost', c='brand_encoded', colormap='viridis')
def test_str(): # 14-digit assert str(GTIN( raw='0123456789012')) == '01234567890128' # str - implicit length assert str(GTIN(raw='0123456789012', length=14)) == '01234567890128' # str - explicit length assert str( GTIN(raw=123456789012)) == '01234567890128' # int - implicit length assert str(GTIN(raw=123456789012, length=14)) == '01234567890128' # int - explicit length assert str(GTIN( raw=123456789012.0)) == '01234567890128' # float - implicit length assert str(GTIN(raw=123456789012.0, length=14)) == '01234567890128' # float - explicit length assert str(GTIN(raw=Decimal( 123456789012))) == '01234567890128' # Decimal - implicit length assert str( GTIN(raw=Decimal(123456789012), length=14)) == '01234567890128' # Decimal - explicit length assert str(GTIN(GTIN('01234567890128'))) == '01234567890128' # GTIN # 12-digit assert str( GTIN(raw='01234567890')) == '012345678905' # str - implicit length assert str(GTIN(raw='01234567890', length=12)) == '012345678905' # str - explicit length assert str(GTIN(raw=1234567890, length=12)) == '012345678905' # int assert str(GTIN(raw=1234567890.0, length=12)) == '012345678905' # float assert str(GTIN(raw=Decimal(1234567890), length=12)) == '012345678905' # Decimal assert str(GTIN(GTIN('01234567890128'))) == '01234567890128' assert str(GTIN(GTIN('01234567890128'), length=12)) == '01234567890128' # 8-digit assert str(GTIN(raw='0123456')) == '01234565' # str - implicit length assert str(GTIN(raw='0123456', length=8)) == '01234565' # str - explicit length assert str(GTIN(raw=123456, length=8)) == '01234565' # int assert str(GTIN(raw=123456.0, length=8)) == '01234565' # float assert str(GTIN(raw=Decimal(123456), length=8)) == '01234565' # Decimal assert str(GTIN(GTIN('01234565'))) == '01234565' assert str(GTIN(GTIN('01234565'), length=8)) == '01234565'
import pandas as pd from gtin import GTIN import time df = pd.read_csv('products.csv') df = df.rename(columns = { 'product_id':'id', 'european_article_number':'GTIN' }) start = time.time() gtinlist = [] for index, row in df.iterrows(): if row['GTIN'] == '': gtinlist.append(str(GTIN(raw ='0', length=13))) else: gtinlist.append(str(GTIN(raw =(row['GTIN']), length=13))) df['GTIN'] = gtinlist end = time.time() print(end - start) df.to_csv('test.csv', index=False)
#NaN column Nan_col = df.columns[df.isna().any()] #count NaN per column num_Nan = df[Nan_col].isna().sum() for i in range(len(Nan_col)): logger.warning('column: {} , NaN per column: {}'.format( Nan_col[i], num_Nan[i])) #check-digit GTIN start = time.time() for i in df.GTIN: if i == '': i = '0' df.loc[df.GTIN == i, ['GTIN']] = str(GTIN(raw=i, length=13)) end = time.time() print(end - start) #check 2114770970000 to 21147709700008 #print(df[df.id == 1477097].GTIN) #check if description is Nan Nan_value_descr = df.description.isna() #print(df['price']) #add currency to price df.currency = 'GBP' df.price = df.price.astype(str) + ' ' + df.currency #create new csv
def test_str(): # 14-digit # str - implicit length assert str(GTIN(raw='0123456789012')) == '01234567890128' # str - explicit length assert str(GTIN(raw='0123456789012', length=14)) == '01234567890128' # int - implicit length assert str(GTIN(raw=123456789012)) == '01234567890128' # int - explicit length assert str(GTIN(raw=123456789012, length=14)) == '01234567890128' # 12-digit # str - implicit length assert str(GTIN(raw='01234567890')) == '012345678905' # str - explicit length assert str(GTIN(raw='01234567890', length=12)) == '012345678905' # int assert str(GTIN(raw=1234567890, length=12)) == '012345678905' # 8-digit # str - implicit length assert str(GTIN(raw='0123456')) == '01234565' # str - explicit length assert str(GTIN(raw='0123456', length=8)) == '01234565' # int assert str(GTIN(raw=123456, length=8)) == '01234565' # Verify that an invalid type throws an error error = None # float - implicit length try: str(GTIN(raw=123456789012.0)) == '01234567890128' except TypeError as e: error = e assert isinstance(error, TypeError)