canlib.ean

class canlib.ean.EAN(decimal)[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.from_string(‘01-2345-67890-1’)

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

EAN.from_bcd(b' ... ')

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

EAN.from_hilo([eanHi, eanLo])

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; from_string supports six-digit (seven-character) input:

EAN.from_string('67890-1')

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()

Note

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

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

Create an EAN object from a bytes-like object

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

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.bcd_int(bcd_bytes)[source]

Calls bcd_digits, and then int_from_digits with the result

Used internally by EAN to decode BCD.

canlib.ean.int_from_digits(digits)[source]

Joins a sequence of decimal digits into a decimal number

Used internally by EAN to decode BCD.

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

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