Initial commit of OpenSPARC T2 design and verification files.
[OpenSPARC-T2-DV] / tools / src / nas,5.n2.os.2 / lib / python / html / python / lib / module-email.Utils.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python Documentation Index' />
<link rel="first" href="lib.html" title='Python Library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="module-email.Iterators.html" />
<link rel="prev" href="module-email.Errors.html" />
<link rel="parent" href="module-email.html" />
<link rel="next" href="module-email.Iterators.html" />
<meta name='aesop' content='information' />
<title>12.2.9 Miscellaneous utilities</title>
</head>
<body>
<DIV CLASS="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="12.2.8 Exception and Defect"
href="module-email.Errors.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="12.2 email "
href="module-email.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="12.2.10 Iterators"
href="module-email.Iterators.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
border='0' height='32' alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-email.Errors.html">12.2.8 Exception and Defect</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="module-email.html">12.2 email </A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="module-email.Iterators.html">12.2.10 Iterators</A>
</div>
<hr /></div>
</DIV>
<!--End of Navigation Panel-->
<H2><A NAME="SECTION0014290000000000000000">
12.2.9 Miscellaneous utilities</A>
</H2>
<A NAME="module-email.Utils"></A>
<P>
There are several useful utilities provided in the <tt class="module">email.Utils</tt>
module:
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-3942' xml:id='l2h-3942' class="function">quote</tt></b>(</nobr></td>
<td><var>str</var>)</td></tr></table></dt>
<dd>
Return a new string with backslashes in <var>str</var> replaced by two
backslashes, and double quotes replaced by backslash-double quote.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-3943' xml:id='l2h-3943' class="function">unquote</tt></b>(</nobr></td>
<td><var>str</var>)</td></tr></table></dt>
<dd>
Return a new string which is an <em>unquoted</em> version of <var>str</var>.
If <var>str</var> ends and begins with double quotes, they are stripped
off. Likewise if <var>str</var> ends and begins with angle brackets, they
are stripped off.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-3944' xml:id='l2h-3944' class="function">parseaddr</tt></b>(</nobr></td>
<td><var>address</var>)</td></tr></table></dt>
<dd>
Parse address - which should be the value of some address-containing
field such as <span class="mailheader">To:</span> or <span class="mailheader">Cc:</span> - into its constituent
<em>realname</em> and <em>email address</em> parts. Returns a tuple of that
information, unless the parse fails, in which case a 2-tuple of
<code>('', '')</code> is returned.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-3945' xml:id='l2h-3945' class="function">formataddr</tt></b>(</nobr></td>
<td><var>pair</var>)</td></tr></table></dt>
<dd>
The inverse of <tt class="method">parseaddr()</tt>, this takes a 2-tuple of the form
<code>(realname, email_address)</code> and returns the string value suitable
for a <span class="mailheader">To:</span> or <span class="mailheader">Cc:</span> header. If the first element of
<var>pair</var> is false, then the second element is returned unmodified.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-3946' xml:id='l2h-3946' class="function">getaddresses</tt></b>(</nobr></td>
<td><var>fieldvalues</var>)</td></tr></table></dt>
<dd>
This method returns a list of 2-tuples of the form returned by
<code>parseaddr()</code>. <var>fieldvalues</var> is a sequence of header field
values as might be returned by <tt class="method">Message.get_all()</tt>. Here's a
simple example that gets all the recipients of a message:
<P>
<div class="verbatim"><pre>
from email.Utils import getaddresses
tos = msg.get_all('to', [])
ccs = msg.get_all('cc', [])
resent_tos = msg.get_all('resent-to', [])
resent_ccs = msg.get_all('resent-cc', [])
all_recipients = getaddresses(tos + ccs + resent_tos + resent_ccs)
</pre></div>
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-3947' xml:id='l2h-3947' class="function">parsedate</tt></b>(</nobr></td>
<td><var>date</var>)</td></tr></table></dt>
<dd>
Attempts to parse a date according to the rules in <a class="rfc" id='rfcref-90972' xml:id='rfcref-90972'
href="http://www.faqs.org/rfcs/rfc2822.html">RFC 2822</a>.
however, some mailers don't follow that format as specified, so
<tt class="function">parsedate()</tt> tries to guess correctly in such cases.
<var>date</var> is a string containing an <a class="rfc" id='rfcref-90974' xml:id='rfcref-90974'
href="http://www.faqs.org/rfcs/rfc2822.html">RFC 2822</a> date, such as
<code>"Mon, 20 Nov 1995 19:12:08 -0500"</code>. If it succeeds in parsing
the date, <tt class="function">parsedate()</tt> returns a 9-tuple that can be passed
directly to <tt class="function">time.mktime()</tt>; otherwise <code>None</code> will be
returned. Note that fields 6, 7, and 8 of the result tuple are not
usable.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-3948' xml:id='l2h-3948' class="function">parsedate_tz</tt></b>(</nobr></td>
<td><var>date</var>)</td></tr></table></dt>
<dd>
Performs the same function as <tt class="function">parsedate()</tt>, but returns
either <code>None</code> or a 10-tuple; the first 9 elements make up a tuple
that can be passed directly to <tt class="function">time.mktime()</tt>, and the tenth
is the offset of the date's timezone from UTC (which is the official
term for Greenwich Mean Time)<A NAME="tex2html124"
HREF="#foot58346"><SUP>12.3</SUP></A>. If the input
string has no timezone, the last element of the tuple returned is
<code>None</code>. Note that fields 6, 7, and 8 of the result tuple are not
usable.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-3949' xml:id='l2h-3949' class="function">mktime_tz</tt></b>(</nobr></td>
<td><var>tuple</var>)</td></tr></table></dt>
<dd>
Turn a 10-tuple as returned by <tt class="function">parsedate_tz()</tt> into a UTC
timestamp. It the timezone item in the tuple is <code>None</code>, assume
local time. Minor deficiency: <tt class="function">mktime_tz()</tt> interprets the
first 8 elements of <var>tuple</var> as a local time and then compensates
for the timezone difference. This may yield a slight error around
changes in daylight savings time, though not worth worrying about for
common use.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-3950' xml:id='l2h-3950' class="function">formatdate</tt></b>(</nobr></td>
<td><var></var><big>[</big><var>timeval</var><big>[</big><var>, localtime</var><big>]</big><var></var><big>[</big><var>, usegmt</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Returns a date string as per <a class="rfc" id='rfcref-90976' xml:id='rfcref-90976'
href="http://www.faqs.org/rfcs/rfc2822.html">RFC 2822</a>, e.g.:
<P>
<div class="verbatim"><pre>
Fri, 09 Nov 2001 01:08:47 -0000
</pre></div>
<P>
Optional <var>timeval</var> if given is a floating point time value as
accepted by <tt class="function">time.gmtime()</tt> and <tt class="function">time.localtime()</tt>,
otherwise the current time is used.
<P>
Optional <var>localtime</var> is a flag that when <code>True</code>, interprets
<var>timeval</var>, and returns a date relative to the local timezone
instead of UTC, properly taking daylight savings time into account.
The default is <code>False</code> meaning UTC is used.
<P>
Optional <var>usegmt</var> is a flag that when <code>True</code>, outputs a
date string with the timezone as an ascii string <code>GMT</code>, rather
than a numeric <code>-0000</code>. This is needed for some protocols (such
as HTTP). This only applies when <var>localtime</var> is <code>False</code>.
<span class="versionnote">New in version 2.4.</span>
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-3951' xml:id='l2h-3951' class="function">make_msgid</tt></b>(</nobr></td>
<td><var></var><big>[</big><var>idstring</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Returns a string suitable for an <a class="rfc" id='rfcref-90978' xml:id='rfcref-90978'
href="http://www.faqs.org/rfcs/rfc2822.html">RFC 2822</a>-compliant
<span class="mailheader">Message-ID:</span> header. Optional <var>idstring</var> if given, is
a string used to strengthen the uniqueness of the message id.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-3952' xml:id='l2h-3952' class="function">decode_rfc2231</tt></b>(</nobr></td>
<td><var>s</var>)</td></tr></table></dt>
<dd>
Decode the string <var>s</var> according to <a class="rfc" id='rfcref-90980' xml:id='rfcref-90980'
href="http://www.faqs.org/rfcs/rfc2231.html">RFC 2231</a>.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-3953' xml:id='l2h-3953' class="function">encode_rfc2231</tt></b>(</nobr></td>
<td><var>s</var><big>[</big><var>, charset</var><big>[</big><var>, language</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Encode the string <var>s</var> according to <a class="rfc" id='rfcref-90982' xml:id='rfcref-90982'
href="http://www.faqs.org/rfcs/rfc2231.html">RFC 2231</a>. Optional
<var>charset</var> and <var>language</var>, if given is the character set name
and language name to use. If neither is given, <var>s</var> is returned
as-is. If <var>charset</var> is given but <var>language</var> is not, the
string is encoded using the empty string for <var>language</var>.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-3954' xml:id='l2h-3954' class="function">collapse_rfc2231_value</tt></b>(</nobr></td>
<td><var>value</var><big>[</big><var>, errors</var><big>[</big><var>,
fallback_charset</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
When a header parameter is encoded in <a class="rfc" id='rfcref-90984' xml:id='rfcref-90984'
href="http://www.faqs.org/rfcs/rfc2231.html">RFC 2231</a> format,
<tt class="method">Message.get_param()</tt> may return a 3-tuple containing the character
set, language, and value. <tt class="function">collapse_rfc2231_value()</tt> turns this into
a unicode string. Optional <var>errors</var> is passed to the <var>errors</var>
argument of the built-in <tt class="function">unicode()</tt> function; it defaults to
<code>replace</code>. Optional <var>fallback_charset</var> specifies the character set
to use if the one in the <a class="rfc" id='rfcref-90986' xml:id='rfcref-90986'
href="http://www.faqs.org/rfcs/rfc2231.html">RFC 2231</a> header is not known by Python; it defaults
to <code>us-ascii</code>.
<P>
For convenience, if the <var>value</var> passed to
<tt class="function">collapse_rfc2231_value()</tt> is not a tuple, it should be a string and
it is returned unquoted.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-3955' xml:id='l2h-3955' class="function">decode_params</tt></b>(</nobr></td>
<td><var>params</var>)</td></tr></table></dt>
<dd>
Decode parameters list according to <a class="rfc" id='rfcref-90988' xml:id='rfcref-90988'
href="http://www.faqs.org/rfcs/rfc2231.html">RFC 2231</a>. <var>params</var> is a
sequence of 2-tuples containing elements of the form
<code>(content-type, string-value)</code>.
</dl>
<P>
<span class="versionnote">Changed in version 2.4:
The <tt class="function">dump_address_pair()</tt> function has been removed;
use <tt class="function">formataddr()</tt> instead..</span>
<P>
<span class="versionnote">Changed in version 2.4:
The <tt class="function">decode()</tt> function has been removed; use the
<tt class="method">Header.decode_header()</tt> method instead..</span>
<P>
<span class="versionnote">Changed in version 2.4:
The <tt class="function">encode()</tt> function has been removed; use the
<tt class="method">Header.encode()</tt> method instead..</span>
<P>
<BR><HR><H4>Footnotes</H4>
<DL>
<DT><A NAME="foot58346">... Time)</A><A
href="module-email.Utils.html#tex2html124"><SUP>12.3</SUP></A></DT>
<DD>Note that the sign of the timezone
offset is the opposite of the sign of the <code>time.timezone</code>
variable for the same timezone; the latter variable follows the
POSIX standard while this module follows <a class="rfc" id='rfcref-90963' xml:id='rfcref-90963'
href="http://www.faqs.org/rfcs/rfc2822.html">RFC 2822</a>.
</DD>
</DL>
<DIV CLASS="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="12.2.8 Exception and Defect"
href="module-email.Errors.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="12.2 email "
href="module-email.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="12.2.10 Iterators"
href="module-email.Iterators.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
border='0' height='32' alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-email.Errors.html">12.2.8 Exception and Defect</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="module-email.html">12.2 email </A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="module-email.Iterators.html">12.2.10 Iterators</A>
</div>
</div>
<hr />
<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>