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

<channel>
	<title>Black Paddle Software</title>
	<atom:link href="http://blackpaddle.com/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://blackpaddle.com</link>
	<description>paddling the digital river</description>
	<lastBuildDate>Wed, 20 Oct 2010 01:33:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>ShipItSquirrel on Twitter</title>
		<link>http://blackpaddle.com/?p=33</link>
		<comments>http://blackpaddle.com/?p=33#comments</comments>
		<pubDate>Fri, 15 Oct 2010 03:08:35 +0000</pubDate>
		<dc:creator>Kevin Turner</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[twitter]]></category>
		<category><![CDATA[github]]></category>
		<category><![CDATA[ship it]]></category>
		<category><![CDATA[squirrel]]></category>
		<category><![CDATA[tweepy]]></category>

		<guid isPermaLink="false">http://blackpaddle.com/?p=33</guid>
		<description><![CDATA[We&#8217;ve been busy working towards a deadline over at Collective Labs, and around the office, Ship It Squirrel has become our unofficial mascot. It&#8217;s not uncommon to hear someone mutter &#8220;Ship It&#8221; as they&#8217;re dealing with some nasty issue, only to hear the rest of the office chime in &#8220;Ship It&#8221; in support. Because of [...]]]></description>
			<content:encoded><![CDATA[<p>We&#8217;ve been busy working towards a deadline over at Collective Labs, and around the office, <a href="http://shipitsquirrel.github.com">Ship It Squirrel</a> has become our unofficial mascot. It&#8217;s not uncommon to hear someone mutter &#8220;Ship It&#8221; as they&#8217;re dealing with some nasty issue, only to hear the rest of the office chime in &#8220;Ship It&#8221; in support.</p>
<p>Because of that, we thought it would be funny to have a Twitter bot ShipItSquirrel that would reply back to keywords in your tweets with &#8220;Ship It!&#8221;. Well, the wait is over. You can now find <a href="http://twitter.com/ShipItSquirrel">ShipItSquirrel on Twitter</a>. Follow him and he&#8217;ll follow you back. Then, whenever certain ship-related terms are tweeted by you, ShipItSquirrel will snap back &#8220;Ship It!&#8221; in support.</p>
<p>Yeah, okay, so it was also an excuse to play with <a href="http://github.com/joshthecoder/tweepy">Tweepy</a>, a <a href="http://www.python.org">Python</a> Twitter library and see how the new OAuth authentication worked. If you&#8217;re curious, you can check out the skeleton <a href="http://github.com/ksturner/ShipItSquirrel">ShipItSquirrel app on GitHub</a>. Oh, but you&#8217;ll need to <a href="http://dev.twitter.com/">get your own damn access tokens for Twitter</a> if you intend to run the code on your Twitter account.</p>
]]></content:encoded>
			<wfw:commentRss>http://blackpaddle.com/?feed=rss2&amp;p=33</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Logging file and line numbers in Python</title>
		<link>http://blackpaddle.com/?p=4</link>
		<comments>http://blackpaddle.com/?p=4#comments</comments>
		<pubDate>Sat, 09 Oct 2010 03:35:49 +0000</pubDate>
		<dc:creator>Kevin Turner</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[logging]]></category>

		<guid isPermaLink="false">http://blackpaddle.com/?p=4</guid>
		<description><![CDATA[If you&#8217;re like me, you abuse &#8216;print&#8217; and have lots of little warning, or generic exceptions printing various levels of messages to the console of your Python applications. Sometimes the messages are informational, and other times they represent critical errors. You&#8217;d like to be able to categorize these levels of printed messages. Sometimes you also have [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re like me, you abuse &#8216;print&#8217; and have lots of little warning, or generic exceptions printing various levels of messages to the console of your Python applications. Sometimes the messages are informational, and other times they represent critical errors. You&#8217;d like to be able to categorize these levels of printed messages. Sometimes you also have messages printed that come from another software library. In those cases, because the message is dynamically generated, you aren&#8217;t sure where in the code, exactly, the error message occurred. In those cases you&#8217;d like to have the file and line numbers where the message was printed.</p>
<p>Well, you&#8217;re in luck. There&#8217;s been a handy little library in Python for a number of years called <strong>logging</strong> and it does all of this. You can categorize the various levels of messages as: <strong>debug, info, warning, error, </strong>and ﻿<strong>critical</strong>. Alternatively, you can then set the level of messages that you want to be logged, or printed, so that you would only see messages for level of &#8216;warning&#8217; or above. Additionally, the messages you log/print can automatically have the time prepended, as well as the file and line number. Here&#8217;s a short example.</p>
<pre><span class="pythonComment">#!/usr/bin/env python</span>
<span class="PreProc">import</span> logging

logging.basicConfig(level=logging.INFO,
    <span class="pythonBuiltin">format</span><span>=</span><span class="String">'%(asctime)s %(levelname)-8s %(filename)s:%(lineno)-4d: %(message)s'</span><span>,</span>
    datefmt=<span class="String">'%m-%d %H:%M'</span><span>,</span>
    )
logging.debug(<span class="String">'A debug message'</span><span>)</span>
logging.info(<span class="String">'Some information'</span><span>)</span>
logging.warning(<span class="String">'A shot across the bow'</span><span>)</span>

<span class="pythonComment"># Should print the following (with current date/time of course)</span>
<span class="pythonComment">#10-19 19:57 INFO     test.py:9   : Some information</span>
<span class="pythonComment">#10-19 19:57 WARNING  test.py:10  : A shot across the bow</span></pre>
<p>To give you an idea of what <strong>logging</strong> is doing for you, you could write your own logging function that did roughly (and I mean roughly), the same thing. Here&#8217;s a logging function that prints the file, line, and a prefix (which could be info, warning, error, etc.). There is no concept of filtering the level of messages with this example, however.</p>
<pre><span class="pythonComment">#!/usr/bin/env python</span>
<span class="PreProc">import</span> inspect

<span class="pythonStatement">def</span> <span class="Function">logfileline</span><span>(msg, prefix=</span><span class="String">''</span><span>):</span>
    <span class="String">''' Utility function to log a message and automatically print the file </span>
<span class="String">        and line number the log function was called from. '''</span>
    frame, module = <span class="pythonBuiltin">None</span><span>, </span><span class="pythonBuiltin">None</span>
    <span class="pythonConditional">if</span><span> prefix != </span><span class="String">''</span><span> </span><span class="pythonOperator">and</span><span> </span><span class="pythonOperator">not</span><span> prefix.endswith(</span><span class="String">' '</span><span>):</span>
        prefix = prefix + <span class="String">' '</span>

    <span class="Special">try</span><span>:</span>
        frame = inspect.stack()[<span class="Constant">1</span><span>][</span><span class="Constant">0</span><span>]</span>
        module = inspect.getmodule(frame)
        <span class="pythonBuiltin">print</span><span> </span><span class="String">'{0}{1}::{2}:</span><span class="Special">\n\t</span><span class="String">{3}'</span><span>.</span><span class="pythonBuiltin">format</span><span>(prefix, frame.f_lineno, </span>
                                        frame.f_code.co_filename, msg)
    <span class="Special">finally</span><span>:</span>
        <span class="pythonConditional">if</span><span> frame </span><span class="pythonOperator">is</span><span> </span><span class="pythonOperator">not</span><span> </span><span class="pythonBuiltin">None</span><span>:</span>
            <span class="pythonStatement">del</span><span> frame</span>
        <span class="pythonConditional">if</span><span> module </span><span class="pythonOperator">is</span><span> </span><span class="pythonOperator">not</span><span> </span><span class="pythonBuiltin">None</span><span>:</span>
            <span class="pythonStatement">del</span><span> module</span></pre>
<p>So there you have it. Two little examples of logging/printing messages at different levels with the option for file and line numbers. </p>
]]></content:encoded>
			<wfw:commentRss>http://blackpaddle.com/?feed=rss2&amp;p=4</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

