Info

# Unsigned 32-bit integer

>>> p

) .pack_

hyper(100)

#

Signed 64-bit integer

>>> p

) .pack_

_uhyper(200)

#

Unsigned 64-bit integer

>>> p

) .pack_

enum(3)

#

Enumerated type

>>> p

) .pack_

bool(1)

#

Booleans are 1 or 0

>>> p

) .pack_

_bool("Hi")

#

Value is true, so stores a 1

The pack_fstring(count, str) method packs a fixed-length string count characters long. The function does not store the size of the string, so to unpack it you have to know how long it is beforehand. Better yet, use pack_string(str), which lets you pack a variable-length string:

>>> p.pack_string('Lovely') >>> p.pack_fstring(3,'day')

The pack_string function calls pack_uint with the size of the string and then pack_fstring with the string itself. To more fully follow the XDR specification, a Packer object also has pack_bytes and pack_opaque methods, but they are really just calls to pack_string. Likewise, a call to pack_fopaque is really just a call to pack_fstring.

The pack_farray(count, list, packFunc) function packs a fixed-length array (count items long) of homogenous data. Unfortunately, pack_farray requires that you pass in the count as well as the list itself, but it won't let you use a count that is different from the length of the list (go figure). As with pack_fstring, the function does not store the length of the array with the data, so you have to know the length when you unpack it. Or you can call pack_array(list, packFunc) to pack the size and then the list itself. The packFunc tells Packer which method to use to pack each item. For example, if each item in the list is an integer:

The pack_list(list,packFunc) method also packs an array of homogenous data, but it works with sequence objects whose size might not be known ahead of time. For example, you could create a class that defines its own_getitem_method:

... raise IndexError

>>> m = MySeq() >>> for i in m: ... print i 0 1 2

0 0

Post a comment