MCPcopy Create free account
hub / github.com/WhyNotHugo/python-barcode / __init__

Method __init__

barcode/upc.py:27–47  ·  view source on GitHub ↗

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. T

(self, upc, writer=None, make_ean=False)

Source from the content-addressed store, hash-verified

25 digits = 11
26
27 def __init__(self, upc, writer=None, make_ean=False) -> None:
28 """Initializes new UPC-A barcode.
29
30 :param str upc: The upc number as string.
31 :param writer: barcode.writer instance. The writer to render the
32 barcode (default: SVGWriter).
33 :param bool make_ean: Indicates if a leading zero should be added to
34 the barcode. This converts the UPC into a valid European Article
35 Number (EAN).
36 """
37 self.ean = make_ean
38 upc = upc[: self.digits]
39 if not upc.isdigit():
40 raise IllegalCharacterError("UPC code can only contain numbers.")
41 if len(upc) != self.digits:
42 raise NumberOfDigitsError(
43 f"UPC must have {self.digits} digits, not {len(upc)}."
44 )
45 self.upc = upc
46 self.upc = f"{upc}{self.calculate_checksum()}"
47 self.writer = writer or self.default_writer()
48
49 def __str__(self) -> str:
50 if self.ean:

Callers

nothing calls this directly

Calls 3

calculate_checksumMethod · 0.95
NumberOfDigitsErrorClass · 0.90

Tested by

no test coverage detected