<?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"
	>

<channel>
	<title>Evan Fosmark</title>
	<atom:link href="http://www.evanfosmark.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.evanfosmark.com</link>
	<description>Hopefully I'll write a new post soon.</description>
	<pubDate>Sat, 28 Nov 2009 20:57:13 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
	<language>en</language>
			<item>
		<title>Remember, remember, the flux capacitor!</title>
		<link>http://www.evanfosmark.com/2009/11/remember-remember-the-flux-capacitor/</link>
		<comments>http://www.evanfosmark.com/2009/11/remember-remember-the-flux-capacitor/#comments</comments>
		<pubDate>Fri, 06 Nov 2009 06:38:26 +0000</pubDate>
		<dc:creator>Evan</dc:creator>
		
		<category><![CDATA[Off Topic]]></category>

		<category><![CDATA[5th of november]]></category>

		<category><![CDATA[doc brown]]></category>

		<category><![CDATA[guy fawkes]]></category>

		<guid isPermaLink="false">http://www.evanfosmark.com/?p=68</guid>
		<description><![CDATA[Everybody celebrates November 5th for it&#039;s Fawkesian roots. But where&#039;s the love for it being the day that Doc Brown came up with the idea for the flux capacitor? Sure, only one of them actually has merit, but still&#8230; Great Scott!
]]></description>
			<content:encoded><![CDATA[<p>Everybody celebrates November 5th for it&#039;s Fawkesian roots. But where&#039;s the love for it being the day that Doc Brown came up with the idea for the flux capacitor? Sure, only one of them actually has merit, but still&#8230; Great Scott!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.evanfosmark.com/2009/11/remember-remember-the-flux-capacitor/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Python Markov Chains and how to use them</title>
		<link>http://www.evanfosmark.com/2009/11/python-markov-chains-and-how-to-use-them/</link>
		<comments>http://www.evanfosmark.com/2009/11/python-markov-chains-and-how-to-use-them/#comments</comments>
		<pubDate>Thu, 05 Nov 2009 02:34:23 +0000</pubDate>
		<dc:creator>Evan</dc:creator>
		
		<category><![CDATA[Programming]]></category>

		<category><![CDATA[Python]]></category>

		<category><![CDATA[markov]]></category>

		<category><![CDATA[markov chain]]></category>

		<category><![CDATA[pseudoword]]></category>

		<category><![CDATA[random text]]></category>

		<guid isPermaLink="false">http://www.evanfosmark.com/?p=67</guid>
		<description><![CDATA[So, due to recommendations to use Markov chains for text generation after my last little script on creating fake words, I have finally gotten around to learning and implementing the Markov chain algorithm for text generation in Python. There were a few different implementations already available, but they all seemed a bit gimpy.  The [...]]]></description>
			<content:encoded><![CDATA[<p>So, due to <a href="http://www.evanfosmark.com/2009/07/creating-fake-words/#comments">recommendations to use Markov chains for text generation</a> after my last little script on <a href="http://www.evanfosmark.com/2009/07/creating-fake-words/">creating fake words</a>, I have finally gotten around to learning and implementing the <a href="http://en.wikipedia.org/wiki/Markov_chain#Markov_text_generators">Markov chain algorithm for text generation</a> in Python. There were <a href="http://code.activestate.com/recipes/194364/">a few</a> <a href="http://uswaretech.com/blog/2009/06/pseudo-random-text-markov-chains-python/">different implementations</a> already available, but they all seemed a bit gimpy.  The solution I wrote up aims to be able to support high volumes of text. Take a look at what I conjured up and tell me what you think: </p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">collections</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">random</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> DynamicDie<span style="color: black;">&#40;</span><span style="color: #dc143c;">collections</span>.<span style="color: black;">defaultdict</span><span style="color: black;">&#41;</span>:
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #dc143c;">collections</span>.<span style="color: black;">defaultdict</span>.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, <span style="color: #008000;">int</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> add_side<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, side<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span><span style="color: black;">&#91;</span>side<span style="color: black;">&#93;</span> += <span style="color: #ff4500;">1</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> total_sides<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">sum</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">values</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> roll<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        random_num = <span style="color: #dc143c;">random</span>.<span style="color: black;">randint</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>, <span style="color: #008000;">self</span>.<span style="color: black;">total_sides</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        total_pos = <span style="color: #ff4500;">0</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> side, qty <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">self</span>.<span style="color: black;">items</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
            total_pos += qty
            <span style="color: #ff7700;font-weight:bold;">if</span> random_num <span style="color: #66cc66;">&lt;</span>= total_pos:
                <span style="color: #ff7700;font-weight:bold;">return</span> side
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> MarkovChain<span style="color: black;">&#40;</span><span style="color: #dc143c;">collections</span>.<span style="color: black;">defaultdict</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot; Yet another markov chain implementation.
        This one differs in the sense that it is able to better support
        huge amounts of data since the weighted randomization doesn't rely
        on duplicates.
    &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># Sentinals </span>
    <span style="color: #808080; font-style: italic;"># Discussion here: http://stackoverflow.com/questions/1677726</span>
    <span style="color: #ff7700;font-weight:bold;">class</span> START<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:<span style="color: #ff7700;font-weight:bold;">pass</span>
    <span style="color: #ff7700;font-weight:bold;">class</span> END<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:<span style="color: #ff7700;font-weight:bold;">pass</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #dc143c;">collections</span>.<span style="color: black;">defaultdict</span>.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, DynamicDie<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> add<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, iterable<span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot; Insert an iterable (pattern) item into the markov chain.
            The order of the pattern will define more of the chain.
        &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        item1 = item2 = MarkovChain.<span style="color: black;">START</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> item3 <span style="color: #ff7700;font-weight:bold;">in</span> iterable:
            <span style="color: #008000;">self</span><span style="color: black;">&#91;</span><span style="color: black;">&#40;</span>item1, item2<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>.<span style="color: black;">add_side</span><span style="color: black;">&#40;</span>item3<span style="color: black;">&#41;</span>
            item1 = item2
            item2 = item3
        <span style="color: #008000;">self</span><span style="color: black;">&#91;</span><span style="color: black;">&#40;</span>item1, item2<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>.<span style="color: black;">add_side</span><span style="color: black;">&#40;</span>MarkovChain.<span style="color: black;">END</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> random_output<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, <span style="color: #008000;">max</span>=<span style="color: #ff4500;">100</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot; Generate a list of elements from the markov chain.
            The `max` value is in place in order to prevent excessive iteration.
        &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        output = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
        item1 = item2 = MarkovChain.<span style="color: black;">START</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #008000;">max</span><span style="color: #ff4500;">-2</span><span style="color: black;">&#41;</span>:
            item3 = <span style="color: #008000;">self</span><span style="color: black;">&#91;</span><span style="color: black;">&#40;</span>item1, item2<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>.<span style="color: black;">roll</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">if</span> item3 <span style="color: #ff7700;font-weight:bold;">is</span> MarkovChain.<span style="color: black;">END</span>:
                <span style="color: #ff7700;font-weight:bold;">break</span>
            output.<span style="color: black;">append</span><span style="color: black;">&#40;</span>item3<span style="color: black;">&#41;</span>
            item1 = item2
            item2 = item3
        <span style="color: #ff7700;font-weight:bold;">return</span> output</pre></div></div>

<p>It&#039;s not really complicated as long as you understand Markov chains. Let&#039;s use this in an example to generate some fake words:</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> markov
chain = markov.<span style="color: black;">MarkovChain</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Load the dictionary into the markov chain</span>
dictionary = <span style="color: #483d8b;">&quot;/usr/share/dict/words&quot;</span>
<span style="color: #ff7700;font-weight:bold;">for</span> word <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">open</span><span style="color: black;">&#40;</span>dictionary<span style="color: black;">&#41;</span>:
    word = word.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> word <span style="color: #66cc66;">!</span>= <span style="color: #483d8b;">&quot;&quot;</span> <span style="color: #ff7700;font-weight:bold;">and</span> <span style="color: #ff7700;font-weight:bold;">not</span> word.<span style="color: black;">endswith</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;'s&quot;</span><span style="color: black;">&#41;</span>:
        chain.<span style="color: black;">add</span><span style="color: black;">&#40;</span>word<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Let's generate 10 random pseudowords</span>
<span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">10</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;&quot;</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>chain.<span style="color: black;">random_output</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>The way I did it above isn&#039;t actually that good since it loads every word in the English language into the Markov chain. Of course, you don&#039;t need all of the words to get good looking output. This is just an example, though. I&#039;ll leave it up to you to make a sufficient solution. </p>
<p>As always, if you see a way that the code in this post could be improved, please leave a comment!</p>
<p><strong>Update:</strong> I updated it to use the dynamic die object. This solves the linear search problem that Marius mentions below.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.evanfosmark.com/2009/11/python-markov-chains-and-how-to-use-them/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Send a message THROUGH TIME in Python</title>
		<link>http://www.evanfosmark.com/2009/09/send-a-message-through-time-in-python/</link>
		<comments>http://www.evanfosmark.com/2009/09/send-a-message-through-time-in-python/#comments</comments>
		<pubDate>Sun, 20 Sep 2009 20:56:20 +0000</pubDate>
		<dc:creator>Evan</dc:creator>
		
		<category><![CDATA[Jokes]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[Python]]></category>

		<category><![CDATA[joke]]></category>

		<category><![CDATA[time travel]]></category>

		<guid isPermaLink="false">http://www.evanfosmark.com/?p=65</guid>
		<description><![CDATA[Who says time travel isn&#039;t possible?

import time
&#160;
def send_to_future&#40;message, seconds&#41;:
    time.sleep&#40;seconds&#41;
    return message

I&#039;ll leave it up to someone else to write the send_to_past() function.
]]></description>
			<content:encoded><![CDATA[<p>Who says time travel isn&#039;t possible?</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">time</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> send_to_future<span style="color: black;">&#40;</span>message, seconds<span style="color: black;">&#41;</span>:
    <span style="color: #dc143c;">time</span>.<span style="color: black;">sleep</span><span style="color: black;">&#40;</span>seconds<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> message</pre></div></div>

<p>I&#039;ll leave it up to someone else to write the <tt>send_to_past()</tt> function.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.evanfosmark.com/2009/09/send-a-message-through-time-in-python/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Creating fake words: a pseudoword generator</title>
		<link>http://www.evanfosmark.com/2009/07/creating-fake-words/</link>
		<comments>http://www.evanfosmark.com/2009/07/creating-fake-words/#comments</comments>
		<pubDate>Sat, 04 Jul 2009 02:02:57 +0000</pubDate>
		<dc:creator>Evan</dc:creator>
		
		<category><![CDATA[Programming]]></category>

		<category><![CDATA[Python]]></category>

		<category><![CDATA[Tools]]></category>

		<category><![CDATA[creating fake words]]></category>

		<category><![CDATA[fake words]]></category>

		<category><![CDATA[word generator]]></category>

		<guid isPermaLink="false">http://www.evanfosmark.com/?p=61</guid>
		<description><![CDATA[Have you ever wished to create a fake word for a more memorable random password or as a project name? Well here's the solution I devised through using Python.]]></description>
			<content:encoded><![CDATA[<p>I was trying to figure out how to create a word that&#039;s not a word. What I ended up doing was creating a way of generating a random syllable, and then simply appending 2 or 3 of them together. It seems to work well enough. Here&#039;s what I got in Python:</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">random</span>
&nbsp;
vowels = <span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;a&quot;</span>, <span style="color: #483d8b;">&quot;e&quot;</span>, <span style="color: #483d8b;">&quot;i&quot;</span>, <span style="color: #483d8b;">&quot;o&quot;</span>, <span style="color: #483d8b;">&quot;u&quot;</span><span style="color: black;">&#93;</span>
consonants = <span style="color: black;">&#91;</span><span style="color: #483d8b;">'b'</span>, <span style="color: #483d8b;">'c'</span>, <span style="color: #483d8b;">'d'</span>, <span style="color: #483d8b;">'f'</span>, <span style="color: #483d8b;">'g'</span>, <span style="color: #483d8b;">'h'</span>, <span style="color: #483d8b;">'j'</span>, <span style="color: #483d8b;">'k'</span>, <span style="color: #483d8b;">'l'</span>, <span style="color: #483d8b;">'m'</span>, <span style="color: #483d8b;">'n'</span>, <span style="color: #483d8b;">'p'</span>, <span style="color: #483d8b;">'q'</span>, 
              <span style="color: #483d8b;">'r'</span>, <span style="color: #483d8b;">'s'</span>, <span style="color: #483d8b;">'t'</span>, <span style="color: #483d8b;">'v'</span>, <span style="color: #483d8b;">'w'</span>, <span style="color: #483d8b;">'x'</span>, <span style="color: #483d8b;">'y'</span>, <span style="color: #483d8b;">'z'</span><span style="color: black;">&#93;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> _vowel<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #dc143c;">random</span>.<span style="color: black;">choice</span><span style="color: black;">&#40;</span>vowels<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> _consonant<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #dc143c;">random</span>.<span style="color: black;">choice</span><span style="color: black;">&#40;</span>consonants<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> _cv<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">return</span> _consonant<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> + _vowel<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> _cvc<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">return</span> _cv<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> + _consonant<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> _syllable<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #dc143c;">random</span>.<span style="color: black;">choice</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span>_vowel, _cv, _cvc<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> create_fake_word<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot; This function generates a fake word by creating between two and three
        random syllables and then joining them together.
    &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
    syllables = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">random</span>.<span style="color: black;">randint</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span>,<span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:
        syllables.<span style="color: black;">append</span><span style="color: black;">&#40;</span>_syllable<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot;&quot;</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>syllables<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">&quot;__main__&quot;</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> create_fake_word<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>The first four functions are for generating an individual type of syllable (V, CV, or CVC) and then <tt>_syllable()</tt> just chooses one of them at random. Finally, <tt>create_fake_word()</tt> calls <tt>_syllable()</tt> a few times and joins them together. Here is some example output:</p>

<div class="wp_syntax"><div class="code"><pre>hojocina
eliphaa
deaketyed
ciboa
tiuzi</pre></div></div>

<p>I haven&#039;t a clue whether or not there is a better means of generating words that look somewhat real. If you know of a better method, I&#039;d love to hear it!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.evanfosmark.com/2009/07/creating-fake-words/feed/</wfw:commentRss>
		</item>
		<item>
		<title>A simple event-driven plugin system in Python</title>
		<link>http://www.evanfosmark.com/2009/07/simple-event-driven-plugin-system-in-python/</link>
		<comments>http://www.evanfosmark.com/2009/07/simple-event-driven-plugin-system-in-python/#comments</comments>
		<pubDate>Fri, 03 Jul 2009 08:08:39 +0000</pubDate>
		<dc:creator>Evan</dc:creator>
		
		<category><![CDATA[Programming]]></category>

		<category><![CDATA[Python]]></category>

		<category><![CDATA[plugin system]]></category>

		<category><![CDATA[recipie]]></category>

		<guid isPermaLink="false">http://www.evanfosmark.com/?p=60</guid>
		<description><![CDATA[So I just wanted to share my solution for integrating a plugin system into Python applications. Overall it&#039;s separated into four parts: the plugin manager, the plugin(s), a config file listing the plugins, and the application that triggers the events. In this system, plugins are just functions that get registered through the use of a [...]]]></description>
			<content:encoded><![CDATA[<p>So I just wanted to share my solution for integrating a plugin system into Python applications. Overall it&#039;s separated into four parts: the plugin manager, the plugin(s), a config file listing the plugins, and the application that triggers the events. In this system, plugins are just functions that get registered through the use of a decorator. </p>
<p>Let&#039;s start with the code for the plugin manager:</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #808080; font-style: italic;"># pluginmanager.py</span>
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">collections</span> <span style="color: #ff7700;font-weight:bold;">import</span> defaultdict
&nbsp;
&nbsp;
plugins = defaultdict<span style="color: black;">&#40;</span><span style="color: #008000;">list</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">def</span> register<span style="color: black;">&#40;</span><span style="color: #66cc66;">*</span>events<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot; This decorator is to be used for registering a function as a plugin for
        a specific event or list of events. 
    &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> registered_plugin<span style="color: black;">&#40;</span>funct<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">for</span> event <span style="color: #ff7700;font-weight:bold;">in</span> events:
            plugins<span style="color: black;">&#91;</span>event<span style="color: black;">&#93;</span>.<span style="color: black;">append</span><span style="color: black;">&#40;</span>funct<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> funct
    <span style="color: #ff7700;font-weight:bold;">return</span> registered_plugin
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> trigger_event<span style="color: black;">&#40;</span>event, <span style="color: #66cc66;">*</span>args, <span style="color: #66cc66;">**</span>kwargs<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot; Call this function to trigger an event. It will run any plugins that
        have registered themselves to the event. Any additional arguments or
        keyword arguments you pass in will be passed to the plugins.
    &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> plugin <span style="color: #ff7700;font-weight:bold;">in</span> plugins<span style="color: black;">&#91;</span>event<span style="color: black;">&#93;</span>:
        plugin<span style="color: black;">&#40;</span><span style="color: #66cc66;">*</span>args, <span style="color: #66cc66;">**</span>kwargs<span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> load_plugins<span style="color: black;">&#40;</span>config_file<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot; This reads a config file of a list of plugins to load. It ignores
        empty lines or lines beginning with a hash mark (#). It is so plugin
        imports are more dynamic and you don't need to continue appending
        import statements to the top of a file.
    &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
    with <span style="color: #008000;">open</span><span style="color: black;">&#40;</span>config_file, <span style="color: #483d8b;">&quot;r&quot;</span><span style="color: black;">&#41;</span> as fh:
        <span style="color: #ff7700;font-weight:bold;">for</span> line <span style="color: #ff7700;font-weight:bold;">in</span> fh:
            line = line.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">if</span> line.<span style="color: black;">startswith</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;#&quot;</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">or</span> line == <span style="color: #483d8b;">&quot;&quot;</span>:
                <span style="color: #ff7700;font-weight:bold;">continue</span>
            <span style="color: #008000;">__import__</span><span style="color: black;">&#40;</span>line,  <span style="color: #008000;">globals</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>, <span style="color: #008000;">locals</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>, <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>, <span style="color: #ff4500;">-1</span><span style="color: black;">&#41;</span></pre></div></div>

<p>The plugin system is separated into three functions. The first is <tt>register()</tt> which is the decorator for registering plugins. The function <tt>trigger_event()</tt> is used by your application notify the plugin system that an event has occured. It goes out and runs any plugins registered with the event. Lastly, <tt>load_plugins()</tt> accepts a file location that contains the plugins to be loaded. We&#039;ll investigate these more closely later on in the article.</p>
<h3>An example plugin</h3>
<p>Let&#039;s build a package called &quot;plugins&quot; and create a module inside that called &quot;example.py&quot; with the following data:</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> pluginmanager
&nbsp;
@pluginmanager.<span style="color: black;">register</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;FOO_ACTIVE&quot;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">def</span> print_data<span style="color: black;">&#40;</span><span style="color: #66cc66;">*</span>args, <span style="color: #66cc66;">**</span>kwargs<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #483d8b;">&quot;data&quot;</span> <span style="color: #ff7700;font-weight:bold;">in</span> kwargs:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Received the following: %s&quot;</span> <span style="color: #66cc66;">%</span> kwargs<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;data&quot;</span><span style="color: black;">&#93;</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Didn't receive any data.&quot;</span></pre></div></div>

<p>Here, the <tt>print_data</tt> plugin is registered for the <tt>FOO_ACTIVE</tt> event, and will be run if that event is triggered.</p>
<h3>How to trigger the plugin</h3>
<p>First, the plugin needs to be loaded. Right now, the source file is just sitting out there and unless we import it we&#039;ll never be able to do anything with it. So, you can either do a manual <tt>import</tt> or you can call the <tt>load_plugins()</tt> function and have it parse a list of plugin locations. We&#039;ll use the latter method.</p>
<h4>The config file (plugins.list)</h4>
<p>Let&#039;s create a config file with data resembling the following:</p>

<div class="wp_syntax"><div class="code"><pre class="ini"># Blank lines and lines starting with a hash <span style="">&#40;</span>#<span style="">&#41;</span> are ignored.
# So be as verbose as you want.
plugins.example</pre></div></div>

<p>As you can see from above, it specifies to import the <tt>plugins.example</tt> module (since <tt>plugins</tt> is a package and <tt>example</tt> is a module inside of it). I&#039;m doing it this way so adding or removing additional plugins is simple.</p>
<h4>Application use</h4>
<p>Finally, let&#039;s look at how an application would utilize this system:</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> pluginmanager
pluginmanager.<span style="color: black;">load_plugins</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;plugins.list&quot;</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># load any plugins in the list</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># ... code ...</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># The FOO_ACTIVE event occurs somewhere</span>
pluginmanager.<span style="color: black;">trigger_event</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;FOO_ACTIVE&quot;</span>, 
                            data=<span style="color: #483d8b;">&quot;this data is sent to the plugin&quot;</span>, 
                            foo=<span style="color: #483d8b;">&quot;so is this&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># ... code ...</span></pre></div></div>

<p>What happenes here is pretty straightforward. The manger loads the plugins, the application eventually triggers the <tt>FOO_ACTIVE</tt> event and sends data along with it. Any arguments or keyword arguments after the event in <tt>trigger_event()</tt> are sent directly to the plugin. When the event is triggered, it finds any plugins that have registered with it and activates it.</p>
<p>So yeah, that&#039;s what I came up with. It does have the downside of not being able to easily extend a plugin like you can with class-based systems since you can&#039;t inherit. Still, it&#039;s pretty nice for smaller apps that want a taste of pluggability. </p>
<p>If you have your own system for dealing with this type of situation, I&#039;d love to hear from you.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.evanfosmark.com/2009/07/simple-event-driven-plugin-system-in-python/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Why do so many websites fail with password restrictions?</title>
		<link>http://www.evanfosmark.com/2009/06/why-do-so-many-websites-fail-with-password-restrictions/</link>
		<comments>http://www.evanfosmark.com/2009/06/why-do-so-many-websites-fail-with-password-restrictions/#comments</comments>
		<pubDate>Sat, 20 Jun 2009 08:32:49 +0000</pubDate>
		<dc:creator>Evan</dc:creator>
		
		<category><![CDATA[Articles]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[password length]]></category>

		<category><![CDATA[password length restrictions]]></category>

		<category><![CDATA[passwords]]></category>

		<guid isPermaLink="false">http://www.evanfosmark.com/?p=59</guid>
		<description><![CDATA[All too often when registering at a site I&#039;ll get prompted with a message along the lines of: &#034;Password must be between 6 and 12 characters long and cannot contain special characters.&#034; The second I see that a little warning goes off in my head that they are probably storing the password as plain-text in [...]]]></description>
			<content:encoded><![CDATA[<p>All too often when <a href="http://www.google.com/search?q=intitle%3A%22create+account%22+between+characters" title="Wow, look at all those password limits!">registering at a site</a> I&#039;ll get prompted with a message along the lines of: &#034;Password must be between 6 and 12 characters long and cannot contain special characters.&#034; The second I see that a little warning goes off in my head that they are probably storing the password as plain-text in their database or that at least they aren&#039;t hashing it. The only other time I get so worried about website password security is when <em>they actually send me my password in an email after registration.</em> </p>
<p>The bottom-line is that <strong>there should never be a case where there are password limitations such as special characters or maximum length.</strong> Why should you care if I decide to have a dollar sign, ampersand, or apostrophe in my password? Why is that considered bad? I mean, as long as you are hashing it (like you <em>should</em> be), it doesn&#039;t matter, right? </p>
<p>Same goes for password length. Since the hashes produced are a constant length, saying that the password would take up too much space in the database is an invalid argument. If I want my password to be the first sentence of my 6th grade report on Leif Erickson, then I should be able to. It&#039;s all about being able to remember and there exist plenty of pass-phrases that&#039;d be easier to remember than any 8-character long password.</p>
<p>What about potential DoS attack with using a really long password? That is <em>almost</em> a valid reason for length restriction since hashing algorithms can be quite intensive on larger bodies of text, but how difficult can it be to spot and block those users with malicious intent? </p>
<h3>What password verification should look like</h3>
<p>No character limitation. No maximum length limitation. What&#039;s really left?</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">def</span> is_valid_password<span style="color: black;">&#40;</span>password, min_length=<span style="color: #ff4500;">6</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>password<span style="color: black;">&#41;</span> <span style="color: #66cc66;">&gt;</span>= min_length</pre></div></div>

<p>Indeed, the only check that should be required is a minimum length. And even that&#039;s a stretch. Beyond being sure that the password isn&#039;t easily guessable, I see no reason for password restrictions in a world of fixed-length hashing.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.evanfosmark.com/2009/06/why-do-so-many-websites-fail-with-password-restrictions/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Sexy Lexing with Python</title>
		<link>http://www.evanfosmark.com/2009/02/sexy-lexing-with-python/</link>
		<comments>http://www.evanfosmark.com/2009/02/sexy-lexing-with-python/#comments</comments>
		<pubDate>Sat, 21 Feb 2009 09:43:56 +0000</pubDate>
		<dc:creator>Evan</dc:creator>
		
		<category><![CDATA[Programming]]></category>

		<category><![CDATA[Python]]></category>

		<category><![CDATA[lex]]></category>

		<category><![CDATA[lexer]]></category>

		<category><![CDATA[lexical analysis]]></category>

		<category><![CDATA[lexing]]></category>

		<category><![CDATA[scanning]]></category>

		<guid isPermaLink="false">http://www.evanfosmark.com/?p=44</guid>
		<description><![CDATA[Lexical analysis, a daunting task right? Wrong! In the following document we&#039;ll walk through different methods of lexical scanning in Python. First, we&#039;ll look at a pre-built solution found in the library, and then at a custom-built solution.
Using re.Scanner
In the re module there is a class called Scanner that can do lexical sanning. It is [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://en.wikipedia.org/wiki/Lexical_analysis">Lexical analysis</a>, a daunting task right? Wrong! In the following document we&#039;ll walk through different methods of lexical scanning in Python. First, we&#039;ll look at a pre-built solution found in the library, and then at a custom-built solution.</p>
<h3>Using re.Scanner</h3>
<p>In the <code>re</code> module there is a class called <code>Scanner</code> that can do lexical sanning. It is completely undocumented other than for a <a href="http://mail.python.org/pipermail/python-dev/2003-April/035075.html">small example code block</a> found on the <a href="http://mail.python.org/pipermail/python-dev/">Python-Dev mailing list</a>, but well worth mentioning. It works by feeding in a list of regular-expressions and callback functions linked to them. When it matches a token, it first runs its value through the appropriate callback and then appends it to the token list being returned. If the scanner reaches a spot where a token match cannot be found, it returns what matches (if any) it did have along with the rest of the document that couldn&#039;t be matched. Here is an example:</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">re</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> identifier<span style="color: black;">&#40;</span>scanner, <span style="color: #dc143c;">token</span><span style="color: black;">&#41;</span>: <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot;IDENT&quot;</span>, <span style="color: #dc143c;">token</span>
<span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #dc143c;">operator</span><span style="color: black;">&#40;</span>scanner, <span style="color: #dc143c;">token</span><span style="color: black;">&#41;</span>:   <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot;OPERATOR&quot;</span>, <span style="color: #dc143c;">token</span>
<span style="color: #ff7700;font-weight:bold;">def</span> digit<span style="color: black;">&#40;</span>scanner, <span style="color: #dc143c;">token</span><span style="color: black;">&#41;</span>:      <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot;DIGIT&quot;</span>, <span style="color: #dc143c;">token</span>
<span style="color: #ff7700;font-weight:bold;">def</span> end_stmnt<span style="color: black;">&#40;</span>scanner, <span style="color: #dc143c;">token</span><span style="color: black;">&#41;</span>:  <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot;END_STATEMENT&quot;</span>
&nbsp;
scanner = <span style="color: #dc143c;">re</span>.<span style="color: black;">Scanner</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span>
    <span style="color: black;">&#40;</span>r<span style="color: #483d8b;">&quot;[a-zA-Z_]<span style="color: #000099; font-weight: bold;">\w</span>*&quot;</span>, identifier<span style="color: black;">&#41;</span>,
    <span style="color: black;">&#40;</span>r<span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\+</span>|<span style="color: #000099; font-weight: bold;">\-</span>|<span style="color: #000099; font-weight: bold;">\\</span>|<span style="color: #000099; font-weight: bold;">\*</span>|<span style="color: #000099; font-weight: bold;">\=</span>&quot;</span>, <span style="color: #dc143c;">operator</span><span style="color: black;">&#41;</span>,
    <span style="color: black;">&#40;</span>r<span style="color: #483d8b;">&quot;[0-9]+(<span style="color: #000099; font-weight: bold;">\.</span>[0-9]+)?&quot;</span>, digit<span style="color: black;">&#41;</span>,
    <span style="color: black;">&#40;</span>r<span style="color: #483d8b;">&quot;;&quot;</span>, end_stmnt<span style="color: black;">&#41;</span>,
    <span style="color: black;">&#40;</span>r<span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\s</span>+&quot;</span>, <span style="color: #008000;">None</span><span style="color: black;">&#41;</span>,
    <span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
&nbsp;
tokens, remainder = scanner.<span style="color: black;">scan</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;foo = 5 * 30; bar = bar - 60;&quot;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">for</span> <span style="color: #dc143c;">token</span> <span style="color: #ff7700;font-weight:bold;">in</span> tokens:
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #dc143c;">token</span></pre></div></div>

<p>Which provides the output:</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: black;">&#40;</span><span style="color: #483d8b;">'IDENT'</span>, <span style="color: #483d8b;">'foo'</span><span style="color: black;">&#41;</span>
<span style="color: black;">&#40;</span><span style="color: #483d8b;">'OPERATOR'</span>, <span style="color: #483d8b;">'='</span><span style="color: black;">&#41;</span>
<span style="color: black;">&#40;</span><span style="color: #483d8b;">'DIGIT'</span>, <span style="color: #483d8b;">'5'</span><span style="color: black;">&#41;</span>
<span style="color: black;">&#40;</span><span style="color: #483d8b;">'OPERATOR'</span>, <span style="color: #483d8b;">'*'</span><span style="color: black;">&#41;</span>
<span style="color: black;">&#40;</span><span style="color: #483d8b;">'DIGIT'</span>, <span style="color: #483d8b;">'30'</span><span style="color: black;">&#41;</span>
END_STATEMENT
<span style="color: black;">&#40;</span><span style="color: #483d8b;">'IDENT'</span>, <span style="color: #483d8b;">'bar'</span><span style="color: black;">&#41;</span>
<span style="color: black;">&#40;</span><span style="color: #483d8b;">'OPERATOR'</span>, <span style="color: #483d8b;">'='</span><span style="color: black;">&#41;</span>
<span style="color: black;">&#40;</span><span style="color: #483d8b;">'IDENT'</span>, <span style="color: #483d8b;">'bar'</span><span style="color: black;">&#41;</span>
<span style="color: black;">&#40;</span><span style="color: #483d8b;">'OPERATOR'</span>, <span style="color: #483d8b;">'-'</span><span style="color: black;">&#41;</span>
<span style="color: black;">&#40;</span><span style="color: #483d8b;">'DIGIT'</span>, <span style="color: #483d8b;">'60'</span><span style="color: black;">&#41;</span>
END_STATEMENT</pre></div></div>

<p>Truly easy, fast, and relatively simple to understand.<br />
Using this is perfect for small projects, but it has some downsides such as not allowing simple error handling and not implicitly handling whitespace. Additionally, it suffers from having to tokenize the whole document before being able to provide anything, and that can get costly on larger projects.</p>
<h3>Custom-Built Lexer</h3>
<p>I had decided to build a custom lexer as a means to break away from the re.Scanner. Here is the code for the actual lexer. It is broken into three classes: UnknownTokenError which gets thrown when a non-recognized token is found, Lexer which holds the settings for scanning, and _InputScanner which is in charge of scanning specific input, as the name implies. A few benefits built into the Lexer include automatic whitespace handling (if desired) and the ability to easily make the scan case-insensitive. Additionally, you can <em>optionally</em> provide a callback with the rule to run the token through before returning it by making the rule a tuple of the rule and callback.</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">re</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> UnknownTokenError<span style="color: black;">&#40;</span><span style="color: #008000;">Exception</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot; This exception is for use to be thrown when an unknown token is
        encountered in the token stream. It hols the line number and the
        offending token.
    &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, <span style="color: #dc143c;">token</span>, lineno<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: #dc143c;">token</span> = <span style="color: #dc143c;">token</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">lineno</span> = lineno
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__str__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot;Line #%s, Found token: %s&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">lineno</span>, <span style="color: #008000;">self</span>.<span style="color: #dc143c;">token</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> _InputScanner<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot; This class manages the scanning of a specific input. An instance of it is
        returned when scan() is called. It is built to be great for iteration. This is
        mainly to be used by the Lexer and ideally not directly.
    &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, lexer, <span style="color: #008000;">input</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot; Put the lexer into this instance so the callbacks can reference it 
            if needed.
        &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        <span style="color: #008000;">self</span>._position = <span style="color: #ff4500;">0</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">lexer</span> = lexer
        <span style="color: #008000;">self</span>.<span style="color: #008000;">input</span> = <span style="color: #008000;">input</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__iter__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot; All of the code for iteration is controlled by the class itself.
            This and next() (or __next__() in Python 3.0) are so syntax
            like `for token in Lexer(...):` is valid and works.
        &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> next<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot; Used for iteration. It returns token after token until there
            are no more tokens. (change this to __next__(self) if using Py3.0)
        &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">self</span>.<span style="color: black;">done_scanning</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
            <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>.<span style="color: black;">scan_next</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">raise</span> <span style="color: #008000;">StopIteration</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> done_scanning<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot; A simple boolean function that returns true if scanning is
            complete and false if it isn't.
        &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>._position <span style="color: #66cc66;">&gt;</span>= <span style="color: #008000;">len</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: #008000;">input</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> scan_next<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot; Retreive the next token from the input. If the
            flag `omit_whitespace` is set to True, then it will
            skip over the whitespace characters present.
        &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">self</span>.<span style="color: black;">done_scanning</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
            <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">None</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">self</span>.<span style="color: black;">lexer</span>.<span style="color: black;">omit_whitespace</span>:
            match = <span style="color: #008000;">self</span>.<span style="color: black;">lexer</span>.<span style="color: black;">ws_regexc</span>.<span style="color: black;">match</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: #008000;">input</span>, <span style="color: #008000;">self</span>._position<span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">if</span> match:
                <span style="color: #008000;">self</span>._position = match.<span style="color: black;">end</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        match = <span style="color: #008000;">self</span>.<span style="color: black;">lexer</span>.<span style="color: black;">regexc</span>.<span style="color: black;">match</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: #008000;">input</span>, <span style="color: #008000;">self</span>._position<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> match <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #008000;">None</span>:
            lineno = <span style="color: #008000;">self</span>.<span style="color: #008000;">input</span><span style="color: black;">&#91;</span>:<span style="color: #008000;">self</span>._position<span style="color: black;">&#93;</span>.<span style="color: black;">count</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: black;">&#41;</span> + <span style="color: #ff4500;">1</span>
            <span style="color: #ff7700;font-weight:bold;">raise</span> UnknownTokenError<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: #008000;">input</span><span style="color: black;">&#91;</span><span style="color: #008000;">self</span>._position<span style="color: black;">&#93;</span>, lineno<span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>._position = match.<span style="color: black;">end</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        value = match.<span style="color: black;">group</span><span style="color: black;">&#40;</span>match.<span style="color: black;">lastgroup</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> match.<span style="color: black;">lastgroup</span> <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">self</span>.<span style="color: black;">lexer</span>._callbacks:
            value = <span style="color: #008000;">self</span>.<span style="color: black;">lexer</span>._callbacks<span style="color: black;">&#91;</span>match.<span style="color: black;">lastgroup</span><span style="color: black;">&#93;</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, value<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> match.<span style="color: black;">lastgroup</span>, value
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Lexer<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot; A lexical scanner. It takes in an input and a set of rules based
        on reqular expressions. It then scans the input and returns the
        tokens one-by-one. It is meant to be used through iterating.
    &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, rules, case_sensitive=<span style="color: #008000;">True</span>, omit_whitespace=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot; Set up the lexical scanner. Build and compile the regular expression
            and prepare the whitespace searcher.
        &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        <span style="color: #008000;">self</span>._callbacks = <span style="color: black;">&#123;</span><span style="color: black;">&#125;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">omit_whitespace</span> = omit_whitespace
        <span style="color: #008000;">self</span>.<span style="color: black;">case_sensitive</span> = case_sensitive
        parts = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> name, rule <span style="color: #ff7700;font-weight:bold;">in</span> rules:
            <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">isinstance</span><span style="color: black;">&#40;</span>rule, <span style="color: #008000;">str</span><span style="color: black;">&#41;</span>:
                rule, callback = rule
                <span style="color: #008000;">self</span>._callbacks<span style="color: black;">&#91;</span>name<span style="color: black;">&#93;</span> = callback
            parts.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;(?P&lt;%s&gt;%s)&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>name, rule<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">self</span>.<span style="color: black;">case_sensitive</span>:
            flags = <span style="color: #dc143c;">re</span>.<span style="color: black;">M</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            flags = <span style="color: #dc143c;">re</span>.<span style="color: black;">M</span>|re.<span style="color: black;">I</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">regexc</span> = <span style="color: #dc143c;">re</span>.<span style="color: #008000;">compile</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;|&quot;</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>parts<span style="color: black;">&#41;</span>, flags<span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">ws_regexc</span> = <span style="color: #dc143c;">re</span>.<span style="color: #008000;">compile</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\s</span>*&quot;</span>, <span style="color: #dc143c;">re</span>.<span style="color: black;">MULTILINE</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> scan<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, <span style="color: #008000;">input</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot; Return a scanner built for matching through the `input` field. 
            The scanner that it returns is built well for iterating.
        &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> _InputScanner<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, <span style="color: #008000;">input</span><span style="color: black;">&#41;</span></pre></div></div>

<p>This version does on-the-fly scanning through the use of building the class as an iterator. So, you can work with a token the moment it gets scanned, and before any other tokens get scanned. This can help reduce overhead in case you have a large document and may need to exit prematurely. And, of course, when you write your own lexer, it is much easier to modify it to your needs. Now let&#039;s test the above code and see what sort of token stream we arrive with.</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">def</span> stmnt_callback<span style="color: black;">&#40;</span>scanner, <span style="color: #dc143c;">token</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot; This is just an example of providing a function to run the
        token through.
    &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot;&quot;</span>
&nbsp;
rules = <span style="color: black;">&#91;</span>
    <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;IDENTIFIER&quot;</span>, r<span style="color: #483d8b;">&quot;[a-zA-Z_]<span style="color: #000099; font-weight: bold;">\w</span>*&quot;</span><span style="color: black;">&#41;</span>,
    <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;OPERATOR&quot;</span>,   r<span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\+</span>|<span style="color: #000099; font-weight: bold;">\-</span>|<span style="color: #000099; font-weight: bold;">\\</span>|<span style="color: #000099; font-weight: bold;">\*</span>|<span style="color: #000099; font-weight: bold;">\=</span>&quot;</span><span style="color: black;">&#41;</span>,
    <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;DIGIT&quot;</span>,      r<span style="color: #483d8b;">&quot;[0-9]+(<span style="color: #000099; font-weight: bold;">\.</span>[0-9]+)?&quot;</span><span style="color: black;">&#41;</span>,
    <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;END_STMNT&quot;</span>,  <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;;&quot;</span>, stmnt_callback<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>, 
    <span style="color: black;">&#93;</span>
&nbsp;
lex = Lexer<span style="color: black;">&#40;</span>rules, case_sensitive=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">for</span> <span style="color: #dc143c;">token</span> <span style="color: #ff7700;font-weight:bold;">in</span> lex.<span style="color: black;">scan</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;foo = 5 * 30; bar = bar - 60;&quot;</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #dc143c;">token</span></pre></div></div>

<p>Outputs:</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: black;">&#40;</span><span style="color: #483d8b;">'IDENTIFIER'</span>, <span style="color: #483d8b;">'foo'</span><span style="color: black;">&#41;</span>
<span style="color: black;">&#40;</span><span style="color: #483d8b;">'OPERATOR'</span>, <span style="color: #483d8b;">'='</span><span style="color: black;">&#41;</span>
<span style="color: black;">&#40;</span><span style="color: #483d8b;">'DIGIT'</span>, <span style="color: #483d8b;">'5'</span><span style="color: black;">&#41;</span>
<span style="color: black;">&#40;</span><span style="color: #483d8b;">'OPERATOR'</span>, <span style="color: #483d8b;">'*'</span><span style="color: black;">&#41;</span>
<span style="color: black;">&#40;</span><span style="color: #483d8b;">'DIGIT'</span>, <span style="color: #483d8b;">'30'</span><span style="color: black;">&#41;</span>
<span style="color: black;">&#40;</span><span style="color: #483d8b;">'END_STMNT'</span>, <span style="color: #483d8b;">''</span><span style="color: black;">&#41;</span>
<span style="color: black;">&#40;</span><span style="color: #483d8b;">'IDENTIFIER'</span>, <span style="color: #483d8b;">'bar'</span><span style="color: black;">&#41;</span>
<span style="color: black;">&#40;</span><span style="color: #483d8b;">'OPERATOR'</span>, <span style="color: #483d8b;">'='</span><span style="color: black;">&#41;</span>
<span style="color: black;">&#40;</span><span style="color: #483d8b;">'IDENTIFIER'</span>, <span style="color: #483d8b;">'bar'</span><span style="color: black;">&#41;</span>
<span style="color: black;">&#40;</span><span style="color: #483d8b;">'OPERATOR'</span>, <span style="color: #483d8b;">'-'</span><span style="color: black;">&#41;</span>
<span style="color: black;">&#40;</span><span style="color: #483d8b;">'DIGIT'</span>, <span style="color: #483d8b;">'60'</span><span style="color: black;">&#41;</span>
<span style="color: black;">&#40;</span><span style="color: #483d8b;">'END_STMNT'</span>, <span style="color: #483d8b;">''</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Pretty easy to understand, right? A great thing about the `Lexer` is that it is easy to subclass. For instance, in a project that I&#039;m doing for a complex template parser, I added in the ability to only do scanning inside specific tags while treating non-tag data as their own type of token. Maybe I&#039;ll cover that in more detail in a future post.</p>
<p><strong>Update:</strong> The custom lexer has been updated to accept a list of tuples as the rules instead of the dict. This is so one can implement an order on the rules.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.evanfosmark.com/2009/02/sexy-lexing-with-python/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Random password generator in Python and Tkinter</title>
		<link>http://www.evanfosmark.com/2009/02/random-password-generator-in-python-and-tkinter/</link>
		<comments>http://www.evanfosmark.com/2009/02/random-password-generator-in-python-and-tkinter/#comments</comments>
		<pubDate>Sun, 15 Feb 2009 00:52:08 +0000</pubDate>
		<dc:creator>Evan</dc:creator>
		
		<category><![CDATA[GUI]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[Python]]></category>

		<category><![CDATA[password generator]]></category>

		<category><![CDATA[Tkinter]]></category>

		<guid isPermaLink="false">http://www.evanfosmark.com/?p=54</guid>
		<description><![CDATA[This is always a fun project. The task? To create a random password of random length. The reason for a password generator obvious: you suck at choosing a password. Let&#039;s start with how to create the actual generator, and then we&#039;ll focus on the presentation.

from random import *
import string
&#160;
# The characters to make up the [...]]]></description>
			<content:encoded><![CDATA[<p>This is always a fun project. The task? To create a random password of random length. The reason for a password generator obvious: you suck at choosing a password. Let&#039;s start with how to create the actual generator, and then we&#039;ll focus on the presentation.</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">random</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">string</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># The characters to make up the random password</span>
chars = <span style="color: #dc143c;">string</span>.<span style="color: black;">ascii_letters</span> + <span style="color: #dc143c;">string</span>.<span style="color: black;">digits</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> random_password<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot; Create a password of random length between 8 and 16
        characters long, made up of numbers and letters.
    &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot;&quot;</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>choice<span style="color: black;">&#40;</span>chars<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>randint<span style="color: black;">&#40;</span><span style="color: #ff4500;">8</span>, <span style="color: #ff4500;">16</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>The above function is pretty easy to follow. What it does is build a generator object that creates a random list of characters between 8 and 16 in length. It then just compresses the list into a string. You can do something as simple as <code>print random_password()</code> and it&#039;ll display a password in the terminal. This, of course, isn&#039;t really the best way of acheiving it. After all, you don&#039;t want to have to navigate the terminal each and every time. So, let&#039;s add in a graphical user interface using the <code>Tkinter</code> window builder:</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">Tkinter</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">random</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">string</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># The characters to make up the random password</span>
chars = <span style="color: #dc143c;">string</span>.<span style="color: black;">ascii_letters</span> + <span style="color: #dc143c;">string</span>.<span style="color: black;">digits</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> random_password<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot; Create a password of random length between 8 and 16
        characters long, made up of numbers and letters.
    &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot;&quot;</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>choice<span style="color: black;">&#40;</span>chars<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>randint<span style="color: black;">&#40;</span><span style="color: #ff4500;">8</span>, <span style="color: #ff4500;">16</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#</span>
<span style="color: #808080; font-style: italic;"># BEGIN GUI CODE</span>
<span style="color: #808080; font-style: italic;">#</span>
&nbsp;
root = Tk<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
root.<span style="color: black;">title</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Password Generator&quot;</span><span style="color: black;">&#41;</span>
root.<span style="color: black;">resizable</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>,<span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
root.<span style="color: black;">minsize</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">300</span>,<span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
&nbsp;
frame = Frame<span style="color: black;">&#40;</span>root<span style="color: black;">&#41;</span>
frame.<span style="color: black;">pack</span><span style="color: black;">&#40;</span>pady=<span style="color: #ff4500;">10</span>, padx=<span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
&nbsp;
content = StringVar<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
updater = <span style="color: #ff7700;font-weight:bold;">lambda</span>:content.<span style="color: #008000;">set</span><span style="color: black;">&#40;</span>random_password<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
gen_btn = Button<span style="color: black;">&#40;</span>frame, text=<span style="color: #483d8b;">&quot;Generate&quot;</span>, command=updater<span style="color: black;">&#41;</span>
gen_btn.<span style="color: black;">config</span><span style="color: black;">&#40;</span>font=<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;sans-serif&quot;</span>, <span style="color: #ff4500;">14</span><span style="color: black;">&#41;</span>,  bg=<span style="color: #483d8b;">&quot;#92CC92&quot;</span><span style="color: black;">&#41;</span>
gen_btn.<span style="color: black;">pack</span><span style="color: black;">&#40;</span>side=LEFT, padx=<span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
&nbsp;
field = Entry<span style="color: black;">&#40;</span>frame, textvariable=content<span style="color: black;">&#41;</span>
field.<span style="color: black;">config</span><span style="color: black;">&#40;</span>fg=<span style="color: #483d8b;">'blue'</span>, font=<span style="color: black;">&#40;</span><span style="color: #483d8b;">'courier'</span>,  <span style="color: #ff4500;">16</span>, <span style="color: #483d8b;">&quot;bold&quot;</span><span style="color: black;">&#41;</span>, justify=<span style="color: #483d8b;">'center'</span><span style="color: black;">&#41;</span>
field.<span style="color: black;">pack</span><span style="color: black;">&#40;</span>fill=BOTH, side=RIGHT, padx=<span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
&nbsp;
root.<span style="color: black;">mainloop</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>The above should be pretty simple to follow. As you can see, pressing the <code>gen_btn</code> activates the <code>updater</code> lambda function which populates the entry field. Here is a sample output:</p>
<p><img src="http://evanfosmark.com/files/pwgen.png" alt="password generator" /></p>
<p>Simple, clean, and easy. This is just a quick project I made for myself and decided to share it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.evanfosmark.com/2009/02/random-password-generator-in-python-and-tkinter/feed/</wfw:commentRss>
		</item>
		<item>
		<title>UNIX Time Clock, in honor of &#034;1234567890 Day&#034;</title>
		<link>http://www.evanfosmark.com/2009/02/unix-timestamp-clock-in-honor-of-1234567890-in-python/</link>
		<comments>http://www.evanfosmark.com/2009/02/unix-timestamp-clock-in-honor-of-1234567890-in-python/#comments</comments>
		<pubDate>Fri, 13 Feb 2009 23:13:40 +0000</pubDate>
		<dc:creator>Evan</dc:creator>
		
		<category><![CDATA[GUI]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[Python]]></category>

		<category><![CDATA[timestamp]]></category>

		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://www.evanfosmark.com/?p=52</guid>
		<description><![CDATA[On this Friday the 13th, we are to witness a very unique moment in history. That is, it is the day when UNIX time will roll over to 1234567890. Here&#039;s a simple Python/Tk app that&#039;ll show the timestamp.

import time
from Tkinter import *
&#160;
class TimestampClock&#40;Frame&#41;:
    def __init__&#40;self, root&#41;:
       [...]]]></description>
			<content:encoded><![CDATA[<p>On this Friday the 13th, we are to witness a very unique moment in history. That is, it is the day when <a href="http://en.wikipedia.org/wiki/Unix_time">UNIX time</a> will roll over to <code>1234567890</code>. Here&#039;s a simple <code>Python/Tk</code> app that&#039;ll show the timestamp.</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">time</span>
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">Tkinter</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> TimestampClock<span style="color: black;">&#40;</span>Frame<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, root<span style="color: black;">&#41;</span>:
        Frame.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, root<span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">pack</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: #dc143c;">time</span> = Label<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, text=<span style="color: #008000;">int</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: #dc143c;">time</span>.<span style="color: black;">config</span><span style="color: black;">&#40;</span>fg=<span style="color: #483d8b;">'red'</span>, font=<span style="color: black;">&#40;</span><span style="color: #483d8b;">'Monospace'</span>, <span style="color: #ff4500;">20</span>, <span style="color: #483d8b;">'bold'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: #dc143c;">time</span>.<span style="color: black;">pack</span><span style="color: black;">&#40;</span>padx=<span style="color: #ff4500;">10</span>, pady=<span style="color: #ff4500;">10</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">update</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> update<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: #dc143c;">time</span>.<span style="color: black;">config</span><span style="color: black;">&#40;</span>text=<span style="color: #008000;">int</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">after</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1000</span>, <span style="color: #008000;">self</span>.<span style="color: black;">update</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">&quot;__main__&quot;</span>:
    root = Tk<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    root.<span style="color: black;">title</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Timestamp-Clock&quot;</span><span style="color: black;">&#41;</span>
    root.<span style="color: black;">wm_attributes</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;-topmost&quot;</span>, <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
    root.<span style="color: black;">resizable</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>,<span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
    root.<span style="color: black;">minsize</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">300</span>,<span style="color: #ff4500;">50</span><span style="color: black;">&#41;</span>
    TimestampClock<span style="color: black;">&#40;</span>root<span style="color: black;">&#41;</span>.<span style="color: black;">mainloop</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>I hope you did something special for the occasion. Me? I was sitting in a computer lab at Chemeketa waiting for the clock to hit that special number. Here is <em>my</em> screen grab of this great event:</p>
<p><img src='/files/1234567890.png' alt='1234567890' /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.evanfosmark.com/2009/02/unix-timestamp-clock-in-honor-of-1234567890-in-python/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Module wsgiref doesn&#039;t work in Python 3.0 - How to fix it</title>
		<link>http://www.evanfosmark.com/2009/01/module-wsgiref-doesnt-work-in-python-30-how-to-fix-it/</link>
		<comments>http://www.evanfosmark.com/2009/01/module-wsgiref-doesnt-work-in-python-30-how-to-fix-it/#comments</comments>
		<pubDate>Sat, 31 Jan 2009 22:33:05 +0000</pubDate>
		<dc:creator>Evan</dc:creator>
		
		<category><![CDATA[Python]]></category>

		<category><![CDATA[WSGI]]></category>

		<category><![CDATA[non-functional]]></category>

		<category><![CDATA[python3.0]]></category>

		<category><![CDATA[wsgiref]]></category>

		<guid isPermaLink="false">http://www.evanfosmark.com/?p=48</guid>
		<description><![CDATA[A very large oddity regarding the newest version of Python is that the wsgiref module is completely broken. Go ahead and try to run the following:

from wsgiref.simple_server import make_server, demo_app
httpd = make_server&#40;'', 8000, demo_app&#41;
httpd.handle_request&#40;&#41;

You should notice that a nice little &#34;ValueError: need more than 1 value to unpack&#34; message when you try to open it [...]]]></description>
			<content:encoded><![CDATA[<p>A very large oddity regarding the newest version of Python is that the <code>wsgiref</code> module is completely broken. Go ahead and try to run the following:</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">from</span> wsgiref.<span style="color: black;">simple_server</span> <span style="color: #ff7700;font-weight:bold;">import</span> make_server, demo_app
httpd = make_server<span style="color: black;">&#40;</span><span style="color: #483d8b;">''</span>, <span style="color: #ff4500;">8000</span>, demo_app<span style="color: black;">&#41;</span>
httpd.<span style="color: black;">handle_request</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>You should notice that a nice little &quot;<code>ValueError: need more than 1 value to unpack</code>&quot; message when you try to open it in your web browser. The main ticket for this bug can be found <a href="http://bugs.python.org/issue4718">here</a>, and it <a href="http://bugs.python.org/file12447/wsgiref.patch">comes with a patch</a>! If you&#039;re running Linux, then the fix is easy. Once you download it simply run this in the command line in the same folder as the patch to issue it:</p>

<div class="wp_syntax"><div class="code"><pre class="bash"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">patch</span> <span style="color: #000000; font-weight: bold;">&lt;</span> wsgiref.<span style="color: #c20cb9; font-weight: bold;">patch</span></pre></div></div>

<p>It will come up with prompts for each of the files to in <code>wsgiref</code> to patch. Simply specify their locations and you&#039;re done!</p>
<p><strong>Update:</strong> Sources have told me that this has been fixed in <a href="http://python.org/download/releases/3.0.1/">Python 3.0.1</a>, so here&#039;s hoping.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.evanfosmark.com/2009/01/module-wsgiref-doesnt-work-in-python-30-how-to-fix-it/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
