def calc_check_digit(number): """Calculate the check digit for the number. The number should not already have a check digit.""" # both the 18-digit decimal format and the 14-digit hex format # containing only decimal digits should use the decimal Luhn check from stdnum import luhn if isdigits(number): return luhn.calc_check_digit(number) else: return luhn.calc_check_digit(number, alphabet=_hex_alphabet)
def calc_check_digit(number): """Calculate the check digit for the number. The number should not already have a check digit.""" # both the 18-digit decimal format and the 14-digit hex format # containing only decimal digits should use the decimal Luhn check from stdnum import luhn if number.isdigit(): return luhn.calc_check_digit(number) else: return luhn.calc_check_digit(number, alphabet=_hex_alphabet)
def format(number, separator='-', add_check_digit=False): """Reformat the passed number to the standard format.""" number = compact(number) if len(number) == 14 and add_check_digit: number += luhn.calc_check_digit(number) number = (number[:2], number[2:8], number[8:14], number[14:]) return separator.join(x for x in number if x)
def format(number, separator='-', add_check_digit=False): """Reformat the number to the standard presentation format.""" number = compact(number) if len(number) == 14 and add_check_digit: number += luhn.calc_check_digit(number) number = (number[:2], number[2:8], number[8:14], number[14:]) return separator.join(x for x in number if x)
def luhn_sign(cls, val): result = None if val.isdigit(): result = val + luhn.calc_check_digit(val) return result
def do_tac(): global IMEI check_tacfile() tac_list=read_tacfile('') TAC=tac_list[0] SN = f'{randrange(999999):06}' IMEI0 = TAC + SN CD = luhn.calc_check_digit(IMEI0) IMEI = IMEI0 + CD print(IMEI,'-',tac_list[1],tac_list[2])
def do_targ(STRING): global IMEI print("guessing", STRING) if STRING.isdigit(): print("we have a number, len:", len(STRING)) if len(STRING) > 15: print("IMEI too long") else: if len(STRING) == 15: print("we've got full IMEI, checking validity") IMEI = STRING IMEI0 = STRING[:14] CD = STRING[14:] if luhn.calc_check_digit(IMEI0) == CD: print("IMEI is valid") else: print("IMEI is invalid, should be ", IMEI0 + luhn.calc_check_digit(IMEI0)) else: print("we've got partial IMEI, calculating the rest") NEED=(14 - len(STRING)) if NEED == 0: IMEI0 = STRING else: RANGE=((10**(NEED)-1)) SN = randrange(RANGE) SN = str(SN).zfill(NEED) IMEI0 = STRING + SN CD = luhn.calc_check_digit(IMEI0) IMEI = IMEI0 + CD print("IMEI: ", IMEI) else: print("we've got string, searching in TAC list") check_tacfile() tac_list=read_tacfile(STRING) # FIXME, repeats -t TAC=tac_list[0] SN = f'{randrange(999999):06}' IMEI0 = TAC + SN CD = luhn.calc_check_digit(IMEI0) IMEI = IMEI0 + CD print(IMEI,'-',tac_list[1],tac_list[2])
def _get_invoice_reference_se_ocr4(self, reference): self.ensure_one() ocr_length = self.journal_id.l10n_se_invoice_ocr_length if len(reference) + 1 > ocr_length: raise UserError( _("OCR Reference Number length is greater than allowed. Allowed length in invoice journal setting is %s." ) % str(ocr_length)) reference = reference.rjust(ocr_length - 1, '0') return reference + luhn.calc_check_digit(reference)
def get_order_no(self): return (self.id * 10) + int(luhn.calc_check_digit(self.id))
def _get_kid_number(self): self.ensure_one() invoice_name = ''.join([i for i in self.name if i.isdigit()]).zfill(7) ref = (str(self.partner_id.id).zfill(7)[-7:] + invoice_name[-7:]) return ref + luhn.calc_check_digit(ref)
#!/usr/bin/env python3 from pathlib import Path from csv import reader from random import randrange from stdnum import luhn tac_file = 'tac.csv' tac_url = 'http://tacdb.osmocom.org/export/tacdb.csv' if (not Path(tac_file).exists()): print(tac_file, 'does not exist, downloading from', tac_url) import urllib.request urllib.request.urlretrieve(tac_url, tac_file) with open(tac_file, 'r') as file: csv_reader = reader(file, delimiter=',') csv_data = list(csv_reader) row_count = len(csv_data) tac_list = csv_data[randrange(2, row_count)] TAC = tac_list[0] SN = f'{randrange(999999):06}' IMEI0 = TAC + SN CD = luhn.calc_check_digit(IMEI0) IMEI = IMEI0 + CD print(IMEI, '-', tac_list[1], tac_list[2])
def _check(value): return luhn.calc_check_digit(value, alphabet=CHECK_ALPHABET)
def _get_invoice_reference_se_ocr2(self, reference): self.ensure_one() return reference + luhn.calc_check_digit(reference)
def _get_invoice_reference_se_ocr3(self, reference): self.ensure_one() reference = reference + str(len(reference) + 2)[:1] return reference + luhn.calc_check_digit(reference)
def calc_check_digits(number): """Calculate the check digits for the specified number. The number passed should not have the check digit included. This function returns both the number and character check digit candidates.""" check = luhn.calc_check_digit(number[1:]) return check + 'JABCDEFGHI'[int(check)]
def do_random(): global IMEI IMEI0 = f'{randrange(99999999999999):014}' CD = luhn.calc_check_digit(IMEI0) IMEI = IMEI0 + CD print(IMEI,'- random')