<?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>This is my world of code. Take a look around and enjoy yourself.</description>
	<pubDate>Sun, 28 Dec 2008 08:04:39 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
	<language>en</language>
			<item>
		<title>Python WSGI Middleware for automatic Gzipping</title>
		<link>http://www.evanfosmark.com/2008/12/python-wsgi-middleware-for-automatic-gzipping/</link>
		<comments>http://www.evanfosmark.com/2008/12/python-wsgi-middleware-for-automatic-gzipping/#comments</comments>
		<pubDate>Thu, 25 Dec 2008 13:11:52 +0000</pubDate>
		<dc:creator>Evan</dc:creator>
		
		<category><![CDATA[Articles]]></category>

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

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

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

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

		<guid isPermaLink="false">http://www.evanfosmark.com/?p=43</guid>
		<description><![CDATA[<br/>I&#039;ve just started learning Python WSGI (PEP-333) and thought the best way to learn would be to write some WSGI tools myself. Most recently, I chose to write a middleware application that converts all output into valid gzipped data. In this article, I will be demonstrating how my middleware gzipper works and how to implement [...]]]></description>
			<content:encoded><![CDATA[<br/><p>I&#039;ve just started learning <a href="http://www.python.org/dev/peps/pep-0333">Python WSGI (PEP-333)</a> and thought the best way to learn would be to write some WSGI tools myself. Most recently, I chose to write a middleware application that converts all output into valid gzipped data. In this article, I will be demonstrating how my middleware gzipper works and how to implement it.</p>
<p><span id="more-43"></span></p>
<h3>Gzipping arbitrary data</h3>
<p>The first obstacle was that Python has no simple way of converting a normal string into a gzipped string. To do this, let&#039;s build off of the gzip.GzipFile object, which isn&#039;t really designed for this sort of task.</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #808080; font-style: italic;">#!/usr/bin/python2.5</span>
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">gzip</span> <span style="color: #ff7700;font-weight:bold;">import</span> GzipFile
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">StringIO</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> gzip_string<span style="color: black;">&#40;</span><span style="color: #dc143c;">string</span>, compression_level<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot; The `gzip` module didn't provide a way to gzip just a string.
        Had to hack together this. I know, it isn't pretty.
    &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
    fake_file = <span style="color: #dc143c;">StringIO</span>.<span style="color: #dc143c;">StringIO</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    gz_file = GzipFile<span style="color: black;">&#40;</span><span style="color: #008000;">None</span>, <span style="color: #483d8b;">'wb'</span>, compression_level, fileobj=fake_file<span style="color: black;">&#41;</span>
    gz_file.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">string</span><span style="color: black;">&#41;</span>
    gz_file.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> fake_file.<span style="color: black;">getvalue</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>The above function accomplishes this nicely. By using a `StringIO` instance for the `fileobj` in the `GzipFile`, it allows us to make the function <em>think</em> that it is writing to a normal file. Instead, we&#039;re grabbing the content through our fake file.</p>
<h3>Does the client <em>want</em> gzipped output?</h3>
<p>This is a pretty important task. What if the client didn&#039;t even ask for gzipped output? Or worse, what if they can&#039;t even support it? Cases like these must be taken into consideration. We&#039;ll use the following code to check.</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">def</span> parse_encoding_header<span style="color: black;">&#40;</span>header<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot; Break up the `HTTP_ACCEPT_ENCODING` header into a dict of
        the form, {'encoding-name':qvalue}.
    &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
    <span style="color: #dc143c;">encodings</span> = <span style="color: black;">&#123;</span><span style="color: #483d8b;">'identity'</span>:<span style="color: #ff4500;">1.0</span><span style="color: black;">&#125;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">for</span> encoding <span style="color: #ff7700;font-weight:bold;">in</span> header.<span style="color: black;">split</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;">if</span><span style="color: black;">&#40;</span>encoding.<span style="color: black;">find</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;;&quot;</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">&amp;</span>gt<span style="color: #66cc66;">;</span> <span style="color: #ff4500;">-1</span><span style="color: black;">&#41;</span>:
            encoding, qvalue = encoding.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;;&quot;</span><span style="color: black;">&#41;</span>
            encoding = encoding.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
            qvalue = qvalue.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'='</span>, <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
            <span style="color: #ff7700;font-weight:bold;">if</span><span style="color: black;">&#40;</span>qvalue <span style="color: #66cc66;">!</span>= <span style="color: #483d8b;">&quot;&quot;</span><span style="color: black;">&#41;</span>:
                <span style="color: #dc143c;">encodings</span><span style="color: black;">&#91;</span>encoding<span style="color: black;">&#93;</span> = <span style="color: #008000;">float</span><span style="color: black;">&#40;</span>qvalue<span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">else</span>:
                <span style="color: #dc143c;">encodings</span><span style="color: black;">&#91;</span>encoding<span style="color: black;">&#93;</span> = <span style="color: #ff4500;">1</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            <span style="color: #dc143c;">encodings</span><span style="color: black;">&#91;</span>encoding<span style="color: black;">&#93;</span> = <span style="color: #ff4500;">1</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #dc143c;">encodings</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> client_wants_gzip<span style="color: black;">&#40;</span>accept_encoding_header<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot; Check to see if the client can accept gzipped output, and whether
        or not it is even the preferred method. If `identity` is higher, then
        no gzipping should occur.
    &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
    <span style="color: #dc143c;">encodings</span> = parse_encoding_header<span style="color: black;">&#40;</span>accept_encoding_header<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># Do the actual comparisons</span>
    <span style="color: #ff7700;font-weight:bold;">if</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'gzip'</span> <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #dc143c;">encodings</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #dc143c;">encodings</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'gzip'</span><span style="color: black;">&#93;</span> <span style="color: #66cc66;">&amp;</span>gt<span style="color: #66cc66;">;</span>= <span style="color: #dc143c;">encodings</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'identity'</span><span style="color: black;">&#93;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">elif</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'*'</span> <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #dc143c;">encodings</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #dc143c;">encodings</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'*'</span><span style="color: black;">&#93;</span> <span style="color: #66cc66;">&amp;</span>gt<span style="color: #66cc66;">;</span>= <span style="color: #dc143c;">encodings</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'identity'</span><span style="color: black;">&#93;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">False</span></pre></div></div>

<p>What this does is parse the `HTTP_ACCEPT_ENCODING` response header to make sure that it says that gzipping is alright and preferred over no compression. It does this by looking at the qvalue weights for each type of encoding. For more information on this, I suggest you read <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3">RFC 2616, section 14</a> which covers the &#034;Accept-Encoding&#034; header.</p>
<h3>Creating the WSGI middleware</h3>
<p>Alright, now it&#039;s time to use that function an create a <a href="http://www.python.org/dev/peps/pep-0333/#id16">middleware application</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">class</span> Gzipper<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; WSGI middleware to wrap around and gzip all output.
        This automatically adds the content-encoding header.
    &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>, app, compresslevel=<span style="color: #ff4500;">6</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">app</span> = app
        <span style="color: #008000;">self</span>.<span style="color: black;">compresslevel</span> = compresslevel
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__call__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, environ, start_response<span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot; Do the actual work. If the host doesn't support gzip as a proper encoding,
            then simply pass over to the next app on the wsgi stack.
        &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        accept_encoding_header = environ.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;HTTP_ACCEPT_ENCODING&quot;</span>, <span style="color: #483d8b;">&quot;&quot;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span><span style="color: black;">&#40;</span><span style="color: #ff7700;font-weight:bold;">not</span> client_wants_gzip<span style="color: black;">&#40;</span>accept_encoding_header<span style="color: black;">&#41;</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;">app</span><span style="color: black;">&#40;</span>environ, start_response<span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">def</span> _start_response<span style="color: black;">&#40;</span>status, headers, <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; Wrapper around the original `start_response` function.
                The sole purpose being to add the proper headers automatically.
            &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
            headers.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Content-Encoding&quot;</span>, <span style="color: #483d8b;">&quot;gzip&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
            headers.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Vary&quot;</span>, <span style="color: #483d8b;">&quot;Accept-Encoding&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">return</span> start_response<span style="color: black;">&#40;</span>status, headers, <span style="color: #66cc66;">*</span>args, <span style="color: #66cc66;">**</span>kwargs<span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;"># Unfortunately, we can't gzip data chunk-by-chunk, so we need to join up whatever</span>
        <span style="color: #808080; font-style: italic;"># is being sent out first. (Remember, WSGI apps return items which can be iterated.)</span>
        data = <span style="color: #483d8b;">&quot;&quot;</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">app</span><span style="color: black;">&#40;</span>environ, _start_response<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: black;">&#91;</span>gzip_string<span style="color: black;">&#40;</span>data, <span style="color: #008000;">self</span>.<span style="color: black;">compresslevel</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span></pre></div></div>

<p>Before doing any encoding, this checks to make sure the client can even support gzipped output by looking at the &#034;HTTP_ACCEPT_ENCODING&#034; header value. What&#039;s really cool about how this works is that it adds a content-encoding header for you. This, however, shows a weakness in WSGI as the middleware is required to write a new start_response function that wraps around the old one. <a href="http://wsgi.org/wsgi/WSGI_2.0">WSGI 2.0</a> clears this up, but this version isn&#039;t in use yet.</p>
<h3>Using the middleware</h3>
<p>Now, let&#039;s see how we would implement this with a simple &#034;hello world&#034; WSGI application.</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #808080; font-style: italic;">#!/usr/bin/python2.5</span>
<span style="color: #ff7700;font-weight:bold;">from</span> gzipper <span style="color: #ff7700;font-weight:bold;">import</span> Gzipper
<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> WSGIServer, WSGIRequestHandler
&nbsp;
<span style="color: #808080; font-style: italic;">#</span>
<span style="color: #808080; font-style: italic;"># Let's create a simple WSGI application to test out the Gzipper.</span>
<span style="color: #808080; font-style: italic;">#</span>
<span style="color: #ff7700;font-weight:bold;">def</span> test_app<span style="color: black;">&#40;</span>environ, start_response<span style="color: black;">&#41;</span>:
    status = <span style="color: #483d8b;">&quot;200 OK&quot;</span>
    headers = <span style="color: black;">&#91;</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;content-type&quot;</span>, <span style="color: #483d8b;">&quot;text/html&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
    start_response<span style="color: black;">&#40;</span>status, headers<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;Hello world!&quot;</span><span style="color: black;">&#93;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Set up the WSGI server and add the middleware that wraps around</span>
<span style="color: #808080; font-style: italic;"># the actual application.</span>
httpd = WSGIServer<span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">''</span>, <span style="color: #ff4500;">8080</span><span style="color: black;">&#41;</span>, WSGIRequestHandler<span style="color: black;">&#41;</span>
httpd.<span style="color: black;">set_app</span><span style="color: black;">&#40;</span>Gzipper<span style="color: black;">&#40;</span>test_app, compresslevel=<span style="color: #ff4500;">8</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
httpd.<span style="color: black;">serve_forever</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<h3>Final thoughts</h3>
<p>Middleware can be incredibly useful. I, however, have been seeing numerous people approaching it incorrectly by modifying the <code>environ</code> and having the application relying on it. I believe that <a href="http://dirtsimple.org/">DirtSimple.org</a> said it best: <a href="http://dirtsimple.org/2007/02/wsgi-middleware-considered-harmful.html">&#034;If your application requires that API to be present, then it&#039;s not middleware any more!&#034;</a> It would be better to just add that code to a library utilized by the application.</p>
<p>To view all of the code in one block, go to the next page.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.evanfosmark.com/2008/12/python-wsgi-middleware-for-automatic-gzipping/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Lighttpd Directory Generator using mod_dirlisting With Sorting</title>
		<link>http://www.evanfosmark.com/2008/09/lighttpd-directory-generator-using-mod_dirlisting-with-sorting/</link>
		<comments>http://www.evanfosmark.com/2008/09/lighttpd-directory-generator-using-mod_dirlisting-with-sorting/#comments</comments>
		<pubDate>Wed, 03 Sep 2008 21:45:36 +0000</pubDate>
		<dc:creator>Evan</dc:creator>
		
		<category><![CDATA[Lighttpd]]></category>

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

		<category><![CDATA[dir-generator]]></category>

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

		<guid isPermaLink="false">http://www.evanfosmark.com/?p=40</guid>
		<description><![CDATA[<br/>I have put up a new script for those of you who are running Lighttpd. You can find it here. It is a dir-generator that is nearly identical to the default, but with the added bonus that you can sort by name, size, modification time, and file type. One downside is that it doesn&#039;t display [...]]]></description>
			<content:encoded><![CDATA[<br/><p>I have put up a new script for those of you who are running Lighttpd. You can find it <a href='http://www.evanfosmark.com/projects/enhanged-lighttpd-directory-generator-using-mod_dirlisting/'>here</a>. It is a dir-generator that is nearly identical to the default, but with the added bonus that you can sort by name, size, modification time, and file type. One downside is that it doesn&#039;t display mime type like the original does since to do that with PHP, it is required that an extension is insalled.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.evanfosmark.com/2008/09/lighttpd-directory-generator-using-mod_dirlisting-with-sorting/feed/</wfw:commentRss>
		</item>
		<item>
		<title>I Before E, Except After C - Not a rule, but a joke</title>
		<link>http://www.evanfosmark.com/2008/07/i-before-e-except-after-c-not-a-rule/</link>
		<comments>http://www.evanfosmark.com/2008/07/i-before-e-except-after-c-not-a-rule/#comments</comments>
		<pubDate>Fri, 04 Jul 2008 04:59:47 +0000</pubDate>
		<dc:creator>Evan</dc:creator>
		
		<category><![CDATA[English]]></category>

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

		<guid isPermaLink="false">http://www.evanfosmark.com/?p=27</guid>
		<description><![CDATA[<br/>When I was in high school, I always had fun finding exceptions the &#034;I before e, except after c&#034; rule. I found over a hundred on my own back then just by searching around the Internet. It took me hours. Today I decided to take a better approach and wrote up a small script in [...]]]></description>
			<content:encoded><![CDATA[<br/><p>When I was in high school, I always had fun finding exceptions the &#034;I before e, except after c&#034; rule. I found over a hundred on my own back then just by searching around the Internet. It took me hours. Today I decided to take a better approach and wrote up a small script in to do the work for me. All in all, it found 533 exceptions to this rule.</p>
<p><a href="http://www.evanfosmark.com/wp-content/uploads/2008/07/i_before_e.txt">Click here to view the list of exceptions.</a></p>
<p>Yeah, that&#039;s quite a few. If you&#039;re interested in how I did it, continue reading. Don&#039;t worry, I won&#039;t be sad if you came here just for the list.</p>
<p><span id="more-27"></span></p>
<h3>The Rule, Broken Down</h3>
<ol>
<li>If the word contains &#034;ei&#034;, it must be in the form of &#034;cei&#034; or make a long-A sound.</li>
<li>If the word contains &#034;ie&#034;, it cannot be in the form of a &#034;cie&#034;.</li>
</ol>
<h3>The Tools Used</h3>
<ol>
<li>The Python Programming Language</li>
<li>Regular Expressions</li>
<li>A file containing all English words [ <a href="http://java.sun.com/docs/books/tutorial/collections/interfaces/examples/dictionary.txt">get here</a> ]</li>
</ol>
<h3>The Code</h3>
<p>Writing up the code is pretty simple. Basically all it does is open the dictionary file and check each word against the rule. If it doesn&#039;t follow the rule, it logs it in the output file.</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: #808080; font-style: italic;"># Open the input &amp;amp; output file streams</span>
input_handle = <span style="color: #008000;">file</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;dictionary.txt&quot;</span>, <span style="color: #483d8b;">&quot;r&quot;</span><span style="color: black;">&#41;</span>
output_handle = <span style="color: #008000;">file</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;i_before_e.txt&quot;</span>, <span style="color: #483d8b;">&quot;w&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Regexes for removal of valid</span>
bad_ei_regexes = <span style="color: black;">&#91;</span>
&nbsp;
      <span style="color: #808080; font-style: italic;"># Valid case by default</span>
      <span style="color: #dc143c;">re</span>.<span style="color: #008000;">compile</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'cei'</span><span style="color: black;">&#41;</span>, 
&nbsp;
      <span style="color: #808080; font-style: italic;"># Valid case with a long-A sound</span>
      <span style="color: #dc143c;">re</span>.<span style="color: #008000;">compile</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'^(z|p)ei'</span><span style="color: black;">&#41;</span>,
      <span style="color: #dc143c;">re</span>.<span style="color: #008000;">compile</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'(?&lt;!--c|s)hei'</span><span style="color: black;">&#41;</span>,
      <span style="color: #dc143c;">re</span>.<span style="color: #008000;">compile</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'rein'</span><span style="color: black;">&#41;</span>,
      <span style="color: #dc143c;">re</span>.<span style="color: #008000;">compile</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'vei[nl]'</span><span style="color: black;">&#41;</span>,
      <span style="color: #dc143c;">re</span>.<span style="color: #008000;">compile</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'eig(h|n|e)'</span><span style="color: black;">&#41;</span>
<span style="color: black;">&#93;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Iterate through all English words</span>
<span style="color: #ff7700;font-weight:bold;">for</span> line <span style="color: #ff7700;font-weight:bold;">in</span> input_handle:
&nbsp;
    <span style="color: #808080; font-style: italic;"># I comes before e, even when after a c</span>
    <span style="color: #ff7700;font-weight:bold;">if</span><span style="color: black;">&#40;</span>line.<span style="color: black;">find</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'cie'</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">!</span>= <span style="color: #ff4500;">-1</span><span style="color: black;">&#41;</span>:
        output_handle.<span style="color: black;">write</span><span style="color: black;">&#40;</span>line<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">continue</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># Remove all of the long-A sounds</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> regexc <span style="color: #ff7700;font-weight:bold;">in</span> bad_ei_regexes:
        line = regexc.<span style="color: black;">sub</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">''</span>, line<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># E comes before I, without a C, and without long-A sound</span>
    <span style="color: #ff7700;font-weight:bold;">if</span><span style="color: black;">&#40;</span>line.<span style="color: black;">find</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'ei'</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">!</span>= <span style="color: #ff4500;">-1</span><span style="color: black;">&#41;</span>:
        output_handle.<span style="color: black;">write</span><span style="color: black;">&#40;</span>line<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># We're done with the files</span>
input_handle.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
output_handle.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Pretty simple, eh? We let Python and regular expressions do all of the work!</p>
<h3>Bonus: Words that have Qs that aren&#039;t followed by Us</h3>
<p>Scrabble players rejoice! I just compiled a list of words that contain a Q that isn&#039;t followed by a U. I did it in the same way that I found exceptions to the &#034;I before E, except after C&#034; rule. This was just for fun. I don&#039;t even play Scrabble. </p>
<p><a href='http://www.evanfosmark.com/wp-content/uploads/2008/07/special_q_cases.txt'>Click here to view the list.</a> (There are 29 words total)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.evanfosmark.com/2008/07/i-before-e-except-after-c-not-a-rule/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Planet SEO, Great Resource to Web Developers</title>
		<link>http://www.evanfosmark.com/2008/06/planet-seo-great-resource-to-web-developers/</link>
		<comments>http://www.evanfosmark.com/2008/06/planet-seo-great-resource-to-web-developers/#comments</comments>
		<pubDate>Tue, 01 Jul 2008 00:43:57 +0000</pubDate>
		<dc:creator>Evan</dc:creator>
		
		<category><![CDATA[News]]></category>

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

		<category><![CDATA[planet seo]]></category>

		<category><![CDATA[search engines]]></category>

		<guid isPermaLink="false">http://www.evanfosmark.com/?p=23</guid>
		<description><![CDATA[<br/>A friend of mine recently embarked on creating a new website centered around search engine optimization known as Planet SEO (www.planet-seo.org). I really have to hand it to him. He&#039;s made quite a site. He managed to write up the code to run the site in a very short amount of time and kept to [...]]]></description>
			<content:encoded><![CDATA[<br/><p>A friend of mine recently embarked on creating a new website centered around search engine optimization known as <a href="http://planet-seo.org/">Planet SEO</a> (<small>www.planet-seo.org</small>). I really have to hand it to him. He&#039;s made quite a site. He managed to write up the code to run the site in a very short amount of time and kept to a schedule very well. <small>(That&#039;s something I have a problem with.)</small></p>
<p>The site&#039;s service is simple and incredibly useful: it reads multiple feeds from websites that all deal with search engine optimization (SEO) and provides all of them at once. Combine the fact that <a href="http://planet-seo.org/">Planet SEO</a> already has a fair number of website feeds that it reads from, it makes sense for it to be the first spot to look at regarding SEO news &amp; techniques.</p>
<p>This seems to be a great resource for those of us who aren&#039;t skilled in SEO. I&#039;m really hoping that it&#039;ll help me expand my knowledge enough to where evanfosmark.com gets higher traffic from search engines.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.evanfosmark.com/2008/06/planet-seo-great-resource-to-web-developers/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Simple Output Buffering in Python</title>
		<link>http://www.evanfosmark.com/2008/06/simple-output-buffering-in-python/</link>
		<comments>http://www.evanfosmark.com/2008/06/simple-output-buffering-in-python/#comments</comments>
		<pubDate>Mon, 23 Jun 2008 11:25:53 +0000</pubDate>
		<dc:creator>Evan</dc:creator>
		
		<category><![CDATA[Articles]]></category>

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

		<category><![CDATA[output buffering]]></category>

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

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

		<guid isPermaLink="false">http://www.evanfosmark.com/?p=22</guid>
		<description><![CDATA[<br/>Recently,  I needed a quick and simple solution to buffer output of a few Python scripts. When  I went to look for a module, I was astonished to find that one didn&#039;t exist. I quickly decided to change this. In this article, we&#039;ll discuss simple ways to buffer output using StringIO and I&#039;ll [...]]]></description>
			<content:encoded><![CDATA[<br/><p>Recently,  I needed a quick and simple solution to buffer output of a few Python scripts. When  I went to look for a module, I was astonished to find that one didn&#039;t exist. I quickly decided to change this. In this article, we&#039;ll discuss simple ways to buffer output using StringIO and I&#039;ll introduce my <a href="http://www.evanfosmark.com/projects/output-buffering-module-for-python/">output buffering module</a> that I constructed.</p>
<p><span id="more-22"></span></p>
<h3>Why Buffer Output?</h3>
<p>Good question. It&#039;s different for everybody. For me, I needed it so that I could allow plugins to be built that could seamlessly print output and have my plugin manager be able to capture and parse the data rather than immediately printing it to <small>stdout</small>. Other cases include situations where one would want to gzip all output or implement a &#034;just-in-time&#034; word filter.</p>
<h3>Possible Solutions</h3>
<p>The way that I see output buffering implemented most often by others is through replacing <small>sys.stdout</small> with an instance of a class which has a </small>write</small> method. This works for some cases, but what many forget is that <small>stdout</small> is considered a file, and thus has the same methods available as a file does. So, implementing only the <small>write</small> method will work for few cases, but can and will break code in other places. It&#039;s best to never even consider this as a solution. I just mention it so people using it will realize that they&#039;re using a weak solution.</p>
<p>The quickest solution to get around this is to use an instance of the <small>StringIO</small> class as a replacement for <small>sys.stdout</small>, but even this has its limitations. For instance, automatic function callbacks and automatic stdout replacement are out of the question.</p>
<p>The best solution is to use something like my <a href="http://www.evanfosmark.com/projects/output-buffering-module-for-python/">output buffering module</a>. It provides a very robust OutputBuffer object that can suit most needs.</p>
<h3>The Quick Solution (using StringIO)</h3>
<p>Using <small>StringIO</small> may not be a very robust choice, but it gets the job done when you need to hack something together really quickly.</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">StringIO</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
&nbsp;
old_stdout = <span style="color: #dc143c;">sys</span>.<span style="color: black;">stdout</span>  <span style="color: #808080; font-style: italic;"># Need to save it for later</span>
<span style="color: #dc143c;">sys</span>.<span style="color: black;">stdout</span> = <span style="color: #dc143c;">StringIO</span>.<span style="color: #dc143c;">StringIO</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>  <span style="color: #808080; font-style: italic;"># Replace stdout with StringIO instance</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Hello, world!&quot;</span>  <span style="color: #808080; font-style: italic;"># Will not print. Is instead saved to the buffer.</span>
&nbsp;
data = <span style="color: #dc143c;">sys</span>.<span style="color: black;">stdout</span>.<span style="color: black;">getvalue</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>  <span style="color: #808080; font-style: italic;"># Retrieve the buffered data.</span>
<span style="color: #dc143c;">sys</span>.<span style="color: black;">stdout</span> = old_stdout  <span style="color: #808080; font-style: italic;"># Set stdout to the real one. ( stops buffering )</span></pre></div></div>

<p>There are a number of problems to doing output buffering this way. Most notably, you have to keep a pointer to the original <small>stdout</small> instance the entire time, otherwise you&#039;re not able to print ever again for the entire script. Yeah, that&#039;s a pretty big deal.</p>
<h3>The Robust Solution (using the <a href="http://www.evanfosmark.com/projects/output-buffering-module-for-python/">ob</a> module)</h3>
<p>The following solution uses a module that I built myself. (Available <a href="http://www.evanfosmark.com/projects/output-buffering-module-for-python/">here</a>)</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> ob
buf = ob.<span style="color: black;">OutputBuffer</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
buf.<span style="color: black;">start</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>  <span style="color: #808080; font-style: italic;"># Begin output buffering.</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Hello, world!&quot;</span>  <span style="color: #808080; font-style: italic;"># Will not print. Is instead saved to the buffer.</span>
&nbsp;
buf.<span style="color: black;">stop</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>  <span style="color: #808080; font-style: italic;"># Stop output buffering.</span>
data = buf.<span style="color: black;">getvalue</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>  <span style="color: #808080; font-style: italic;"># Retrieve the buffered data</span></pre></div></div>

<p>Now, using the <a href="http://www.evanfosmark.com/projects/output-buffering-module-for-python/">ob</a> module has a number of improvements over simply using <small>StringIO</small>. Most notably, it allows you to never have to directly manipulate <small>sys.stdout</small> - it does everything for you. Additionally, if you lose a reference to your <small>OutputBuffer</small> object, not to worry; the <small>ob</small> module provides a way of getting the buffer object:</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> ob
buf = ob.<span style="color: black;">get_top</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>  <span style="color: #808080; font-style: italic;"># Retrieve the top-most buffer.</span></pre></div></div>

<p>At that point, you can again work with the OutputBuffer instance without ever having to directly touch <small>sys.stdout</small> or perform any tests. I&#039;d suggest going to the <a href="http://www.evanfosmark.com/projects/output-buffering-module-for-python/">output buffering module</a> page and reading up on what else it can provide you. </p>
<h3>Final Thoughts</h3>
<p>Output buffering is a powerful tool. I&#039;m incredibly surprised that Python didn&#039;t already provide a solution to it, but at the same time I&#039;m a bit glad because it allowed me to get creative and build my own solution. If you&#039;re going to do output buffering, don&#039;t be lazy. At the very least use the <small>StringIO</small> solution, but if you want to do it right, then use the <small>ob</small> module or something equivalent.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.evanfosmark.com/2008/06/simple-output-buffering-in-python/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Near-Implicit String Interpolation in Python</title>
		<link>http://www.evanfosmark.com/2008/06/string-interpolation-in-python/</link>
		<comments>http://www.evanfosmark.com/2008/06/string-interpolation-in-python/#comments</comments>
		<pubDate>Wed, 18 Jun 2008 08:27:54 +0000</pubDate>
		<dc:creator>Evan</dc:creator>
		
		<category><![CDATA[Articles]]></category>

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

		<category><![CDATA[string interpolation]]></category>

		<category><![CDATA[string.format]]></category>

		<category><![CDATA[string.Template]]></category>

		<guid isPermaLink="false">http://www.evanfosmark.com/?p=17</guid>
		<description><![CDATA[<br/>A great thing about working in Python is that it provides you with numerous methods of string formatting and interpolation. Unfortunately, it isn&#039;t as implicit as it is in PHP, where just using double-quotes will parse the string. In Python at the very minimum string interpolation requires the use of the modulo operator (%) or [...]]]></description>
			<content:encoded><![CDATA[<br/><p>A great thing about working in Python is that it provides you with numerous methods of string formatting and interpolation. Unfortunately, it isn&#039;t as implicit as it is in PHP, where just using double-quotes will parse the string. In Python at the very minimum string interpolation requires the use of the <a href="http://docs.python.org/lib/typesseq-strings.html">modulo operator (%)</a> or a custom-built function. In this article, we&#039;ll discuss building a string interpolation function using the <small>string.Template</small> object and Python 3.0&#039;s <small>format</small> method.</p>
<p><span id="more-17"></span></p>
<h3>Using string.Template</h3>
<p>This is by far my favorite way of string interpolation. It allows you to use the built in template system that comes in the <small>string</small> module, thus making the code short and sweet.</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">string</span> <span style="color: #ff7700;font-weight:bold;">import</span> Template
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> interpolate<span style="color: black;">&#40;</span>s<span style="color: black;">&#41;</span>:
	template = Template<span style="color: black;">&#40;</span>s<span style="color: black;">&#41;</span>
	frame = <span style="color: #dc143c;">sys</span>._getframe<span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">return</span> template.<span style="color: black;">substitute</span><span style="color: black;">&#40;</span><span style="color: #66cc66;">**</span>frame.<span style="color: black;">f_locals</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Here is an example of use:</span>
name = <span style="color: #483d8b;">&quot;mary&quot;</span>
animal = <span style="color: #483d8b;">&quot;lamb&quot;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> interpolate<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;${name} had a little ${animal}&quot;</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># prints &quot;mary had a little lamb&quot;</span></pre></div></div>

<p>This function starts off by building a template object based on the string provided to the function. Then it grabs the last frame on the call stack so we can get access to the variables being used one step back when we called the function. Lastly, it performs the <small>substitute</small> method on the template object and returns the interpolated string. By using <small>substitute</small>, it&#039;ll throw a <small>KeyError</small> if a variable is found in the string that doesn&#039;t exist. if this is undesired, you can change it to use <small>safe_substitute</small> instead, which suppresses the <small>KeyError</small>.</p>
<h3>Python 3.0 and string.format</h3>
<p>In Python 3.0, a new method for string interpolation was introduced onto strings directly. It&#039;s called <small>format</small>. It was brought about through <a href="http://www.python.org/dev/peps/pep-3101/">PEP-3101</a>. I&#039;d say that in most cases, a function for implicit interpolation wouldn&#039;t be necessary with the <small>format</small> method. The format method works as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Mary had a little {animal}&quot;</span>.<span style="color: black;">format</span><span style="color: black;">&#40;</span>animal=<span style="color: #483d8b;">&quot;dog&quot;</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># prints: &quot;Mary had a little dog&quot;</span></pre></div></div>

<p>It uses braces with the name of the parameter inside. To set the parameter in the string, pass in a named parameter into the <small>format</small> method . Now, if you want a solution that functions similar to the <small>interpolate</small> function we built above, then here it is:</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #808080; font-style: italic;"># Python 3.0 required</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
<span style="color: #ff7700;font-weight:bold;">def</span> interpolate<span style="color: black;">&#40;</span>s<span style="color: black;">&#41;</span>:
	frame = <span style="color: #dc143c;">sys</span>._getframe<span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">return</span><span style="color: black;">&#40;</span>s.<span style="color: black;">format</span><span style="color: black;">&#40;</span><span style="color: #66cc66;">**</span>frame.<span style="color: black;">f_locals</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Example of use:</span>
name = <span style="color: #483d8b;">&quot;mary&quot;</span>
animal = <span style="color: #483d8b;">&quot;lamb&quot;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> interpolate<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;{name} had a little {animal}&quot;</span><span style="color: black;">&#41;</span>   <span style="color: #808080; font-style: italic;"># prints &quot;mary had a little lamb&quot;</span></pre></div></div>

<p>This code works incredibly similar to the way using <small>string.Template</small>, as you can see. From an outsider&#039;s perspective, the only difference in the interpolate function is a difference in the template format. For instance, the <small>string.Template</small> way of interpolating uses <small>${animal}</small> or <small>$animal</small>, while the <small>format</small> method uses <small>{animal}</small>.</p>
<h3>Final Thoughts</h3>
<p>Implicit string interpolation like this can be a very useful tool, but it doesn&#039;t need to be overused. In most situations, using the <small>format</small> method (if you&#039;re using Python 3.0), and the built-in <a href="http://docs.python.org/lib/typesseq-strings.html">modulo operator</a> will suffice for most string manipulation and formatting needs. Happy interpolating!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.evanfosmark.com/2008/06/string-interpolation-in-python/feed/</wfw:commentRss>
		</item>
		<item>
		<title>XOR Encryption With Python</title>
		<link>http://www.evanfosmark.com/2008/06/xor-encryption-with-python/</link>
		<comments>http://www.evanfosmark.com/2008/06/xor-encryption-with-python/#comments</comments>
		<pubDate>Tue, 17 Jun 2008 06:25:28 +0000</pubDate>
		<dc:creator>Evan</dc:creator>
		
		<category><![CDATA[Articles]]></category>

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

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

		<category><![CDATA[xor cipher]]></category>

		<guid isPermaLink="false">http://www.evanfosmark.com/?p=14</guid>
		<description><![CDATA[<br/>XOR encryption is a great solution to go with when a task requires that a piece of data is encrypted with a key when one doesn&#039;t have the means to use a more well-rounded algorithm. I&#039;ve used it on a few occasions with great success. In this document, we&#039;ll discuss how to code the XOR [...]]]></description>
			<content:encoded><![CDATA[<br/><p>XOR encryption is a great solution to go with when a task requires that a piece of data is encrypted with a key when one doesn&#039;t have the means to use a more well-rounded algorithm. I&#039;ve used it on a few occasions with great success. In this document, we&#039;ll discuss how to code the XOR cipher in Python and we&#039;ll cover the pros and cons of using it.</p>
<p><span id="more-14"></span></p>
<h3>The Code</h3>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">itertools</span> <span style="color: #ff7700;font-weight:bold;">import</span> izip, cycle
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> xor_crypt_string<span style="color: black;">&#40;</span>data, key<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">''</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: #008000;">chr</span><span style="color: black;">&#40;</span><span style="color: #008000;">ord</span><span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span> ^ <span style="color: #008000;">ord</span><span style="color: black;">&#40;</span>y<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">for</span> <span style="color: black;">&#40;</span>x,y<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">in</span> izip<span style="color: black;">&#40;</span>data, cycle<span style="color: black;">&#40;</span>key<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>The code for XOR string encryption is surprisingly simple since the whole concept of XOR encryption is simple. Basically, it iterates over a piece of data, and for every letter, it XORs it with the key. In this function, it is done in a list comprehension. Once the list is built, it gets joined into a string and returned. Encryption and decryption both use the same function.</p>
<h3>The Good</h3>
<p>XOR encryption is a very quick way to encrypt/decrypt strings based on a key. They&#039;re perfect for cases where you&#039;d like to transfer data safely <small>(but not <em>too safely</em>. Read the next section)</small>. Furthermore, the XOR cipher needs only one function that does both encrypting and decrypting. For instance, in the above code if you encrypt something and then feed it directly back into the function with the same key, it&#039;ll produce the unencrypted data. Also, the XOR cipher is considered the only perfect encryption method under the proper scheme for a one-time pad. <small>(There are exceptions. Keep reading&#8230;)</small></p>
<h3>The Bad</h3>
<p>Okay, remember when I said that it can be a perfect encryption? Well, that comes with a few limitations. Namely, the key must be completely random, the key must be the length of the data being encrypted, and the key can be used once and only once. If those rules aren&#039;t followed, then the cipher can be broken using <a href="http://en.wikipedia.org/wiki/Frequency_analysis">frequency analysis</a>.</p>
<h3>The Right Way</h3>
<p>Use the <a href="http://www.amk.ca/python/code/crypto.html">Python Cryptography Toolkit</a>. It provides you with plenty of other methods to use instead of XOR encryption. A manual can be found <a href="http://www.amk.ca/python/writing/pycrypt/">here</a>. Of course, doing it this way causes your code to become less portable since anybody you send it to will also be required to have the toolkit installed.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.evanfosmark.com/2008/06/xor-encryption-with-python/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Ternary Operator in Python - People got Clever</title>
		<link>http://www.evanfosmark.com/2008/06/ternary-operator-in-python-people-got-clever/</link>
		<comments>http://www.evanfosmark.com/2008/06/ternary-operator-in-python-people-got-clever/#comments</comments>
		<pubDate>Mon, 16 Jun 2008 11:09:21 +0000</pubDate>
		<dc:creator>Evan</dc:creator>
		
		<category><![CDATA[Articles]]></category>

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

		<category><![CDATA[ternary operator]]></category>

		<guid isPermaLink="false">http://www.evanfosmark.com/?p=15</guid>
		<description><![CDATA[<br/>The ternary operator can be incredibly useful in numerous situations in Python, which is why I&#039;m surprised that prior to Python 2.5, there was no standard way of using one. In this article, we&#039;ll cover the ternary operator that was added in Python 2.5 along with the numerous ways that people emulated the operator before [...]]]></description>
			<content:encoded><![CDATA[<br/><p>The ternary operator can be incredibly useful in numerous situations in Python, which is why I&#039;m surprised that prior to Python 2.5, there was no <i>standard</i> way of using one. In this article, we&#039;ll cover the ternary operator that was added in Python 2.5 along with the numerous ways that people emulated the operator before then.</p>
<p><span id="more-15"></span></p>
<h3>Python 2.5 or Greater</h3>
<p>When <a title="Python Enhancment Proposal: 308" href="http://www.python.org/dev/peps/pep-0308/" target="_blank">PEP 308</a> was created, it finally added the ternary operator into the release of Python 2.5. Here is the syntax that Guido chose:</p>

<div class="wp_syntax"><div class="code"><pre class="python">x = trueValue <span style="color: #ff7700;font-weight:bold;">if</span> condition <span style="color: #ff7700;font-weight:bold;">else</span> falseValue</pre></div></div>

<p>By doing it this way, it makes it easy to follow it as trueValue to be the normal and falseValue to be the less common case. Here is an example:</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;The temperature is %s freezing.&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;below&quot;</span> <span style="color: #ff7700;font-weight:bold;">if</span> temp <span style="color: #66cc66;">&lt;</span> <span style="color: #ff4500;">32</span> <span style="color: #ff7700;font-weight:bold;">else</span> <span style="color: #483d8b;">&quot;above&quot;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>In this example, Python will print to the console <small>&#034;The temperature is below freezing.&#034;</small> if the variable <small>temp</small> is less than 32. Otherwise, it prints <small>&#034;The temperature is below freezing.&#034;</small></p>
<h3>Python 2.4 or Less</h3>
<p>People actually got rather clever and managed to create two ternary &#034;operators&#034; before Python 2.5 came to be. One relies on tuples and the other relies on rather funky logic.</p>

<div class="wp_syntax"><div class="code"><pre class="python">x = <span style="color: black;">&#40;</span>falseValue, trueValue<span style="color: black;">&#41;</span><span style="color: black;">&#91;</span>condition<span style="color: black;">&#93;</span></pre></div></div>

<p>The above code is the way I see used more out in the wild with older scripts. It works based on the fact that a condition evaluated to True or False, which is equivalent to 1 or 0 respectively. Once the condition is evaluated, it gets used as an index for the tuple being used. Here is another less common way:</p>

<div class="wp_syntax"><div class="code"><pre class="python">x = condition <span style="color: #ff7700;font-weight:bold;">and</span> trueValue <span style="color: #ff7700;font-weight:bold;">or</span> falseValue</pre></div></div>

<p>This version works because if the condition evaluates to True (or 1), then trueValue will be chosen since &#039;anding&#039; the two won&#039;t return False, and thus will select the trueValue. The opposite is true when the &#039;anding&#039; results in False, at which point the other value in the &#039;or&#039; will be chosen. This version is highly discouraged because it is impossible to have trueValue be equal to False since it&#039;ll break the logic. There are a few workarounds for this, but it only makes it more confusing.</p>
<h3>Background</h3>
<p>In my opinion, he chose the best of those that were available. It continues Python&#039;s long history of readability and the zen of python (if you don&#039;t know what that is, type &#039;<em>import this</em>&#039; into the python console); however, there were numerous styles that the community proposed:</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">if</span> C then x <span style="color: #ff7700;font-weight:bold;">else</span> y
<span style="color: black;">&#40;</span><span style="color: #ff7700;font-weight:bold;">if</span> C: x <span style="color: #ff7700;font-weight:bold;">else</span>: y<span style="color: black;">&#41;</span>
C <span style="color: #66cc66;">?</span> x : y
C <span style="color: #66cc66;">?</span> x <span style="color: #66cc66;">!</span> y
cond<span style="color: black;">&#40;</span>C, x, y<span style="color: black;">&#41;</span>
C then x <span style="color: #ff7700;font-weight:bold;">else</span> y
C <span style="color: #66cc66;">?</span> x <span style="color: #ff7700;font-weight:bold;">else</span> y
C -<span style="color: #66cc66;">&gt;</span> <span style="color: black;">&#40;</span>x, y<span style="color: black;">&#41;</span></pre></div></div>

<p>Yes, yes, I know. Most of these are ugly and few actually represent what a Python junkie would enjoy using, which is why I&#039;m glad that Guido chose what he did over the standard in most languages:</p>

<div class="wp_syntax"><div class="code"><pre class="c">x <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>condition<span style="color: #009900;">&#41;</span>? trueValue <span style="color: #339933;">:</span> falseValue</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.evanfosmark.com/2008/06/ternary-operator-in-python-people-got-clever/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
