canlib.ean

class canlib.ean.EAN(source)[source]

Bases: object

Helper object for dealing with European Article Numbers

Depending on the format the ean is in, EAN objects are created in different ways;

For strings:

EAN('73-30130-01234-5')

For integers:

EAN(7330130012345)

For iterables of integers:

EAN([7, 3, 3, 0, 1, 3, 0, 0, 1, 2, 3, 4, 5])

For BCD-coded bytes or bytearrays (str in python 2):

EAN.from_bcd(b'\x45\x23\x01\x30\x01\x33\x07')

For “hi-lo” format, i.e. two 32-bit integers containing half the ean each, both BCD-coded:

EAN.from_hilo([eanHi, eanLo])

The various representations can then be produced from the resulting object:

>>> str(ean)
'73-30130-01234-5'
>>> int(ean)
7330130012345
>>> tuple(ean)  # or list(), or any other sequence type
(7, 3, 3, 0, 1, 3, 0, 0, 1, 2, 3, 4, 5)
>>> ean.bcd()
b'E#\x010\x013\x07'
>>> ean.hilo()
(805380933, 471809)

Sometimes it is easier to only use the last six digits of the ean, the product code and check digit. This is supported when working with string representations; the constructor supports six-digit (seven-character) input:

EAN('01234-5')

In that cases, the country and manufacturer code is assumed to be that of Kvaser AB (73-30130).

A string containing only the product code and check digit can also be retrieved:

ean.product()

Instances can also be indexed which yields specific digits as integers:

>>> ean[7]
0
>>> ean[7:]
(0, 1, 2, 3, 4, 5)

Note

The byteorder is currently always assumed to be ‘little’.

bcd()[source]

Return a binary-coded bytes object with this EAN

fmt = '##-#####-#####-#'
classmethod from_bcd(bcd_bytes)[source]

Create an EAN object from a binary coded bytes-like object

The EAN is automatically shortened to the correct length.

classmethod from_hilo(hilo)[source]

Create an EAN object from a pair of 32-bit integers, (eanHi, eanLo)

classmethod from_string(ean_string)[source]

Create an EAN object from a specially formatted string

Deprecated since version 1.6: Use the constructor, EAN(ean_string), instead.

hilo()[source]

Return a pair of 32-bit integers, (eanHi, eanLo), with this EAN

num_digits = 13
product()[source]

Return only the product code and check digit of the string representation

s = '#'
exception canlib.ean.IllegalEAN[source]

Bases: exceptions.ValueError

Could not parse EAN

canlib.ean.bcd_digits(bcd_bytes)[source]

Split a byte sequence into four-bit BCD digits

Used internally by EAN to decode BCD.

For example 0x12345 is turned into 1, 2, 3, 4, 5.

bcd_bytes must be an iterable of eight bit objects supporting & and >>.

Note

The byteorder is currently assumed to be ‘little’.

canlib.ean.int_from_digits(digits, base=10)[source]

Joins a sequence of decimal digits into a decimal number

Used internally by EAN.

For example (1, 2, 3, 4, 5) is turned into 54321.

Iterating through digits is assumed to only yield integers between 0 and 9, inclusive.