Package lepl :: Package bin :: Module encode
[hide private]
[frames] | no frames]

Module encode

source code

Convert structured Python data to a binary stream.

Writing a good API for binary encoding of arbitrary objects does not seem to be easy. In addition, this is my first attempt. My apologies in advance. This is a very basic library - the hope is that something like ASN.1 can then be built on this (if someone buys me a copy of the spec...!)

The most obvious solution might be to require everything that must be encoded implement some method. Given Python's dynamic nature, ABCs, etc, this might be possible, but it does seem that it could require some rather ugly hacks in some cases, when using existing types.

The next simplest approach seems to be to use some kind of separate dispatch (rather than the classes themselves) to convert things to a standard intermediate fmt. That is what I do here. The intermediate fmt is the pair (type, BitString), where "type" can be any value (but will be the type of the value in all implementations here - value could be used, but we're trying to give some impression of a layered approach).

Encoding a structure then requires three steps:

  1. Defining a serialisation of composite structures. Only acyclic structures are considered (I am more interested in network protocols than pickling, which already has a Python solution)
  2. Converting individual values in the serial stream to the intermediate representation.
  3. Encoding the intermediate representation into a final BitString.

Support for each of these steps is provided by LEPL. Stage 1 comes from the graph and node modules; 2 is provided below (leveraging BitString's class methods); 3 is only supported in a simple way below, with the expectation that future modules might extend both encoding and matching to, for example, ASN.1.

Functions [hide private]
 
dispatch_table(big_endian=True, encoding=None, errors=STRICT)
Convert types appropriately.
source code
 
make_converter(table)
Given a table, create the converter.
source code
 
simple_serialiser(node, table)
Serialize using the given table.
source code