Package: gtin
gtinA library and CLI for parsing and validating GTINs ("Global Trade
Item Numbers"—also known as UPC/EAN/JAN/ISBN).Installationpip3installgtinUsageThis package can be used as a CLI (command-line-interface) or library.Please see theNotes Concerning GCPif calculating
a correct GCP is important to your usage.CLIgtin ccd | gtin calculate-check-digit$gtinccd-h
usage:gtincalculate-check-digit[-h]GTINgtinccd[-h]GTIN

positionalarguments:GTINAGTINwithoutthecheck-digit

optionalarguments:-h,--helpshowthishelpmessageandexitgtin acd | gtin append-check-digit$gtinacd-h
usage:gtinappend-check-digit[-h][-lLENGTH]GTINgtinacd[-h][-lLENGTH]GTIN

positionalarguments:GTINAGTINwithoutthecheck-digit

optionalarguments:-h,--helpshowthishelpmessageandexit-lLENGTH,--lengthLENGTHThelengthofGTINtoreturngtin vcd | gtin validate-check-digit$gtinvcd-h
usage:gtinvalidate-check-digit[-h]GTINgtinvcd[-h]GTIN

IftheprovidedGTINis*invalid*,thiscommandwillterminatewithanon-zeroexitstatus.

positionalarguments:GTINAGTIN*with*check-digit

optionalarguments:-h,--helpshowthishelpmessageandexitgtin hvcd | gtin has-valid-check-digit$gtinhvcd-h
usage:gtinhas-valid-check-digit[-h]GTINgtinhvcd[-h]GTIN

IftheprovidedGTINis*valid*,thiscommandwillreturn"YES".Ifthe
providedGTINis*invalid*,thiscommandwillreturn"NO".

positionalarguments:GTINAGTIN*with*check-digit

optionalarguments:-h,--helpshowthishelpmessageandexitgtin gcp | gtin get-gcp$gtingcp-h
usage:gtinget-gcp[-h]GTINgtingcp[-h]GTIN

positionalarguments:GTINAGTIN*with*check-digit

optionalarguments:-h,--helpshowthishelpmessageandexitLibrarygtin.calculate_check_digitThis function accepts a GTINwithoutcheck-digit and returns the check-digit.Parameters:unchecked_gtin (str|int): A GTINwithoutcheck-digitNote: If the providedunchecked_gtinis astr, the returned value is astr. If the providedunchecked_gtinis anint, the returned value is anint.Example:>>>fromgtinimportcalculate_check_digit...calculate_check_digit("02345678901289")'4'>>>calculate_check_digit(2345678901289)4gtin.append_check_digitThis function accepts a GTINwithoutcheck-digit and returns the same
GTIN with a check-digit appended.Note: If the providedunchecked_gtinis astr, the returned value is astr. If the providedunchecked_gtinis anint, the returned value is anint.Parameters:unchecked_gtin (str|int): A GTINwithoutcheck-digitExample:>>>fromgtinimportappend_check_digit...append_check_digit("02345678901289")'023456789012894'>>>append_check_digit(2345678901289)23456789012894gtin.has_valid_check_digitThis function accepts a GTINwithcheck-digit and returnsTrueif the
check-digit is valid, orFalseif the check-digit is invalid.>>>fromgtinimporthas_valid_check_digit...has_valid_check_digit("02345678901289")True>>>has_valid_check_digit(2345678901281)False23456789012894gtin.validate_check_digitThis function accepts a GTINwithcheck-digit and raises agtin.CheckDigitErrorif the provided GTIN's check-digit is invalid.>>>fromgtinimportvalidate_check_digit...validate_check_digit("02345678901289")>>>validate_check_digit("02345678901281")Traceback(mostrecentcalllast):File"<stdin>",line1,in<module>File"/Users/David/Code/gtin/gtin/__init__.py",line159,invalidate_check_digitraiseCheckDigitError(gtin,check_digit)gtin.CheckDigitError:"02345678901281"isnotavalidGTIN,thelastdigitis"1",whereasthecorrectcheck-digitwouldbe"9".gtin.get_gcpThis function accepts a GTINwith check-digitand returns the GCP
(GS1 Company Prefix).>>>fromgtinimportget_gcp...get_gcp("00332100000001")'033210'>>>get_gcp(332100000001)'033210'gtin.GTINThis class represents a Global Trade Item Number, and can be used to:Identify a trade item's Indicator-Digit, GCP (GS1 Company Prefix),
Item Reference, and Check-Digit.Validate a GTIN's check-digit.Calculate a check-digit from a raw GTIN.Parameters:gtin(str|int): A string or number representing a GTIN, including the
check-digit.When thegtinparameter is provided, the last (rightmost) digit is used
to validate the GTIN.length(int):The length of the GTIN.If no value is provided forlength, andgtinis astr—lengthis
inferred based on the character length ofgtin.If no value is passed forlength,gtinisNone, andrawis astr—lengthis inferred based
on the length ofraw(adding 1, to account for the absent check-digit).If no length is passed, and none can be inferred fromgtinorraw,lengthdefaults to 14.raw(str|int):A string or number representing the GTIN, excluding the check-digit.If a value is provided for the parametergtin, this parameter is not
used, but is instead derived fromgtin.An instance ofGTINhas the following properties:indicator_digit(str): This is the first (leftmost) digit of a GTIN-14."0" indicates a base unit."1" through "8" are used to define the packaging hierarchy of a product
with the same item reference."9" indicates a variable-measure trade item.gcp(str): The GCP (GS1 Company Prefix) is a globally unique identifier
assigned to an entity (usually a company) by GS1 Member Organizations, in
order to create identifiers within the GS1 System. Company Prefixes, which
vary in length, are comprised of a GS1 Member Organization Prefix followed by
a company/entity-specific number.item_reference(str): The item reference is the part of the GTIN that is
allocated by a GS1 entity to identify a trade item. An item reference varies
in length as a function of a GTIN's GCP length.check_digit(str): The last digit in the GTIN when cast as astr, the
check-digit is a mod-10 algorithm digit used to check for input
errors. To understand how this digit is calculated, refer to:http://www.gs1.org/how-calculate-check-digit-manually.length(int): The number of characters comprising the GTIN
(includingthe check-digit).ExamplesAGTINinitialized without any arguments:>>>fromgtinimportGTIN...print(repr(GTIN()))gtin.GTIN("00000000000000")Typical usage will require converting yourGTINto astrprior to use in
your application.>>>fromgtinimportGTIN...print(str(GTIN()))00000000000000Given a raw GTIN, the check-digit is calculated and appended.>>>fromgtinimportGTIN...print(str(GTIN(raw="0978289450809")))09782894508091Given a valid GTINstrforgtin, the return value ofstr(GTIN(gtin))is
equal togtin.>>>fromgtinimportGTIN...print(str(GTIN("04000101613600")))04000101613600Non-numeric characters are ignored/discarded.>>>fromgtinimportGTIN...print(str(GTIN("0-4000101-6136-00")))04000101613600Given a anintfor the parameterraw, the length defaults to 14.>>>fromgtinimportGTIN...print(str(GTIN(raw=7447010150)))00074470101505>>>print(str(GTIN(74470101505)))00074470101505Given a GTIN, and a length:>>>fromgtinimportGTIN...print(str(GTIN(raw=7447010150,length=12)))074470101505>>>print(str(GTIN(74470101505,length=12)))074470101505>>>fromgtinimportGTIN...print(str(GTIN("74470101505",length=14)))00074470101505Get the GCP of a GTIN:>>>fromgtinimportGTIN...print(GTIN("00041333704647").gcp)0041333>>>print(GTIN("00811068011972").gcp)081106801>>>fromgtinimportGTIN...print(GTIN("00188781000171").gcp)0188781000Get the component parts of aGTINinstance as a tuple containingGTIN.indicator_digit,GTIN.gcp,GTIN.item_reference, andGTIN.check_digit:>>>fromgtinimportGTIN...print(tuple(GTIN(raw="0400010161360")))("0","4000101","61360","0")Notes Concerning GCPIf inferring a correct GCP (GS1 Company Prefix) is important for your usage,
you should update this package periodically:pip3install--upgradegtinWhy? Because GCP allocation is subject to change. When the
GS1 (the organization which governs GCP allocation) publishes a newGCP
Prefix Format List(an XML document specifying GCP lengths according to
variable-length prefix blocks), a new distribution of this package is
automatically packaged and distributed topypi.orgwiththe newGCP Prefix Format List. If you have the most recent version of this
package, you will be calculating GCPs based on the most recentGCP Prefix
Format List.