Skip to main content

Python utility to encode/decode GS1 EPCs

Project description

Python EPC toolkit

This package provides utilities for building, encoding, decoding and translating EPCs.

Installation

pip install pyepc

Usage

SGTIN

# Encoding
# --------

# Always import from the root of the package
>>> from pyepc import SGTIN

# You can build an epc object in many ways. If you are starting
# from an application that manages items and GS1 company prefixes
# then building an SGTIN from the components is the likely path
>>> company_prefix = '0614141'
>>> indicator = '8'
>>> item_ref = '12345'
>>> serial = '12345'
>>> sgtin = SGTIN(company_prefix, indicator, item_ref, serial)

# Get pure identity URI
>>> sgtin.pure_identity_uri
'urn:epc:id:sgtin:0614141.812345.12345'

# Get GS1 element string
>>> sgtin.gs1_element_string
'(01)80614141123458(21)12345'

# Get a GTIN from the EPC
>>> sgtin.gtin
'80614141123458'

# You can also build a SGTIN object from the GTIN
# if a GTIN is what you have as a starting point
>>> sgtin = SGTIN.from_sgtin('80614141123458', serial_number='123456')
'<urn:epc:id:sgtin:0614141.812345.123456>'

# Get the tag URI
>>> sgtin.get_tag_uri()
'urn:epc:tag:sgtin-96:1.0614141.812345.12345'

# The sgtin-96 scheme was automatically selected as the most
# efficient binary encoding scheme for a numeric serial
# number.

# To explicitly use another encoding scheme like 'sgtin-198',
# specify the encoding scheme
>>> sgtin.get_tag_uri(SGTIN.BinarySchemes.SGTIN_198)
'urn:epc:tag:sgtin-198:1.0614141.812345.12345'

# You can also change the filter value. In this case
# 1 (for POS item) was used as the default
>>> sgtin.get_tag_uri(
...    SGTIN.BinarySchemes.SGTIN_198,
...    SGTIN.FilterValues.UNIT_LOAD,
... )
'urn:epc:tag:sgtin-198:6.0614141.812345.12345'
# The filter value is now 6

# If you want to encode the EPC into the EPC bank of an RFID
# tag, you will need the hex encoded value of the tag uri.
>>> sgtin.encode()
'3034257BF7194E4000003039'

# Similar to the `get_tag_uri` methods, you can enforce which
# scheme should be used and the filter value
>>> sgtin.encode(
...     SGTIN.BinarySchemes.SGTIN_198,
...     SGTIN.FilterValues.UNIT_LOAD,
... )
'36D4257BF7194E58B266D1A800000000000000000000000000'

# Decoding
# --------
>>> SGTIN.decode('36D4257BF7194E58B266D1A800000000000000000000000000')
'<urn:epc:id:sgtin:0614141.812345.12345>'

# EPC from GTIN
# -------------
# If all what you have is a GTIN, then you can build an EPC from it

>>> SGTIN.from_sgtin('80614141123458', '6789AB')
'<urn:epc:id:sgtin:0614141.812345.6789AB>'

# However, this has to lookup the company prefix length from the GS1
# prefix list and could be expensive the first time. So if you already
# know your company prefix length, then pass that along

>>> company_prefix_len = len('0614141')
>>> SGTIN.from_sgtin('80614141123458', '6789AB', company_prefix_len)
'<urn:epc:id:sgtin:0614141.812345.6789AB>'

SSCC

# Encoding
# --------

# Always import from the root of the package
>>> from pyepc import SSCC

# Build an SSCC object from the company prefix, extension digit
# and a serial reference for the logistics unit
>>> company_prefix = '0614141'
>>> extension_digit = '1'
>>> serial_ref = '234567890'
>>> sscc = SSCC(company_prefix, extension_digit, serial_ref)

# Get pure identity URI
>>> sscc.pure_identity_uri
'urn:epc:id:sscc:0614141.1234567890'

# Get GS1 element string
>>> sscc.gs1_element_string
'(00)106141412345678908'

# Get the tag URI
>>> sscc.get_tag_uri()
'urn:epc:tag:sscc-96:0.0614141.1234567890'

# If you want to encode the EPC into the EPC bank of an RFID
# tag, you will need the hex encoded value of the tag uri.
>>> sscc.encode()
'3114257BF4499602D2000000'

# Decoding
# --------

>>> sscc.decode('3114257BF4499602D2000000')
'<urn:epc:id:sscc:0614141.1234567890>'

# EPC from SSCC Code
# ------------------

>>> SSCC.from_sscc('106141412345678908')
'<urn:epc:id:sscc:0614141.1234567890>'

However, this has to lookup the company prefix length from the GS1
prefix list and could be expensive the first time. So if you already
know your company prefix length, then pass that along

>>> company_prefix_len = len('0614141')
>>> SSCC.from_sscc('106141412345678908', company_prefix_len)
'<urn:epc:id:sscc:0614141.1234567890>'

Decoding EPC from Hex value in an EPC

If you want to convert the EPC Hex back into an EPC object, you can use the decode method.

If you don't know the type of the code, then use the decode method

from pyepc import decode
sgtin = decode('3034257BF7194E4000003039')

sgtin.company_prefix
# '0614141'

sgtin.item_ref
# '12345'

sgtin.serial_number
# '12345'

Additional Resources

Tag translation between UPC and EPC

History

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyepc-0.0.3.tar.gz (13.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyepc-0.0.3-py2.py3-none-any.whl (13.1 kB view details)

Uploaded Python 2Python 3

File details

Details for the file pyepc-0.0.3.tar.gz.

File metadata

  • Download URL: pyepc-0.0.3.tar.gz
  • Upload date:
  • Size: 13.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.7

File hashes

Hashes for pyepc-0.0.3.tar.gz
Algorithm Hash digest
SHA256 5cf98735620c48dcc24422af49e959c09bb6cb38418f11b6932f7e0540dc58ef
MD5 06a73dc2927a6c3cf8c18dd45e69335f
BLAKE2b-256 4541f069139b75be81b87e33739682daed0f0dc60e9ffe0b6adc31514188fc80

See more details on using hashes here.

File details

Details for the file pyepc-0.0.3-py2.py3-none-any.whl.

File metadata

  • Download URL: pyepc-0.0.3-py2.py3-none-any.whl
  • Upload date:
  • Size: 13.1 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.7

File hashes

Hashes for pyepc-0.0.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 3baec293bb8f206a2467a5b8b70a161e829f89c50e67e39ceba4c649fe0a7923
MD5 5febbecf0ce86f0110e57b2a4c4acff0
BLAKE2b-256 ed1b175792f23f37ae8f9ac161cd646edee6468fcb6ce06ae1565ab0fcabc1c5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page