rattail.gpc

Global Product Code

class rattail.gpc.GPC(value, calc_check_digit=False)[source]

Class to abstract the details of Global Product Code data. Examples of this would be UPC or EAN barcodes.

The initial motivation for this class was to provide better SIL support. To that end, the instances are assumed to always be comprised of only numeric digits, and must include a check digit. If you do not know the check digit, provide a calc_check_digit value to the constructor.

Parameters:
  • value – Must be either an integer or a long value, or (most commonly) a string containing only digits.

  • calc_check_digit

    Controls if/how check digit should be calculated.

    Default is False which means do not calculate a check digit (i.e. assume it is already present in value).

    You can specify the string 'upc' to force calculation of check digit using the standard UPC algorithm.

    Or specify the string 'auto' to invoke automagic logic which tries to guess whether or not the given value has/needs a check digit. Please note, this is not fool-proof so you should avoid if possible.

property data_length

Returns the length of the “data” for the barcode. This is just a convenience that returns len(self.data_str).

property data_str

Returns the “data” for the barcode as unicode string, i.e. minus check digit and also with all leading zeroes removed. A visual is maybe helpful here:

>>> upc = GPC('7430500132', calc_check_digit='upc')
>>> print(repr(upc))
GPC('00074305001321')
>>> print(repr(upc.data_str))
u'7430500132'

Note that in this case the data_str value is the same as was originally provided to the constructor, but that isn’t always the case.

pretty()[source]

Returns the UPC as a somewhat more human-readable string. Basically that just means the check digit is distinguished by a hyphen.

property type2_upc

Returns boolean indicating whether the barcode has “type 2” UPC data.