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)
| 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: |
nothing calls this directly
no test coverage detected