home *** CD-ROM | disk | FTP | other *** search
- <TITLE>Packer Objects -- Python library reference</TITLE>
- Next: <A HREF="../u/unpacker_objects" TYPE="Next">Unpacker Objects</A>
- Prev: <A HREF="../x/xdrlib" TYPE="Prev">xdrlib</A>
- Up: <A HREF="../x/xdrlib" TYPE="Up">xdrlib</A>
- Top: <A HREF="../t/top" TYPE="Top">Top</A>
- <H2>10.16.1. Packer Objects</H2>
- <CODE>Packer</CODE> is the class for packing data into XDR representation.
- The <CODE>Packer</CODE> class is instantiated with no arguments.
- <P>
- <DL><DT><B>get_buffer</B> () -- function of module xdrlib<DD>
- Returns the current pack buffer as a string.
- </DL>
- <DL><DT><B>reset</B> () -- function of module xdrlib<DD>
- Resets the pack buffer to the empty string.
- </DL>
- In general, you can pack any of the most common XDR data types by
- calling the appropriate <CODE>pack_<VAR>type</VAR></CODE> method. Each method
- takes a single argument, the value to pack. The following simple data
- type packing methods are supported: <CODE>pack_uint</CODE>, <CODE>pack_int</CODE>,
- <CODE>pack_enum</CODE>, <CODE>pack_bool</CODE>, <CODE>pack_uhyper</CODE>,
- and <CODE>pack_hyper</CODE>.
- <P>
- The following methods pack floating point numbers, however they
- require C library support. Without the optional C built-in module,
- both of these methods will raise an <CODE>xdrlib.ConversionError</CODE>
- exception. See the note at the end of this chapter for details.
- <P>
- <DL><DT><B>pack_float</B> (<VAR>value</VAR>) -- function of module xdrlib<DD>
- Packs the single-precision floating point number <VAR>value</VAR>.
- </DL>
- <DL><DT><B>pack_double</B> (<VAR>value</VAR>) -- function of module xdrlib<DD>
- Packs the double-precision floating point number <VAR>value</VAR>.
- </DL>
- The following methods support packing strings, bytes, and opaque data:
- <P>
- <DL><DT><B>pack_fstring</B> (<VAR>n</VAR>, <VAR>s</VAR>) -- function of module xdrlib<DD>
- Packs a fixed length string, <VAR>s</VAR>. <VAR>n</VAR> is the length of the
- string but it is <I>not</I> packed into the data buffer. The string
- is padded with null bytes if necessary to guaranteed 4 byte alignment.
- </DL>
- <DL><DT><B>pack_fopaque</B> (<VAR>n</VAR>, <VAR>data</VAR>) -- function of module xdrlib<DD>
- Packs a fixed length opaque data stream, similarly to
- <CODE>pack_fstring</CODE>.
- </DL>
- <DL><DT><B>pack_string</B> (<VAR>s</VAR>) -- function of module xdrlib<DD>
- Packs a variable length string, <VAR>s</VAR>. The length of the string is
- first packed as an unsigned integer, then the string data is packed
- with <CODE>pack_fstring</CODE>.
- </DL>
- <DL><DT><B>pack_opaque</B> (<VAR>data</VAR>) -- function of module xdrlib<DD>
- Packs a variable length opaque data string, similarly to
- <CODE>pack_string</CODE>.
- </DL>
- <DL><DT><B>pack_bytes</B> (<VAR>bytes</VAR>) -- function of module xdrlib<DD>
- Packs a variable length byte stream, similarly to <CODE>pack_string</CODE>.
- </DL>
- The following methods support packing arrays and lists:
- <P>
- <DL><DT><B>pack_list</B> (<VAR>list</VAR>, <VAR>pack_item</VAR>) -- function of module xdrlib<DD>
- Packs a <VAR>list</VAR> 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 <CODE>1</CODE> is packed first, followed by the data value
- from the list. <VAR>pack_item</VAR> is the function that is called to pack
- the individual item. At the end of the list, an unsigned integer
- <CODE>0</CODE> is packed.
- </DL>
- <DL><DT><B>pack_farray</B> (<VAR>n</VAR>, <VAR>array</VAR>, <VAR>pack_item</VAR>) -- function of module xdrlib<DD>
- Packs a fixed length list (<VAR>array</VAR>) of homogeneous items. <VAR>n</VAR>
- is the length of the list; it is <I>not</I> packed into the buffer,
- but a <CODE>ValueError</CODE> exception is raised if <CODE>len(array)</CODE> is not
- equal to <VAR>n</VAR>. As above, <VAR>pack_item</VAR> is the function used to
- pack each element.
- </DL>
- <DL><DT><B>pack_array</B> (<VAR>list</VAR>, <VAR>pack_item</VAR>) -- function of module xdrlib<DD>
- Packs a variable length <VAR>list</VAR> of homogeneous items. First, the
- length of the list is packed as an unsigned integer, then each element
- is packed as in <CODE>pack_farray</CODE> above.
- </DL>
-