GNU Info

Info Node: (python2.1-lib.info)Packer Objects

(python2.1-lib.info)Packer Objects


Next: Unpacker Objects Prev: xdrlib Up: xdrlib
Enter node , (file) or (file)node

Packer Objects
--------------

`Packer' instances have the following methods:

`get_buffer()'
     Returns the current pack buffer as a string.

`reset()'
     Resets the pack buffer to the empty string.

In general, you can pack any of the most common XDR data types by
calling the appropriate `pack_TYPE()' method.  Each method takes a
single argument, the value to pack.  The following simple data type
packing methods are supported: `pack_uint()', `pack_int()',
`pack_enum()', `pack_bool()', `pack_uhyper()', and `pack_hyper()'.

`pack_float(value)'
     Packs the single-precision floating point number VALUE.

`pack_double(value)'
     Packs the double-precision floating point number VALUE.

The following methods support packing strings, bytes, and opaque data:

`pack_fstring(n, s)'
     Packs a fixed length string, S.  N is the length of the string but
     it is _not_ packed into the data buffer.  The string is padded
     with null bytes if necessary to guaranteed 4 byte alignment.

`pack_fopaque(n, data)'
     Packs a fixed length opaque data stream, similarly to
     `pack_fstring()'.

`pack_string(s)'
     Packs a variable length string, S.  The length of the string is
     first packed as an unsigned integer, then the string data is packed
     with `pack_fstring()'.

`pack_opaque(data)'
     Packs a variable length opaque data string, similarly to
     `pack_string()'.

`pack_bytes(bytes)'
     Packs a variable length byte stream, similarly to `pack_string()'.

The following methods support packing arrays and lists:

`pack_list(list, pack_item)'
     Packs a LIST of homogeneous items.  This method is useful for
     lists with an indeterminate size; i.e. the size is not available
     until the entire list has been walked.  For each item in the list,
     an unsigned integer `1' is packed first, followed by the data value
     from the list.  PACK_ITEM is the function that is called to pack
     the individual item.  At the end of the list, an unsigned integer
     `0' is packed.

     For example, to pack a list of integers, the code might appear like
     this:

          import xdrlib
          p = xdrlib.Packer()
          p.pack_list([1, 2, 3], p.pack_int)

`pack_farray(n, array, pack_item)'
     Packs a fixed length list (ARRAY) of homogeneous items.  N is the
     length of the list; it is _not_ packed into the buffer, but a
     `ValueError' exception is raised if `len(ARRAY)' is not equal to
     N.  As above, PACK_ITEM is the function used to pack each element.

`pack_array(list, pack_item)'
     Packs a variable length LIST of homogeneous items.  First, the
     length of the list is packed as an unsigned integer, then each
     element is packed as in `pack_farray()' above.


automatically generated by info2www version 1.2.2.9