biip.upc

Universal Product Code (UPC).

The biip.upc module contains Biip’s support for parsing UPC formats.

This class can interpret the following UPC formats:

  • UPC-A, 12 digits.

  • UPC-E, 6 digits, with implicit number system 0 and no check digit.

  • UPC-E, 7 digits, with explicit number system and no check digit.

  • UPC-E, 8 digits, with explicit number system and a check digit.

If you only want to parse UPCs, you can import the UPC parser directly instead of using biip.parse()

>>> from biip.upc import Upc

If parsing succeds, it returns a Upc object.

>>> upc_a = Upc.parse("042100005264")
>>> upc_a
Upc(value='042100005264', format=UpcFormat.UPC_A, number_system_digit=0,
payload='04210000526', check_digit=4)

A subset of the UPC-A values can be converted to a shorter UPC-E format by suppressing zeros.

>>> upc_a.as_upc_e()
'04252614'

All UPC-E values can be expanded to an UPC-A.

>>> upc_e = Upc.parse("04252614")
>>> upc_e
Upc(value='04252614', format=UpcFormat.UPC_E, number_system_digit=0,
payload='0425261', check_digit=4)
>>> upc_e.as_upc_a()
'042100005264'

UPC is a subset of the later GTIN standard: An UPC-A value is also a valid GTIN-12 value.

>>> upc_e.as_gtin_12()
'042100005264'

The canonical format for persisting UPCs to e.g. a database is GTIN-14.

>>> upc_e.as_gtin_14()
'00042100005264'
class biip.upc.UpcFormat(value)

Enum of UPC formats.

class biip.upc.Upc(value, format, number_system_digit, payload, check_digit=None)

Data class containing an UPC.

value: str

Raw unprocessed value.

format: biip.upc.UpcFormat

UPC format, either UPC-A or UPC-E.

number_system_digit: int

Number system digit.

payload: str

The actual payload, including number system digit, manufacturer code, and product code. Excludes the check digit.

check_digit: Optional[int] = None

Check digit used to check if the UPC-A as a whole is valid.

Set for UPC-A, but not set for UPC-E.

classmethod parse(value)

Parse the given value into a Upc object.

Parameters

value (str) – The value to parse.

Return type

Upc

Returns

UPC data structure with the successfully extracted data. The checksum is guaranteed to be valid if an UPC object is returned.

Raises

ParseError – If the parsing fails.

as_upc_a()

Format as UPC-A.

Return type

str

Returns

A string with the UPC encoded as UPC-A.

References

GS1 General Specifications, section 5.2.2.4.2

as_upc_e()

Format as UPC-E.

Return type

str

Returns

A string with the UPC encoded as UPC-E, if possible.

Raises

EncodeError – If encoding as UPC-E fails.

References

GS1 General Specifications, section 5.2.2.4.1

as_gtin_12()

Format as GTIN-12.

Return type

str

as_gtin_13()

Format as GTIN-13.

Return type

str

as_gtin_14()

Format as GTIN-14.

Return type

str