Skip to main content

Legible Encoding for Addressable Packets

Project description

L3aP for Python

Legible encoding for addressable packets for python

Specification documentation: https://leap-protocol.github.io/

Installation

pip install leap-protocol

Basic Usage

Encoding a packet:

import leap-protocol as leap

codec = leap.Codec("leap-config.json")
packet = leap.Packet("set", "led/red", True)
encoded = codec.encode(packet)

...

Decoding a packet

import leap-protocol as leap

codec = leap.Codec("leap-config.json")

...

# Note, if there is a remainder it will be stored back in bytes
received, packet = codec.Decode(received)
data = codec.unpack(packet)

for branch, value in data.items():
  ... do stuff ...

...

Usage

Codec Class

codec = Codec(config_file_path)

  • config_file_path a string to point to the L3aP config file.
  • codec L3aP codec object

Instantiates a L3aP codec object for encoding packets to strings and decoding strings to packets.

Example:

codec = leap.Codec("leap-config.json")

bytes = encode(packets)

  • packets either a leap.Packet object or a list of leap.packet objects.
  • bytes utf-8 byte string

Encodes one or more packets into a utf-8 byte string.

Example:

packet_red = leap.Packet("set", "led/red", True)
packet_blue = leap.Packet("set", "led/blue", True)

encoded = codec.encode([packet_red, packet_blue])

(remainder, packets) = decode(bytes)

  • bytes utf-8 encoded byte-string
  • remainder unused bytes (if available)
  • packets an array of one or more decoded packets, empty if none

Decodes a utf-8 byte string into one or more packets

Example:

received_bytes += rx.read()
received_bytes, packets = codec.decode(received_bytes)

for packet in packets:
  ...

data = unpack(packet)

  • packet a leap.Packet
  • data a dictionary with address paths as keys (eg. led/red) mapping to thier respective values.

Extracts a dictionary from a packet to map address paths to thier respective values.

Example:

if packet.category == "set":
  commands = codec.unpack(packet)
  if 'led/red' in commands:
    led_red.set(commands['led/red']
    ...

Packet Class

packet = Packet(category, path, payload)

  • category the type of packet
  • path (optional) a root path of payload data
  • payload (optional) the data to accompany the root path
  • packet a L3aP packet object

Constructs a L3aP packet for encoding. Note, payload can be an array and set multiple fields at once when the path is a parent.

Example:

accelerometer_packet = leap.Packet("pub", "imu/accel", [accel_x, accel_y, accel_z])
disable_packet = leap.Packet("set", "control/balance/disable")
...

add(path, payload)

  • path a root path of payload data
  • payload (optional) the data to accompany the root path

Adds path to the packet and optionally a payload. This can be used to create compound packets which allows sets of data to be processed at the same time.

Example:

sensor_packet = leap.Packet("pub", "imu/accel", [accel_x, accel_y, accel_z])
sensor_packet.add("barometer/pressure", baro_pressure)
...

category

The packet's category string.

Example:

if packet.category == "pub":
  update_model(codec.unpack(packet))
...

Verification

result = verify(config_file)

  • config_file a valid L3aP config file
  • result false if config_file is invalid, true otherwise

Checks the contents of a config_file for errors. Prints details of the first failure to stdout. Useful for regression testing.

Example:

...
def test_valid_config(self):
  assert(leap.verify("leap-config.json"))
...

Command Line

Generate a default json config file:

python3 -m leap --json filename.toml

Generate a default toml config file:

python3 -m leap --toml filename.toml

Verify the contents of your toml/json config file:

python3 -m leap --validate filename.json

Help:

python3 -m leap --help

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

leap-protocol-1.0.1.tar.gz (16.3 kB view details)

Uploaded Source

Built Distribution

leap_protocol-1.0.1-py3-none-any.whl (12.9 kB view details)

Uploaded Python 3

File details

Details for the file leap-protocol-1.0.1.tar.gz.

File metadata

  • Download URL: leap-protocol-1.0.1.tar.gz
  • Upload date:
  • Size: 16.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.7.6

File hashes

Hashes for leap-protocol-1.0.1.tar.gz
Algorithm Hash digest
SHA256 76c61f160174f604b63c922dcd00479650df6639b1dca2256a0e6bf7a7d9461d
MD5 48f47959ee94fe8385fc5c6a75c4af9f
BLAKE2b-256 2671b2fb486eb1ce5d441c79040a1580df1f663097f90fd2f91b7c77e04d4a1a

See more details on using hashes here.

File details

Details for the file leap_protocol-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: leap_protocol-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 12.9 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/46.0.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.7.6

File hashes

Hashes for leap_protocol-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0075f879022fc6b897b860e6691c48042e1967d9c3e5f585ee4cd6996ec6a368
MD5 40bd5b14089277a56292a7ba13d9f868
BLAKE2b-256 79528f98670c211175b4137b0933e28e5ba591970622e990fc1cee527158ca26

See more details on using hashes here.

Supported by

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