12.9.1 Packer Objects

Packer instances have the following methods:

Returns the current pack buffer as a string.

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().

Packs the single-precision floating point number 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().

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().

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

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.

See About this document... for information on suggesting changes.