(
self, ean: str, writer=None, no_checksum=False, guardbar=False
)
| 42 | digits = 12 |
| 43 | |
| 44 | def __init__( |
| 45 | self, ean: str, writer=None, no_checksum=False, guardbar=False |
| 46 | ) -> None: |
| 47 | if not ean[: self.digits].isdigit(): |
| 48 | raise IllegalCharacterError(f"EAN code can only contain numbers {ean}.") |
| 49 | |
| 50 | if len(ean) < self.digits: |
| 51 | raise NumberOfDigitsError( |
| 52 | f"EAN must have {self.digits} digits, received {len(ean)}." |
| 53 | ) |
| 54 | |
| 55 | base = ean[: self.digits] |
| 56 | if no_checksum: |
| 57 | # Use the thirteenth digit if given in parameter, otherwise pad with zero |
| 58 | if len(ean) > self.digits and ean[self.digits].isdigit(): |
| 59 | last = int(ean[self.digits]) |
| 60 | else: |
| 61 | last = 0 |
| 62 | else: |
| 63 | last = self.calculate_checksum(base) |
| 64 | |
| 65 | self.ean = f"{base}{last}" |
| 66 | |
| 67 | self.guardbar = guardbar |
| 68 | if guardbar: |
| 69 | self.EDGE = _ean.EDGE.replace("1", "G") |
| 70 | self.MIDDLE = _ean.MIDDLE.replace("1", "G") |
| 71 | else: |
| 72 | self.EDGE = _ean.EDGE |
| 73 | self.MIDDLE = _ean.MIDDLE |
| 74 | self.writer = writer or self.default_writer() |
| 75 | |
| 76 | def __str__(self) -> str: |
| 77 | return self.ean |
nothing calls this directly
no test coverage detected