Skip to main content

Python library for TLV objects

Project description

TLV Python Parser - Version 0.4.0

A Tag-Length-Value (also known as Type-Length-Value) is an encoding scheme used for many protocols.

The tag and length are fixed in size (varying from 1 to 4 bytes) and the value field is of variable size.

The fields are:

  • Tag: An alphanumeric code that represents the kind of field the object represents;
  • Length: Size of the value field (in bytes);
  • Value: Variable-sized series of bytes which contains data for this field object.

Advantages of using TLV:

  • Sequences are usually easy to parse;
  • Unknown tags or elements can be skipped or ignored, so new versions can be added without a problem;
  • Elements can be placed in any order;
  • New elements can be created without breaking the protocol itself or the parsing function.

For more information, you can see: https://en.wikipedia.org/wiki/Type-length-value

Installation

You can install directly from PyPI:

  pip install uttlv

Or download the source code and install using pip:

  pip install .

How to use

To start using this package, just import the package and create an object

  from uttlv import TLV

  # Create object
  t = TLV()

To add a tag to object, do it like a dict value:

  # A tag of int value
  t[0x01] = 10
  # A tag of string value
  t[0x02] = 'test'
  # A tag of an array of bytes
  t[0x03] = bytes([1, 2, 3])
  # Or another TLV object
  another_one = TLV()
  another_one[0x05] = 234
  t[0x04] = another_one

A tag can only be int, str, bytes or a TLV itself. Any other type will raise a TypeError exception. If a tag is inserted and another object with same tag value already exists on the object, the tag will be overriden with the new value.

To get the underlying array, just call to_byte_array() method:

  arr = t.to_byte_array()
  print('TLV:', arr)

Parse

To parse an array, just call the method parse_array():

  # create object
  t = TLV()
  # parse from object
  data = bytes([0x03, 0x00, 0x04, 0x00, 0x00, 0x00, 0x0A])
  t.parse_array(data)

Pretty print

If you call tree(), the object will create a string with a tree-like structure to print:

  from prtlv import TLV

  # Create object
  t = TLV()
  # Add value
  t[0x01] = 10
  # Print it
  print('Value:\n', t.tree())
  ## <output>
  ## Value: 
  ## 1: 10
  ##

Tag map

You can also add a dictionary to map a tag to its underline class type, so it's showed as correct type instead of a bytearray.

The dictionay must have all keys as the tag values and its respective values as the class type of the tag:

  config = {
    0x01: {'type': 'int', 'name': 'NUM_POINTS'},
    0x02: {'type': 'int', 'name': 'IDLE_PERIOD'},
    0x03: {'type': 'str', 'name': 'NAME'},
    0x04: {'type': 'str', 'name': 'CITY'},
    0x05: {'type': 'bytes', 'name': 'VERSION'},
    0x06: {'type': 'bytes', 'name': 'DATA'},
    0x07: {'type': 'TLV', 'name': 'RELATED'},
    0x08: {'type': 'TLV', 'name': 'COMMENT'}
  }

  # Set map
  TLV.set_tag_map(config)

For now, only 'int', 'str', 'bytes' and 'TLV' are accepted as valid classes. Any other class will raise AttributeError.

If a tag map is configured, one can use the tag name to access its value:

 t = TLV()
 t['NUM_POINTS'] = 10
 print(t['NUM_POINTS'])

And also can print it with all tag names instead of values:

 t.tree(use_names=True)
 ## <output>
 ## NUM_POINTS: 10

You can access also the tags directly:

 t = TLV()
 t['NUM_POINTS'] = 10
 print(t.NUM_POINTS)

To-Do Features

  • Different tag length simultaneously

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

uttlv-0.4.0.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

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

uttlv-0.4.0-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file uttlv-0.4.0.tar.gz.

File metadata

  • Download URL: uttlv-0.4.0.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/47.3.1 requests-toolbelt/0.8.0 tqdm/4.46.1 CPython/3.8.2

File hashes

Hashes for uttlv-0.4.0.tar.gz
Algorithm Hash digest
SHA256 3dec8aa72cbddd9963ed126108bc61c27c771f445f1111fbd95e4401b27451f6
MD5 579a8db3d55d022a4225dc2325c6095a
BLAKE2b-256 c2a93ae2887c8c20bf38e915835c10513febcb276989fdd11d5defa90858022b

See more details on using hashes here.

File details

Details for the file uttlv-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: uttlv-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 6.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/47.3.1 requests-toolbelt/0.8.0 tqdm/4.46.1 CPython/3.8.2

File hashes

Hashes for uttlv-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1a68ab22c62ac35c4fc4163613f5f308feeb8dac74310bca05445f76db781285
MD5 2d0587b41b14023a1868291d6868c6b7
BLAKE2b-256 28ab6f3a5614dddc5444f5d4b8460f29c385bd3016dafcc042ec0fda730403e2

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