Legible encoding for addressable packets for python
Legible encoding for addressable packets for python
pip install leap-protocol
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 ...
...
Instantiates a L3aP codec object for encoding packets to strings and decoding strings to packets.
Example:
codec = leap.Codec("leap-config.json")
leap.Packet
object or a list of leap.packet
objects.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])
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:
...
leap.Packet
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']
...
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")
...
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)
...
The packet’s category string.
Example:
if packet.category == "pub":
update_model(codec.unpack(packet))
...
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"))
...
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