EAN¶
- class canlib.EAN(source)[source]¶
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’.
- 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.
- num_digits = 13¶