<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>stacyprowell.com &#187; Python</title>
	<atom:link href="http://stacyprowell.com/blog/category/programming/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://stacyprowell.com/blog</link>
	<description>Ugh, Stacy&#039;s talking again...</description>
	<lastBuildDate>Sat, 04 Feb 2012 06:01:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Handling multipart/form-data in Python</title>
		<link>http://stacyprowell.com/blog/2009/05/29/handling-multipartform-data-in-python/</link>
		<comments>http://stacyprowell.com/blog/2009/05/29/handling-multipartform-data-in-python/#comments</comments>
		<pubDate>Fri, 29 May 2009 14:43:35 +0000</pubDate>
		<dc:creator>stacy</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[linkedin]]></category>
		<category><![CDATA[multipart]]></category>
		<category><![CDATA[multipart/form-data]]></category>

		<guid isPermaLink="false">http://stacyprowell.com/blog/?p=225</guid>
		<description><![CDATA[I need to generate multipart/form-data (see here) messages from Python. Never mind why. I dug around in the documentation for httplib, urllib, and urllib2, but it seems this is not currently supported (it&#8217;s Issue 3244). I didn&#8217;t like the code I found on the web to do it, because I needed to set additional headers [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_235" class="wp-caption alignright" style="width: 160px"><img class="size-full wp-image-235" title="programming" src="http://stacyprowell.com/blog/wp-content/uploads/2009/05/1ebeb1d2f212046a4e47fcd414dbad9b1-150x150.jpg" alt="Programming" width="150" height="150" /><p class="wp-caption-text">Programming</p></div>
<p>I need to generate <tt>multipart/form-data</tt> (see <a href="http://www.w3.org/TR/html401/interact/forms.html">here</a>) messages from Python.  Never mind why.  I dug around in the documentation for httplib, urllib, and urllib2, but it seems this is not currently supported (it&#8217;s <a href="http://bugs.python.org/issue3244">Issue 3244</a>).  I didn&#8217;t like the code I found on the web to do it, because I needed to set additional headers on each piece.  So&#8230; I wrote something.  Here it is.  If it&#8217;s useful to you, great!  If you find bugs in it, please let me know.  I think this is pretty easy to use.<br />
<span id="more-225"></span><br />
Make an instance of Multipart, and then add parts using the field and file methods.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&gt;&gt;&gt; from multipart import Multipart
&gt;&gt;&gt; m = Multipart()
&gt;&gt;&gt; m.field('search','searchish term')
&gt;&gt;&gt; m.file('greet','greet.txt','Hello multipart world!',{'Content-Type':'text/text'})
&gt;&gt;&gt; ct,body = m.get()
&gt;&gt;&gt; print ct
multipart/form-data; boundary=----------AaB03x
&gt;&gt;&gt; print body
------------AaB03x
Content-Type: application/octet-stream
Content-Disposition: form-data; name=&quot;search&quot;
&nbsp;
searchish term
------------AaB03x
Content-Type: text/text
Content-Disposition: form-data; name=&quot;greet&quot;; filename=&quot;greet.txt&quot;
&nbsp;
Hello multipart world!
------------AaB03x--</pre></div></div>

<p>If no content type is specified for a value, then the default of application/octet-stream is chosen.  If none is specified for a file, then the mime libraries are consulted to guess one based on the filename, and again the default is application/octet-stream if none can be guessed.</p>
<p>If you want to specify the content type, make sure to use the string &#8216;Content-Type&#8217; (note the caps) or it will be ignored.  I&#8217;ve seen the capitalization all over the map, but needed to choose one to use in the case-sensitive dict.  If you don&#8217;t like my choice&#8230; change the code.  See the constants at the start of the Client class.</p>
<p>Here&#8217;s how you might continue the above example to send this out in an HTTP request.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">request = <span style="color: #dc143c;">urllib2</span>.<span style="color: black;">Request</span><span style="color: black;">&#40;</span>url=<span style="color: #483d8b;">'http://my.fake.server'</span>,
    headers=<span style="color: black;">&#123;</span><span style="color: #483d8b;">'Content-Type'</span>:ct<span style="color: black;">&#125;</span>,
    data=body<span style="color: black;">&#41;</span>
reply = <span style="color: #dc143c;">urllib2</span>.<span style="color: black;">urlopen</span><span style="color: black;">&#40;</span>request<span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> reply.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Finally, here&#8217;s the code.  Enjoy!  This comes, of course, with <em>no warranty</em>, use at your own risk, etc.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/python</span>
<span style="color: #483d8b;">''</span><span style="color: #483d8b;">'
Classes for using multipart form data from Python, which does not (at the
time of writing) support this directly.
&nbsp;
To use this, make an instance of Multipart and add parts to it via the factory
methods field and file.  When you are done, get the content via the get method.
&nbsp;
@author: Stacy Prowell (http://stacyprowell.com)
'</span><span style="color: #483d8b;">''</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">mimetypes</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Part<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">''</span><span style="color: #483d8b;">'
    Class holding a single part of the form.  You should never need to use
    this class directly; instead, use the factory methods in Multipart:
    field and file.
    '</span><span style="color: #483d8b;">''</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># The boundary to use.  This is shamelessly taken from the standard.</span>
    BOUNDARY = <span style="color: #483d8b;">'----------AaB03x'</span>
    CRLF = <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>'</span>
    <span style="color: #808080; font-style: italic;"># Common headers.</span>
    CONTENT_TYPE = <span style="color: #483d8b;">'Content-Type'</span>
    CONTENT_DISPOSITION = <span style="color: #483d8b;">'Content-Disposition'</span>
    <span style="color: #808080; font-style: italic;"># The default content type for parts.</span>
    DEFAULT_CONTENT_TYPE = <span style="color: #483d8b;">'application/octet-stream'</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, name, filename, body, headers<span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">''</span><span style="color: #483d8b;">'
        Make a new part.  The part will have the given headers added initially.
&nbsp;
        @param name: The part name.
        @type name: str
        @param filename: If this is a file, the name of the file.  Otherwise
                        None.
        @type filename: str
        @param body: The body of the part.
        @type body: str
        @param headers: Additional headers, or overrides, for this part.
                        You can override Content-Type here.
        @type headers: dict
        '</span><span style="color: #483d8b;">''</span>
        <span style="color: #008000;">self</span>._headers = headers.<span style="color: #dc143c;">copy</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>._name = name
        <span style="color: #008000;">self</span>._filename = filename
        <span style="color: #008000;">self</span>._body = body
        <span style="color: #808080; font-style: italic;"># We respect any content type passed in, but otherwise set it here.</span>
        <span style="color: #808080; font-style: italic;"># We set the content disposition now, overwriting any prior value.</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">self</span>._filename == <span style="color: #008000;">None</span>:
            <span style="color: #008000;">self</span>._headers<span style="color: black;">&#91;</span>Part.<span style="color: black;">CONTENT_DISPOSITION</span><span style="color: black;">&#93;</span> = \
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">'form-data; name=&quot;%s&quot;'</span> <span style="color: #66cc66;">%</span> <span style="color: #008000;">self</span>._name<span style="color: black;">&#41;</span>
            <span style="color: #008000;">self</span>._headers.<span style="color: black;">setdefault</span><span style="color: black;">&#40;</span>Part.<span style="color: black;">CONTENT_TYPE</span>,
                                     Part.<span style="color: black;">DEFAULT_CONTENT_TYPE</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            <span style="color: #008000;">self</span>._headers<span style="color: black;">&#91;</span>Part.<span style="color: black;">CONTENT_DISPOSITION</span><span style="color: black;">&#93;</span> = \
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">'form-data; name=&quot;%s&quot;; filename=&quot;%s&quot;'</span> <span style="color: #66cc66;">%</span>
                 <span style="color: black;">&#40;</span><span style="color: #008000;">self</span>._name, <span style="color: #008000;">self</span>._filename<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
            <span style="color: #008000;">self</span>._headers.<span style="color: black;">setdefault</span><span style="color: black;">&#40;</span>Part.<span style="color: black;">CONTENT_TYPE</span>,
                                     <span style="color: #dc143c;">mimetypes</span>.<span style="color: black;">guess_type</span><span style="color: black;">&#40;</span>filename<span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>
                                     <span style="color: #ff7700;font-weight:bold;">or</span> Part.<span style="color: black;">DEFAULT_CONTENT_TYPE</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> get<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">''</span><span style="color: #483d8b;">'
        Convert the part into a list of lines for output.  This includes
        the boundary lines, part header lines, and the part itself.  A
        blank line is included between the header and the body.
&nbsp;
        @return: Lines of this part.
        @rtype: list
        '</span><span style="color: #483d8b;">''</span>
        lines = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
        lines.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'--'</span> + Part.<span style="color: black;">BOUNDARY</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> <span style="color: black;">&#40;</span>key, val<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">self</span>._headers.<span style="color: black;">items</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
            lines.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'%s: %s'</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>key, val<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        lines.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">''</span><span style="color: black;">&#41;</span>
        lines.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>._body<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> lines
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Multipart<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">''</span><span style="color: #483d8b;">'
    Encapsulate multipart form data.  To use this, make an instance and then
    add parts to it via the two methods (field and file).  When done, you can
    get the result via the get method.
&nbsp;
    See http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2 for
    details on multipart/form-data.
&nbsp;
    Watch http://bugs.python.org/issue3244 to see if this is fixed in the
    Python libraries.
&nbsp;
    @return: content type, body
    @rtype: tuple
    '</span><span style="color: #483d8b;">''</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">parts</span> = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> field<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, name, value, headers=<span style="color: black;">&#123;</span><span style="color: black;">&#125;</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">''</span><span style="color: #483d8b;">'
        Create and append a field part.  This kind of part has a field name
        and value.
&nbsp;
        @param name: The field name.
        @type name: str
        @param value: The field value.
        @type value: str
        @param headers: Headers to set in addition to disposition.
        @type headers: dict
        '</span><span style="color: #483d8b;">''</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">parts</span>.<span style="color: black;">append</span><span style="color: black;">&#40;</span>Part<span style="color: black;">&#40;</span>name, <span style="color: #008000;">None</span>, value, headers<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #008000;">file</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, name, filename, value, headers=<span style="color: black;">&#123;</span><span style="color: black;">&#125;</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">''</span><span style="color: #483d8b;">'
        Create and append a file part.  THis kind of part has a field name,
        a filename, and a value.
&nbsp;
        @param name: The field name.
        @type name: str
        @param value: The field value.
        @type value: str
        @param headers: Headers to set in addition to disposition.
        @type headers: dict
        '</span><span style="color: #483d8b;">''</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">parts</span>.<span style="color: black;">append</span><span style="color: black;">&#40;</span>Part<span style="color: black;">&#40;</span>name, filename, value, headers<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> get<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">''</span><span style="color: #483d8b;">'
        Get the multipart form data.  This returns the content type, which
        specifies the boundary marker, and also returns the body containing
        all parts and bondary markers.
&nbsp;
        @return: content type, body
        @rtype: tuple
        '</span><span style="color: #483d8b;">''</span>
        <span style="color: #008000;">all</span> = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> part <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">self</span>.<span style="color: black;">parts</span>:
            <span style="color: #008000;">all</span> += part.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">all</span>.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'--'</span> + Part.<span style="color: black;">BOUNDARY</span> + <span style="color: #483d8b;">'--'</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">all</span>.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">''</span><span style="color: black;">&#41;</span>
        <span style="color: #808080; font-style: italic;"># We have to return the content type, since it specifies the boundary.</span>
        content_type = <span style="color: #483d8b;">'multipart/form-data; boundary=%s'</span> <span style="color: #66cc66;">%</span> Part.<span style="color: black;">BOUNDARY</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> content_type, Part.<span style="color: black;">CRLF</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: #008000;">all</span><span style="color: black;">&#41;</span></pre></div></div>

<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fstacyprowell.com%2Fblog%2F2009%2F05%2F29%2Fhandling-multipartform-data-in-python%2F&amp;title=Handling%20multipart%2Fform-data%20in%20Python" id="wpa2a_2"><img src="http://stacyprowell.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://stacyprowell.com/blog/2009/05/29/handling-multipartform-data-in-python/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Trapping CTRL+C in Python</title>
		<link>http://stacyprowell.com/blog/2009/03/30/trapping-ctrlc-in-python/</link>
		<comments>http://stacyprowell.com/blog/2009/03/30/trapping-ctrlc-in-python/#comments</comments>
		<pubDate>Mon, 30 Mar 2009 18:12:30 +0000</pubDate>
		<dc:creator>stacy</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[break]]></category>
		<category><![CDATA[ctrl+c]]></category>
		<category><![CDATA[interrupt]]></category>
		<category><![CDATA[linkedin]]></category>

		<guid isPermaLink="false">http://stacyprowell.com/blog/?p=123</guid>
		<description><![CDATA[I&#8217;ve been doing some nasty database work with Python and MySQL.  Specifically, I&#8217;ve got long programs that run eight to ten hours, and sometimes I need to kill them and restart them later. So&#8230; I wanted a way to trap CTRL+C and clean up.  That is, I wanted to have transactions without actually having to [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignright size-thumbnail wp-image-126" title="1ebeb1d2f212046a4e47fcd414dbad9b1" src="http://stacyprowell.com/blog/wp-content/uploads/2009/03/1ebeb1d2f212046a4e47fcd414dbad9b1-150x150.jpg" alt="1ebeb1d2f212046a4e47fcd414dbad9b1" width="150" height="150" />I&#8217;ve been doing some nasty database work with Python and MySQL.  Specifically, I&#8217;ve got long programs that run eight to ten hours, and sometimes I need to kill them and restart them later.</p>
<p>So&#8230; I wanted a way to trap CTRL+C and clean up.  That is, I wanted to have transactions without actually having to use transactions.  I&#8217;m funny that way.  (In other news, I&#8217;m probably moving to PostgreSQL at some point.)</p>
<p>Anyway, here&#8217;s what I came up with.  You might find it useful, too.<span id="more-123"></span></p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/python -i</span>
&nbsp;
<span style="color: #483d8b;">''</span><span style="color: #483d8b;">'
Trap keyboard interrupts.  No rights reserved; use at your own risk.
&nbsp;
@author: Stacy Prowell (http://stacyprowell.com)
'</span><span style="color: #483d8b;">''</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">signal</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> BreakHandler:
    <span style="color: #483d8b;">''</span><span style="color: #483d8b;">'
    Trap CTRL-C, set a flag, and keep going.  This is very useful for
    gracefully exiting database loops while simulating transactions.
&nbsp;
    To use this, make an instance and then enable it.  You can check
    whether a break was trapped using the trapped property.
&nbsp;
    # Create and enable a break handler.
    ih = BreakHandler()
    ih.enable()
    for x in big_set:
        complex_operation_1()
        complex_operation_2()
        complex_operation_3()
        # Check whether there was a break.
        if ih.trapped:
            # Stop the loop.
            break
    ih.disable()
    # Back to usual operation...
    '</span><span style="color: #483d8b;">''</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, emphatic=<span style="color: #ff4500;">9</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">''</span><span style="color: #483d8b;">'
        Create a new break handler.
&nbsp;
        @param emphatic: This is the number of times that the user must
                    press break to *disable* the handler.  If you press
                    break this number of times, the handler is automagically
                    disabled, and one more break will trigger an old
                    style keyboard interrupt.  The default is nine.  This
                    is a Good Idea, since if you happen to lose your
                    connection to the handler you can *still* disable it.
        '</span><span style="color: #483d8b;">''</span>
        <span style="color: #008000;">self</span>._count = <span style="color: #ff4500;">0</span>
        <span style="color: #008000;">self</span>._enabled = <span style="color: #008000;">False</span>
        <span style="color: #008000;">self</span>._emphatic = emphatic
        <span style="color: #008000;">self</span>._oldhandler = <span style="color: #008000;">None</span>
        <span style="color: #ff7700;font-weight:bold;">return</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> _reset<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">''</span><span style="color: #483d8b;">'
        Reset the trapped status and count.  You should not need to use this
        directly; instead you can disable the handler and then re-enable it.
        This is better, in case someone presses CTRL-C during this operation.
        '</span><span style="color: #483d8b;">''</span>
        <span style="color: #008000;">self</span>._count = <span style="color: #ff4500;">0</span>
        <span style="color: #ff7700;font-weight:bold;">return</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> enable<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">''</span><span style="color: #483d8b;">'
        Enable trapping of the break.  This action also resets the
        handler count and trapped properties.
        '</span><span style="color: #483d8b;">''</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">self</span>._enabled:
            <span style="color: #008000;">self</span>._reset<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
            <span style="color: #008000;">self</span>._enabled = <span style="color: #008000;">True</span>
            <span style="color: #008000;">self</span>._oldhandler = <span style="color: #dc143c;">signal</span>.<span style="color: #dc143c;">signal</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">signal</span>.<span style="color: black;">SIGINT</span>, <span style="color: #008000;">self</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> disable<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">''</span><span style="color: #483d8b;">'
        Disable trapping the break.  You can check whether a break
        was trapped using the count and trapped properties.
        '</span><span style="color: #483d8b;">''</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">self</span>._enabled:
            <span style="color: #008000;">self</span>._enabled = <span style="color: #008000;">False</span>
            <span style="color: #dc143c;">signal</span>.<span style="color: #dc143c;">signal</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">signal</span>.<span style="color: black;">SIGINT</span>, <span style="color: #008000;">self</span>._oldhandler<span style="color: black;">&#41;</span>
            <span style="color: #008000;">self</span>._oldhandler = <span style="color: #008000;">None</span>
        <span style="color: #ff7700;font-weight:bold;">return</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__call__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, signame, sf<span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">''</span><span style="color: #483d8b;">'
        An break just occurred.  Save information about it and keep
        going.
        '</span><span style="color: #483d8b;">''</span>
        <span style="color: #008000;">self</span>._count += <span style="color: #ff4500;">1</span>
        <span style="color: #808080; font-style: italic;"># If we've exceeded the &quot;emphatic&quot; count disable this handler.</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">self</span>._count <span style="color: #66cc66;">&gt;</span>= <span style="color: #008000;">self</span>._emphatic:
            <span style="color: #008000;">self</span>.<span style="color: black;">disable</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__del__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">''</span><span style="color: #483d8b;">'
        Python is reclaiming this object, so make sure we are disabled.
        '</span><span style="color: #483d8b;">''</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">disable</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span>
&nbsp;
    @<span style="color: #008000;">property</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> count<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">''</span><span style="color: #483d8b;">'
        The number of breaks trapped.
        '</span><span style="color: #483d8b;">''</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>._count
&nbsp;
    @<span style="color: #008000;">property</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> trapped<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">''</span><span style="color: #483d8b;">'
        Whether a break was trapped.
        '</span><span style="color: #483d8b;">''</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>._count <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">0</span></pre></div></div>

<p>To use this, make an instance and then enable the instance.  If you repeatedly hit CTRL+C you will eventually disable it (see <tt>emphatic</tt> in the <tt>__init__</tt>).</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># Trap break.</span>
bh = BreakHandler<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
bh.<span style="color: black;">enable</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">for</span> item <span style="color: #ff7700;font-weight:bold;">in</span> big_set:
    <span style="color: #ff7700;font-weight:bold;">if</span> bh.<span style="color: black;">trapped</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'Stopping at user request (keyboard interrupt)...'</span>
        <span style="color: #ff7700;font-weight:bold;">break</span>
    do_thing_1<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    do_thing_2<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    do_thing_3<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
do_cleanup<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
bh.<span style="color: black;">disable</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>If you press CTRL+C, it is detected at the <em>start</em> of the loop, and break is called.  Then <tt>do_cleanup()</tt> runs, and the break handler is disabled, returning things to normal.  The idea is that <tt>do_thing_1()</tt>, <tt>do_thing_2()</tt>, and <tt>do_thing_3()</tt> all run to completion, even when the user presses CTRL+C, and <tt>do_cleanup()</tt> always runs when the loop is exited.</p>
<p>I consider this public domain; if you like it, use it. If you don&#8217;t like it, don&#8217;t use it. If you have suggestions, please leave me a comment.<!--more--></p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fstacyprowell.com%2Fblog%2F2009%2F03%2F30%2Ftrapping-ctrlc-in-python%2F&amp;title=Trapping%20CTRL%2BC%20in%20Python" id="wpa2a_4"><img src="http://stacyprowell.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://stacyprowell.com/blog/2009/03/30/trapping-ctrlc-in-python/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

