| 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="node595.html" /> |
| 13 | <link rel="prev" href="module-email.Utils.html" /> |
| 14 | <link rel="parent" href="module-email.html" /> |
| 15 | <link rel="next" href="node595.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>12.2.10 Iterators</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.2.9 Miscellaneous utilities" |
| 25 | href="module-email.Utils.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.2 email " |
| 28 | href="module-email.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.2.11 Package History" |
| 31 | href="node595.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-email.Utils.html">12.2.9 Miscellaneous utilities</A> |
| 46 | <b class="navlabel">Up:</b> |
| 47 | <a class="sectref" rel="parent" href="module-email.html">12.2 email </A> |
| 48 | <b class="navlabel">Next:</b> |
| 49 | <a class="sectref" rel="next" href="node595.html">12.2.11 Package History</A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H2><A NAME="SECTION00142100000000000000000"> |
| 56 | 12.2.10 Iterators</A> |
| 57 | </H2> |
| 58 | <A NAME="module-email.Iterators"></A> |
| 59 | |
| 60 | <P> |
| 61 | Iterating over a message object tree is fairly easy with the |
| 62 | <tt class="method">Message.walk()</tt> method. The <tt class="module">email.Iterators</tt> module |
| 63 | provides some useful higher level iterations over message object |
| 64 | trees. |
| 65 | |
| 66 | <P> |
| 67 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 68 | <td><nobr><b><tt id='l2h-3957' xml:id='l2h-3957' class="function">body_line_iterator</tt></b>(</nobr></td> |
| 69 | <td><var>msg</var><big>[</big><var>, decode</var><big>]</big><var></var>)</td></tr></table></dt> |
| 70 | <dd> |
| 71 | This iterates over all the payloads in all the subparts of <var>msg</var>, |
| 72 | returning the string payloads line-by-line. It skips over all the |
| 73 | subpart headers, and it skips over any subpart with a payload that |
| 74 | isn't a Python string. This is somewhat equivalent to reading the |
| 75 | flat text representation of the message from a file using |
| 76 | <tt class="method">readline()</tt>, skipping over all the intervening headers. |
| 77 | |
| 78 | <P> |
| 79 | Optional <var>decode</var> is passed through to <tt class="method">Message.get_payload()</tt>. |
| 80 | </dl> |
| 81 | |
| 82 | <P> |
| 83 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 84 | <td><nobr><b><tt id='l2h-3958' xml:id='l2h-3958' class="function">typed_subpart_iterator</tt></b>(</nobr></td> |
| 85 | <td><var>msg</var><big>[</big><var>, |
| 86 | maintype</var><big>[</big><var>, subtype</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt> |
| 87 | <dd> |
| 88 | This iterates over all the subparts of <var>msg</var>, returning only those |
| 89 | subparts that match the MIME type specified by <var>maintype</var> and |
| 90 | <var>subtype</var>. |
| 91 | |
| 92 | <P> |
| 93 | Note that <var>subtype</var> is optional; if omitted, then subpart MIME |
| 94 | type matching is done only with the main type. <var>maintype</var> is |
| 95 | optional too; it defaults to <span class="mimetype">text</span>. |
| 96 | |
| 97 | <P> |
| 98 | Thus, by default <tt class="function">typed_subpart_iterator()</tt> returns each |
| 99 | subpart that has a MIME type of <span class="mimetype">text/*</span>. |
| 100 | </dl> |
| 101 | |
| 102 | <P> |
| 103 | The following function has been added as a useful debugging tool. It |
| 104 | should <em>not</em> be considered part of the supported public interface |
| 105 | for the package. |
| 106 | |
| 107 | <P> |
| 108 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 109 | <td><nobr><b><tt id='l2h-3959' xml:id='l2h-3959' class="function">_structure</tt></b>(</nobr></td> |
| 110 | <td><var>msg</var><big>[</big><var>, fp</var><big>[</big><var>, level</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt> |
| 111 | <dd> |
| 112 | Prints an indented representation of the content types of the |
| 113 | message object structure. For example: |
| 114 | |
| 115 | <P> |
| 116 | <div class="verbatim"><pre> |
| 117 | >>> msg = email.message_from_file(somefile) |
| 118 | >>> _structure(msg) |
| 119 | multipart/mixed |
| 120 | text/plain |
| 121 | text/plain |
| 122 | multipart/digest |
| 123 | message/rfc822 |
| 124 | text/plain |
| 125 | message/rfc822 |
| 126 | text/plain |
| 127 | message/rfc822 |
| 128 | text/plain |
| 129 | message/rfc822 |
| 130 | text/plain |
| 131 | message/rfc822 |
| 132 | text/plain |
| 133 | text/plain |
| 134 | </pre></div> |
| 135 | |
| 136 | <P> |
| 137 | Optional <var>fp</var> is a file-like object to print the output to. It |
| 138 | must be suitable for Python's extended print statement. <var>level</var> |
| 139 | is used internally. |
| 140 | </dl> |
| 141 | |
| 142 | <P> |
| 143 | |
| 144 | <DIV CLASS="navigation"> |
| 145 | <div class='online-navigation'> |
| 146 | <p></p><hr /> |
| 147 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 148 | <tr> |
| 149 | <td class='online-navigation'><a rel="prev" title="12.2.9 Miscellaneous utilities" |
| 150 | href="module-email.Utils.html"><img src='../icons/previous.png' |
| 151 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 152 | <td class='online-navigation'><a rel="parent" title="12.2 email " |
| 153 | href="module-email.html"><img src='../icons/up.png' |
| 154 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 155 | <td class='online-navigation'><a rel="next" title="12.2.11 Package History" |
| 156 | href="node595.html"><img src='../icons/next.png' |
| 157 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 158 | <td align="center" width="100%">Python Library Reference</td> |
| 159 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 160 | href="contents.html"><img src='../icons/contents.png' |
| 161 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 162 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 163 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 164 | <td class='online-navigation'><a rel="index" title="Index" |
| 165 | href="genindex.html"><img src='../icons/index.png' |
| 166 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 167 | </tr></table> |
| 168 | <div class='online-navigation'> |
| 169 | <b class="navlabel">Previous:</b> |
| 170 | <a class="sectref" rel="prev" href="module-email.Utils.html">12.2.9 Miscellaneous utilities</A> |
| 171 | <b class="navlabel">Up:</b> |
| 172 | <a class="sectref" rel="parent" href="module-email.html">12.2 email </A> |
| 173 | <b class="navlabel">Next:</b> |
| 174 | <a class="sectref" rel="next" href="node595.html">12.2.11 Package History</A> |
| 175 | </div> |
| 176 | </div> |
| 177 | <hr /> |
| 178 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 179 | </DIV> |
| 180 | <!--End of Navigation Panel--> |
| 181 | <ADDRESS> |
| 182 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 183 | </ADDRESS> |
| 184 | </BODY> |
| 185 | </HTML> |