<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Collin's Code</title>
	<atom:link href="http://collincode.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://collincode.wordpress.com</link>
	<description>Lots of Random Python</description>
	<lastBuildDate>Tue, 28 Jul 2009 01:51:13 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='collincode.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/78e0e3aae63eaedc5b7026f02b09bf59?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>Collin's Code</title>
		<link>http://collincode.wordpress.com</link>
	</image>
			<item>
		<title>IMAP Import</title>
		<link>http://collincode.wordpress.com/2009/07/27/imap-import/</link>
		<comments>http://collincode.wordpress.com/2009/07/27/imap-import/#comments</comments>
		<pubDate>Tue, 28 Jul 2009 01:51:13 +0000</pubDate>
		<dc:creator>Collin Anderson</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[append]]></category>
		<category><![CDATA[chat]]></category>
		<category><![CDATA[email]]></category>
		<category><![CDATA[gchat]]></category>
		<category><![CDATA[gmail]]></category>
		<category><![CDATA[gtalk]]></category>
		<category><![CDATA[imap]]></category>
		<category><![CDATA[import]]></category>
		<category><![CDATA[migrate]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[talk]]></category>
		<category><![CDATA[transfer]]></category>

		<guid isPermaLink="false">http://collincode.wordpress.com/?p=152</guid>
		<description><![CDATA[I used this to upload all of my old chats saved as .eml files directly into my new Gmail account. It uses IMAP&#8217;s append to do this. I had tried Thunderbird and the ImportExportTools plugin, but ran into trouble.
This puts all of the messages into a label called &#8220;oldchats&#8221;. This label needs to exist before [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=collincode.wordpress.com&blog=2705418&post=152&subd=collincode&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>I used this to upload all of my old chats saved as .eml files directly into my new Gmail account. It uses IMAP&#8217;s append to do this. I had tried Thunderbird and the <a href="http://nic-nac-project.de/~kaosmos/mboximport-en.html">ImportExportTools plugin</a>, but ran into trouble.</p>
<p>This puts all of the messages into a label called &#8220;oldchats&#8221;. This label needs to exist before this program is run. It also deletes each message after it is uploaded. I made a backup copy of my messages.</p>
<div class="highlight">
<pre><span style="color:#007020;font-weight:bold;">import</span> <span style="color:#0e84b5;font-weight:bold;">email</span>
<span style="color:#007020;font-weight:bold;">import</span> <span style="color:#0e84b5;font-weight:bold;">os</span>
<span style="color:#007020;font-weight:bold;">import</span> <span style="color:#0e84b5;font-weight:bold;">time</span>
<span style="color:#007020;font-weight:bold;">import</span> <span style="color:#0e84b5;font-weight:bold;">imaplib</span>

imap <span style="color:#666666;">=</span> imaplib<span style="color:#666666;">.</span>IMAP4_SSL(<span style="color:#4070a0;">&#39;imap.gmail.com&#39;</span>, <span style="color:#40a070;">993</span>)
imap<span style="color:#666666;">.</span>login(<span style="color:#4070a0;">&#39;email@gmail.com&#39;</span>, <span style="color:#4070a0;">&#39;password&#39;</span>)

os<span style="color:#666666;">.</span>chdir(<span style="color:#4070a0;">&quot;chats&quot;</span>)
<span style="color:#007020;font-weight:bold;">for</span> filename <span style="color:#007020;font-weight:bold;">in</span> os<span style="color:#666666;">.</span>listdir(<span style="color:#4070a0;">&quot;.&quot;</span>):
    <span style="color:#007020;font-weight:bold;">print</span> filename,
    raw_eml <span style="color:#666666;">=</span> <span style="color:#007020;">file</span>(filename)<span style="color:#666666;">.</span>read()
    msg <span style="color:#666666;">=</span> email<span style="color:#666666;">.</span>message_from_string(raw_eml)
    date <span style="color:#666666;">=</span> email<span style="color:#666666;">.</span>utils<span style="color:#666666;">.</span>parsedate(msg[<span style="color:#4070a0;">&#39;Date&#39;</span>])
    <span style="color:#007020;font-weight:bold;">print</span> date,
    imap<span style="color:#666666;">.</span>append(<span style="color:#4070a0;">&#39;oldchats&#39;</span>, <span style="color:#007020;">None</span>, date, raw_eml)
    <span style="color:#007020;font-weight:bold;">print</span> <span style="color:#4070a0;">&quot;done&quot;</span>
    os<span style="color:#666666;">.</span>remove(filename)
</pre>
</div>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/collincode.wordpress.com/152/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/collincode.wordpress.com/152/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/collincode.wordpress.com/152/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/collincode.wordpress.com/152/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/collincode.wordpress.com/152/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/collincode.wordpress.com/152/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/collincode.wordpress.com/152/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/collincode.wordpress.com/152/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/collincode.wordpress.com/152/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/collincode.wordpress.com/152/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=collincode.wordpress.com&blog=2705418&post=152&subd=collincode&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://collincode.wordpress.com/2009/07/27/imap-import/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/05081bd09da27830150776c53318e0ef?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Collin Anderson</media:title>
		</media:content>
	</item>
		<item>
		<title>Google Chat History Downloader</title>
		<link>http://collincode.wordpress.com/2009/07/20/google-chat-history-downloader/</link>
		<comments>http://collincode.wordpress.com/2009/07/20/google-chat-history-downloader/#comments</comments>
		<pubDate>Tue, 21 Jul 2009 01:07:27 +0000</pubDate>
		<dc:creator>Collin Anderson</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[chat]]></category>
		<category><![CDATA[chat history]]></category>
		<category><![CDATA[download]]></category>
		<category><![CDATA[email]]></category>
		<category><![CDATA[export]]></category>
		<category><![CDATA[gchat]]></category>
		<category><![CDATA[gmail]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[gtalk]]></category>
		<category><![CDATA[history]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[migrate]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[talk]]></category>
		<category><![CDATA[transfer]]></category>

		<guid isPermaLink="false">http://collincode.wordpress.com/?p=137</guid>
		<description><![CDATA[A couple weeks ago, I decided to migrate from one Google Account to another. I was able to transfer all of my emails from one to the other without too much difficulty. However, I looked around for a while and have not found any way to export all of my Google Talk Chat history. I [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=collincode.wordpress.com&blog=2705418&post=137&subd=collincode&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>A couple weeks ago, I decided to migrate from one Google Account to another. I was able to transfer all of my emails from one to the other without too much difficulty. However, I looked around for a while and have not found any way to export all of my Google Talk Chat history. I don&#8217;t think there is any way to access saved chats from either IMAP or POP. I did notice though, that through the Gmail web interface, you can view saved chats as a raw message. There happens to be an old python library for interacting with the Gmail web interface called <a href="http://libgmail.sourceforge.net/">libgmail</a>. I found however that it does not scale very well to large amounts of messages, so I had to write my own method to only process results one page at a time. Also, I found that I was easily blocked using this method over a long time, so I added 13 second delays after every request so as not to get my account suspended. It took me a day and a half to actually export all of the messages. I&#8217;m not sure if this is over kill or not, but I am tired of getting my account blocked.</p>
<p>Anyway, This program goes through and saves each chat history message as an .eml file. One they are in that format, it is not super hard to get them into a different Gmail account, but I&#8217;ll save that for another post.</p>
<div class="highlight">
<pre><span style="color:#007020;font-weight:bold;">import</span> <span style="color:#0e84b5;font-weight:bold;">os</span>
<span style="color:#007020;font-weight:bold;">import</span> <span style="color:#0e84b5;font-weight:bold;">time</span>
<span style="color:#007020;font-weight:bold;">import</span> <span style="color:#0e84b5;font-weight:bold;">libgmail</span> <span style="color:#60a0b0;font-style:italic;"># http://libgmail.sourceforge.net/</span>

<span style="color:#007020;font-weight:bold;">def</span> <span style="color:#06287e;">thread_search</span>(ga, searchType, <span style="color:#666666;">**</span>kwargs):
    index <span style="color:#666666;">=</span> <span style="color:#40a070;">0</span>
    <span style="color:#007020;font-weight:bold;">while</span> (index <span style="color:#666666;">==</span> <span style="color:#40a070;">0</span>) <span style="color:#007020;font-weight:bold;">or</span> index <span style="color:#666666;">&lt;</span> threadListSummary[libgmail<span style="color:#666666;">.</span>TS_TOTAL]:
            threadsInfo <span style="color:#666666;">=</span> []
            items <span style="color:#666666;">=</span> ga<span style="color:#666666;">.</span>_parseSearchResult(searchType, index, <span style="color:#666666;">**</span>kwargs)
            <span style="color:#007020;font-weight:bold;">try</span>:
                threads <span style="color:#666666;">=</span> items[libgmail<span style="color:#666666;">.</span>D_THREAD]
            <span style="color:#007020;font-weight:bold;">except</span> <span style="color:#007020;">KeyError</span>:
                <span style="color:#007020;font-weight:bold;">break</span>
            <span style="color:#007020;font-weight:bold;">else</span>:
                <span style="color:#007020;font-weight:bold;">for</span> th <span style="color:#007020;font-weight:bold;">in</span> threads:
                    <span style="color:#007020;font-weight:bold;">if</span> <span style="color:#007020;font-weight:bold;">not</span> <span style="color:#007020;">type</span>(th[<span style="color:#40a070;">0</span>]) <span style="color:#007020;font-weight:bold;">is</span> libgmail<span style="color:#666666;">.</span>types<span style="color:#666666;">.</span>ListType:
                        th <span style="color:#666666;">=</span> [th]
                    threadsInfo<span style="color:#666666;">.</span>append(th)
                threadListSummary <span style="color:#666666;">=</span> items[libgmail<span style="color:#666666;">.</span>D_THREADLIST_SUMMARY][<span style="color:#40a070;">0</span>]
                threadsPerPage <span style="color:#666666;">=</span> threadListSummary[libgmail<span style="color:#666666;">.</span>TS_NUM]
                index <span style="color:#666666;">+=</span> threadsPerPage
            <span style="color:#007020;font-weight:bold;">yield</span> libgmail<span style="color:#666666;">.</span>GmailSearchResult(ga, (searchType, kwargs), threadsInfo)

ga <span style="color:#666666;">=</span> libgmail<span style="color:#666666;">.</span>GmailAccount(<span style="color:#4070a0;">&quot;username@gmail.com&quot;</span>, <span style="color:#4070a0;">&quot;password&quot;</span>)
ga<span style="color:#666666;">.</span>login()

<span style="color:#007020;font-weight:bold;">for</span> page <span style="color:#007020;font-weight:bold;">in</span> thread_search(ga, <span style="color:#4070a0;">&quot;query&quot;</span>, q<span style="color:#666666;">=</span><span style="color:#4070a0;">&quot;is:chat&quot;</span>):
    <span style="color:#007020;font-weight:bold;">print</span> <span style="color:#4070a0;">&quot;New Page&quot;</span>
    time<span style="color:#666666;">.</span>sleep(<span style="color:#40a070;">13</span>)
    <span style="color:#007020;font-weight:bold;">for</span> thread <span style="color:#007020;font-weight:bold;">in</span> page:
        <span style="color:#007020;font-weight:bold;">if</span> thread<span style="color:#666666;">.</span>info[<span style="color:#40a070;">0</span>] <span style="color:#666666;">==</span> thread<span style="color:#666666;">.</span>info[<span style="color:#40a070;">10</span>]:
            <span style="color:#60a0b0;font-style:italic;"># Common case: Chats that only span one message</span>
            filename <span style="color:#666666;">=</span> <span style="color:#4070a0;">&quot;chats/</span><span style="color:#70a0d0;font-style:italic;">%s</span><span style="color:#4070a0;">_</span><span style="color:#70a0d0;font-style:italic;">%s</span><span style="color:#4070a0;">.eml&quot;</span> <span style="color:#666666;">%</span> (thread<span style="color:#666666;">.</span>id, thread<span style="color:#666666;">.</span>id)
            <span style="color:#60a0b0;font-style:italic;">#only download the message if we don&#39;t have it already</span>
            <span style="color:#007020;font-weight:bold;">if</span> os<span style="color:#666666;">.</span>path<span style="color:#666666;">.</span>exists(filename):
                <span style="color:#007020;font-weight:bold;">print</span> <span style="color:#4070a0;">&quot;already have </span><span style="color:#70a0d0;font-style:italic;">%s</span><span style="color:#4070a0;">&quot;</span> <span style="color:#666666;">%</span> filename
                <span style="color:#007020;font-weight:bold;">continue</span>
            <span style="color:#007020;font-weight:bold;">print</span> <span style="color:#4070a0;">&quot;Downloading raw message: </span><span style="color:#70a0d0;font-style:italic;">%s</span><span style="color:#4070a0;">&quot;</span> <span style="color:#666666;">%</span> filename,
            message <span style="color:#666666;">=</span> ga<span style="color:#666666;">.</span>getRawMessage(thread<span style="color:#666666;">.</span>id)<span style="color:#666666;">.</span>decode(<span style="color:#4070a0;">&#39;utf-8&#39;</span>)<span style="color:#666666;">.</span>lstrip()
            <span style="color:#007020;font-weight:bold;">print</span> <span style="color:#4070a0;">&quot;done.&quot;</span>
            <span style="color:#007020;">file</span>(filename, <span style="color:#4070a0;">&#39;wb&#39;</span>)<span style="color:#666666;">.</span>write(message)
            time<span style="color:#666666;">.</span>sleep(<span style="color:#40a070;">13</span>)
            <span style="color:#007020;font-weight:bold;">continue</span>
        <span style="color:#60a0b0;font-style:italic;"># Less common case: A thread that has multiple messages</span>
        <span style="color:#007020;font-weight:bold;">print</span> <span style="color:#4070a0;">&quot;Looking up messages in thread </span><span style="color:#70a0d0;font-style:italic;">%s</span><span style="color:#4070a0;">&quot;</span> <span style="color:#666666;">%</span> thread<span style="color:#666666;">.</span>id
        time<span style="color:#666666;">.</span>sleep(<span style="color:#40a070;">13</span>)
        <span style="color:#007020;font-weight:bold;">for</span> message <span style="color:#007020;font-weight:bold;">in</span> thread:
            filename <span style="color:#666666;">=</span> <span style="color:#4070a0;">&quot;chats/</span><span style="color:#70a0d0;font-style:italic;">%s</span><span style="color:#4070a0;">_</span><span style="color:#70a0d0;font-style:italic;">%s</span><span style="color:#4070a0;">.eml&quot;</span> <span style="color:#666666;">%</span> (thread<span style="color:#666666;">.</span>id, message<span style="color:#666666;">.</span>id)
            <span style="color:#60a0b0;font-style:italic;">#only download the message if we don&#39;t have it already</span>
            <span style="color:#007020;font-weight:bold;">if</span> os<span style="color:#666666;">.</span>path<span style="color:#666666;">.</span>exists(filename):
                <span style="color:#007020;font-weight:bold;">print</span> <span style="color:#4070a0;">&quot;already have </span><span style="color:#70a0d0;font-style:italic;">%s</span><span style="color:#4070a0;">&quot;</span> <span style="color:#666666;">%</span> filename
                <span style="color:#007020;font-weight:bold;">continue</span>
            <span style="color:#007020;font-weight:bold;">print</span> <span style="color:#4070a0;">&quot;Downloading raw message: </span><span style="color:#70a0d0;font-style:italic;">%s</span><span style="color:#4070a0;">&quot;</span> <span style="color:#666666;">%</span> filename,
            <span style="color:#007020;">file</span>(filename, <span style="color:#4070a0;">&#39;wb&#39;</span>)<span style="color:#666666;">.</span>write(message<span style="color:#666666;">.</span>source<span style="color:#666666;">.</span>lstrip())
            <span style="color:#007020;font-weight:bold;">print</span> <span style="color:#4070a0;">&quot;done.&quot;</span>
            time<span style="color:#666666;">.</span>sleep(<span style="color:#40a070;">13</span>)
</pre>
</div>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/collincode.wordpress.com/137/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/collincode.wordpress.com/137/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/collincode.wordpress.com/137/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/collincode.wordpress.com/137/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/collincode.wordpress.com/137/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/collincode.wordpress.com/137/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/collincode.wordpress.com/137/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/collincode.wordpress.com/137/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/collincode.wordpress.com/137/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/collincode.wordpress.com/137/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=collincode.wordpress.com&blog=2705418&post=137&subd=collincode&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://collincode.wordpress.com/2009/07/20/google-chat-history-downloader/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/05081bd09da27830150776c53318e0ef?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Collin Anderson</media:title>
		</media:content>
	</item>
		<item>
		<title>This one checks to make sure my Gmail Contacts&#8217; names are spelt how the contact spells them.</title>
		<link>http://collincode.wordpress.com/2009/07/19/this-one-checks-to-make-sure-my-gmail-contacts-names-are-spelt-how-the-contact-spells-them/</link>
		<comments>http://collincode.wordpress.com/2009/07/19/this-one-checks-to-make-sure-my-gmail-contacts-names-are-spelt-how-the-contact-spells-them/#comments</comments>
		<pubDate>Sun, 19 Jul 2009 19:54:06 +0000</pubDate>
		<dc:creator>Collin Anderson</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[contacts]]></category>
		<category><![CDATA[email]]></category>
		<category><![CDATA[gmail]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[libgmail]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[rename]]></category>
		<category><![CDATA[spelling]]></category>

		<guid isPermaLink="false">http://collincode.wordpress.com/?p=133</guid>
		<description><![CDATA[It looks at emails sent from the contact, and compares the sender name with the name on file for that contact.

import libgmail # http://libgmail.sourceforge.net/

ga = libgmail.GmailAccount(&#34;email@gmail.com&#34;, &#34;password&#34;)
ga.login()
all_contacts = ga.getContacts().getAllContacts()

def test_thread(thread, contact):
    for message in thread:
        if message.sender.lower() != contact.email.lower():
       [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=collincode.wordpress.com&blog=2705418&post=133&subd=collincode&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>It looks at emails sent from the contact, and compares the sender name with the name on file for that contact.</p>
<div class="highlight">
<pre><span style="color:#007020;font-weight:bold;">import</span> <span style="color:#0e84b5;font-weight:bold;">libgmail</span> <span style="color:#60a0b0;font-style:italic;"># http://libgmail.sourceforge.net/</span>

ga <span style="color:#666666;">=</span> libgmail<span style="color:#666666;">.</span>GmailAccount(<span style="color:#4070a0;">&quot;email@gmail.com&quot;</span>, <span style="color:#4070a0;">&quot;password&quot;</span>)
ga<span style="color:#666666;">.</span>login()
all_contacts <span style="color:#666666;">=</span> ga<span style="color:#666666;">.</span>getContacts()<span style="color:#666666;">.</span>getAllContacts()

<span style="color:#007020;font-weight:bold;">def</span> <span style="color:#06287e;">test_thread</span>(thread, contact):
    <span style="color:#007020;font-weight:bold;">for</span> message <span style="color:#007020;font-weight:bold;">in</span> thread:
        <span style="color:#007020;font-weight:bold;">if</span> message<span style="color:#666666;">.</span>sender<span style="color:#666666;">.</span>lower() <span style="color:#666666;">!=</span> contact<span style="color:#666666;">.</span>email<span style="color:#666666;">.</span>lower():
            <span style="color:#007020;font-weight:bold;">continue</span>
        <span style="color:#007020;font-weight:bold;">if</span> <span style="color:#4070a0;">&#39;@&#39;</span> <span style="color:#007020;font-weight:bold;">in</span> message<span style="color:#666666;">.</span>author_fullname:
            <span style="color:#007020;font-weight:bold;">continue</span>
        <span style="color:#007020;font-weight:bold;">if</span> message<span style="color:#666666;">.</span>author_fullname <span style="color:#666666;">==</span> contact<span style="color:#666666;">.</span>name:
            <span style="color:#007020;font-weight:bold;">return</span> <span style="color:#007020;">True</span>
        <span style="color:#007020;font-weight:bold;">else</span>:
            <span style="color:#007020;font-weight:bold;">print</span> <span style="color:#4070a0;">&quot;</span><span style="color:#4070a0;font-weight:bold;">\t</span><span style="color:#4070a0;">&quot;</span>, message<span style="color:#666666;">.</span>author_fullname, <span style="color:#4070a0;">&quot;-&gt;&quot;</span>, contact<span style="color:#666666;">.</span>name
            <span style="color:#007020;font-weight:bold;">return</span> <span style="color:#007020;">True</span>

<span style="color:#007020;font-weight:bold;">for</span> contact <span style="color:#007020;font-weight:bold;">in</span> all_contacts:
    results <span style="color:#666666;">=</span> ga<span style="color:#666666;">.</span>getMessagesByQuery(<span style="color:#4070a0;">&quot;from: </span><span style="color:#70a0d0;font-style:italic;">%s</span><span style="color:#4070a0;"> -is:chat&quot;</span> <span style="color:#666666;">%</span> contact<span style="color:#666666;">.</span>email)
    <span style="color:#007020;font-weight:bold;">if</span> <span style="color:#007020;font-weight:bold;">not</span> results:
        <span style="color:#007020;font-weight:bold;">print</span> <span style="color:#4070a0;">&quot;</span><span style="color:#70a0d0;font-style:italic;">%s</span><span style="color:#4070a0;"> -------No Email&quot;</span> <span style="color:#666666;">%</span> contact<span style="color:#666666;">.</span>email
        <span style="color:#007020;font-weight:bold;">continue</span>
    <span style="color:#007020;font-weight:bold;">for</span> thread <span style="color:#007020;font-weight:bold;">in</span> results:
        result <span style="color:#666666;">=</span> test_thread(thread, contact)
        <span style="color:#007020;font-weight:bold;">if</span> result:
            <span style="color:#007020;font-weight:bold;">break</span>
    <span style="color:#007020;font-weight:bold;">else</span>:
        <span style="color:#007020;font-weight:bold;">print</span> <span style="color:#4070a0;">&quot;</span><span style="color:#70a0d0;font-style:italic;">%s</span><span style="color:#4070a0;"> -------No Good Email&quot;</span> <span style="color:#666666;">%</span> contact<span style="color:#666666;">.</span>email
</pre>
</div>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/collincode.wordpress.com/133/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/collincode.wordpress.com/133/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/collincode.wordpress.com/133/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/collincode.wordpress.com/133/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/collincode.wordpress.com/133/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/collincode.wordpress.com/133/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/collincode.wordpress.com/133/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/collincode.wordpress.com/133/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/collincode.wordpress.com/133/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/collincode.wordpress.com/133/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=collincode.wordpress.com&blog=2705418&post=133&subd=collincode&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://collincode.wordpress.com/2009/07/19/this-one-checks-to-make-sure-my-gmail-contacts-names-are-spelt-how-the-contact-spells-them/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/05081bd09da27830150776c53318e0ef?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Collin Anderson</media:title>
		</media:content>
	</item>
		<item>
		<title>XMPP Jabber Photo Module</title>
		<link>http://collincode.wordpress.com/2009/01/31/xmpp-jabber-photo-module-2/</link>
		<comments>http://collincode.wordpress.com/2009/01/31/xmpp-jabber-photo-module-2/#comments</comments>
		<pubDate>Sat, 31 Jan 2009 17:23:41 +0000</pubDate>
		<dc:creator>Collin Anderson</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[avatar]]></category>
		<category><![CDATA[gchat]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[gtalk]]></category>
		<category><![CDATA[jabber]]></category>
		<category><![CDATA[photo]]></category>
		<category><![CDATA[picture]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[talk]]></category>
		<category><![CDATA[vCard]]></category>
		<category><![CDATA[xmpp]]></category>
		<category><![CDATA[xmpppy]]></category>

		<guid isPermaLink="false">http://collincode.wordpress.com/?p=68</guid>
		<description><![CDATA[I was looking around for a XMPP vCard Photo Module in May of 2007, and could not find one, so I wrote my own. It uses the xmpppy python module for communicating with the jabber server. Once the connection is made with the server, call register_handler(session). It will then download avatars of people on the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=collincode.wordpress.com&blog=2705418&post=68&subd=collincode&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>I was looking around for a XMPP vCard Photo Module in May of 2007, and could not find one, so I wrote my own. It uses the xmpppy python module for communicating with the jabber server. Once the connection is made with the server, call register_handler(session). It will then download avatars of people on the roster list. To get the filename of a person&#8217;s avatar, call get_photo(photo_hash).</p>
<div class="highlight">
<pre><span style="color:#007020;font-weight:bold;">import</span> <span style="color:#0e84b5;font-weight:bold;">base64</span>
<span style="color:#007020;font-weight:bold;">import</span> <span style="color:#0e84b5;font-weight:bold;">os</span>
<span style="color:#007020;font-weight:bold;">import</span> <span style="color:#0e84b5;font-weight:bold;">sha</span>
<span style="color:#007020;font-weight:bold;">import</span> <span style="color:#0e84b5;font-weight:bold;">xmpp</span>

PHOTO_DIR <span style="color:#666666;">=</span> <span style="color:#4070a0;">&quot;./photos/&quot;</span>

PHOTO_TYPES <span style="color:#666666;">=</span> {
    <span style="color:#4070a0;">&#39;image/png&#39;</span>: <span style="color:#4070a0;">&#39;.png&#39;</span>,
    <span style="color:#4070a0;">&#39;image/jpeg&#39;</span>: <span style="color:#4070a0;">&#39;.jpg&#39;</span>,
    <span style="color:#4070a0;">&#39;image/gif&#39;</span>: <span style="color:#4070a0;">&#39;.gif&#39;</span>,
    <span style="color:#4070a0;">&#39;image/bmp&#39;</span>: <span style="color:#4070a0;">&#39;.bmp&#39;</span>,
    }

<span style="color:#007020;font-weight:bold;">def</span> <span style="color:#06287e;">append_directory</span>(filename):
    <span style="color:#007020;font-weight:bold;">return</span> os<span style="color:#666666;">.</span>path<span style="color:#666666;">.</span>join(PHOTO_DIR, filename)

<span style="color:#007020;font-weight:bold;">def</span> <span style="color:#06287e;">register_handler</span>(session):
    session<span style="color:#666666;">.</span>RegisterHandler(<span style="color:#4070a0;">&#39;presence&#39;</span>, photo_update_handler)

<span style="color:#007020;font-weight:bold;">def</span> <span style="color:#06287e;">photo_update_handler</span>(session, stanza):
    JID <span style="color:#666666;">=</span> stanza[<span style="color:#4070a0;">&#39;from&#39;</span>]<span style="color:#666666;">.</span>getStripped()
    vupdate <span style="color:#666666;">=</span> stanza<span style="color:#666666;">.</span>getTag(<span style="color:#4070a0;">&#39;x&#39;</span>, namespace<span style="color:#666666;">=</span><span style="color:#4070a0;">&#39;vcard-temp:x:update&#39;</span>)
    <span style="color:#007020;font-weight:bold;">if</span> <span style="color:#007020;font-weight:bold;">not</span> vupdate:
        <span style="color:#007020;font-weight:bold;">return</span>
    photo <span style="color:#666666;">=</span> vupdate<span style="color:#666666;">.</span>getTag(<span style="color:#4070a0;">&#39;photo&#39;</span>)
    <span style="color:#007020;font-weight:bold;">if</span> <span style="color:#007020;font-weight:bold;">not</span> photo:
        <span style="color:#007020;font-weight:bold;">return</span>
    photo <span style="color:#666666;">=</span> photo<span style="color:#666666;">.</span>getData()
    <span style="color:#007020;font-weight:bold;">if</span> <span style="color:#007020;font-weight:bold;">not</span> photo:
        <span style="color:#007020;font-weight:bold;">return</span>
    <span style="color:#60a0b0;font-style:italic;">#request the photo only if we don&#39;t have it already</span>
    <span style="color:#007020;font-weight:bold;">if</span> <span style="color:#007020;font-weight:bold;">not</span> get_photo(photo):
        request_vcard(session, JID)

<span style="color:#007020;font-weight:bold;">def</span> <span style="color:#06287e;">get_photo</span>(photo_hash):
    <span style="color:#007020;font-weight:bold;">for</span> ext <span style="color:#007020;font-weight:bold;">in</span> PHOTO_TYPES<span style="color:#666666;">.</span>values():
        filepath <span style="color:#666666;">=</span> append_directory(photo_hash <span style="color:#666666;">+</span> ext)
        <span style="color:#007020;font-weight:bold;">if</span> os<span style="color:#666666;">.</span>path<span style="color:#666666;">.</span>exists(filepath):
            <span style="color:#007020;font-weight:bold;">return</span> filepath

<span style="color:#007020;font-weight:bold;">def</span> <span style="color:#06287e;">request_vcard</span>(session, JID):
    n <span style="color:#666666;">=</span> xmpp<span style="color:#666666;">.</span>Node(<span style="color:#4070a0;">&#39;vCard&#39;</span>, attrs<span style="color:#666666;">=</span>{<span style="color:#4070a0;">&#39;xmlns&#39;</span>: xmpp<span style="color:#666666;">.</span>NS_VCARD})
    iq <span style="color:#666666;">=</span> xmpp<span style="color:#666666;">.</span>Protocol(<span style="color:#4070a0;">&#39;iq&#39;</span>, JID, <span style="color:#4070a0;">&#39;get&#39;</span>, payload<span style="color:#666666;">=</span>[n])
    <span style="color:#007020;font-weight:bold;">return</span> session<span style="color:#666666;">.</span>SendAndCallForResponse(iq, recieve_vcard)

<span style="color:#007020;font-weight:bold;">def</span> <span style="color:#06287e;">recieve_vcard</span>(session, stanza):
    photo <span style="color:#666666;">=</span> stanza<span style="color:#666666;">.</span>getTag(<span style="color:#4070a0;">&#39;vCard&#39;</span>)<span style="color:#666666;">.</span>getTag(<span style="color:#4070a0;">&#39;PHOTO&#39;</span>)
    <span style="color:#007020;font-weight:bold;">if</span> <span style="color:#007020;font-weight:bold;">not</span> photo:
        <span style="color:#007020;font-weight:bold;">return</span>
    photo_type <span style="color:#666666;">=</span> photo<span style="color:#666666;">.</span>getTag(<span style="color:#4070a0;">&#39;TYPE&#39;</span>)<span style="color:#666666;">.</span>getData()
    photo_bin <span style="color:#666666;">=</span> photo<span style="color:#666666;">.</span>getTag(<span style="color:#4070a0;">&#39;BINVAL&#39;</span>)<span style="color:#666666;">.</span>getData()
    photo_bin <span style="color:#666666;">=</span> base64<span style="color:#666666;">.</span>b64decode(photo_bin)
    ext <span style="color:#666666;">=</span> PHOTO_TYPES[photo_type]
    photo_hash <span style="color:#666666;">=</span> sha<span style="color:#666666;">.</span>new()
    photo_hash<span style="color:#666666;">.</span>update(photo_bin)
    photo_hash <span style="color:#666666;">=</span> photo_hash<span style="color:#666666;">.</span>hexdigest()
    filename <span style="color:#666666;">=</span> append_directory(photo_hash <span style="color:#666666;">+</span> ext)
    <span style="color:#007020;">file</span>(filename, <span style="color:#4070a0;">&#39;wb&#39;</span>)<span style="color:#666666;">.</span>write(photo_bin)
</pre>
</div>
<p>UPDATE 7/20/09<br />
Here is roughly how I would solve the use case in the comment. I realize that my code doesn&#8217;t solve that case very nicely. If it made this more object oriented I could solve that problem. Until then, here is some rough code that I think does what it needs to. Save the above code as photo.py and be sure to create a &#8220;photos&#8221; directory</p>
<div class="highlight">
<pre><span style="color:#007020;font-weight:bold;">import</span> <span style="color:#0e84b5;font-weight:bold;">xmpp</span>
<span style="color:#007020;font-weight:bold;">import</span> <span style="color:#0e84b5;font-weight:bold;">photo</span>

jid_photo_map <span style="color:#666666;">=</span> {}

<span style="color:#007020;font-weight:bold;">def</span> <span style="color:#06287e;">receive_presence</span>(session, stanza):
    jid <span style="color:#666666;">=</span> stanza[<span style="color:#4070a0;">&#39;from&#39;</span>]<span style="color:#666666;">.</span>getStripped()
    vupdate <span style="color:#666666;">=</span> stanza<span style="color:#666666;">.</span>getTag(<span style="color:#4070a0;">&#39;x&#39;</span>, namespace<span style="color:#666666;">=</span><span style="color:#4070a0;">&#39;vcard-temp:x:update&#39;</span>)
    <span style="color:#007020;font-weight:bold;">if</span> <span style="color:#007020;font-weight:bold;">not</span> vupdate:
        <span style="color:#007020;font-weight:bold;">return</span>
    photo <span style="color:#666666;">=</span> vupdate<span style="color:#666666;">.</span>getTag(<span style="color:#4070a0;">&#39;photo&#39;</span>)
    <span style="color:#007020;font-weight:bold;">if</span> <span style="color:#007020;font-weight:bold;">not</span> photo:
        <span style="color:#007020;font-weight:bold;">return</span>
    photo <span style="color:#666666;">=</span> photo<span style="color:#666666;">.</span>getData()
    <span style="color:#007020;font-weight:bold;">if</span> <span style="color:#007020;font-weight:bold;">not</span> photo:
        <span style="color:#007020;font-weight:bold;">return</span>
    jid_photo_map[jid] <span style="color:#666666;">=</span> photo

<span style="color:#007020;font-weight:bold;">def</span> <span style="color:#06287e;">login</span>(username, password):
    jabber <span style="color:#666666;">=</span> xmpp<span style="color:#666666;">.</span>Client(<span style="color:#4070a0;">&#39;gmail.com&#39;</span>)
    jabber<span style="color:#666666;">.</span>connect(server<span style="color:#666666;">=</span>(<span style="color:#4070a0;">&#39;talk.google.com&#39;</span>, <span style="color:#40a070;">5223</span>))
    jabber<span style="color:#666666;">.</span>auth(username, password, <span style="color:#4070a0;">&#39;test_client&#39;</span>)
    jabber<span style="color:#666666;">.</span>sendInitPresence()
    jabber<span style="color:#666666;">.</span>RegisterHandler(<span style="color:#4070a0;">&#39;presence&#39;</span>, receive_presence)
    photo<span style="color:#666666;">.</span>register_handler(jabber)
    <span style="color:#007020;font-weight:bold;">return</span> jabber

<span style="color:#007020;font-weight:bold;">def</span> <span style="color:#06287e;">display_filenames</span>(j):
    roster <span style="color:#666666;">=</span> j<span style="color:#666666;">.</span>getRoster()
    <span style="color:#007020;font-weight:bold;">for</span> jid <span style="color:#007020;font-weight:bold;">in</span> roster<span style="color:#666666;">.</span>getItems():
        <span style="color:#007020;font-weight:bold;">if</span> jid <span style="color:#007020;font-weight:bold;">in</span> jid_photo_map:
            photo_filename <span style="color:#666666;">=</span> photo<span style="color:#666666;">.</span>get_photo(jid_photo_map[jid])
        <span style="color:#007020;font-weight:bold;">else</span>:
            photo_filename <span style="color:#666666;">=</span> <span style="color:#4070a0;">&quot;&quot;</span>
        <span style="color:#007020;font-weight:bold;">print</span> jid, photo_filename

<span style="color:#007020;font-weight:bold;">if</span> __name__ <span style="color:#666666;">==</span> <span style="color:#4070a0;">&#39;__main__&#39;</span>:
    j <span style="color:#666666;">=</span> login(<span style="color:#4070a0;">&#39;username&#39;</span>, <span style="color:#4070a0;">&#39;password&#39;</span>)
    <span style="color:#007020;font-weight:bold;">for</span> x <span style="color:#007020;font-weight:bold;">in</span> <span style="color:#007020;">range</span>(<span style="color:#40a070;">30</span>):
        j<span style="color:#666666;">.</span>Process(<span style="color:#40a070;">1</span>) <span style="color:#60a0b0;font-style:italic;"># process for 1 second</span>
    display_filenames(j)
</pre>
</div>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/collincode.wordpress.com/68/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/collincode.wordpress.com/68/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/collincode.wordpress.com/68/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/collincode.wordpress.com/68/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/collincode.wordpress.com/68/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/collincode.wordpress.com/68/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/collincode.wordpress.com/68/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/collincode.wordpress.com/68/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/collincode.wordpress.com/68/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/collincode.wordpress.com/68/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=collincode.wordpress.com&blog=2705418&post=68&subd=collincode&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://collincode.wordpress.com/2009/01/31/xmpp-jabber-photo-module-2/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/05081bd09da27830150776c53318e0ef?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Collin Anderson</media:title>
		</media:content>
	</item>
		<item>
		<title>Message Queue</title>
		<link>http://collincode.wordpress.com/2008/12/11/message-queue/</link>
		<comments>http://collincode.wordpress.com/2008/12/11/message-queue/#comments</comments>
		<pubDate>Thu, 11 Dec 2008 13:37:33 +0000</pubDate>
		<dc:creator>Collin Anderson</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[comet]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[message]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[push]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[queue]]></category>
		<category><![CDATA[server]]></category>

		<guid isPermaLink="false">http://collincode.wordpress.com/?p=58</guid>
		<description><![CDATA[I wrote some code for a group project that I am kind of proud of. It&#8217;s not very clean code, but it accomplishes something cool. It&#8217;s a way for a website to sent messages to a browser in real time, without the browser needing to constantly be checking to see if the website has a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=collincode.wordpress.com&blog=2705418&post=58&subd=collincode&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>I wrote some code for a group project that I am kind of proud of. It&#8217;s not very clean code, but it accomplishes something cool. It&#8217;s a way for a website to sent messages to a browser in real time, without the browser needing to constantly be checking to see if the website has a message that is ready to be sent.</p>
<p>We did most of our project in php. Here is our php code:</p>
<pre>
<div><span style="color:#000000;">&lt;?</span><span style="color:#000000;">php
</span><span style="color:#0000FF;">include_once</span><span style="color:#000000;">(</span><span style="color:#000000;">&quot;</span><span style="color:#000000;">json.php</span><span style="color:#000000;">&quot;</span><span style="color:#000000;">);

</span><span style="color:#0000FF;">function</span><span style="color:#000000;"> get_url(</span><span style="color:#800080;">$url</span><span style="color:#000000;">) {
  </span><span style="color:#800080;">$output</span><span style="color:#000000;"> </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#0000FF;">array</span><span style="color:#000000;">();
  </span><span style="color:#008080;">exec</span><span style="color:#000000;">(</span><span style="color:#000000;">&quot;</span><span style="color:#000000;">curl </span><span style="color:#000000;">&quot;</span><span style="color:#000000;"> </span><span style="color:#000000;">.</span><span style="color:#000000;"> </span><span style="color:#800080;">$url</span><span style="color:#000000;">,</span><span style="color:#000000;"> </span><span style="color:#000000;">&amp;</span><span style="color:#800080;">$output</span><span style="color:#000000;">);
  </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> </span><span style="color:#800080;">$output</span><span style="color:#000000;">[</span><span style="color:#000000;">0</span><span style="color:#000000;">];
}

</span><span style="color:#0000FF;">function</span><span style="color:#000000;"> msgq_new() {
  </span><span style="color:#800080;">$id</span><span style="color:#000000;"> </span><span style="color:#000000;">=</span><span style="color:#000000;"> get_url(</span><span style="color:#000000;">&quot;</span><span style="color:#000000;">http://127.0.0.1:8888/new/</span><span style="color:#000000;">&quot;</span><span style="color:#000000;">);
  </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> </span><span style="color:#800080;">$id</span><span style="color:#000000;">;
}

</span><span style="color:#0000FF;">function</span><span style="color:#000000;"> send_message(</span><span style="color:#800080;">$data</span><span style="color:#000000;">) {
  </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> get_url(</span><span style="color:#000000;">&quot;</span><span style="color:#000000;">http://127.0.0.1:8888/post/</span><span style="color:#000000;">&quot;</span><span style="color:#000000;"> </span><span style="color:#000000;">.</span><span style="color:#000000;"> </span><span style="color:#008080;">urlencode</span><span style="color:#000000;">(array2json(</span><span style="color:#800080;">$data</span><span style="color:#000000;">)));
}

</span><span style="color:#0000FF;">if</span><span style="color:#000000;"> (</span><span style="color:#0000FF;">isset</span><span style="color:#000000;">(</span><span style="color:#800080;">$_GET</span><span style="color:#000000;">[</span><span style="color:#000000;">'</span><span style="color:#000000;">action</span><span style="color:#000000;">'</span><span style="color:#000000;">]) </span><span style="color:#000000;">&amp;&amp;</span><span style="color:#000000;"> </span><span style="color:#800080;">$_GET</span><span style="color:#000000;">[</span><span style="color:#000000;">'</span><span style="color:#000000;">action</span><span style="color:#000000;">'</span><span style="color:#000000;">] </span><span style="color:#000000;">==</span><span style="color:#000000;"> </span><span style="color:#000000;">&quot;</span><span style="color:#000000;">wait_for_message</span><span style="color:#000000;">&quot;</span><span style="color:#000000;">) {
  </span><span style="color:#008080;">header</span><span style="color:#000000;">(</span><span style="color:#000000;">&quot;</span><span style="color:#000000;">Content-type: text/plain</span><span style="color:#000000;">&quot;</span><span style="color:#000000;">);
  </span><span style="color:#0000FF;">echo</span><span style="color:#000000;"> get_url(</span><span style="color:#000000;">&quot;</span><span style="color:#000000;">http://127.0.0.1:8888/wait/</span><span style="color:#000000;">&quot;</span><span style="color:#000000;"> </span><span style="color:#000000;">.</span><span style="color:#000000;"> </span><span style="color:#800080;">$_GET</span><span style="color:#000000;">[</span><span style="color:#000000;">'</span><span style="color:#000000;">id</span><span style="color:#000000;">'</span><span style="color:#000000;">]);
}
</span></div>
</pre>
<p>Here is the Javascript part. We were using the YUI library, but you could easily do this without it.:</p>
<div class="highlight">
<pre><span style="color:#007020;font-weight:bold;">function</span> wait_for_message() {
  <span style="color:#007020;font-weight:bold;">var</span> id <span style="color:#666666;">=</span> <span style="color:#007020;">document</span>.body.id<span style="color:#666666;">;</span>
  <span style="color:#666666;">YAHOO</span>.util.<span style="color:#666666;">C</span>onnect.asyncRequest(<span style="color:#4070a0;">&#39;GET&#39;</span><span style="color:#666666;">,</span> <span style="color:#4070a0;">&#39;/wait_for_message.php?id=&#39;</span> <span style="color:#666666;">+</span> id<span style="color:#666666;">,</span> {success<span style="color:#666666;">:</span> <span style="color:#007020;font-weight:bold;">function</span>(response) {
    wait_for_message();
    <span style="color:#007020;font-weight:bold;">if</span> (<span style="color:#666666;">!</span>response.responseText <span style="color:#666666;">||</span> response.responseText <span style="color:#666666;">==</span> <span style="color:#4070a0;">&quot;\n&quot;</span>) <span style="color:#007020;font-weight:bold;">return</span><span style="color:#666666;">;</span> <span style="color:#60a0b0;font-style:italic;">// Server sent a nop</span>
<span style="color:#60a0b0;font-style:italic;"></span>    <span style="color:#007020;font-weight:bold;">var</span> data <span style="color:#666666;">=</span> <span style="color:#666666;">YAHOO</span>.lang.<span style="color:#666666;">JSON</span>.parse(response.responseText);
    <span style="color:#007020;">window</span><span style="color:#666666;">[</span>data.handler](data);
  }});
}
<span style="color:#666666;">YAHOO</span>.util.<span style="color:#666666;">E</span>vent.addListener(<span style="color:#007020;">window</span><span style="color:#666666;">,</span><span style="color:#4070a0;">&#39;load&#39;</span><span style="color:#666666;">,</span> wait_for_message)
</pre>
</div>
<p>Here is the Python part:</p>
<div class="highlight">
<pre><span style="color:#007020;font-weight:bold;">from</span> <span style="color:#0e84b5;font-weight:bold;">time</span> <span style="color:#007020;font-weight:bold;">import</span> time
<span style="color:#007020;font-weight:bold;">from</span> <span style="color:#0e84b5;font-weight:bold;">random</span> <span style="color:#007020;font-weight:bold;">import</span> uniform
<span style="color:#007020;font-weight:bold;">from</span> <span style="color:#0e84b5;font-weight:bold;">Queue</span> <span style="color:#007020;font-weight:bold;">import</span> Queue,Empty
<span style="color:#007020;font-weight:bold;">from</span> <span style="color:#0e84b5;font-weight:bold;">socket</span> <span style="color:#007020;font-weight:bold;">import</span> error
<span style="color:#007020;font-weight:bold;">from</span> <span style="color:#0e84b5;font-weight:bold;">threading</span> <span style="color:#007020;font-weight:bold;">import</span> Thread
<span style="color:#007020;font-weight:bold;">from</span> <span style="color:#0e84b5;font-weight:bold;">urllib</span> <span style="color:#007020;font-weight:bold;">import</span> unquote_plus
<span style="color:#007020;font-weight:bold;">from</span> <span style="color:#0e84b5;font-weight:bold;">wsgiref.simple_server</span> <span style="color:#007020;font-weight:bold;">import</span> make_server

clients<span style="color:#666666;">=</span>{} <span style="color:#60a0b0;font-style:italic;"># A Queue and a http server thread for each client</span>

<span style="color:#007020;font-weight:bold;">def</span> <span style="color:#06287e;">send_to_all</span>(msg):
    <span style="color:#007020;font-weight:bold;">print</span> <span style="color:#4070a0;">&quot;sending </span><span style="color:#70a0d0;font-style:italic;">%r</span><span style="color:#4070a0;"> to all&quot;</span> <span style="color:#666666;">%</span> msg
    <span style="color:#007020;font-weight:bold;">for</span> k, x <span style="color:#007020;font-weight:bold;">in</span> clients<span style="color:#666666;">.</span>items()[:]:
        <span style="color:#007020;font-weight:bold;">if</span> (time() <span style="color:#666666;">-</span> x<span style="color:#666666;">.</span>last_get <span style="color:#666666;">&gt;</span> <span style="color:#40a070;">300</span>):
            <span style="color:#60a0b0;font-style:italic;"># Client has not asked for any messages for 5 minutes</span>
            <span style="color:#60a0b0;font-style:italic;"># Delete them.</span>
	    x<span style="color:#666666;">.</span>active <span style="color:#666666;">=</span> <span style="color:#007020;">False</span>
	    <span style="color:#007020;font-weight:bold;">del</span> clients[k]
	    <span style="color:#007020;font-weight:bold;">continue</span>
        <span style="color:#007020;font-weight:bold;">print</span> <span style="color:#4070a0;">&quot;sending to </span><span style="color:#70a0d0;font-style:italic;">%s</span><span style="color:#4070a0;">&quot;</span> <span style="color:#666666;">%</span> k
        x<span style="color:#666666;">.</span>q<span style="color:#666666;">.</span>put(msg)
    <span style="color:#007020;font-weight:bold;">print</span> <span style="color:#4070a0;">&quot;finished sending messages.&quot;</span>

<span style="color:#007020;font-weight:bold;">def</span> <span style="color:#06287e;">wait_for_message</span>(q):
    <span style="color:#007020;font-weight:bold;">try</span>:
        <span style="color:#60a0b0;font-style:italic;">#Wait for a new message.</span>
        <span style="color:#007020;font-weight:bold;">return</span> q<span style="color:#666666;">.</span>get(<span style="color:#007020;">True</span>, uniform(<span style="color:#40a070;">55</span>, <span style="color:#40a070;">59</span>))
    <span style="color:#007020;font-weight:bold;">except</span> Empty:
        <span style="color:#007020;font-weight:bold;">return</span> <span style="color:#4070a0;">&quot;&quot;</span> <span style="color:#60a0b0;font-style:italic;"># no message within a minute, send keep-alive</span>

<span style="color:#007020;font-weight:bold;">def</span> <span style="color:#06287e;">handle</span>(environ, start_response):
    start_response(<span style="color:#4070a0;">&#39;200 OK&#39;</span>, [(<span style="color:#4070a0;">&#39;Content-type&#39;</span>, <span style="color:#4070a0;">&#39;text/plain&#39;</span>)])
    path <span style="color:#666666;">=</span> environ[<span style="color:#4070a0;">&#39;PATH_INFO&#39;</span>]

    <span style="color:#007020;font-weight:bold;">if</span> path<span style="color:#666666;">.</span>startswith(<span style="color:#4070a0;">&quot;/wait/&quot;</span>):
	<span style="color:#007020;">id</span> <span style="color:#666666;">=</span> unquote_plus(path[<span style="color:#007020;">len</span>(<span style="color:#4070a0;">&quot;/wait/&quot;</span>):])
	<span style="color:#007020;font-weight:bold;">if</span> <span style="color:#007020;">id</span> <span style="color:#007020;font-weight:bold;">not</span> <span style="color:#007020;font-weight:bold;">in</span> clients:
	    clients[<span style="color:#007020;">id</span>] <span style="color:#666666;">=</span> Server()
	clients[<span style="color:#007020;">id</span>]<span style="color:#666666;">.</span>last_get <span style="color:#666666;">=</span> time()
        <span style="color:#007020;font-weight:bold;">print</span> <span style="color:#4070a0;">&quot;</span><span style="color:#70a0d0;font-style:italic;">%s</span><span style="color:#4070a0;"> is waiting for a message...&quot;</span> <span style="color:#666666;">%</span> <span style="color:#007020;">id</span>
        <span style="color:#007020;font-weight:bold;">return</span> wait_for_message(clients[<span style="color:#007020;">id</span>]<span style="color:#666666;">.</span>q)

    <span style="color:#007020;font-weight:bold;">if</span> path<span style="color:#666666;">.</span>startswith(<span style="color:#4070a0;">&quot;/new&quot;</span>):
        <span style="color:#007020;font-weight:bold;">from</span> <span style="color:#0e84b5;font-weight:bold;">hashlib</span> <span style="color:#007020;font-weight:bold;">import</span> md5
        <span style="color:#007020;">id</span> <span style="color:#666666;">=</span> md5(<span style="color:#007020;">str</span>(time()))<span style="color:#666666;">.</span>hexdigest()
        clients[<span style="color:#007020;">id</span>] <span style="color:#666666;">=</span> Server()
        <span style="color:#007020;font-weight:bold;">return</span> <span style="color:#007020;">id</span>

    <span style="color:#007020;font-weight:bold;">if</span> path<span style="color:#666666;">.</span>startswith(<span style="color:#4070a0;">&quot;/post/&quot;</span>):
	msg <span style="color:#666666;">=</span> unquote_plus(path[<span style="color:#007020;">len</span>(<span style="color:#4070a0;">&quot;/post/&quot;</span>):])
        send_to_all(msg)

    <span style="color:#007020;font-weight:bold;">return</span> <span style="color:#4070a0;">&quot;&quot;</span>

<span style="color:#007020;font-weight:bold;">class</span> <span style="color:#0e84b5;font-weight:bold;">Server</span>(Thread):
    <span style="color:#4070a0;">&quot;A Queue and a http server thread for each client&quot;</span>
    <span style="color:#007020;font-weight:bold;">def</span> <span style="color:#06287e;">__init__</span>(<span style="color:#007020;">self</span>):
        Thread<span style="color:#666666;">.</span>__init__(<span style="color:#007020;">self</span>)
	<span style="color:#007020;">self</span><span style="color:#666666;">.</span>q <span style="color:#666666;">=</span> Queue()
        <span style="color:#007020;">self</span><span style="color:#666666;">.</span>setDaemon(<span style="color:#40a070;">1</span>)
	<span style="color:#007020;">self</span><span style="color:#666666;">.</span>active <span style="color:#666666;">=</span> <span style="color:#007020;">True</span>
	<span style="color:#007020;">self</span><span style="color:#666666;">.</span>last_get <span style="color:#666666;">=</span> time()
        <span style="color:#007020;">self</span><span style="color:#666666;">.</span>start()
    <span style="color:#007020;font-weight:bold;">def</span> <span style="color:#06287e;">run</span>(<span style="color:#007020;">self</span>):
        <span style="color:#007020;font-weight:bold;">while</span> <span style="color:#007020;">self</span><span style="color:#666666;">.</span>active:
            <span style="color:#007020;">self</span><span style="color:#666666;">.</span>httpd<span style="color:#666666;">.</span>handle_request()

<span style="color:#007020;font-weight:bold;">def</span> <span style="color:#06287e;">start</span>():
    httpd <span style="color:#666666;">=</span> make_server(<span style="color:#4070a0;">&#39;0.0.0.0&#39;</span>, <span style="color:#40a070;">8888</span>, handle)
    Server<span style="color:#666666;">.</span>httpd <span style="color:#666666;">=</span> httpd
    httpd<span style="color:#666666;">.</span>serve_forever()

start()
</pre>
</div>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/collincode.wordpress.com/58/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/collincode.wordpress.com/58/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/collincode.wordpress.com/58/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/collincode.wordpress.com/58/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/collincode.wordpress.com/58/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/collincode.wordpress.com/58/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/collincode.wordpress.com/58/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/collincode.wordpress.com/58/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/collincode.wordpress.com/58/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/collincode.wordpress.com/58/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=collincode.wordpress.com&blog=2705418&post=58&subd=collincode&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://collincode.wordpress.com/2008/12/11/message-queue/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/05081bd09da27830150776c53318e0ef?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Collin Anderson</media:title>
		</media:content>
	</item>
		<item>
		<title>This one changes my status message on Google Talk</title>
		<link>http://collincode.wordpress.com/2008/09/30/this-one-changes-my-status-message-on-google-talk/</link>
		<comments>http://collincode.wordpress.com/2008/09/30/this-one-changes-my-status-message-on-google-talk/#comments</comments>
		<pubDate>Wed, 01 Oct 2008 00:39:25 +0000</pubDate>
		<dc:creator>Collin Anderson</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[gchat]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[gtalk]]></category>
		<category><![CDATA[jabber]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[status]]></category>
		<category><![CDATA[talk]]></category>
		<category><![CDATA[xmpp]]></category>
		<category><![CDATA[xmpppy]]></category>

		<guid isPermaLink="false">http://collincode.wordpress.com/?p=56</guid>
		<description><![CDATA[
import xmpp

def set_status(username, password, status):
    jabber = xmpp.Client(&#39;gmail.com&#39;)
    jabber.connect(server=(&#39;talk.google.com&#39;,5223))
    jabber.auth(username, password, &#39;python_status_message&#39;)
    jabber.send(xmpp.Iq(typ=&#39;set&#39;, queryNS=&#39;google:shared-status&#39;,
        payload=[xmpp.Node(&#39;status&#39;, payload=status)]))


       <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=collincode.wordpress.com&blog=2705418&post=56&subd=collincode&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><div class="highlight">
<pre><span style="color:#007020;font-weight:bold;">import</span> <span style="color:#0e84b5;font-weight:bold;"><a href="http://xmpppy.sourceforge.net/">xmpp</a></span>

<span style="color:#007020;font-weight:bold;">def</span> <span style="color:#06287e;">set_status</span>(username, password, status):
    jabber <span style="color:#666666;">=</span> xmpp<span style="color:#666666;">.</span>Client(<span style="color:#4070a0;">&#39;gmail.com&#39;</span>)
    jabber<span style="color:#666666;">.</span>connect(server<span style="color:#666666;">=</span>(<span style="color:#4070a0;">&#39;talk.google.com&#39;</span>,<span style="color:#40a070;">5223</span>))
    jabber<span style="color:#666666;">.</span>auth(username, password, <span style="color:#4070a0;">&#39;python_status_message&#39;</span>)
    jabber<span style="color:#666666;">.</span>send(xmpp<span style="color:#666666;">.</span>Iq(typ<span style="color:#666666;">=</span><span style="color:#4070a0;">&#39;set&#39;</span>, queryNS<span style="color:#666666;">=</span><span style="color:#4070a0;">&#39;google:shared-status&#39;</span>,
        payload<span style="color:#666666;">=</span>[xmpp<span style="color:#666666;">.</span>Node(<span style="color:#4070a0;">&#39;status&#39;</span>, payload<span style="color:#666666;">=</span>status)]))
</pre>
</div>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/collincode.wordpress.com/56/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/collincode.wordpress.com/56/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/collincode.wordpress.com/56/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/collincode.wordpress.com/56/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/collincode.wordpress.com/56/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/collincode.wordpress.com/56/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/collincode.wordpress.com/56/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/collincode.wordpress.com/56/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/collincode.wordpress.com/56/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/collincode.wordpress.com/56/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=collincode.wordpress.com&blog=2705418&post=56&subd=collincode&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://collincode.wordpress.com/2008/09/30/this-one-changes-my-status-message-on-google-talk/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/05081bd09da27830150776c53318e0ef?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Collin Anderson</media:title>
		</media:content>
	</item>
		<item>
		<title>Timesheet program I wrote for Chris this summer.</title>
		<link>http://collincode.wordpress.com/2008/08/25/timesheet-program-i-wrote-for-chris-this-summer/</link>
		<comments>http://collincode.wordpress.com/2008/08/25/timesheet-program-i-wrote-for-chris-this-summer/#comments</comments>
		<pubDate>Tue, 26 Aug 2008 03:17:04 +0000</pubDate>
		<dc:creator>Collin Anderson</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[chris]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[timesheet]]></category>

		<guid isPermaLink="false">http://collincode.wordpress.com/?p=54</guid>
		<description><![CDATA[
from datetime import datetime
import os

def load_people():
    lines = [x.strip() for x in file(&#39;people.txt&#39;) if x.strip()]
    data = {}
    for line in lines:
        print line
        parts = line.split(&#39; &#39;)
     [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=collincode.wordpress.com&blog=2705418&post=54&subd=collincode&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><div class="highlight">
<pre><span style="color:#007020;font-weight:bold;">from</span> <span style="color:#0e84b5;font-weight:bold;">datetime</span> <span style="color:#007020;font-weight:bold;">import</span> datetime
<span style="color:#007020;font-weight:bold;">import</span> <span style="color:#0e84b5;font-weight:bold;">os</span>

<span style="color:#007020;font-weight:bold;">def</span> <span style="color:#06287e;">load_people</span>():
    lines <span style="color:#666666;">=</span> [x<span style="color:#666666;">.</span>strip() <span style="color:#007020;font-weight:bold;">for</span> x <span style="color:#007020;font-weight:bold;">in</span> <span style="color:#007020;">file</span>(<span style="color:#4070a0;">&#39;people.txt&#39;</span>) <span style="color:#007020;font-weight:bold;">if</span> x<span style="color:#666666;">.</span>strip()]
    data <span style="color:#666666;">=</span> {}
    <span style="color:#007020;font-weight:bold;">for</span> line <span style="color:#007020;font-weight:bold;">in</span> lines:
        <span style="color:#007020;font-weight:bold;">print</span> line
        parts <span style="color:#666666;">=</span> line<span style="color:#666666;">.</span>split(<span style="color:#4070a0;">&#39; &#39;</span>)
        data[parts[<span style="color:#40a070;">0</span>]] <span style="color:#666666;">=</span> <span style="color:#4070a0;">&#39; &#39;</span><span style="color:#666666;">.</span>join(parts[<span style="color:#40a070;">1</span>:])
    <span style="color:#007020;font-weight:bold;">return</span> data

people <span style="color:#666666;">=</span> load_people()

cur_year <span style="color:#666666;">=</span> datetime<span style="color:#666666;">.</span>today()<span style="color:#666666;">.</span>year

<span style="color:#007020;font-weight:bold;">def</span> <span style="color:#06287e;">get_name</span>():
    <span style="color:#007020;font-weight:bold;">while</span> <span style="color:#40a070;">1</span>:
        <span style="color:#007020;font-weight:bold;">try</span>:
            num <span style="color:#666666;">=</span> <span style="color:#007020;">raw_input</span>(<span style="color:#4070a0;">&quot;Type your number and press enter: &quot;</span>)
            <span style="color:#007020;font-weight:bold;">return</span> people[num]
        <span style="color:#007020;font-weight:bold;">except</span> <span style="color:#007020;">KeyboardInterrupt</span>:
            <span style="color:#007020;font-weight:bold;">raise</span>
        <span style="color:#007020;font-weight:bold;">except</span>:
            <span style="color:#007020;font-weight:bold;">print</span> <span style="color:#4070a0;">&quot;Invalid number&quot;</span>

<span style="color:#007020;font-weight:bold;">def</span> <span style="color:#06287e;">newday</span>():
    <span style="color:#007020;font-weight:bold;">return</span> datetime<span style="color:#666666;">.</span>now()<span style="color:#666666;">.</span>strftime(<span style="color:#4070a0;">&quot;%m/</span><span style="color:#70a0d0;font-style:italic;">%d</span><span style="color:#4070a0;"> %a&quot;</span>)

<span style="color:#007020;font-weight:bold;">def</span> <span style="color:#06287e;">dateofline</span>(line):
    <span style="color:#007020;font-weight:bold;">return</span> datetime(cur_year, <span style="color:#007020;">int</span>(line[<span style="color:#40a070;">0</span>:<span style="color:#40a070;">2</span>]), <span style="color:#007020;">int</span>(line[<span style="color:#40a070;">3</span>:<span style="color:#40a070;">5</span>]))<span style="color:#666666;">.</span>date()<span style="color:#666666;">.</span>day

<span style="color:#007020;font-weight:bold;">def</span> <span style="color:#06287e;">input_time</span>():
    <span style="color:#007020;font-weight:bold;">while</span> <span style="color:#40a070;">1</span>:
        <span style="color:#007020;font-weight:bold;">try</span>:
            time <span style="color:#666666;">=</span> <span style="color:#007020;">raw_input</span>(<span style="color:#4070a0;">&quot;Enter the time that you finished working (ie 4:32): &quot;</span>)
            hour, minute <span style="color:#666666;">=</span> (<span style="color:#007020;">int</span>(x) <span style="color:#007020;font-weight:bold;">for</span> x <span style="color:#007020;font-weight:bold;">in</span> x<span style="color:#666666;">.</span>split(<span style="color:#4070a0;">&#39;:&#39;</span>))
            <span style="color:#007020;font-weight:bold;">if</span> hour <span style="color:#666666;">&lt;</span> <span style="color:#40a070;">7</span>:
                hour <span style="color:#666666;">+=</span> <span style="color:#40a070;">12</span>
            time <span style="color:#666666;">=</span> datetime<span style="color:#666666;">.</span>now()
            time<span style="color:#666666;">.</span>hour, time<span style="color:#666666;">.</span>minute, <span style="color:#666666;">=</span> hour, minute
            <span style="color:#007020;font-weight:bold;">return</span> time
        <span style="color:#007020;font-weight:bold;">except</span>:
            <span style="color:#007020;font-weight:bold;">print</span> <span style="color:#4070a0;">&quot;Invalid time.&quot;</span>

<span style="color:#007020;font-weight:bold;">def</span> <span style="color:#06287e;">time</span>():
    <span style="color:#007020;font-weight:bold;">return</span> datetime<span style="color:#666666;">.</span>now()<span style="color:#666666;">.</span>strftime(<span style="color:#4070a0;">&quot;%H:%M&quot;</span>)

<span style="color:#007020;font-weight:bold;">def</span> <span style="color:#06287e;">punchin</span>():
    <span style="color:#007020;font-weight:bold;">print</span> <span style="color:#4070a0;">&quot;Punched In&quot;</span>
    <span style="color:#007020;font-weight:bold;">return</span> <span style="color:#4070a0;">&#39;</span><span style="color:#4070a0;font-weight:bold;">t</span><span style="color:#4070a0;">I &#39;</span> <span style="color:#666666;">+</span> time()

<span style="color:#007020;font-weight:bold;">def</span> <span style="color:#06287e;">punchout</span>():
    <span style="color:#007020;font-weight:bold;">print</span> <span style="color:#4070a0;">&quot;Punched Out&quot;</span>
    <span style="color:#007020;font-weight:bold;">return</span> <span style="color:#4070a0;">&#39;</span><span style="color:#4070a0;font-weight:bold;">t</span><span style="color:#4070a0;">O &#39;</span> <span style="color:#666666;">+</span> time()

<span style="color:#007020;font-weight:bold;">def</span> <span style="color:#06287e;">parsetime</span>(string):
    <span style="color:#007020;font-weight:bold;">return</span> <span style="color:#007020;">int</span>(string[<span style="color:#40a070;">2</span>:<span style="color:#40a070;">4</span>]), <span style="color:#007020;">int</span>(string[<span style="color:#40a070;">5</span>:<span style="color:#40a070;">7</span>])

<span style="color:#007020;font-weight:bold;">def</span> <span style="color:#06287e;">record_time</span>(name):
    filename <span style="color:#666666;">=</span> <span style="color:#4070a0;">&quot;</span><span style="color:#70a0d0;font-style:italic;">%s</span><span style="color:#4070a0;">.txt&quot;</span> <span style="color:#666666;">%</span> name
    <span style="color:#007020;font-weight:bold;">if</span> <span style="color:#007020;font-weight:bold;">not</span> os<span style="color:#666666;">.</span>path<span style="color:#666666;">.</span>exists(filename):
        f <span style="color:#666666;">=</span> <span style="color:#007020;">file</span>(filename, <span style="color:#4070a0;">&#39;w&#39;</span>)
        f<span style="color:#666666;">.</span>write(<span style="color:#4070a0;">&#39;</span><span style="color:#70a0d0;font-style:italic;">%s</span><span style="color:#4070a0;font-weight:bold;">n</span><span style="color:#4070a0;">&#39;</span> <span style="color:#666666;">%</span> name)
        f<span style="color:#666666;">.</span>write(newday())
        <span style="color:#007020;font-weight:bold;">return</span> f<span style="color:#666666;">.</span>write(punchin())
    f <span style="color:#666666;">=</span> <span style="color:#007020;">file</span>(filename, <span style="color:#4070a0;">&#39;a&#39;</span>)
    line <span style="color:#666666;">=</span> [x<span style="color:#666666;">.</span>strip() <span style="color:#007020;font-weight:bold;">for</span> x <span style="color:#007020;font-weight:bold;">in</span> <span style="color:#007020;">file</span>(filename) <span style="color:#007020;font-weight:bold;">if</span> x<span style="color:#666666;">.</span>strip()][<span style="color:#666666;">-</span><span style="color:#40a070;">1</span>]
    <span style="color:#007020;font-weight:bold;">if</span> datetime<span style="color:#666666;">.</span>today()<span style="color:#666666;">.</span>day <span style="color:#666666;">!=</span> dateofline(line):
        times <span style="color:#666666;">=</span> line<span style="color:#666666;">.</span>split(<span style="color:#4070a0;">&#39;</span><span style="color:#4070a0;font-weight:bold;">t</span><span style="color:#4070a0;">&#39;</span>)[<span style="color:#40a070;">1</span>:]
        <span style="color:#007020;font-weight:bold;">if</span> <span style="color:#007020;">len</span>(times) <span style="color:#666666;">%</span> <span style="color:#40a070;">2</span>:
            <span style="color:#007020;font-weight:bold;">print</span> <span style="color:#4070a0;">&quot;You did not punch out last time.&quot;</span>
            time <span style="color:#666666;">=</span> input_time()
            new_time <span style="color:#666666;">=</span> <span style="color:#4070a0;">&#39;O &#39;</span> <span style="color:#666666;">+</span> time<span style="color:#666666;">.</span>strftime(<span style="color:#4070a0;">&quot;%H:%M&quot;</span>)
            f<span style="color:#666666;">.</span>write(<span style="color:#4070a0;">&#39;</span><span style="color:#4070a0;font-weight:bold;">t</span><span style="color:#4070a0;">&#39;</span> <span style="color:#666666;">+</span> new_time)
            times<span style="color:#666666;">.</span>append(new_time)
        times <span style="color:#666666;">=</span> [parsetime(x) <span style="color:#007020;font-weight:bold;">for</span> x <span style="color:#007020;font-weight:bold;">in</span> times]
        punched_in <span style="color:#666666;">=</span> <span style="color:#666666;">-</span><span style="color:#40a070;">1</span>
        total_hours <span style="color:#666666;">=</span> <span style="color:#40a070;">0</span>
        total_minutes <span style="color:#666666;">=</span> <span style="color:#40a070;">0</span>
        <span style="color:#007020;font-weight:bold;">for</span> x <span style="color:#007020;font-weight:bold;">in</span> times:
            total_hours <span style="color:#666666;">+=</span> punched_in <span style="color:#666666;">*</span> x[<span style="color:#40a070;">0</span>]
            total_minutes <span style="color:#666666;">+=</span> punched_in <span style="color:#666666;">*</span> x[<span style="color:#40a070;">1</span>]
            punched_in <span style="color:#666666;">*=</span> <span style="color:#666666;">-</span><span style="color:#40a070;">1</span>
        total_time <span style="color:#666666;">=</span> total_hours <span style="color:#666666;">+</span> total_minutes<span style="color:#666666;">/</span><span style="color:#40a070;">60.</span>
        f<span style="color:#666666;">.</span>write(<span style="color:#4070a0;">&#39;</span><span style="color:#4070a0;font-weight:bold;">t</span><span style="color:#4070a0;">Total: </span><span style="color:#70a0d0;font-style:italic;">%0.2f</span><span style="color:#4070a0;">&#39;</span> <span style="color:#666666;">%</span> total_time)
        f<span style="color:#666666;">.</span>write(<span style="color:#4070a0;">&#39;</span><span style="color:#4070a0;font-weight:bold;">n</span><span style="color:#4070a0;">&#39;</span>)
        f<span style="color:#666666;">.</span>write(newday())
        <span style="color:#007020;font-weight:bold;">return</span> f<span style="color:#666666;">.</span>write(punchin())
        <span style="color:#007020;font-weight:bold;">raise</span> <span style="color:#007020;">NotImplemented</span>
    times <span style="color:#666666;">=</span> line<span style="color:#666666;">.</span>split(<span style="color:#4070a0;">&#39;</span><span style="color:#4070a0;font-weight:bold;">t</span><span style="color:#4070a0;">&#39;</span>)[<span style="color:#40a070;">1</span>:]
    <span style="color:#007020;font-weight:bold;">if</span> times[<span style="color:#666666;">-</span><span style="color:#40a070;">1</span>][<span style="color:#40a070;">0</span>] <span style="color:#666666;">==</span> <span style="color:#4070a0;">&#39;I&#39;</span>:
        <span style="color:#007020;font-weight:bold;">return</span> f<span style="color:#666666;">.</span>write(punchout())
    <span style="color:#007020;font-weight:bold;">if</span> times[<span style="color:#666666;">-</span><span style="color:#40a070;">1</span>][<span style="color:#40a070;">0</span>] <span style="color:#666666;">==</span> <span style="color:#4070a0;">&#39;O&#39;</span>:
        <span style="color:#007020;font-weight:bold;">return</span> f<span style="color:#666666;">.</span>write(punchin())
    <span style="color:#007020;font-weight:bold;">raise</span> <span style="color:#007020;">Exception</span>(<span style="color:#4070a0;">&quot;Invalid data: </span><span style="color:#70a0d0;font-style:italic;">%s</span><span style="color:#4070a0;">&quot;</span> <span style="color:#666666;">%</span> times)    

<span style="color:#007020;font-weight:bold;">while</span> <span style="color:#40a070;">1</span>:
    name <span style="color:#666666;">=</span> get_name()
    <span style="color:#007020;font-weight:bold;">print</span> name
    record_time(name)
</pre>
</div>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/collincode.wordpress.com/54/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/collincode.wordpress.com/54/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/collincode.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/collincode.wordpress.com/54/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/collincode.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/collincode.wordpress.com/54/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/collincode.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/collincode.wordpress.com/54/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/collincode.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/collincode.wordpress.com/54/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/collincode.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/collincode.wordpress.com/54/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=collincode.wordpress.com&blog=2705418&post=54&subd=collincode&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://collincode.wordpress.com/2008/08/25/timesheet-program-i-wrote-for-chris-this-summer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/05081bd09da27830150776c53318e0ef?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Collin Anderson</media:title>
		</media:content>
	</item>
		<item>
		<title>Counties in the Mississippi River Valley</title>
		<link>http://collincode.wordpress.com/2008/07/29/counties-in-the-mississippi-river-valley/</link>
		<comments>http://collincode.wordpress.com/2008/07/29/counties-in-the-mississippi-river-valley/#comments</comments>
		<pubDate>Wed, 30 Jul 2008 02:18:44 +0000</pubDate>
		<dc:creator>Collin Anderson</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[counties]]></category>
		<category><![CDATA[county]]></category>
		<category><![CDATA[mississippi]]></category>
		<category><![CDATA[mrv]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[river]]></category>
		<category><![CDATA[valley]]></category>

		<guid isPermaLink="false">http://collincode.wordpress.com/?p=51</guid>
		<description><![CDATA[Last Friday I took the code from an earlier go at this and made it filter census data by county to only have data in the target area. You can also view my results of the program.

#!/usr/bin/env python
import sys
import urllib

def dms(d,m,s):
    return d+m/60.+s/3600.

def parsedms(f):
    &#34;&#34;&#34;
    Imports [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=collincode.wordpress.com&blog=2705418&post=51&subd=collincode&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Last Friday I took the code <a href="http://collincode.wordpress.com/2007/05/16/mississippi-river-valley/">from an earlier go at this</a> and made it filter <a href="http://www.census.gov/statab/ccdb/ccdbstcounty.html">census data by county</a> to only have data in the target area. You can also <a href="http://sweecoo.com/wp-content/uploads/2008/07/census_data.zip">view my results of the program</a>.</p>
<div class="highlight">
<pre><span style="color:#60a0b0;font-style:italic;">#!/usr/bin/env python</span>
<span style="color:#007020;font-weight:bold;">import</span> <span style="color:#0e84b5;font-weight:bold;">sys</span>
<span style="color:#007020;font-weight:bold;">import</span> <span style="color:#0e84b5;font-weight:bold;">urllib</span>

<span style="color:#007020;font-weight:bold;">def</span> <span style="color:#06287e;">dms</span>(d,m,s):
    <span style="color:#007020;font-weight:bold;">return</span> d<span style="color:#666666;">+</span>m<span style="color:#666666;">/</span><span style="color:#40a070;">60.</span><span style="color:#666666;">+</span>s<span style="color:#666666;">/</span><span style="color:#40a070;">3600.</span>

<span style="color:#007020;font-weight:bold;">def</span> <span style="color:#06287e;">parsedms</span>(f):
    <span style="color:#4070a0;font-style:italic;">&quot;&quot;&quot;</span>
<span style="color:#4070a0;font-style:italic;">    Imports a coordinates from tsv file and returns it as a list</span>
<span style="color:#4070a0;font-style:italic;">    &quot;&quot;&quot;</span>
    a<span style="color:#666666;">=</span>[]
    <span style="color:#007020;font-weight:bold;">for</span> line <span style="color:#007020;font-weight:bold;">in</span> f:
        t<span style="color:#666666;">=</span>line<span style="color:#666666;">.</span>rsplit(<span style="color:#4070a0;">&quot;</span><span style="color:#4070a0;font-weight:bold;">t</span><span style="color:#4070a0;">&quot;</span>)
        lon<span style="color:#666666;">=</span>dms(<span style="color:#007020;">int</span>(t[<span style="color:#40a070;">0</span>]),<span style="color:#007020;">int</span>(t[<span style="color:#40a070;">1</span>]),<span style="color:#007020;">int</span>(t[<span style="color:#40a070;">2</span>]))
        lat<span style="color:#666666;">=</span>dms(<span style="color:#007020;">int</span>(t[<span style="color:#40a070;">3</span>]),<span style="color:#007020;">int</span>(t[<span style="color:#40a070;">4</span>]),<span style="color:#007020;">int</span>(t[<span style="color:#40a070;">5</span>]))
        a<span style="color:#666666;">.</span>append((<span style="color:#666666;">-</span>lon,lat))
    <span style="color:#007020;font-weight:bold;">return</span> a

<span style="color:#007020;font-weight:bold;">print</span> <span style="color:#666666;">&gt;&gt;</span> sys<span style="color:#666666;">.</span>stderr, <span style="color:#4070a0;">&quot;loading river data&quot;</span>
river_file <span style="color:#666666;">=</span> urllib<span style="color:#666666;">.</span>urlopen(<span style="color:#4070a0;">&quot;http://collincode.files.wordpress.com/2008/02/mississippi-river.txt&quot;</span>)
<span style="color:#007020;font-weight:bold;">print</span> <span style="color:#666666;">&gt;&gt;</span> sys<span style="color:#666666;">.</span>stderr, <span style="color:#4070a0;">&quot;parsing river data&quot;</span>
river <span style="color:#666666;">=</span> parsedms(river_file)

<span style="color:#007020;font-weight:bold;">def</span> <span style="color:#06287e;">distance</span>(Lon1,Lat1,Lon2,Lat2):
    <span style="color:#4070a0;font-style:italic;">&quot;&quot;&quot;</span>
<span style="color:#4070a0;font-style:italic;">    Calculates the distance between two coordinates</span>
<span style="color:#4070a0;font-style:italic;">    Based on calcDistance in http://nmviewogc.cr.usgs.gov/javascript/aimsMap.js</span>
<span style="color:#4070a0;font-style:italic;">    &quot;&quot;&quot;</span>
    <span style="color:#007020;font-weight:bold;">import</span> <span style="color:#0e84b5;font-weight:bold;">math</span> <span style="color:#007020;font-weight:bold;">as</span> <span style="color:#0e84b5;font-weight:bold;">Math</span>
    Lon1 <span style="color:#666666;">=</span> Lon1 <span style="color:#666666;">*</span> Math<span style="color:#666666;">.</span>pi <span style="color:#666666;">/</span> <span style="color:#40a070;">180</span>;
    Lon2 <span style="color:#666666;">=</span> Lon2 <span style="color:#666666;">*</span> Math<span style="color:#666666;">.</span>pi <span style="color:#666666;">/</span> <span style="color:#40a070;">180</span>;
    Lat1 <span style="color:#666666;">=</span> Lat1 <span style="color:#666666;">*</span> Math<span style="color:#666666;">.</span>pi <span style="color:#666666;">/</span> <span style="color:#40a070;">180</span>;
    Lat2 <span style="color:#666666;">=</span> Lat2 <span style="color:#666666;">*</span> Math<span style="color:#666666;">.</span>pi <span style="color:#666666;">/</span> <span style="color:#40a070;">180</span>;
    LonDist <span style="color:#666666;">=</span> Lon1<span style="color:#666666;">-</span>Lon2;
    LatDist <span style="color:#666666;">=</span> Lat1<span style="color:#666666;">-</span>Lat2;
    A <span style="color:#666666;">=</span> Math<span style="color:#666666;">.</span>pow(Math<span style="color:#666666;">.</span>sin(LatDist <span style="color:#666666;">/</span> <span style="color:#40a070;">2</span>),<span style="color:#40a070;">2</span>) <span style="color:#666666;">+</span> Math<span style="color:#666666;">.</span>cos(Lat1) <span style="color:#666666;">*</span> Math<span style="color:#666666;">.</span>cos(Lat2) <span style="color:#666666;">*</span> Math<span style="color:#666666;">.</span>pow(Math<span style="color:#666666;">.</span>sin(LonDist <span style="color:#666666;">/</span><span style="color:#40a070;">2</span>),<span style="color:#40a070;">2</span>);
    C <span style="color:#666666;">=</span> <span style="color:#40a070;">2</span> <span style="color:#666666;">*</span> Math<span style="color:#666666;">.</span>asin(Math<span style="color:#666666;">.</span>sqrt(A));
    D <span style="color:#666666;">=</span> (<span style="color:#40a070;">3963.1676</span> <span style="color:#666666;">-</span> <span style="color:#40a070;">13</span> <span style="color:#666666;">*</span> Math<span style="color:#666666;">.</span>sin((Lat1 <span style="color:#666666;">+</span> Lat2) <span style="color:#666666;">/</span> <span style="color:#40a070;">2</span>)) <span style="color:#666666;">*</span> C;
    <span style="color:#007020;font-weight:bold;">return</span> D

<span style="color:#007020;font-weight:bold;">def</span> <span style="color:#06287e;">dist2river</span>(lon,lat,river):
    <span style="color:#4070a0;">&#39;returns the distance and closest point on the river to the point&#39;</span>
    smallest <span style="color:#666666;">=</span> distance(river[<span style="color:#40a070;">0</span>][<span style="color:#40a070;">0</span>],river[<span style="color:#40a070;">0</span>][<span style="color:#40a070;">1</span>],lon,lat) <span style="color:#60a0b0;font-style:italic;">#check against initial point</span>
    <span style="color:#007020;font-weight:bold;">for</span> point <span style="color:#007020;font-weight:bold;">in</span> river:
        d <span style="color:#666666;">=</span> distance(point[<span style="color:#40a070;">0</span>],point[<span style="color:#40a070;">1</span>],lon,lat)
        <span style="color:#007020;font-weight:bold;">if</span> d <span style="color:#666666;">&lt;=</span> smallest: <span style="color:#60a0b0;font-style:italic;">#see if this point is closer</span>
            thepoint<span style="color:#666666;">=</span>point
            smallest<span style="color:#666666;">=</span>d
    <span style="color:#007020;font-weight:bold;">return</span> smallest, thepoint

COLUMNS <span style="color:#666666;">=</span> (
    (<span style="color:#40a070;">1</span>, <span style="color:#40a070;">2</span>, <span style="color:#4070a0;">&#39;state&#39;</span>, <span style="color:#007020;">str</span>),
    (<span style="color:#40a070;">8</span>, <span style="color:#40a070;">71</span>, <span style="color:#4070a0;">&#39;name&#39;</span>, <span style="color:#007020;font-weight:bold;">lambda</span> x: <span style="color:#007020;">str</span>(x)<span style="color:#666666;">.</span>strip()),
    (<span style="color:#40a070;">72</span>, <span style="color:#40a070;">80</span>, <span style="color:#4070a0;">&#39;pop&#39;</span>, <span style="color:#007020;">int</span>),
    (<span style="color:#40a070;">81</span>, <span style="color:#40a070;">89</span>, <span style="color:#4070a0;">&#39;houses&#39;</span>, <span style="color:#007020;">long</span>),
    (<span style="color:#40a070;">118</span>, <span style="color:#40a070;">129</span>, <span style="color:#4070a0;">&#39;land&#39;</span>, <span style="color:#007020;">float</span>),
    (<span style="color:#40a070;">130</span>, <span style="color:#40a070;">141</span>, <span style="color:#4070a0;">&#39;water&#39;</span>, <span style="color:#007020;">float</span>),
    (<span style="color:#40a070;">142</span>, <span style="color:#40a070;">151</span>, <span style="color:#4070a0;">&#39;lat&#39;</span>, <span style="color:#007020;">float</span>),
    (<span style="color:#40a070;">152</span>, <span style="color:#40a070;">162</span>, <span style="color:#4070a0;">&#39;lon&#39;</span>, <span style="color:#007020;">float</span>),
)

<span style="color:#007020;font-weight:bold;">def</span> <span style="color:#06287e;">parse_line</span>(line):
    <span style="color:#007020;font-weight:bold;">return</span> <span style="color:#007020;">dict</span>([(name, klass(line[s<span style="color:#666666;">-</span><span style="color:#40a070;">1</span>:e])) <span style="color:#007020;font-weight:bold;">for</span> s, e, name, klass <span style="color:#007020;font-weight:bold;">in</span> COLUMNS])

<span style="color:#007020;font-weight:bold;">def</span> <span style="color:#06287e;">parse_data</span>(county_file):
    <span style="color:#007020;font-weight:bold;">for</span> line <span style="color:#007020;font-weight:bold;">in</span> county_file:
        <span style="color:#007020;font-weight:bold;">if</span> line:
            <span style="color:#007020;font-weight:bold;">yield</span> parse_line(line)

<span style="color:#007020;font-weight:bold;">print</span> <span style="color:#666666;">&gt;&gt;</span> sys<span style="color:#666666;">.</span>stderr, <span style="color:#4070a0;">&quot;loading county data&quot;</span>
county_file <span style="color:#666666;">=</span> urllib<span style="color:#666666;">.</span>urlopen(<span style="color:#4070a0;">&quot;http://www.census.gov/tiger/tms/gazetteer/county2k.txt&quot;</span>)
<span style="color:#007020;font-weight:bold;">print</span> <span style="color:#666666;">&gt;&gt;</span> sys<span style="color:#666666;">.</span>stderr, <span style="color:#4070a0;">&quot;parsing county data&quot;</span>
data <span style="color:#666666;">=</span> []
<span style="color:#007020;font-weight:bold;">for</span> county <span style="color:#007020;font-weight:bold;">in</span> parse_data(county_file):
    <span style="color:#007020;font-weight:bold;">if</span> county[<span style="color:#4070a0;">&#39;name&#39;</span>]<span style="color:#666666;">.</span>endswith(<span style="color:#4070a0;">&#39; County&#39;</span>) <span style="color:#007020;font-weight:bold;">or</span>  county[<span style="color:#4070a0;">&#39;name&#39;</span>]<span style="color:#666666;">.</span>endswith(<span style="color:#4070a0;">&#39; Parish&#39;</span>):
        county[<span style="color:#4070a0;">&#39;name&#39;</span>] <span style="color:#666666;">=</span> county[<span style="color:#4070a0;">&#39;name&#39;</span>][:<span style="color:#666666;">-</span><span style="color:#007020;">len</span>(<span style="color:#4070a0;">&#39; County&#39;</span>)]
        data<span style="color:#666666;">.</span>append(county)

<span style="color:#007020;font-weight:bold;">print</span> <span style="color:#666666;">&gt;&gt;</span> sys<span style="color:#666666;">.</span>stderr, <span style="color:#4070a0;">&quot;calculating distances to river&quot;</span>
distances <span style="color:#666666;">=</span> {}
<span style="color:#007020;font-weight:bold;">for</span> county <span style="color:#007020;font-weight:bold;">in</span> data:
    key <span style="color:#666666;">=</span> <span style="color:#4070a0;">&quot;</span><span style="color:#70a0d0;font-style:italic;">%s</span><span style="color:#4070a0;">, </span><span style="color:#70a0d0;font-style:italic;">%s</span><span style="color:#4070a0;">&quot;</span> <span style="color:#666666;">%</span> (county[<span style="color:#4070a0;">&#39;name&#39;</span>], county[<span style="color:#4070a0;">&#39;state&#39;</span>])
    key <span style="color:#666666;">=</span> key<span style="color:#666666;">.</span>lower()
    dist <span style="color:#666666;">=</span> dist2river(county[<span style="color:#4070a0;">&#39;lon&#39;</span>], county[<span style="color:#4070a0;">&#39;lat&#39;</span>], river)[<span style="color:#40a070;">0</span>]
    <span style="color:#007020;font-weight:bold;">if</span> dist <span style="color:#666666;">&lt;</span> <span style="color:#40a070;">305</span>:
        distances[key] <span style="color:#666666;">=</span> dist

<span style="color:#007020;font-weight:bold;">print</span> <span style="color:#666666;">&gt;&gt;</span> sys<span style="color:#666666;">.</span>stderr, <span style="color:#4070a0;">&quot;processing your data&quot;</span>
<span style="color:#007020;font-weight:bold;">def</span> <span style="color:#06287e;">runfile</span>(i, o):
    <span style="color:#007020;font-weight:bold;">for</span> line <span style="color:#007020;font-weight:bold;">in</span> i:
        line <span style="color:#666666;">=</span> line<span style="color:#666666;">.</span>strip(<span style="color:#4070a0;">&quot;</span><span style="color:#4070a0;font-weight:bold;">n</span><span style="color:#4070a0;">&quot;</span>)
        columns <span style="color:#666666;">=</span> line<span style="color:#666666;">.</span>split(<span style="color:#4070a0;">&#39;</span><span style="color:#4070a0;font-weight:bold;">t</span><span style="color:#4070a0;">&#39;</span>)
        county <span style="color:#666666;">=</span> columns[<span style="color:#40a070;">0</span>]
        key <span style="color:#666666;">=</span> county<span style="color:#666666;">.</span>strip()<span style="color:#666666;">.</span>lower()
        <span style="color:#007020;font-weight:bold;">if</span> key <span style="color:#007020;font-weight:bold;">in</span> distances:
            <span style="color:#007020;font-weight:bold;">print</span> <span style="color:#666666;">&gt;&gt;</span> o, <span style="color:#4070a0;">&quot;</span><span style="color:#70a0d0;font-style:italic;">%.3f</span><span style="color:#4070a0;font-weight:bold;">t</span><span style="color:#70a0d0;font-style:italic;">%s</span><span style="color:#4070a0;">&quot;</span> <span style="color:#666666;">%</span> (distances[key], line)

<span style="color:#007020;font-weight:bold;">import</span> <span style="color:#0e84b5;font-weight:bold;">os</span>
<span style="color:#007020;font-weight:bold;">for</span> x <span style="color:#007020;font-weight:bold;">in</span> os<span style="color:#666666;">.</span>listdir(<span style="color:#4070a0;">&#39;.&#39;</span>):
    <span style="color:#007020;font-weight:bold;">if</span> x<span style="color:#666666;">.</span>startswith(<span style="color:#4070a0;">&#39;b&#39;</span>):
        <span style="color:#007020;font-weight:bold;">print</span> <span style="color:#4070a0;">&quot;running </span><span style="color:#70a0d0;font-style:italic;">%s</span><span style="color:#4070a0;">&quot;</span> <span style="color:#666666;">%</span> x
        runfile(<span style="color:#007020;">file</span>(x), <span style="color:#007020;">file</span>(<span style="color:#4070a0;">&#39;o&#39;</span> <span style="color:#666666;">+</span> x, <span style="color:#4070a0;">&#39;w&#39;</span>))
</pre>
</div>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/collincode.wordpress.com/51/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/collincode.wordpress.com/51/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/collincode.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/collincode.wordpress.com/51/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/collincode.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/collincode.wordpress.com/51/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/collincode.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/collincode.wordpress.com/51/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/collincode.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/collincode.wordpress.com/51/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/collincode.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/collincode.wordpress.com/51/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=collincode.wordpress.com&blog=2705418&post=51&subd=collincode&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://collincode.wordpress.com/2008/07/29/counties-in-the-mississippi-river-valley/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/05081bd09da27830150776c53318e0ef?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Collin Anderson</media:title>
		</media:content>
	</item>
		<item>
		<title>This one tells me the mac address (bssid) of the access point I am connected to with wireless zero configuration using wmi.</title>
		<link>http://collincode.wordpress.com/2008/06/08/this-one-tells-me-the-mac-address-bssid-of-the-access-point-i-am-connected-to-with-wireless-zero-configuration-using-wmi/</link>
		<comments>http://collincode.wordpress.com/2008/06/08/this-one-tells-me-the-mac-address-bssid-of-the-access-point-i-am-connected-to-with-wireless-zero-configuration-using-wmi/#comments</comments>
		<pubDate>Mon, 09 Jun 2008 00:06:52 +0000</pubDate>
		<dc:creator>Collin Anderson</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[access]]></category>
		<category><![CDATA[address]]></category>
		<category><![CDATA[bssid]]></category>
		<category><![CDATA[mac]]></category>
		<category><![CDATA[point]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[win32]]></category>
		<category><![CDATA[windows]]></category>
		<category><![CDATA[wireless]]></category>

		<guid isPermaLink="false">http://collincode.wordpress.com/?p=50</guid>
		<description><![CDATA[
import win32com.client

def mhex(mac):
    return &#34;&#34;.join(&#39;%02x&#39; % x for x in mac)

wmi2 = win32com.client.GetObject(&#34;winmgmts:!root/wmi&#34;)
adapters = wmi2.execquery(&#34;select * from MSNdis_80211_BaseServiceSetIdentifier&#34;)
for ad in adapters:
    print ad.InstanceName
    print ad.Active and &#39;Active&#39; or &#39;Inactive&#39;
    print mhex(ad.Ndis80211MacAddress)


       <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=collincode.wordpress.com&blog=2705418&post=50&subd=collincode&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><div class="highlight">
<pre><span style="color:#007020;font-weight:bold;">import</span> <span style="color:#0e84b5;font-weight:bold;">win32com.client</span>

<span style="color:#007020;font-weight:bold;">def</span> <span style="color:#06287e;">mhex</span>(mac):
    <span style="color:#007020;font-weight:bold;">return</span> <span style="color:#4070a0;">&quot;&quot;</span><span style="color:#666666;">.</span>join(<span style="color:#4070a0;">&#39;</span><span style="color:#70a0d0;font-style:italic;">%02x</span><span style="color:#4070a0;">&#39;</span> <span style="color:#666666;">%</span> x <span style="color:#007020;font-weight:bold;">for</span> x <span style="color:#007020;font-weight:bold;">in</span> mac)

wmi2 <span style="color:#666666;">=</span> win32com<span style="color:#666666;">.</span>client<span style="color:#666666;">.</span>GetObject(<span style="color:#4070a0;">&quot;winmgmts:!root/wmi&quot;</span>)
adapters <span style="color:#666666;">=</span> wmi2<span style="color:#666666;">.</span>execquery(<span style="color:#4070a0;">&quot;select * from MSNdis_80211_BaseServiceSetIdentifier&quot;</span>)
<span style="color:#007020;font-weight:bold;">for</span> ad <span style="color:#007020;font-weight:bold;">in</span> adapters:
    <span style="color:#007020;font-weight:bold;">print</span> ad<span style="color:#666666;">.</span>InstanceName
    <span style="color:#007020;font-weight:bold;">print</span> ad<span style="color:#666666;">.</span>Active <span style="color:#007020;font-weight:bold;">and</span> <span style="color:#4070a0;">&#39;Active&#39;</span> <span style="color:#007020;font-weight:bold;">or</span> <span style="color:#4070a0;">&#39;Inactive&#39;</span>
    <span style="color:#007020;font-weight:bold;">print</span> mhex(ad<span style="color:#666666;">.</span>Ndis80211MacAddress)
</pre>
</div>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/collincode.wordpress.com/50/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/collincode.wordpress.com/50/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/collincode.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/collincode.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/collincode.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/collincode.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/collincode.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/collincode.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/collincode.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/collincode.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/collincode.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/collincode.wordpress.com/50/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=collincode.wordpress.com&blog=2705418&post=50&subd=collincode&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://collincode.wordpress.com/2008/06/08/this-one-tells-me-the-mac-address-bssid-of-the-access-point-i-am-connected-to-with-wireless-zero-configuration-using-wmi/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/05081bd09da27830150776c53318e0ef?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Collin Anderson</media:title>
		</media:content>
	</item>
		<item>
		<title>So apparently unicode(string, &#8216;cp1252&#8242;) is what I want.</title>
		<link>http://collincode.wordpress.com/2008/05/30/so-apparently-unicodestring-cp1252-is-what-i-want/</link>
		<comments>http://collincode.wordpress.com/2008/05/30/so-apparently-unicodestring-cp1252-is-what-i-want/#comments</comments>
		<pubDate>Sat, 31 May 2008 04:09:37 +0000</pubDate>
		<dc:creator>Collin Anderson</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[encode]]></category>
		<category><![CDATA[encoding]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[str]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[strings]]></category>
		<category><![CDATA[unicode]]></category>

		<guid isPermaLink="false">http://collincode.wordpress.com/?p=49</guid>
		<description><![CDATA[ So apparently unicode(string, &#8216;cp1252&#8242;) is what I want.
       <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=collincode.wordpress.com&blog=2705418&post=49&subd=collincode&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p> So apparently unicode(string, &#8216;cp1252&#8242;) is what I want.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/collincode.wordpress.com/49/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/collincode.wordpress.com/49/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/collincode.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/collincode.wordpress.com/49/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/collincode.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/collincode.wordpress.com/49/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/collincode.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/collincode.wordpress.com/49/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/collincode.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/collincode.wordpress.com/49/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/collincode.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/collincode.wordpress.com/49/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=collincode.wordpress.com&blog=2705418&post=49&subd=collincode&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://collincode.wordpress.com/2008/05/30/so-apparently-unicodestring-cp1252-is-what-i-want/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/05081bd09da27830150776c53318e0ef?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Collin Anderson</media:title>
		</media:content>
	</item>
	</channel>
</rss>