| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 2 | <html> |
| 3 | <head> |
| 4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> |
| 5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> |
| 6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> |
| 7 | <link rel="first" href="lib.html" title='Python Library Reference' /> |
| 8 | <link rel='contents' href='contents.html' title="Contents" /> |
| 9 | <link rel='index' href='genindex.html' title='Index' /> |
| 10 | <link rel='last' href='about.html' title='About this document...' /> |
| 11 | <link rel='help' href='about.html' title='About this document...' /> |
| 12 | <link rel="next" href="xdr-unpacker-objects.html" /> |
| 13 | <link rel="prev" href="module-xdrlib.html" /> |
| 14 | <link rel="parent" href="module-xdrlib.html" /> |
| 15 | <link rel="next" href="xdr-unpacker-objects.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>12.17.1 Packer Objects </title> |
| 18 | </head> |
| 19 | <body> |
| 20 | <DIV CLASS="navigation"> |
| 21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 23 | <tr> |
| 24 | <td class='online-navigation'><a rel="prev" title="12.17 xdrlib " |
| 25 | href="module-xdrlib.html"><img src='../icons/previous.png' |
| 26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 27 | <td class='online-navigation'><a rel="parent" title="12.17 xdrlib " |
| 28 | href="module-xdrlib.html"><img src='../icons/up.png' |
| 29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 30 | <td class='online-navigation'><a rel="next" title="12.17.2 Unpacker Objects" |
| 31 | href="xdr-unpacker-objects.html"><img src='../icons/next.png' |
| 32 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 33 | <td align="center" width="100%">Python Library Reference</td> |
| 34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 35 | href="contents.html"><img src='../icons/contents.png' |
| 36 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 37 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 38 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 39 | <td class='online-navigation'><a rel="index" title="Index" |
| 40 | href="genindex.html"><img src='../icons/index.png' |
| 41 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 42 | </tr></table> |
| 43 | <div class='online-navigation'> |
| 44 | <b class="navlabel">Previous:</b> |
| 45 | <a class="sectref" rel="prev" href="module-xdrlib.html">12.17 xdrlib </A> |
| 46 | <b class="navlabel">Up:</b> |
| 47 | <a class="sectref" rel="parent" href="module-xdrlib.html">12.17 xdrlib </A> |
| 48 | <b class="navlabel">Next:</b> |
| 49 | <a class="sectref" rel="next" href="xdr-unpacker-objects.html">12.17.2 Unpacker Objects</A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H2><A NAME="SECTION00141710000000000000000"></A><A NAME="xdr-packer-objects"></A> |
| 56 | <BR> |
| 57 | 12.17.1 Packer Objects |
| 58 | </H2> |
| 59 | |
| 60 | <P> |
| 61 | <tt class="class">Packer</tt> instances have the following methods: |
| 62 | |
| 63 | <P> |
| 64 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 65 | <td><nobr><b><tt id='l2h-4165' xml:id='l2h-4165' class="method">get_buffer</tt></b>(</nobr></td> |
| 66 | <td><var></var>)</td></tr></table></dt> |
| 67 | <dd> |
| 68 | Returns the current pack buffer as a string. |
| 69 | </dl> |
| 70 | |
| 71 | <P> |
| 72 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 73 | <td><nobr><b><tt id='l2h-4166' xml:id='l2h-4166' class="method">reset</tt></b>(</nobr></td> |
| 74 | <td><var></var>)</td></tr></table></dt> |
| 75 | <dd> |
| 76 | Resets the pack buffer to the empty string. |
| 77 | </dl> |
| 78 | |
| 79 | <P> |
| 80 | In general, you can pack any of the most common XDR data types by |
| 81 | calling the appropriate <code>pack_<var>type</var>()</code> method. Each method |
| 82 | takes a single argument, the value to pack. The following simple data |
| 83 | type packing methods are supported: <tt class="method">pack_uint()</tt>, |
| 84 | <tt class="method">pack_int()</tt>, <tt class="method">pack_enum()</tt>, <tt class="method">pack_bool()</tt>, |
| 85 | <tt class="method">pack_uhyper()</tt>, and <tt class="method">pack_hyper()</tt>. |
| 86 | |
| 87 | <P> |
| 88 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 89 | <td><nobr><b><tt id='l2h-4167' xml:id='l2h-4167' class="method">pack_float</tt></b>(</nobr></td> |
| 90 | <td><var>value</var>)</td></tr></table></dt> |
| 91 | <dd> |
| 92 | Packs the single-precision floating point number <var>value</var>. |
| 93 | </dl> |
| 94 | |
| 95 | <P> |
| 96 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 97 | <td><nobr><b><tt id='l2h-4168' xml:id='l2h-4168' class="method">pack_double</tt></b>(</nobr></td> |
| 98 | <td><var>value</var>)</td></tr></table></dt> |
| 99 | <dd> |
| 100 | Packs the double-precision floating point number <var>value</var>. |
| 101 | </dl> |
| 102 | |
| 103 | <P> |
| 104 | The following methods support packing strings, bytes, and opaque data: |
| 105 | |
| 106 | <P> |
| 107 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 108 | <td><nobr><b><tt id='l2h-4169' xml:id='l2h-4169' class="method">pack_fstring</tt></b>(</nobr></td> |
| 109 | <td><var>n, s</var>)</td></tr></table></dt> |
| 110 | <dd> |
| 111 | Packs a fixed length string, <var>s</var>. <var>n</var> is the length of the |
| 112 | string but it is <em>not</em> packed into the data buffer. The string |
| 113 | is padded with null bytes if necessary to guaranteed 4 byte alignment. |
| 114 | </dl> |
| 115 | |
| 116 | <P> |
| 117 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 118 | <td><nobr><b><tt id='l2h-4170' xml:id='l2h-4170' class="method">pack_fopaque</tt></b>(</nobr></td> |
| 119 | <td><var>n, data</var>)</td></tr></table></dt> |
| 120 | <dd> |
| 121 | Packs a fixed length opaque data stream, similarly to |
| 122 | <tt class="method">pack_fstring()</tt>. |
| 123 | </dl> |
| 124 | |
| 125 | <P> |
| 126 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 127 | <td><nobr><b><tt id='l2h-4171' xml:id='l2h-4171' class="method">pack_string</tt></b>(</nobr></td> |
| 128 | <td><var>s</var>)</td></tr></table></dt> |
| 129 | <dd> |
| 130 | Packs a variable length string, <var>s</var>. The length of the string is |
| 131 | first packed as an unsigned integer, then the string data is packed |
| 132 | with <tt class="method">pack_fstring()</tt>. |
| 133 | </dl> |
| 134 | |
| 135 | <P> |
| 136 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 137 | <td><nobr><b><tt id='l2h-4172' xml:id='l2h-4172' class="method">pack_opaque</tt></b>(</nobr></td> |
| 138 | <td><var>data</var>)</td></tr></table></dt> |
| 139 | <dd> |
| 140 | Packs a variable length opaque data string, similarly to |
| 141 | <tt class="method">pack_string()</tt>. |
| 142 | </dl> |
| 143 | |
| 144 | <P> |
| 145 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 146 | <td><nobr><b><tt id='l2h-4173' xml:id='l2h-4173' class="method">pack_bytes</tt></b>(</nobr></td> |
| 147 | <td><var>bytes</var>)</td></tr></table></dt> |
| 148 | <dd> |
| 149 | Packs a variable length byte stream, similarly to <tt class="method">pack_string()</tt>. |
| 150 | </dl> |
| 151 | |
| 152 | <P> |
| 153 | The following methods support packing arrays and lists: |
| 154 | |
| 155 | <P> |
| 156 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 157 | <td><nobr><b><tt id='l2h-4174' xml:id='l2h-4174' class="method">pack_list</tt></b>(</nobr></td> |
| 158 | <td><var>list, pack_item</var>)</td></tr></table></dt> |
| 159 | <dd> |
| 160 | Packs a <var>list</var> of homogeneous items. This method is useful for |
| 161 | lists with an indeterminate size; i.e. the size is not available until |
| 162 | the entire list has been walked. For each item in the list, an |
| 163 | unsigned integer <code>1</code> is packed first, followed by the data value |
| 164 | from the list. <var>pack_item</var> is the function that is called to pack |
| 165 | the individual item. At the end of the list, an unsigned integer |
| 166 | <code>0</code> is packed. |
| 167 | |
| 168 | <P> |
| 169 | For example, to pack a list of integers, the code might appear like |
| 170 | this: |
| 171 | |
| 172 | <P> |
| 173 | <div class="verbatim"><pre> |
| 174 | import xdrlib |
| 175 | p = xdrlib.Packer() |
| 176 | p.pack_list([1, 2, 3], p.pack_int) |
| 177 | </pre></div> |
| 178 | </dl> |
| 179 | |
| 180 | <P> |
| 181 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 182 | <td><nobr><b><tt id='l2h-4175' xml:id='l2h-4175' class="method">pack_farray</tt></b>(</nobr></td> |
| 183 | <td><var>n, array, pack_item</var>)</td></tr></table></dt> |
| 184 | <dd> |
| 185 | Packs a fixed length list (<var>array</var>) of homogeneous items. <var>n</var> |
| 186 | is the length of the list; it is <em>not</em> packed into the buffer, |
| 187 | but a <tt class="exception">ValueError</tt> exception is raised if |
| 188 | <code>len(<var>array</var>)</code> is not equal to <var>n</var>. As above, |
| 189 | <var>pack_item</var> is the function used to pack each element. |
| 190 | </dl> |
| 191 | |
| 192 | <P> |
| 193 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 194 | <td><nobr><b><tt id='l2h-4176' xml:id='l2h-4176' class="method">pack_array</tt></b>(</nobr></td> |
| 195 | <td><var>list, pack_item</var>)</td></tr></table></dt> |
| 196 | <dd> |
| 197 | Packs a variable length <var>list</var> of homogeneous items. First, the |
| 198 | length of the list is packed as an unsigned integer, then each element |
| 199 | is packed as in <tt class="method">pack_farray()</tt> above. |
| 200 | </dl> |
| 201 | |
| 202 | <P> |
| 203 | |
| 204 | <DIV CLASS="navigation"> |
| 205 | <div class='online-navigation'> |
| 206 | <p></p><hr /> |
| 207 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 208 | <tr> |
| 209 | <td class='online-navigation'><a rel="prev" title="12.17 xdrlib " |
| 210 | href="module-xdrlib.html"><img src='../icons/previous.png' |
| 211 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 212 | <td class='online-navigation'><a rel="parent" title="12.17 xdrlib " |
| 213 | href="module-xdrlib.html"><img src='../icons/up.png' |
| 214 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 215 | <td class='online-navigation'><a rel="next" title="12.17.2 Unpacker Objects" |
| 216 | href="xdr-unpacker-objects.html"><img src='../icons/next.png' |
| 217 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 218 | <td align="center" width="100%">Python Library Reference</td> |
| 219 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 220 | href="contents.html"><img src='../icons/contents.png' |
| 221 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 222 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 223 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 224 | <td class='online-navigation'><a rel="index" title="Index" |
| 225 | href="genindex.html"><img src='../icons/index.png' |
| 226 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 227 | </tr></table> |
| 228 | <div class='online-navigation'> |
| 229 | <b class="navlabel">Previous:</b> |
| 230 | <a class="sectref" rel="prev" href="module-xdrlib.html">12.17 xdrlib </A> |
| 231 | <b class="navlabel">Up:</b> |
| 232 | <a class="sectref" rel="parent" href="module-xdrlib.html">12.17 xdrlib </A> |
| 233 | <b class="navlabel">Next:</b> |
| 234 | <a class="sectref" rel="next" href="xdr-unpacker-objects.html">12.17.2 Unpacker Objects</A> |
| 235 | </div> |
| 236 | </div> |
| 237 | <hr /> |
| 238 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 239 | </DIV> |
| 240 | <!--End of Navigation Panel--> |
| 241 | <ADDRESS> |
| 242 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 243 | </ADDRESS> |
| 244 | </BODY> |
| 245 | </HTML> |