### Install gtin Package Source: https://github.com/edsono/gtin/blob/master/docs/index.md This command installs the 'gtin' Python package using pip. Ensure you have pip installed and accessible in your environment. ```bash $ pip install gtin ``` -------------------------------- ### Get GTIN Component Parts as Tuple Source: https://github.com/edsono/gtin/blob/master/docs/index.md Illustrates how to retrieve the component parts of a GTIN instance as a tuple. The tuple contains the indicator digit, GCP, item reference, and check digit. ```python from gtin import GTIN # Get the component parts of a GTIN instance as a tuple containing GTIN.indicator_digit, GTIN.gcp, GTIN.item_reference, and GTIN.check_digit: print(tuple(GTIN(raw='0400010161360'))) # ('0', '4000101', '61360', '0') ``` -------------------------------- ### Get GS1 Company Prefix (GCP) from GTIN Source: https://github.com/edsono/gtin/blob/master/docs/index.md Shows how to access the GS1 Company Prefix (GCP) of a GTIN object. The GCP is a component of the GTIN used for company identification. ```python from gtin import GTIN # Get the GCP of a GTIN: print(GTIN('00041333704647').gcp) # 0041333 print(GTIN('00811068011972').gcp) # 081106801 print(GTIN('00188781000171').gcp) # 0188781000 ``` -------------------------------- ### Initialize GTIN with Valid String Source: https://github.com/edsono/gtin/blob/master/docs/index.md Demonstrates initializing a GTIN object with a complete, valid GTIN string. The string representation will match the input. ```python from gtin import GTIN # Given a valid GTIN str for gtin, the return value of str(GTIN(gtin)) is equal to gtin. print(str(GTIN('04000101613600'))) # 04000101613600 ``` -------------------------------- ### Initialize GTIN without arguments (Python) Source: https://github.com/edsono/gtin/blob/master/README.rst Demonstrates initializing a GTIN object without any arguments. The default representation is a 14-digit string of zeros. This is useful for creating a placeholder or default GTIN object. ```python from gtin import GTIN # A GTIN initialized without any arguments: print(repr(GTIN())) ``` -------------------------------- ### Initialize GTIN with String and Specified Length Source: https://github.com/edsono/gtin/blob/master/docs/index.md Demonstrates initializing a GTIN object with a string and specifying the GTIN length. This is useful for ensuring a specific GTIN format. ```python from gtin import GTIN print(str(GTIN('74470101505',length=14))) # 00074470101505 ``` -------------------------------- ### Initialize GTIN with raw value and specified length (Python) Source: https://github.com/edsono/gtin/blob/master/README.rst Shows how to initialize a GTIN object with both a raw GTIN value (as an integer) and a specific length. The package uses this length to format the GTIN and calculate the check digit. ```python from gtin import GTIN # Given a GTIN, and a length: print(str(GTIN(raw=7447010150,length=12))) ``` -------------------------------- ### Create and Validate GTINs in Python Source: https://context7.com/edsono/gtin/llms.txt Demonstrates how to create GTIN objects from various input formats (strings, integers, floats, Decimals) and how the library automatically validates check digits. It also shows how to handle invalid check digits using a try-except block. ```python from gtin import GTIN, GTINError, CheckDigitError, GCPNotFoundError # Create GTIN from complete barcode string (validates check digit) gtin = GTIN('04000101613600') print(str(gtin)) # Output: 04000101613600 # Create GTIN from formatted string (dashes/spaces stripped) gtin = GTIN('0-4000101-6136-00') print(str(gtin)) # Output: 04000101613600 # Create GTIN from integer (pads to 14 digits by default) gtin = GTIN(74470101505) print(str(gtin)) # Output: 00074470101505 # Create GTIN with specific length gtin = GTIN(74470101505, length=12) print(str(gtin)) # Output: 074470101505 # Handle invalid check digit try: GTIN('04000101613601') # Wrong check digit except CheckDigitError as e: print(f"Invalid GTIN: {e}") # Output: Invalid GTIN: This GTIN ("04000101613601") has an invalid check-digit ("1"). # The correct check-digit would be "0". ``` -------------------------------- ### Initialize GTIN with Raw Value and Specified Length Source: https://github.com/edsono/gtin/blob/master/docs/index.md Shows how to initialize a GTIN object with a raw value and explicitly specify the desired GTIN length. ```python from gtin import GTIN # Given a GTIN, and a length: print(str(GTIN(raw=7447010150,length=12))) # 074470101505 ``` -------------------------------- ### Initialize GTIN with Integer Value Source: https://github.com/edsono/gtin/blob/master/docs/index.md Demonstrates initializing a GTIN object directly with an integer. The package handles the conversion and check digit calculation. ```python from gtin import GTIN print(str(GTIN(74470101505))) # 00074470101505 ``` -------------------------------- ### GTIN Class - Creating and Validating GTINs Source: https://context7.com/edsono/gtin/llms.txt Demonstrates how to create and validate GTIN objects from various input formats, including strings, integers, and formatted strings. It also shows how to handle check digit errors. ```APIDOC ## GTIN Class - Creating and Validating GTINs ### Description The `GTIN` class is the primary interface for working with Global Trade Item Numbers. It accepts various input formats including strings, integers, floats, and Decimals, automatically handling check digit validation and calculation. Non-numeric characters in string inputs are automatically stripped, making it tolerant of formatted barcodes with dashes or spaces. ### Method `GTIN(value, length=None)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **value** (str, int, float, Decimal) - The GTIN value or raw barcode. - **length** (int, optional) - The desired length of the GTIN (e.g., 8, 12, 13, 14). ### Request Example ```python from gtin import GTIN, GTINError, CheckDigitError, GCPNotFoundError # Create GTIN from complete barcode string (validates check digit) gtin = GTIN('04000101613600') print(str(gtin)) # Output: 04000101613600 # Create GTIN from formatted string (dashes/spaces stripped) gtin = GTIN('0-4000101-6136-00') print(str(gtin)) # Output: 04000101613600 # Create GTIN from integer (pads to 14 digits by default) gtin = GTIN(74470101505) print(str(gtin)) # Output: 00074470101505 # Create GTIN with specific length gtin = GTIN(74470101505, length=12) print(str(gtin)) # Output: 074470101505 # Handle invalid check digit try: GTIN('04000101613601') # Wrong check digit except CheckDigitError as e: print(f"Invalid GTIN: {e}") # Output: Invalid GTIN: This GTIN ("04000101613601") has an invalid check-digit ("1"). # The correct check-digit would be "0". ``` ### Response #### Success Response (200) - **GTIN object** - A GTIN object representing the validated or created GTIN. #### Response Example ```json { "gtin_string": "04000101613600" } ``` ``` -------------------------------- ### Initialize GTIN with Integer Raw Value Source: https://github.com/edsono/gtin/blob/master/docs/index.md Illustrates initializing a GTIN object using an integer for the 'raw' parameter. The length defaults to 14 if not specified. ```python from gtin import GTIN # Given an int for the parameter raw, the length defaults to 14. print(str(GTIN(raw=7447010150))) # 00074470101505 ``` -------------------------------- ### Initialize GTIN with Integer Value and Specified Length Source: https://github.com/edsono/gtin/blob/master/docs/index.md Illustrates initializing a GTIN object with an integer value and explicitly setting the GTIN length. ```python from gtin import GTIN print(str(GTIN(74470101505,length=12))) # 074470101505 ``` -------------------------------- ### Calculate Check Digit from Raw GTIN Source: https://github.com/edsono/gtin/blob/master/docs/index.md Illustrates initializing a GTIN object with a raw GTIN string. The package automatically calculates and appends the correct check digit. ```python from gtin import GTIN # Given a raw GTIN, the check-digit is calculated and appended. print(str(GTIN(raw='0978289450809'))) # 09782894508091 ``` -------------------------------- ### Lookup GCP Prefix Lengths (Python) Source: https://context7.com/edsono/gtin/llms.txt Retrieve a mapping of GS1 prefixes to their corresponding Company Prefix (GCP) lengths using the `prefixes_lengths` function. It can fetch the latest list from GS1's server or use a local cached copy. ```python from gtin.gcp import prefixes_lengths, GCP_PREFIX_FORMAT_LIST_URL, GCP_PREFIX_FORMAT_LIST_PATH # Get prefix-to-length mapping (fetches from GS1 server) prefix_map = prefixes_lengths() print(prefix_map['03321']) # Output: 6 (GS1 US prefix) print(prefix_map['081']) # Output: 9 # Use local cached copy only (no network request) prefix_map_local = prefixes_lengths(local=True) print(prefix_map_local['02']) # Output: 0 (restricted distribution) # View the GS1 server URL and local cache path print(f"Remote URL: {GCP_PREFIX_FORMAT_LIST_URL}") # Output: http://www.gs1.org/docs/gcp_length/gcpprefixformatlist.xml print(f"Local cache: {GCP_PREFIX_FORMAT_LIST_PATH}") # Output: /path/to/gtin/gcp/GCPPrefixFormatList.xml ``` -------------------------------- ### GTIN Class - Calculating Check Digits from Raw Input Source: https://context7.com/edsono/gtin/llms.txt Explains how to use the `raw` parameter to create a GTIN from a barcode string that omits the check digit. The library automatically calculates and appends the correct check digit. ```APIDOC ## GTIN Class - Calculating Check Digits from Raw Input ### Description The `raw` parameter allows creating a GTIN from a barcode without the check digit, automatically calculating and appending the correct check digit using the GS1 mod-10 algorithm. ### Method `GTIN(raw=value, length=None)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **raw** (str, int, float, Decimal) - The raw GTIN value without the check digit. - **length** (int, optional) - The desired length of the GTIN (e.g., 8, 12, 13, 14). ### Request Example ```python from gtin import GTIN # Calculate check digit from raw GTIN (without check digit) gtin = GTIN(raw='0978289450809') print(str(gtin)) # Output: 09782894508091 (check digit '1' calculated) # Raw with integer input (defaults to 14-digit GTIN) gtin = GTIN(raw=7447010150) print(str(gtin)) # Output: 00074470101505 # Raw with specific length for UPC-12 gtin = GTIN(raw=7447010150, length=12) print(str(gtin)) # Output: 074470101505 # Raw for GTIN-8 gtin = GTIN(raw='0123456', length=8) print(str(gtin)) # Output: 01234565 # Access the calculated check digit directly gtin = GTIN(raw=123456789012) print(gtin.check_digit) # Output: 8 ``` ### Response #### Success Response (200) - **GTIN object** - A GTIN object with the calculated check digit appended. #### Response Example ```json { "gtin_string": "09782894508091" } ``` ``` -------------------------------- ### Initialize and Represent GTIN Object Source: https://github.com/edsono/gtin/blob/master/docs/index.md Demonstrates initializing a GTIN object without arguments and displaying its string representation. The default representation is a 14-digit GTIN. ```python from gtin import GTIN # A GTIN initialized without any arguments: print(repr(GTIN())) # GTIN('00000000000000') ``` -------------------------------- ### Construct GTIN from Components (Python) Source: https://context7.com/edsono/gtin/llms.txt Build a GTIN object by providing its constituent parts: indicator digit, GS1 Company Prefix (GCP), item reference, and optionally the check digit for validation. The indicator digit is only valid for GTIN-14. ```python from gtin import GTIN, CheckDigitError, IndicatorDigitError # Build GTIN from GCP and item reference gtin = GTIN(gcp='4000101', item_reference='61360') print(str(gtin)) # Output: 04000101613600 # Build with indicator digit (must be GTIN-14) gtin = GTIN(indicator_digit='1', gcp='4000101', item_reference='61360') print(str(gtin)) # Output: 14000101613609 # Build and validate check digit try: gtin = GTIN(gcp='4000101', item_reference='61360', check_digit='5') except CheckDigitError as e: print(f"Check digit mismatch: {e}") # Indicator digit only valid for GTIN-14 try: gtin = GTIN(indicator_digit='1', gcp='4000101', item_reference='61360', length=12) except IndicatorDigitError as e: print(f"Invalid: {e}") # Indicator digit only applies to GTIN-14 ``` -------------------------------- ### Convert GTIN to String Source: https://github.com/edsono/gtin/blob/master/docs/index.md Shows how to convert a GTIN object to its string representation. This is typically done before using the GTIN in other applications. ```python from gtin import GTIN # Typical usage will require converting your GTIN to a str prior to use in your application. print(str(GTIN())) # 00000000000000 ``` -------------------------------- ### Calculate GTIN Check Digits in Python Source: https://context7.com/edsono/gtin/llms.txt Illustrates how to use the `raw` parameter to create a GTIN from a barcode string or integer that omits the check digit. The library automatically calculates and appends the correct check digit using the GS1 mod-10 algorithm. ```python from gtin import GTIN # Calculate check digit from raw GTIN (without check digit) gtin = GTIN(raw='0978289450809') print(str(gtin)) # Output: 09782894508091 (check digit '1' calculated) # Raw with integer input (defaults to 14-digit GTIN) gtin = GTIN(raw=7447010150) print(str(gtin)) # Output: 00074470101505 # Raw with specific length for UPC-12 gtin = GTIN(raw=7447010150, length=12) print(str(gtin)) # Output: 074470101505 # Raw for GTIN-8 gtin = GTIN(raw='0123456', length=8) print(str(gtin)) # Output: 01234565 # Access the calculated check digit directly gtin = GTIN(raw=123456789012) print(gtin.check_digit) # Output: 8 ``` -------------------------------- ### Extract GS1 Company Prefix (GCP) in Python Source: https://context7.com/edsono/gtin/llms.txt Shows how to extract the GS1 Company Prefix (GCP) from a GTIN object using the `.gcp` property. It covers varying GCP lengths and handling cases where the GCP might not be found. ```python from gtin import GTIN, GCPNotFoundError # Extract GCP from various GTINs (length varies by company) gtin1 = GTIN('00041333704647') print(gtin1.gcp) # Output: 0041333 (7-digit GCP) gtin2 = GTIN('00811068011972') print(gtin2.gcp) # Output: 081106801 (9-digit GCP) gtin3 = GTIN('00188781000171') print(gtin3.gcp) # Output: 0188781000 (10-digit GCP) # Restricted distribution GTINs (prefix 02 or 2) have empty GCP restricted = GTIN('02345678901289') print(restricted.gcp) # Output: '' (empty string) # Handle GTINs with unknown GCP prefix try: invalid = GTIN('01345678901280') gcp = invalid.gcp except GCPNotFoundError as e: print(f"GCP not found: {e}") ``` -------------------------------- ### GTIN Class - Extracting GCP (GS1 Company Prefix) Source: https://context7.com/edsono/gtin/llms.txt Shows how to extract the GS1 Company Prefix (GCP) from a GTIN object. The library dynamically determines the GCP length based on GS1 standards. ```APIDOC ## GTIN Class - Extracting GCP (GS1 Company Prefix) ### Description The `gcp` property extracts the GS1 Company Prefix from a GTIN. The GCP length varies based on the prefix registered with GS1, and the library uses an up-to-date prefix format list to determine the correct length. ### Method `GTIN_object.gcp` ### Parameters None ### Request Example ```python from gtin import GTIN, GCPNotFoundError # Extract GCP from various GTINs (length varies by company) gtin1 = GTIN('00041333704647') print(gtin1.gcp) # Output: 0041333 (7-digit GCP) gtin2 = GTIN('00811068011972') print(gtin2.gcp) # Output: 081106801 (9-digit GCP) gtin3 = GTIN('00188781000171') print(gtin3.gcp) # Output: 0188781000 (10-digit GCP) # Restricted distribution GTINs (prefix 02 or 2) have empty GCP restricted = GTIN('02345678901289') print(restricted.gcp) # Output: '' (empty string) # Handle GTINs with unknown GCP prefix try: invalid = GTIN('01345678901280') gcp = invalid.gcp except GCPNotFoundError as e: print(f"GCP not found: {e}") ``` ### Response #### Success Response (200) - **gcp** (str) - The GS1 Company Prefix string. Returns an empty string for restricted distribution GTINs. #### Response Example ```json { "gcp": "0041333" } ``` ``` -------------------------------- ### Convert GTIN to string (Python) Source: https://github.com/edsono/gtin/blob/master/README.rst Shows how to convert a GTIN object to its string representation. This is typically required before using the GTIN in applications. The default string representation is a 14-digit string of zeros. ```python from gtin import GTIN # Typical usage will require converting your GTIN to a str prior to use in your application. print(str(GTIN())) ``` -------------------------------- ### Decompose GTIN into Component Parts in Python Source: https://context7.com/edsono/gtin/llms.txt Demonstrates how to decompose a GTIN into its constituent parts: indicator digit, GS1 Company Prefix (GCP), item reference, and check digit. This can be done by converting the GTIN object to a tuple or by accessing individual properties. ```python from gtin import GTIN # Get all components as tuple gtin = GTIN(raw='0400010161360') components = tuple(gtin) print(components) # Output: ('0', '4000101', '61360', '0') # Access individual components gtin = GTIN('04000101613600') print(f"Indicator digit: {gtin.indicator_digit}") # Output: 0 print(f"GCP: {gtin.gcp}") # Output: 4000101 print(f"Item reference: {gtin.item_reference}") # Output: 61360 print(f"Check digit: {gtin.check_digit}") # Output: 0 print(f"Length: {len(gtin)}") # Output: 14 # Indicator digit meanings for GTIN-14: # '0' = base unit (consumer-level) ``` -------------------------------- ### Handle Non-Numeric Characters in GTIN Source: https://github.com/edsono/gtin/blob/master/docs/index.md Shows that non-numeric characters in the input GTIN string are ignored and discarded during initialization. ```python from gtin import GTIN # Non-numeric characters are ignored/discarded. print(str(GTIN('0-4000101-6136-00'))) # 04000101613600 ``` -------------------------------- ### GTIN Class - Decomposing into Component Parts Source: https://context7.com/edsono/gtin/llms.txt Details how to decompose a GTIN object into its constituent parts: indicator digit, GS1 Company Prefix (GCP), item reference, and check digit. Both iterable conversion and individual property access are shown. ```APIDOC ## GTIN Class - Decomposing into Component Parts ### Description The GTIN class is iterable and can be converted to a tuple containing all component parts: indicator digit, GCP, item reference, and check digit. Individual properties provide direct access to each component. ### Method - `tuple(GTIN_object)` - `GTIN_object.indicator_digit` - `GTIN_object.gcp` - `GTIN_object.item_reference` - `GTIN_object.check_digit` - `len(GTIN_object)` ### Parameters None ### Request Example ```python from gtin import GTIN # Get all components as tuple gtin = GTIN(raw='0400010161360') components = tuple(gtin) print(components) # Output: ('0', '4000101', '61360', '0') # Access individual components gtin = GTIN('04000101613600') print(f"Indicator digit: {gtin.indicator_digit}") # Output: 0 print(f"GCP: {gtin.gcp}") # Output: 4000101 print(f"Item reference: {gtin.item_reference}") # Output: 61360 print(f"Check digit: {gtin.check_digit}") # Output: 0 print(f"Length: {len(gtin)}") # Output: 14 # Indicator digit meanings for GTIN-14: # '0' = base unit (consumer-level) ``` ### Response #### Success Response (200) - **tuple** - A tuple containing ('indicator_digit', 'gcp', 'item_reference', 'check_digit'). - **indicator_digit** (str) - The first digit of the GTIN. - **gcp** (str) - The GS1 Company Prefix. - **item_reference** (str) - The item reference part of the GTIN. - **check_digit** (str) - The calculated check digit. - **length** (int) - The total length of the GTIN. #### Response Example ```json { "components_tuple": ["0", "4000101", "61360", "0"], "indicator_digit": "0", "gcp": "4000101", "item_reference": "61360", "check_digit": "0", "length": 14 } ``` ``` -------------------------------- ### Handle GTIN Exceptions (Python) Source: https://context7.com/edsono/gtin/llms.txt Utilize specific exception classes provided by the GTIN library for robust error handling during barcode validation. These exceptions inherit from a base `GTINError` class for simplified catch-all error management. ```python from gtin import GTIN, GTINError, GTINTypeError, CheckDigitError, IndicatorDigitError, GCPNotFoundError def validate_barcode(barcode): """Validate and parse a barcode with comprehensive error handling.""" try: gtin = GTIN(barcode) return { 'valid': True, 'gtin': str(gtin), 'gcp': gtin.gcp, 'item_reference': gtin.item_reference } except CheckDigitError as e: return {'valid': False, 'error': 'Invalid check digit', 'details': str(e)} except GTINTypeError as e: return {'valid': False, 'error': 'Invalid input type', 'details': str(e)} except GCPNotFoundError as e: return {'valid': False, 'error': 'Unknown company prefix', 'details': str(e)} except GTINError as e: return {'valid': False, 'error': 'Invalid GTIN', 'details': str(e)} # Test various inputs print(validate_barcode('04000101613600')) # Valid print(validate_barcode('04000101613601')) # Invalid check digit print(validate_barcode('not-a-number')) # Invalid input ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.