pyf.transport | PyF, flow-based python programming pyf.transport

pyf.transport

pyf.transport.Packet

class pyf.transport.Packet(data=None, data_type='simple')

A packet is a dictionnary-like object that is easily serializable to simple dictionnaries with base data types.

Its members can be accessed either with attributes or items:

>>> my_packet.foo = 'bar'
>>> my_packet.foo == my_packet['foo']
True
>>> my_packet['foo'] == 'bar'
True

It takes an optionnal dictionnary as argument for initialization, to have its data set as the initialization data for the packet (if data_type is set to “simple”):

>>> my_packet = Packet(dict(foo="bar"))
>>> my_packet['foo'] == 'bar'
True

To serialize, deserialize it, just access the serialized argument:

>>> my_packet.serialized
{'foo': {'metadata': {'compound': False,
              'multi_values': False,
              'type': 'string'},
 'value': 'bar'}}
>>> import datetime
>>> my_complex_packet = Packet(dict(now=datetime.datetime.now(),
...                                 test='bar'))
>>> my_complex_packet
{'test': 'bar', 'now': datetime.datetime(2010, 12, 16, 12, 23, 47, 259568)}
>>> my_complex_packet.serialized
{'now': {'metadata': {'compound': False,
                      'multi_values': False,
                      'type': 'datetime'},
         'value': '2010-12-16T12:23:47.259568'},
 'test': {'metadata': {'compound': False,
                       'multi_values': False,
                       'type': 'string'},
          'value': 'bar'}}
>>> my_new_packet = Packet(data=my_complex_packet.serialized, data_type="serialized")
>>> my_new_packet.now
datetime.datetime(2010, 12, 16, 12, 23, 47)
>>> my_complex_packet.test
'bar'

pyf.transport.encode_packet_flow()

pyf.transport.encode_packet_flow(object_flow, separator='\x00', iterencode=False, compress=False)

Encodes a packet flow to a bytestring flow of json serialized packets.

pyf.transport.decode_packet_flow()

pyf.transport.decode_packet_flow(source, buffersize=16384, separator='\x00', decompress=False, pure_flow=False)

Decodes a packet flow from a source.

If pure_flow is set to False (default), the source is either a file-like of a iterator over strings containing the sepator. If pure_flow is True, the source is a generator containing strings without the separator.

If decompress is set to True, the data is decompressed using zlib.