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

<channel>
	<title>Tsukasa's ramblings &#187; python</title>
	<atom:link href="http://blag.tsukasa.net.au/category/programming/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://blag.tsukasa.net.au</link>
	<description>Tech ramblings of Greg Darke, a University of Sydney student</description>
	<lastBuildDate>Fri, 28 May 2010 12:40:26 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>NCSS Python Golf</title>
		<link>http://blag.tsukasa.net.au/2010/01/03/ncss-python-golf/</link>
		<comments>http://blag.tsukasa.net.au/2010/01/03/ncss-python-golf/#comments</comments>
		<pubDate>Sun, 03 Jan 2010 13:49:52 +0000</pubDate>
		<dc:creator>Tsukasa</dc:creator>
				<category><![CDATA[golf]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[NCSS]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[python golf]]></category>

		<guid isPermaLink="false">http://blag.tsukasa.net.au/?p=159</guid>
		<description><![CDATA[It is time again for me to run a Python Golf challenge&#8230; The aim? To write a python program that solves a problem in the least number of bytes (of source code).
The first of a few problems is:
Given a list of words on stdin (one per line), find the words that have the largest number [...]]]></description>
			<content:encoded><![CDATA[<p>It is time again for me to run a Python Golf challenge&#8230; The aim? To write a python program that solves a problem in the least number of bytes (of source code).</p>
<p>The first of a few problems is:</p>
<p>Given a list of words on stdin (one per line), find the words that have the largest number of <a href="http://en.wikipedia.org/wiki/Anagram">anagrams</a> in that list.</p>
<p>Print all of the words that have the meet the criteria of having the largest number anagrams (One per line, in alphabetical order).</p>
<p>This competition has now finished. The winner was Nick Cooper at 103 bytes, with the following awesome solution:</p>
<pre class="brush: python;">
import sys
s=sorted
o=s(sys.stdin)
r=map(s,o)
d=map(r.count,r)
for e,t in zip(o,d):print e*(max(d)==t),
</pre>
<p><!-- more --></p>
<p>Input:<br />
<code>caret<br />
crate<br />
react<br />
trace<br />
ester<br />
reset<br />
steer<br />
terse<br />
organ<br />
groan</code></p>
<p>Output:<br />
<code>caret<br />
crate<br />
ester<br />
react<br />
reset<br />
steer<br />
terse<br />
trace</code></p>
]]></content:encoded>
			<wfw:commentRss>http://blag.tsukasa.net.au/2010/01/03/ncss-python-golf/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Default email addresses for contacts in OSX</title>
		<link>http://blag.tsukasa.net.au/2009/04/16/default-email-addresses-for-contacts-in-osx/</link>
		<comments>http://blag.tsukasa.net.au/2009/04/16/default-email-addresses-for-contacts-in-osx/#comments</comments>
		<pubDate>Thu, 16 Apr 2009 12:53:48 +0000</pubDate>
		<dc:creator>Tsukasa</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://blag.tsukasa.net.au/?p=83</guid>
		<description><![CDATA[Have you ever wondered how OSX decides what email address to use when you email a contact? A lot of the time, you get to choose, but there are times when the system decides for itself&#8230; Such as when you are using iCal to send out event invitations.
There is actually a way that the system [...]]]></description>
			<content:encoded><![CDATA[<p>Have you ever wondered how OSX decides what email address to use when you email a contact? A lot of the time, you get to choose, but there are times when the system decides for itself&#8230; Such as when you are using iCal to send out event invitations.</p>
<p>There is actually a way that the system decides on which address to pick, and this is even a public API. The problem though, is that Address Book does not expose this interface to the user.</p>
<p>So, after some researching (to find the actual way this is implemented), I developed a small (command line) tool to change the default email address for a user. This tool, takes the form of a small python program, using the pyobjc bindings to the AddressBook framework.</p>
<p>The source code is avaliable from my repository in the stuff repository under the <a href="http://tsukasa.net.au/~hg/greg_stuff/file/b6b0e9bc226d/osx/addressbook/edit_default_address.py">osx/addressbook/edit_default_address.py</a> path.</p>
<p>You will also find in the same folder: a <a href="http://tsukasa.net.au/~hg/greg_stuff/file/b6b0e9bc226d/osx/addressbook/export_to_alias.py">script to create a mutt alias file from the system address book</a>, and a <a href="http://tsukasa.net.au/~hg/greg_stuff/file/b6b0e9bc226d/osx/addressbook/make_international.py">script to convert all of the phone numbers into the international format (from the Australian format)</a>.</p>
<p>These three programs form the basis of a good set of examples of the python objective-c bridge for the AddressBook format.</p>
]]></content:encoded>
			<wfw:commentRss>http://blag.tsukasa.net.au/2009/04/16/default-email-addresses-for-contacts-in-osx/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Making RSS less crap &#8211; Feed Fixer</title>
		<link>http://blag.tsukasa.net.au/2009/03/09/making-rss-less-crap-feed-fixer/</link>
		<comments>http://blag.tsukasa.net.au/2009/03/09/making-rss-less-crap-feed-fixer/#comments</comments>
		<pubDate>Mon, 09 Mar 2009 03:28:18 +0000</pubDate>
		<dc:creator>Tsukasa</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[rss]]></category>

		<guid isPermaLink="false">http://blag.tsukasa.net.au/?p=71</guid>
		<description><![CDATA[As some people know, I read a lot of RSS feeds. I am currently subscribed to 69 separate feeds, and this number is slowly growing.
Something that I am starting to notice, is that quite a few websites/blogs don&#8217;t give you the whole story within the RSS feed. This is quite annoying, I read a lot [...]]]></description>
			<content:encoded><![CDATA[<p>As some people know, I read a lot of RSS feeds. I am currently subscribed to 69 separate feeds, and this number is slowly growing.</p>
<p>Something that I am starting to notice, is that quite a few websites/blogs don&#8217;t give you the whole story within the RSS feed. This is quite annoying, I read a lot of things while I am mobile (read with limited Internet connectivity), so clicking on each article&#8217;s link before I leave for the train is just annoying.</p>
<p>I have finally gotten around to creating a (web) service that will take these said feeds, and &#8216;fix&#8217; them. To do this, I take the RSS feed that is provided by the website/blog, and grab the full page that is linked from it. Then I run a <a href="http://www.w3.org/TR/xpath">xpath</a> query over this page and dump the result of it into the the output RSS feed.</p>
<p>You can grab the latest version of the service from it&#8217;s <a href="http://tsukasa.net.au/~hg/feed_fixer">development repository</a> at <a href="http://tsukasa.net.au/~hg/feed_fixer">http://tsukasa.net.au/~hg/feed_fixer</a>, or you may use my <a href="http://tsukasa.net.au/feed_fixer">installed version</a> at <a href="http://tsukasa.net.au/feed_fixer">http://tsukasa.net.au/feed_fixer</a>.</p>
<p>Remember that I am actually using this service, so <em>DO NOT</em> delete any of the entries that are there.</p>
<p>Some features that will be coming in a future version will be the ability to have &#8216;hidden&#8217; feeds (with a special key to gain access to the feed), the ability to add feeds requiring a password, the ability to add password protected trac feeds (as they decided to require user&#8217;s to have a cookie). Feature requests are welcome.</p>
]]></content:encoded>
			<wfw:commentRss>http://blag.tsukasa.net.au/2009/03/09/making-rss-less-crap-feed-fixer/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Multiplayer Snake</title>
		<link>http://blag.tsukasa.net.au/2009/01/05/multiplayer-snake/</link>
		<comments>http://blag.tsukasa.net.au/2009/01/05/multiplayer-snake/#comments</comments>
		<pubDate>Sun, 04 Jan 2009 14:04:11 +0000</pubDate>
		<dc:creator>Tsukasa</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://blag.tsukasa.net.au/?p=57</guid>
		<description><![CDATA[Well, here we have yet another challenge&#8230; Though this one does not involve any prizes, as I want my students at the NCSS camp to actually do their projects and not spend all their time on my competition.
So, what is has to be done here? Well it is quite simple really&#8230; Design a bot to [...]]]></description>
			<content:encoded><![CDATA[<p>Well, here we have yet another challenge&#8230; Though this one does not involve any prizes, as I want my students at the <a href="http://www.ncss.edu.au/">NCSS</a> camp to actually do their projects and not spend all their time on my competition.</p>
<p>So, what is has to be done here? Well it is quite simple really&#8230; Design a bot to play snake for you. I am forcing people who submit to write their solution in python (though there is no real need, other than I am teaching python at the moment). The input/output of the program is quite simple&#8230; The output consists of one of the following four letters: &#8216;U&#8217;, &#8216;D&#8217;, &#8216;L&#8217;, &#8216;R&#8217; (corresponding to Up, Down, Left and Right).</p>
<p>The input consists of the following:<br />
<code>width height snake_id<br />
<i>board layout</i></code><br />
An example is:<br />
<code>7 7 A<br />
..*....<br />
..A....<br />
..a....<br />
..a....<br />
.......<br />
.......<br />
..Bbbb.</code></p>
<p>Where <code>*</code> is an apple, <code>.</code> is an empty cell, the uppercase letters are the head&#8217;s of the snakes, with the lowercase being the body.</p>
<p>Code for the engine is located in my repository under the <a href="http://tsukasa.net.au/~hg/snakes">snake project</a>. Submissions should be made to my email address, or in person.</p>
<p>The initial competition will have bots go up against each other, with no time limit given per move. The later rounds will ensure that all bots get equal cpu time (by running the faster bots more often).</p>
]]></content:encoded>
			<wfw:commentRss>http://blag.tsukasa.net.au/2009/01/05/multiplayer-snake/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PyGolf &#8211; Winner of competition 1</title>
		<link>http://blag.tsukasa.net.au/2008/11/29/pygolf-winner-of-competition-1/</link>
		<comments>http://blag.tsukasa.net.au/2008/11/29/pygolf-winner-of-competition-1/#comments</comments>
		<pubDate>Sat, 29 Nov 2008 12:15:27 +0000</pubDate>
		<dc:creator>Tsukasa</dc:creator>
				<category><![CDATA[golf]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://blag.tsukasa.net.au/?p=55</guid>
		<description><![CDATA[Well the deadline has come and passed, and we have a clear winner. That winner is Katie. Her solution comes in at a tiny 121 characters, with the closest solution coming from Tim with 187 characters.
The main difference came down to the fact that Katie decided to avoid using the regular expression that was given [...]]]></description>
			<content:encoded><![CDATA[<p>Well the deadline has come and passed, and we have a clear winner. That winner is <a rel="friend met" href="http://katharos.id.au/blog/">Katie</a>. Her solution comes in at a tiny 121 characters, with the closest solution coming from Tim with 187 characters.</p>
<p>The main difference came down to the fact that Katie decided to avoid using the regular expression that was given in the sample, and just parse the lines with <code>str.split</code>.<br />
So without much more talking, here is the winning solution.<br />
<!-- File:081129_katies_winning_solution.py -->
<div class="codeBlock">
<pre class="VimPre"><span class="VimPreProc">import</span> sys
s=sum(([l[0]]*int(l[-1])<span class="VimStatement">for</span> l <span class="VimStatement">in</span> map(str.split,sys.stdin)<span class="VimStatement">if</span> l[-1]!=<span class="VimNormal">'</span><span class="VimConstant">-</span><span class="VimNormal">'</span>),[])
<span class="VimStatement">for</span> x <span class="VimStatement">in</span> set(s):<span class="VimStatement">print</span> x,s.count(x)</pre>
</div>
<p>It is also interesting to note how she uses the sum function to append a set of lists together.</p>
]]></content:encoded>
			<wfw:commentRss>http://blag.tsukasa.net.au/2008/11/29/pygolf-winner-of-competition-1/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Python golf competition</title>
		<link>http://blag.tsukasa.net.au/2008/11/23/python-golf-competition/</link>
		<comments>http://blag.tsukasa.net.au/2008/11/23/python-golf-competition/#comments</comments>
		<pubDate>Sun, 23 Nov 2008 13:21:46 +0000</pubDate>
		<dc:creator>Tsukasa</dc:creator>
				<category><![CDATA[golf]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://blag.tsukasa.net.au/?p=53</guid>
		<description><![CDATA[I have written a few entries on python golf before, but I have now decided to make an official competition from it. The rules are fairly simple: I pose a problem simple problem which must be solved (in python) in the lowest number of characters (where a new line counts as one byte). Solutions may [...]]]></description>
			<content:encoded><![CDATA[<p>I have written a few entries on python golf before, but I have now decided to make an official competition from it. The rules are fairly simple: I pose a problem simple problem which must be solved (in python) in the lowest number of characters (where a new line counts as one byte). Solutions may be written for any 2.x version of python (ie, 2.3, 2.4, 2.5 or 2.6), and may use any library found on default install on a Debian machine.</p>
<p>Solutions must be emailed to me. In the case where two people have the same character count, the solution that arrived in my inbox first will be declared the winner (this is to stop people from playing with the date header in the email ^^). The winner will receive a chocolate bar or coffee &#8212; their choice.</p>
<p>The problem this week is one of parsing log files. You must parse a log file in the <a href="http://httpd.apache.org/docs/2.2/logs.html#common">common log format</a> that will be given on to your program on stdin. You must then print on stdout the amount of data that was sent to each ip address (and the ip address). A sample program has been provided:</p>
<p><!-- File:081124_sample_solution.py -->
<div class="codeBlock">
<pre class="VimPre"><span class="VimComment">#!/usr/bin/env python2.5</span>
<span class="VimComment"># Copyright 2008 Greg Darke &lt;greg+laptop@tsukasa.net.au&gt;</span>
<span class="VimComment"># Licensed for distribution under the GPL version 2, check COPYING for details</span>
<span class="VimComment"># Sample program to show the output format for the python golf problem posed at</span>
<span class="VimComment"># <a href="http://blag.tsukasa.net.au/2008/11/23/python-golf-competitionpython-golf-competition/">http://blag.tsukasa.net.au/2008/11/23/python-golf-competitionpython-golf-competition/</a></span>

<span class="VimPreProc">import</span> re, sys

<span class="VimStatement">def</span> <span class="VimIdentifier">main</span>():
    clf_regexp = re.compile(<span class="VimNormal">r'''</span><span class="VimConstant">^(\S+)\s(\S+)\s(\S+)\s\[([^\]]*)\]\s&quot;([^&quot;]*)&quot;\s(\d*)\s(\d*)$</span><span class="VimNormal">'''</span>)
    mapping = {}

    <span class="VimStatement">for</span> line <span class="VimStatement">in</span> sys.stdin:
        m = clf_regexp.match(line)
        <span class="VimStatement">if</span> <span class="VimStatement">not</span> m:
            <span class="VimStatement">continue</span>
        ip, _, _, _, _, _, size = m.groups()
        size = int(size)

        <span class="VimStatement">if</span> ip <span class="VimStatement">not</span> <span class="VimStatement">in</span> mapping:
            mapping[ip] = 0

        mapping[ip] += size

    <span class="VimStatement">for</span> ip <span class="VimStatement">in</span> mapping:
        <span class="VimStatement">print</span> <span class="VimNormal">'</span><span class="VimConstant">%s %d</span><span class="VimNormal">'</span> % (ip, mapping[ip])

<span class="VimStatement">if</span> __name__ == <span class="VimNormal">&quot;</span><span class="VimConstant">__main__</span><span class="VimNormal">&quot;</span>:
    main()</pre>
</div>
<p>Solutions will be accepted until 11:59:59pm(EDT) on Friday 28th November 2008.</p>
]]></content:encoded>
			<wfw:commentRss>http://blag.tsukasa.net.au/2008/11/23/python-golf-competition/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Recursive tuples</title>
		<link>http://blag.tsukasa.net.au/2008/09/26/recursive-tuples/</link>
		<comments>http://blag.tsukasa.net.au/2008/09/26/recursive-tuples/#comments</comments>
		<pubDate>Fri, 26 Sep 2008 12:05:44 +0000</pubDate>
		<dc:creator>Tsukasa</dc:creator>
				<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://blag.tsukasa.net.au/?p=13</guid>
		<description><![CDATA[Now,
I have been thinking about this for a few days now&#8230; Is it possible to create a tuple in python that refers to itself. I don&#8217;t mean via some other object, so the following does not count:


def recursive():
    l = []
    t = (l,)
    l.append(t)
  [...]]]></description>
			<content:encoded><![CDATA[<p>Now,<br />
I have been thinking about this for a few days now&#8230; Is it possible to create a tuple in python that refers to itself. I don&#8217;t mean via some other object, so the following does not count:<br />
<!-- File:080926_recursive.py -->
<div class="codeBlock">
<pre class="VimPre"><span class="VimStatement">def</span> <span class="VimIdentifier">recursive</span>():
    l = []
    t = (l,)
    l.append(t)
    <span class="VimStatement">return</span> t</pre>
</div>
<p>I believe it is possible  to do directly from C, but I can not think of a way to do it from within python.</p>
<p>The idea of creating a recursive tuple was spawned from  this little comment I found in the <a href="http://www.python.org/doc/current/lib/module-pickle.html">pickle</a> source code: &#8220;&#8230; recursive  tuples are a rare thing&#8221;. At first I thought it was talking about a tuple that directly refers to itself, but then figured that it must be talking about tuple&#8217;s that indirectly refer to themselves.</p>
]]></content:encoded>
			<wfw:commentRss>http://blag.tsukasa.net.au/2008/09/26/recursive-tuples/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Significant whitespace</title>
		<link>http://blag.tsukasa.net.au/2008/09/18/significant-whitespace/</link>
		<comments>http://blag.tsukasa.net.au/2008/09/18/significant-whitespace/#comments</comments>
		<pubDate>Thu, 18 Sep 2008 01:19:09 +0000</pubDate>
		<dc:creator>Tsukasa</dc:creator>
				<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://blag.tsukasa.net.au/?p=11</guid>
		<description><![CDATA[One of the most common complains I hear from people about python is that whitespace is significant. I would have to disagree with them, I think that significant whitespace is an excellent feature in a programming language. Though only if it is implemented correctly, which I believe it is not in python.
My major complaint about [...]]]></description>
			<content:encoded><![CDATA[<p>One of the most common complains I hear from people about python is that whitespace is significant. I would have to disagree with them, I think that significant whitespace is an excellent feature in a programming language. Though <em>only</em> if it is implemented correctly, which I believe it is not in python.</p>
<p>My major complaint about python is that it allows users to mix both tabs and spaces, and also different amounts of spaces to mark a code block. I believe that python should not allow this. I think a there should be one consistent method of indenting used within a file. I would love if this method was tabs, but I don&#8217;t really mind that much if it spaces.</p>
<p>This problem usually occurs when you have multiple people modifying code, over an extended period of time. Each person has their own preferred style of indenting code, and will use it (generally without thinking about it).</p>
<p>A friend of mine recently came across this exact problem, and in an attempt to make the file &#8220;sane&#8221; used a regular expression to fix the whitespace in the file. This ended up being a major problem, as their had inadvertently changed the meaning of the code, by subtle changing the indentation levels of the code. I came up with the idea of parsing the python program, then outputting the program back from the parse tree (thus ensuring the meaning of the code is unchanged).</p>
<p>After being shown the <a href="http://www.python.org/doc/2.5.2/lib/module-compiler.html">compiler module</a>, I started playing around with some code to reproduce code from the abstract parse tree that was provided to me. When I went looking for documentation, I found that there is a piece of sample code provided with the python source that does exactly what I was after (After a few bug fixes). This example is called unparse.py (located in the Demo/parser/ directory of the python 2.5.x source code).</p>
<p>I suggest anybody who is trying to fix inconsistent indentation in a python file to look at this program. There are a few things to note though:
<ul>
<li>The code must already work as wanted</li>
<li>This program will strip all comments from the program</li>
<li>The code will loose all of it&#8217;s layout &#8211; that is, code that may have been split over multiple lines, will now be over one line</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blag.tsukasa.net.au/2008/09/18/significant-whitespace/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>I can write normal code too</title>
		<link>http://blag.tsukasa.net.au/2008/08/23/i-can-write-normal-code-too/</link>
		<comments>http://blag.tsukasa.net.au/2008/08/23/i-can-write-normal-code-too/#comments</comments>
		<pubDate>Fri, 22 Aug 2008 14:52:54 +0000</pubDate>
		<dc:creator>Tsukasa</dc:creator>
				<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://blag.tsukasa.net.au/2008/08/23/i-can-write-normal-code-too/</guid>
		<description><![CDATA[Now,
I am sure that you are all thinking that I must write horrible code normally in python&#8230; But that is incorrect, I can write quite nice code. Below I have placed a &#8220;normal&#8221; version of a prime sieve. Please note, I usually have a bit of whitespace between functions, but I am unable to at [...]]]></description>
			<content:encoded><![CDATA[<p>Now,<br />
I am sure that you are all thinking that I must write horrible code normally in python&#8230; But that is incorrect, I can write quite nice code. Below I have placed a &#8220;normal&#8221; version of a prime sieve. Please note, I usually have a bit of whitespace between functions, but I am unable to at the moment due to some wordpress bugs.</p>
<p>I quite like this code, even though I am using a little trick in that the list the generator is iterating over, is being changed while the generator is still using it.<br />
<!-- File:080923_prime_sieve.py -->
<div class="codeBlock">
<pre class="VimPre"><span class="VimPreProc">import</span> math
<span class="VimStatement">def</span> <span class="VimIdentifier">primes</span>(maxPrime):
    maxSearch = int(math.sqrt(maxPrime))
    nums = range(maxPrime+1)
    nums[1] = 0 <span class="VimComment"># 1 is not a prime</span>
    primes = (n <span class="VimStatement">for</span> n <span class="VimStatement">in</span> nums <span class="VimStatement">if</span> n) <span class="VimComment"># This line is nasty... Changing the thing</span>
                                    <span class="VimComment"># I am iterating over may cause problems</span>
    <span class="VimStatement">for</span> n <span class="VimStatement">in</span> primes:
        <span class="VimStatement">if</span> n &lt;= maxSearch:
            <span class="VimStatement">for</span> i <span class="VimStatement">in</span> xrange(n**2, maxPrime+1, n):
                nums[i] = 0 <span class="VimComment"># Mark as non prime</span>
        <span class="VimStatement">yield</span> n

<span class="VimStatement">def</span> <span class="VimIdentifier">main</span>():
    <span class="VimStatement">print</span> <span class="VimNormal">'</span><span class="VimConstant">, </span><span class="VimNormal">'</span>.join(str(i) <span class="VimStatement">for</span> i <span class="VimStatement">in</span> primes(10**6))

<span class="VimStatement">if</span> __name__ == <span class="VimNormal">&quot;</span><span class="VimConstant">__main__</span><span class="VimNormal">&quot;</span>:
    main()</pre>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blag.tsukasa.net.au/2008/08/23/i-can-write-normal-code-too/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Round two of golf</title>
		<link>http://blag.tsukasa.net.au/2008/08/22/round-two-of-golf/</link>
		<comments>http://blag.tsukasa.net.au/2008/08/22/round-two-of-golf/#comments</comments>
		<pubDate>Fri, 22 Aug 2008 10:05:18 +0000</pubDate>
		<dc:creator>Tsukasa</dc:creator>
				<category><![CDATA[golf]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://blag.tsukasa.net.au/2008/08/22/round-two-of-golf/</guid>
		<description><![CDATA[Well, here we go again. After my last posting about python golf, one of my friends pointed me to a little gem by the guys who are writing PyPy. This link talks about how list comprehensions are implemented in python, and this sparked another piece of code.
What you see below is actually a prime sieve [...]]]></description>
			<content:encoded><![CDATA[<p>Well, here we go again. After my last posting about python golf, one of my friends pointed me to a little gem by the guys who are writing PyPy. This link talks about <a href="http://morepypy.blogspot.com/2008/06/list-comprehension-implementation.html">how list comprehensions are implemented in python</a>, and this sparked another piece of code.</p>
<p>What you see below is actually a <a href="http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes">prime sieve</a> that is written in a single statement. I have used as many tricks as I could think of to lower the number of characters. Note, this code only works on python 2.5 (as I am using the new ternary operator). I also know that this code give a deprecation warning, that is because I am using <code>1e3</code> instead of the longer <code>1000</code>.</p>
<p>Please feel free to leave a comment if you either have a shorter version, or an idea on how to make it shorter.</p>
<p><!-- File:080822_golf_1.py -->
<div class="codeBlock">
<pre class="VimPre"><span class="VimStatement">print</span>[n <span class="VimStatement">for</span>(n,l)<span class="VimStatement">in</span>[((i,locals()[<span class="VimNormal">'</span><span class="VimConstant">_[2]</span><span class="VimNormal">'</span>]) <span class="VimStatement">if</span> i&gt;1 <span class="VimStatement">else</span>(0,[]))<span class="VimStatement">for</span> i <span class="VimStatement">in</span> range(1e3)]<span class="VimStatement">if</span> n <span class="VimStatement">and</span>(<span class="VimStatement">not</span> l.__setitem__(slice(n*2,None,n),[(0,None)]*len(l[n*2::n])))]</pre>
</div>
<p>Edit: After a little trip home on the bus, I have it down to 132 characters ^^.<br />
<!-- File:080822_golf_2.py -->
<div class="codeBlock">
<pre class="VimPre"><span class="VimStatement">print</span>[n <span class="VimStatement">for</span>(n,l)<span class="VimStatement">in</span>[(i,vars()[<span class="VimNormal">'</span><span class="VimConstant">_[2]</span><span class="VimNormal">'</span>])<span class="VimStatement">for</span> i <span class="VimStatement">in</span> range(2,1e3)]<span class="VimStatement">if</span> n <span class="VimStatement">and</span>(l.__setitem__(slice(n*2-2,None,n),[(0,0)]*len(l[n*2-2::n]))!=0)]</pre>
</div>
<p>Edit: Make that 128 characters<br />
<!-- File:080822_golf_3.py -->
<div class="codeBlock">
<pre class="VimPre"><span class="VimStatement">print</span>[n <span class="VimStatement">for</span>(n,l)<span class="VimStatement">in</span>[(i,vars()[<span class="VimNormal">'</span><span class="VimConstant">_[2]</span><span class="VimNormal">'</span>])<span class="VimStatement">for</span> i <span class="VimStatement">in</span> range(1e3)]<span class="VimStatement">if</span> n&gt;1 <span class="VimStatement">and</span>(l.__setitem__(slice(n*n,None,n),[(0,0)]*len(l[n*n::n]))!=0)]</pre>
</div>
<p>Edit: 84 characters, though it is no longer a one line/one statement thing<br />
<!-- File:080822_golf_4.py -->
<div class="codeBlock">
<pre class="VimPre">z=range(1e3);p=(n <span class="VimStatement">for</span> n <span class="VimStatement">in</span> z <span class="VimStatement">if</span> n&gt;1)<span class="VimStatement">for</span> n <span class="VimStatement">in</span> p:z[n*n::n]=[0]*len(z[n*n::n]);<span class="VimStatement">print</span> n</pre>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blag.tsukasa.net.au/2008/08/22/round-two-of-golf/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
