Package: leap-protocol
L3aP for PythonLegible encoding for addressable packets for pythonSpecification documentation:https://leap-protocol.github.io/Installationpip install leap-protocolBasic UsageEncoding a packet:importleap-protocolasleapcodec=leap.Codec("leap-config.json")packet=leap.Packet("set","led/red",True)encoded=codec.encode(packet)...Decoding a packetimportleap-protocolasleapcodec=leap.Codec("leap-config.json")...# Note, if there is a remainder it will be stored back in bytesreceived,packet=codec.Decode(received)data=codec.unpack(packet)forbranch,valueindata.items():...dostuff......UsageCodec Classcodec = Codec(config_file_path)config_file_patha string to point to the L3aP config file.codecL3aP codec objectInstantiates a L3aP codec object for encoding packets to strings and decoding strings to packets.Example:codec=leap.Codec("leap-config.json")bytes = encode(packets)packetseither aleap.Packetobject or a list ofleap.packetobjects.bytesutf-8 byte stringEncodes 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)bytesutf-8 encoded byte-stringremainderunused bytes (if available)packetsan array of one or more decoded packets, empty if noneDecodes a utf-8 byte string into one or more packetsExample:received_bytes+=rx.read()received_bytes,packets=codec.decode(received_bytes)forpacketinpackets:...data = unpack(packet)packetaleap.Packetdataa 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:ifpacket.category=="set":commands=codec.unpack(packet)if'led/red'incommands:led_red.set(commands['led/red']...Packet Classpacket = Packet(category,path,payload)categorythe type of packetpath(optional) a root path of payload datapayload(optional) the data to accompany the root pathpacketa L3aP packet objectConstructs 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)patha root path of payload datapayload(optional) the data to accompany the root pathAdds 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)...categoryThe packet's category string.Example:ifpacket.category=="pub":update_model(codec.unpack(packet))...Verificationresult = verify(config_file)config_filea valid L3aP config fileresultfalse if config_file is invalid, true otherwiseChecks the contents of a config_file for errors. Prints details of the first failure to stdout. Useful for regression testing.Example:...deftest_valid_config(self):assert(leap.verify("leap-config.json"))...Command LineGenerate a default json config file:python3 -m leap --json filename.tomlGenerate a default toml config file:python3 -m leap --toml filename.tomlVerify the contents of your toml/json config file:python3 -m leap --validate filename.jsonHelp:python3 -m leap --help