Package dap :: Module xdr :: Class DapPacker
[hide private]
[frames] | no frames]

Class DapPacker

source code

object --+
         |
        DapPacker

Pack variable data into XDR.

This is a faster reimplementation of xdrlib using the native module "array".
>>> from dap.dtypes import *
>>> dapvar = BaseType(data=1, type='Int32')
>>> xdrdata = DapPacker(dapvar)
>>> for i in xdrdata:
...     print repr(i)
'\x00\x00\x00\x01'
>>> dapvar = ArrayType(data=['one', 'two'], shape=[2], type='String')
>>> xdrdata = DapPacker(dapvar)
>>> for i in xdrdata:
...     print repr(i)
'\x00\x00\x00\x02'
'\x00\x00\x00\x03one\x00'
'\x00\x00\x00\x03two\x00'
>>> dapvar = ArrayType(data=range(2), shape=[2], type='Int32')
>>> xdrdata = DapPacker(dapvar)
>>> for i in xdrdata:
...     print repr(i)
'\x00\x00\x00\x02\x00\x00\x00\x02'
'\x00\x00\x00\x00\x00\x00\x00\x01'
>>> dapvar = ArrayType(data=range(2), shape=[2], type='Float64')
>>> xdrdata = DapPacker(dapvar)
>>> for i in xdrdata:
...     print repr(i)
'\x00\x00\x00\x02\x00\x00\x00\x02'
'\x00\x00\x00\x00\x00\x00\x00\x00?\xf0\x00\x00\x00\x00\x00\x00'


Instance Methods [hide private]
  __init__(self, dapvar, data=None)
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
  __iter__(self)
Iterate over the XDR encoded data.
  _pack_length(self)
Yield array length.
  _yield_bytes(self)
Yield bytes.
  _pack_string(self, s)
Pack a string.

Inherited from object: __delattr__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __str__


Properties [hide private]

Inherited from object: __class__


Method Details [hide private]

__init__(self, dapvar, data=None)
(Constructor)

source code 
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
Overrides: object.__init__
(inherited documentation)

__iter__(self)

source code 
Iterate over the XDR encoded data.

_pack_length(self)

source code 
Yield array length.

_yield_bytes(self)

source code 

Yield bytes.

Bytes are encoded as is, padded to a four-byte boundary. An array of five bytes, eg, is encoded as eight bytes:
>>> from dap.dtypes import *
>>> dapvar = ArrayType(data=range(5), shape=[5], type='Byte')
>>> xdrdata = DapPacker(dapvar)
>>> for i in xdrdata:
...     print repr(i)
'\x00\x00\x00\x05\x00\x00\x00\x05'
'\x00'
'\x01'
'\x02'
'\x03'
'\x04'
'\x00\x00\x00'
Again, the first line correponds to the array size packed twice, followed by the bytes and the padding.

_pack_string(self, s)

source code 

Pack a string.

We first pack the string length, followed by the string padded to size 4n.