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

Method calculate_checksum

barcode/ean.py:84–95  ·  view source on GitHub ↗

Calculates and returns the checksum for EAN13-Code. Calculates the checksum for the supplied `value` (if any) or for this barcode's internal ``self.ean`` property.

(self, value: str | None = None)

Source from the content-addressed store, hash-verified

82 return self.ean
83
84 def calculate_checksum(self, value: str | None = None) -> int:
85 """Calculates and returns the checksum for EAN13-Code.
86
87 Calculates the checksum for the supplied `value` (if any) or for this barcode's
88 internal ``self.ean`` property.
89 """
90
91 ean_without_checksum = value or self.ean[: self.digits]
92
93 evensum = sum(int(x) for x in ean_without_checksum[-2::-2])
94 oddsum = sum(int(x) for x in ean_without_checksum[-1::-2])
95 return (10 - ((evensum + oddsum * 3) % 10)) % 10
96
97 def build(self) -> list[str]:
98 """Builds the barcode pattern from `self.ean`.

Callers 1

__init__Method · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected