def __init__(self, ean, writer=None, no_checksum=False, guardbar=False): ean = ean[:self.digits] if not ean.isdigit(): raise IllegalCharacterError("EAN code can only contain numbers.") if len(ean) != self.digits: raise NumberOfDigitsError( "EAN must have {} digits, not {}.".format( self.digits, len(ean), )) self.ean = ean # If no checksum if no_checksum: # Add a thirteen char if given in parameter, # otherwise pad with zero self.ean = "{}{}".format( ean, ean[self.digits] if len(ean) > self.digits else 0) else: self.ean = "{}{}".format(ean, self.calculate_checksum()) self.guardbar = guardbar if guardbar: self.EDGE = _ean.EDGE.replace("1", "G") self.MIDDLE = _ean.MIDDLE.replace("1", "G") else: self.EDGE = _ean.EDGE self.MIDDLE = _ean.MIDDLE self.writer = writer or self.default_writer()
def check_code(code, name, allowed): wrong = [] for char in code: if char not in allowed: wrong.append(char) if wrong: raise IllegalCharacterError( "The following characters are not valid for {name}: {wrong}". format(name=name, wrong=", ".join(wrong)))
def __init__(self, code, writer=None, narrow=2, wide=5): if not code.isdigit(): raise IllegalCharacterError('ITF code can only contain numbers.') # Length must be even, prepend 0 if necessary if len(code) % 2 != 0: code = '0' + code self.code = code self.writer = writer or Barcode.default_writer() self.narrow = narrow self.wide = wide
def __init__(self, upc, writer=None, make_ean=False): self.ean = make_ean upc = upc[:self.digits] if not upc.isdigit(): raise IllegalCharacterError('UPC code can only contain numbers.') if len(upc) != self.digits: raise NumberOfDigitsError('UPC must have {0} digits, not ' '{1}.'.format(self.digits, len(upc))) self.upc = upc self.upc = '{}{}'.format(upc, self.calculate_checksum()) self.writer = writer or Barcode.default_writer()
def __init__(self, pzn, writer=None): pzn = pzn[: self.digits] if not pzn.isdigit(): raise IllegalCharacterError("PZN can only contain numbers.") if len(pzn) != self.digits: raise NumberOfDigitsError( "PZN must have {} digits, not {}.".format(self.digits, len(pzn)) ) self.pzn = pzn self.pzn = "{}{}".format(pzn, self.calculate_checksum()) Code39.__init__(self, "PZN-{}".format(self.pzn), writer, add_checksum=False)
def __init__(self, pzn, writer=None): pzn = pzn[: self.digits] if not pzn.isdigit(): raise IllegalCharacterError("PZN can only contain numbers.") if len(pzn) != self.digits: raise NumberOfDigitsError( f"PZN must have {self.digits} digits, not {len(pzn)}." ) self.pzn = pzn self.pzn = f"{pzn}{self.calculate_checksum()}" super().__init__(f"PZN-{self.pzn}", writer, add_checksum=False)
def __init__(self, upc, writer=None, make_ean=False): """Initializes new UPC-A barcode. :param str upc: The upc number as string. :param writer: barcode.writer instance. The writer to render the barcode (default: SVGWriter). :param bool make_ean: Indicates if a leading zero should be added to the barcode. This converts the UPC into a valid European Article Number (EAN). """ self.ean = make_ean upc = upc[:self.digits] if not upc.isdigit(): raise IllegalCharacterError("UPC code can only contain numbers.") if len(upc) != self.digits: raise NumberOfDigitsError( "UPC must have {} digits, not {}.".format( self.digits, len(upc))) self.upc = upc self.upc = "{}{}".format(upc, self.calculate_checksum()) self.writer = writer or self.default_writer()
def __init__(self, ean, writer=None, no_checksum=False): ean = ean[:self.digits] if not ean.isdigit(): raise IllegalCharacterError('EAN code can only contain numbers.') if len(ean) != self.digits: raise NumberOfDigitsError( 'EAN must have {0} digits, not {1}.'.format( self.digits, len(ean), ) ) self.ean = ean # If no checksum if no_checksum: # Add a thirteen char if given in parameter, # otherwise pad with zero self.ean = '{0}{1}'.format( ean, ean[self.digits] if len(ean) > self.digits else 0 ) else: self.ean = '{0}{1}'.format(ean, self.calculate_checksum()) self.writer = writer or Barcode.default_writer()