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

<channel>
	<title>code monk</title>
	<atom:link href="http://drj11.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://drj11.wordpress.com</link>
	<description>hacking habits</description>
	<lastBuildDate>Wed, 08 Jul 2009 13:39:40 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<image>
		<url>http://www.gravatar.com/blavatar/c41d4a56916ce1e14b666d40005737ba?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>code monk</title>
		<link>http://drj11.wordpress.com</link>
	</image>
			<item>
		<title>Unusable Train Reservations</title>
		<link>http://drj11.wordpress.com/2009/07/08/unusable-train-reservations/</link>
		<comments>http://drj11.wordpress.com/2009/07/08/unusable-train-reservations/#comments</comments>
		<pubDate>Wed, 08 Jul 2009 13:39:40 +0000</pubDate>
		<dc:creator>drj11</dc:creator>
				<category><![CDATA[rant]]></category>

		<guid isPermaLink="false">http://drj11.wordpress.com/?p=607</guid>
		<description><![CDATA[Returning from EuroPython I got the 1730 from Birmingham New Street to Sheffield.  The seat reservations in this class of train appear above each pair of seats on a little illuminated dot matrix display that shows 2 rows of text.  Each row displays 16 characters (it was quite tricky to count, but it [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=drj11.wordpress.com&blog=258145&post=607&subd=drj11&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Returning from EuroPython I got the 1730 from Birmingham New Street to Sheffield.  The seat reservations in this class of train appear above each pair of seats on a little illuminated dot matrix display that shows 2 rows of text.  Each row displays 16 characters (it was quite tricky to count, but it was close enough to 16 that surely, if there is any god, it must be 16).</p>
<p>Each row displays the seat reservation information for one seat.  As a little message that scrolls along.  Most of these messages were of the form: &#8220;20 This seat is not reserved&#8221;.  This message is 28 characters long.  Which means it needs to scroll to fit on the display.  Some genius decided to pad the scrolling message with 16 blanks, so that the end of the message scrolls off completely before the message begins again.  So, for the display of this message, there are 44 states that a row can be in.  Each state corresponds to a position in the 44 character string (each position in the string can be identified with the display state that has that position at the extreme left-hand end of the display).</p>
<p>The seat number is 2 digits long.  It is only displayed for 15 of the 44 states.  Meaning it is only visible for 34% of the time.  It&#8217;s actually kind of important to display the seat number.  Especially as they made the mistake of putting the larger of the two seat numbers on the top row.  The two rows display the reservations for a pair of seats: N (bottom row), and N+1 (top row).  Nuts. In fact it&#8217;s not necessary to display the seat number on the display itself.  Adjacent to the display (on either side) are stickers showing the numbers for the two seats and whether they are window or aisle.  It would be a trivial design change for these stickers to point to the appropriate row of the display.</p>
<p>Some of the time the display will show <code>t reserved</code> or <code>eserved</code> or something else that could reasonably be mistaken for <code>reserved</code>.  So if you just glance at the display then you have about a 7% of interpreting it as &#8220;reserved&#8221; (when in fact it&#8217;s not reserved).</p>
<p>The shorter the message is, the more likely we are too be able to comprehend it instantaneously without having to wait for it to scroll.  So it would be good to get rid of unnecessary text. &#8220;This seat is&#8221; is totally unnecessary.  We&#8217;re on a train, we can tell that the little display refers to a seat reservation.  So all it needs to say is &#8220;20 not reserved&#8221;.  And that&#8217;s only 15 characters long, so it can be displayed permanently, without needing any anti-assistive scrolling.</p>
<p>The case where the seat is not reserved is a bit special, but it&#8217;s worth concentrating on, because those are the seats that people will want to find when they are boarding the train.  At least, people without reservations.  People with reservations don&#8217;t need the overhead displays at all because they can just look at their ticket to find the seat number.  To recap: The reservation displays are only useful for people without reservations, so they should be organised around making it clear which seats are free.</p>
<p>The remaining cases, where the seat is reserved for some of the remaining journey, should probably be handled with text that is something like &#8220;free until chesterfield&#8221; or &#8220;reserved until york&#8221;.  Possibly the &#8220;free&#8221; and &#8220;reserved&#8221; can be &#8220;stuck&#8221; at the left-hand side of the display while the remainder of the display scrolls to show the whole message.  Dunno.  But I bet a day of trying out a dozen ideas would be a vast usability improvement on how it works now.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/drj11.wordpress.com/607/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/drj11.wordpress.com/607/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/drj11.wordpress.com/607/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/drj11.wordpress.com/607/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/drj11.wordpress.com/607/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/drj11.wordpress.com/607/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/drj11.wordpress.com/607/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/drj11.wordpress.com/607/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/drj11.wordpress.com/607/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/drj11.wordpress.com/607/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=drj11.wordpress.com&blog=258145&post=607&subd=drj11&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://drj11.wordpress.com/2009/07/08/unusable-train-reservations/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9c6dfaac50b9c43815dd18081e87f3e3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">drj11</media:title>
		</media:content>
	</item>
		<item>
		<title>After Copper</title>
		<link>http://drj11.wordpress.com/2009/07/08/after-copper/</link>
		<comments>http://drj11.wordpress.com/2009/07/08/after-copper/#comments</comments>
		<pubDate>Wed, 08 Jul 2009 12:02:43 +0000</pubDate>
		<dc:creator>drj11</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://drj11.wordpress.com/?p=650</guid>
		<description><![CDATA[We gather in the dim space afforded by the overgrowing ash (Fraxinus excelsior) and cow parsley (Anthriscus sylvestris).  The trees provide some shelter from the rain, after all, it is July in the Peak District.  The key is brought forth.  The old iron gate swings open.  We step down into a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=drj11.wordpress.com&blog=258145&post=650&subd=drj11&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>We gather in the dim space afforded by the overgrowing ash (<em>Fraxinus excelsior</em>) and cow parsley (<em>Anthriscus sylvestris</em>).  The trees provide some shelter from the rain, after all, it is July in the Peak District.  The key is brought forth.  The old iron gate swings open.  We step down into a small stream, and through the gate, passing under a stone reading &#8220;DEEP ECTON: DRIVEN 1774&#8243;.  A tunnel arch leads inward, towards the heart of the hillside.</p>
<p>My wellingtons are in muddy water.  The pavement beneath, which I cannot see, is uneven. The daylight fades, but my eyes have not yet adapted to the dim torchlight.  Stumbling forwards, reaching out for the walls.  Tracing my hand along the wall to steady myself, missing my footing as my hand discovers crumbling gaps in the walls.  The dwarves that once worked this place left over 100 years ago; they must have been dwarves, for sure they were smaller folk than me, for I keep banging my helmet on the odd out of place stone in the ceiling.  We are in Deep Ecton Mine, once the source of the Peak District&#8217;s copper.</p>
<p>After a short while, plaster gives way to (limestone) brick. Little nooks appear, presumably once filled with candles. Then the brick gives way to a rough unlined tunnel through the bedrock.  The horizontal tunnel we are walking, stumbling, along would have been called an <em>adit</em> by the dwarves. Its upward slope is imperceptible save for the fact that we are walking through a stream; running downwards and outward to rejoin its brethren waters of the Manifold.  My eyes are adapted now, and my foot and inner ear give me mostly steady passage.</p>
<p>We pause at the first &#8220;chamber&#8221;, a swelling of the rough tunnel.  A crawl above a pile of rock debris, &#8220;deads&#8221; as the dwarves called them, leads to a much older, smaller, adit.  Its roof has fallen in, now blocked and unsafe.  A narrow shaft leads upwards, presumably once connecting to the surface.  The surface, and all evidence of the outside world, seems very distant now.</p>
<p>Passages leading off into the gloom.  Stepping over discarded ironwork, occasional rocks (fallen from the roof?), and&#8230; tramway sleepers.  The adit would once have been busy with minecarts.  I see a little metal tag marked with &#8220;A6&#8243; (a modern survey tag).  One dwarf has mezzotinted his initials, IB, into the wall with his pick.  The whole thing reminds me of, well, what else but Colossal Cave (and damnit, why didn&#8217;t I think to try saying &#8220;PLUGH&#8221;?).</p>
<p>The &#8220;IB&#8221; chamber has a big rectangular hole in the floor.  Full of water.  We are on the lowest dry level of the mine (the entrance that we used is only a few meters above the River Manifold).  Just around the corner we come to the main attraction.  A huge vertical cavern, or <em>pipe</em>.  Big enough to contain a house and extending upwards in a complex series of natural shafts, platforms, and other connecting chambers.  The house would have to float, for the &#8220;floor&#8221; of the cavern is a gigantic pool filled with crystal clear water.  No plant infiltrates, and no animal stirs the mud.  A tiny waterfall chirps and trickles its way down into the pool from some higher cavern.  Downwards, through the water, we can see that the pipe continues.  Occasionally we can see massive wooden props, essentially whole mature oaks trimmed to a rough rectangle, fitted across where they once would&#8217;ve supported platforms.  The mine continue downward, as does the pipe, for at least another 300m.  But the underwater areas remain unexplored since a diver&#8217;s death here in the 1960&#8217;s.</p>
<p>This pipe is where the copper was.  Formed when the bedrock flexed and cracked, allowing copper carrying water to seep in and deposit its lode.  Of course the dwarves took all the copper, but we can see occasional spots where copper has remineralised and formed a greenish colouration on the walls (copper carbonate?).  All around we can see places where the bedrock has bent into huge curves, cracked, and the cracks filled with worthless calcite (by contrast, the limestone bedrock nearer the entrace has no calcite veining).</p>
<p>This pipe was formed by hacking out valuable copper ores.  The next cavern is 10 metres across and big enough to stand in (and in parts up to about 8m high).  It was dug out not for copper, for it never contained any, but to house an engine.  It is the engine chamber.  Housing engines for pumping water out of the depths.  Engines powered not by steam, but by horse, and by water.  The amount of effort involved is quite incredible: all that worthless rock removed to create this large room, rivers diverted, engines installed, a vertical 300m oak beam (bolted in sections, naturally).  All just to remove water so that the lower sections of the mine could be worked for their copper.</p>
<p>Then the tour was over.  We had to make our way to the endgame before our batteries ran out and the cave collapsed (just kidding, another text adventure reference).  Whilst we had been underground for quite a while, it didn&#8217;t take us long to go back along the adit and reach daylight and the smell of fresh air, a smell you really appreciate when you&#8217;ve been underground.</p>
<p>Thanks to the staff of the Peak District National Park who used their own time to give us the opportunity to see the mine and benefit from their experience.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/drj11.wordpress.com/650/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/drj11.wordpress.com/650/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/drj11.wordpress.com/650/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/drj11.wordpress.com/650/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/drj11.wordpress.com/650/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/drj11.wordpress.com/650/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/drj11.wordpress.com/650/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/drj11.wordpress.com/650/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/drj11.wordpress.com/650/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/drj11.wordpress.com/650/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=drj11.wordpress.com&blog=258145&post=650&subd=drj11&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://drj11.wordpress.com/2009/07/08/after-copper/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9c6dfaac50b9c43815dd18081e87f3e3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">drj11</media:title>
		</media:content>
	</item>
		<item>
		<title>My EuroPython talk: Python Sucks!</title>
		<link>http://drj11.wordpress.com/2009/07/06/my-europython-talk-python-sucks/</link>
		<comments>http://drj11.wordpress.com/2009/07/06/my-europython-talk-python-sucks/#comments</comments>
		<pubDate>Mon, 06 Jul 2009 11:50:49 +0000</pubDate>
		<dc:creator>drj11</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[europython]]></category>
		<category><![CDATA[europython2009]]></category>

		<guid isPermaLink="false">http://drj11.wordpress.com/?p=640</guid>
		<description><![CDATA[On 2009-07-01 I gave a talk at EuroPython called &#8220;Python Sucks!&#8221;.  They made me change the title of the talk, but the first slide sticks, so &#8220;Python Sucks!&#8221; it is.
It was a bit of a misleading title.  As I did actually mention some things that I like about Python.
The slides (updated in blue [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=drj11.wordpress.com&blog=258145&post=640&subd=drj11&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>On 2009-07-01 I gave a talk at EuroPython called &#8220;Python Sucks!&#8221;.  <a href="http://mg.pov.lt/europython-irclog/%23europython.2009-04-08.log.html#t2009-04-08T20:48:24">They made me change the title of the talk</a>, but the first slide sticks, so &#8220;Python Sucks!&#8221; it is.</p>
<p>It was a bit of a misleading title.  As I did actually mention some things that I like about Python.</p>
<p>The slides (updated in blue to add useful things that people said in the talk itself). <a href='http://drj11.files.wordpress.com/2009/07/pythonsucks.pdf'>are available in PDF</a>.  I&#8217;m not sure the slides are particularly useful without a transcript; it&#8217;s not always clear if the point illustrated on the slide is something that I think is a good thing, or a bad thing.</p>
<p>I was a bit overwhelmed with the talk actually.  I was thinking that, as I am not as famous as Bruce Eckel or Tony Hoare, about 30 people would turn up; and I think I could probably wrangle about that many people. The Recital Hall holds about 150 people, and it <a href="http://www.europython.eu/gallery/media/gallery/thumbs/2009-07-04-83_jpeg_960x700_q85.jpg">was pretty much full</a>.  *gulp*.</p>
<p>The audience included Tony Hoare (<a href="http://drj11.wordpress.com/2009/07/04/tony-hoare-man-of-science/">Man of Science</a>); when I spotted that I sort of thought &#8220;oh no, Tony Hoare is in the house, I&#8217;d better behave now&#8221;.  He usefully (and somewhat embarrassingly on my part) suggested that Occam be added to the cloud of &#8220;languages I don&#8217;t know enough about&#8221;.  And it should.</p>
<p>One of the points of the talk was to get the audience talking; I think I did okay at  providing a very lively forum for people, not just me, to get their python gripes off their chest.  The contributions from members of the audience were well appreciated, and often informative.  Certainly I felt that the audience provided a useful contribution, which of course made my job easier.</p>
<p>Later on in his keynote Tony Hoare said something like &#8220;from what I&#8217;ve seen here today you are doing a good job&#8221; [of being scientific engineers, or of steering Python, or something].  I hope he wasn&#8217;t referring to me.</p>
<p>Note to self: do not show a slide with &#8220;distutils&#8221; on it to 150 people.  Unless you have nothing to say.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/drj11.wordpress.com/640/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/drj11.wordpress.com/640/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/drj11.wordpress.com/640/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/drj11.wordpress.com/640/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/drj11.wordpress.com/640/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/drj11.wordpress.com/640/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/drj11.wordpress.com/640/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/drj11.wordpress.com/640/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/drj11.wordpress.com/640/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/drj11.wordpress.com/640/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=drj11.wordpress.com&blog=258145&post=640&subd=drj11&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://drj11.wordpress.com/2009/07/06/my-europython-talk-python-sucks/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9c6dfaac50b9c43815dd18081e87f3e3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">drj11</media:title>
		</media:content>
	</item>
		<item>
		<title>Tony Hoare, man of Science</title>
		<link>http://drj11.wordpress.com/2009/07/04/tony-hoare-man-of-science/</link>
		<comments>http://drj11.wordpress.com/2009/07/04/tony-hoare-man-of-science/#comments</comments>
		<pubDate>Sat, 04 Jul 2009 20:52:23 +0000</pubDate>
		<dc:creator>drj11</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[europython]]></category>
		<category><![CDATA[europython2009]]></category>
		<category><![CDATA[tony hoare]]></category>

		<guid isPermaLink="false">http://drj11.wordpress.com/?p=621</guid>
		<description><![CDATA[At EuroPython, on 2009-07-01, Tony Hoare gave a very interesting speech on the opposition between science and engineering.  In many ways it reprised the themes of Laura Creighton&#8217;s keynote from PyCon UK 2007, but from a science perspective rather than a history of science perspective.  In the middle of the talk, some wit [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=drj11.wordpress.com&blog=258145&post=621&subd=drj11&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>At EuroPython, on 2009-07-01, Tony Hoare gave a very interesting speech on the opposition between science and engineering.  In many ways it reprised the themes of Laura Creighton&#8217;s <a href="http://www.pyconuk.org/community/Audio2007">keynote from PyCon UK 2007</a>, but from a science perspective rather than a <em>history</em> of science perspective.  In the middle of the talk, <a href="http://twitter.com/hawkz">some wit</a> twittered &#8220;Stand back! Sir Tony Hoare is about to do science!&#8221;; shame it wasn&#8217;t on the <a href="http://www.flickr.com/photos/39841418@N05/3677338679/">big twitter wall</a>.</p>
<p>Tony Hoare is clearly old skool.  His slides had the calm and aged patina of the <a href="http://en.wikipedia.org/wiki/Overhead_projector">OHP</a> era, and I thought they were all the better for that.  If you have a message, then that message can be conveyed without all the flash and shine that PowerPoint tempts you with (although, being a Microsoft man, of course <a href='http://drj11.files.wordpress.com/2009/07/tony_hoare_science_and_engineering.ppt'>his slides <em>were</em> in PowerPoint</a>).  As <a href="http://us.pycon.org/media/2009/talkdata/PyCon2009/006/giving-python-talks-kuchling.pdf">Andrew Kuchling says</a>: &#8220;(good talk, plain slides) &gt; (bad talk, fancy slides)&#8221;</p>
<p>I was particularly impressed with this slide from Tony&#8217;s talk outlining a few &#8220;special interests&#8221; of the scientist and engineer respectively:</p>
<p><a href="http://drj11.files.wordpress.com/2009/07/tony_hoare_science_and_engineering-single-12.png"><img src="http://drj11.files.wordpress.com/2009/07/tony_hoare_science_and_engineering-single-12.png?w=400&#038;h=300" alt="Tony_Hoare_Science_and_Engineering-single-12" title="Tony_Hoare_Science_and_Engineering-single-12" width="400" height="300" class="alignnone size-full wp-image-624" /></a></p>
<p>On the scales on the side of Science we have things like &#8220;long-term&#8221;, &#8220;perfection&#8221;, and &#8220;originality&#8221;.  Balancing the scales for Engineering we have &#8220;short-term&#8221;, &#8220;adequacy&#8221;, and &#8220;best practice&#8221;.</p>
<p>What I liked about this slide is that many of the things on the Science side would be seen as defects in an engineer, and many things on the Engineering side would be seen as defects in a scientist.  We have all seen scientists attacked for relying on intuition or merely amalgamating best practice.  And what engineer has not been barracked (by their manager) for attempting solutions that were too perfectionist or wasting time on long-term goals?</p>
<p>Tony Hoare&#8217;s insights are clearly the product of long and hard work.  He seems very optimistic about the possibilities of a virtuous feedback between the engineering and scientific sides of computing.  Perhaps we have every right to look forward to the day when &#8220;Software will be the most reliable component of every product which contains it.&#8221; (<a href="http://www.flickr.com/photos/adewale_oshineye/3688509820/">the last slide from his talk</a>).  But right now&#8230; it seems a long way off.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/drj11.wordpress.com/621/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/drj11.wordpress.com/621/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/drj11.wordpress.com/621/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/drj11.wordpress.com/621/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/drj11.wordpress.com/621/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/drj11.wordpress.com/621/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/drj11.wordpress.com/621/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/drj11.wordpress.com/621/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/drj11.wordpress.com/621/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/drj11.wordpress.com/621/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=drj11.wordpress.com&blog=258145&post=621&subd=drj11&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://drj11.wordpress.com/2009/07/04/tony-hoare-man-of-science/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9c6dfaac50b9c43815dd18081e87f3e3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">drj11</media:title>
		</media:content>

		<media:content url="http://drj11.files.wordpress.com/2009/07/tony_hoare_science_and_engineering-single-12.png" medium="image">
			<media:title type="html">Tony_Hoare_Science_and_Engineering-single-12</media:title>
		</media:content>
	</item>
		<item>
		<title>Python: dictionary of functions</title>
		<link>http://drj11.wordpress.com/2009/07/03/python-dictionary-of-functions/</link>
		<comments>http://drj11.wordpress.com/2009/07/03/python-dictionary-of-functions/#comments</comments>
		<pubDate>Fri, 03 Jul 2009 12:34:47 +0000</pubDate>
		<dc:creator>drj11</dc:creator>
				<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://drj11.wordpress.com/?p=603</guid>
		<description><![CDATA[On twitter recently I was wondering what the best way to create a dictionary of functions in Python was.  On a suggestion of Paul Hankin&#8217;s I looked into classes and metaclasses.  The most direct way I discovered is to use a class; metaclass not required:


class foo:
  def bar(): pass
  def zon(x): [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=drj11.wordpress.com&blog=258145&post=603&subd=drj11&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>On <a href="http://twitter.com/drjtwit">twitter</a> recently I was wondering what the best way to create a dictionary of functions in Python was.  On a suggestion of Paul Hankin&#8217;s I looked into classes and metaclasses.  The most direct way I discovered is to use a class; metaclass not required:</p>
<pre name="code" class="python">

class foo:
  def bar(): pass
  def zon(x): return 1+x
fundict = foo.__dict__
</pre>
<p>This is direct, but not at all obvious.  Hint to people that are mystified already: the <code>__dict__</code> attribute of the class is a dictionary containing everything defined in the class.</p>
<p><var>fundict</var> is now a dictionary that contains the two functions <var>bar</var> and <var>zon</var>.</p>
<p>I made this discovery a few days before <a>EuroPython</a>, and was fortunate enough to bump into Bruce Eckel in the hallway at EuroPython just before he gave his <a href="http://www.europython.eu/talks/talk_abstracts/index.html#talk51">metaprogramming talk</a>, so I showed it to him in what I call my &#8220;naughty decorator&#8221; form:</p>
<pre name="code" class="python">

def asdict(x):
  return x.__dict__

@asdict
class baz:
  def bar(): pass
  def zon(x): return 1+x
</pre>
<p>(By the way, one of the things I learnt at EuroPython is that the crappy decorator syntax gives Java refugees a warm fuzzy feeling).</p>
<p>Bruce pronounced this &#8220;not as naughty as you imply&#8221; and &#8220;worth showing&#8221; (this blog post was half-written before I bumped into Bruce, but he is definitely encouraging me).</p>
<p>It&#8217;s worth showing for the following reason:</p>
<p>The functions you get from the class&#8217;s <code>__dict__</code> dictionary are not the same as the methods you get by accessing the attributes of the class.  In other words «<code>foo.bar is not foo.__dict__['bar']</code>».  I was surprised by this, and so was Bruce.</p>
<p>As well being a little weird, it makes a compact example to show the different between a <em>function</em>, an <em>unbound method</em>, and a <em>bound method</em>.</p>
<p>I hope I don&#8217;t need to introduce a <em>function</em>.  It&#8217;s just a thing you call with some arguments.  Where it differs from a <em>method</em> is that a method is regarded as a message sent to an object, and receives that object as its first argument.</p>
<p>In Python, an <em>unbound method</em> is a method that isn&#8217;t associated with any particular object; it requires an object as its first argument (and Python loses big here, by requiring the object to be an instance of the class that defined the method).  A <em>bound method</em> is a method already associated with an object; that object becomes the method&#8217;s first argument when the bound method is invoked with the remaining arguments.</p>
<p>So if we define a class <var>foo</var> (as we did, above), then:</p>
<p><var>foo.bar</var> is an unbound method;</p>
<p><var>foo().bar</var> is a bound method (bound to the object we just created by invoking the <var>foo</var> class); and,</p>
<p><var>foo.__dict__['bar']</var> is a function.</p>
<p>This last fact was a great surprise to me.  I had expected it to be an unbound method, and thought that my naughty decorator would have to have some hacky code to dig the function out of the unbound method.  But it doesn&#8217;t.</p>
<p>Tiny problem: Using the <var>asdict</var> gives a dictionary that contains <code>__module__</code> and <code>__doc__</code> keys.  Solution: another decorator:</p>
<pre name="code" class="python">

def cleandict(x):
  for k in (&#039;__module__&#039;, &#039;__doc__&#039;):
    del x[k]
  return x

@cleandict
@asdict
class baz:
  def bar(): pass
  def zon(x): return 1+x
</pre>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/drj11.wordpress.com/603/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/drj11.wordpress.com/603/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/drj11.wordpress.com/603/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/drj11.wordpress.com/603/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/drj11.wordpress.com/603/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/drj11.wordpress.com/603/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/drj11.wordpress.com/603/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/drj11.wordpress.com/603/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/drj11.wordpress.com/603/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/drj11.wordpress.com/603/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=drj11.wordpress.com&blog=258145&post=603&subd=drj11&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://drj11.wordpress.com/2009/07/03/python-dictionary-of-functions/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9c6dfaac50b9c43815dd18081e87f3e3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">drj11</media:title>
		</media:content>
	</item>
		<item>
		<title>EuroPython 2009</title>
		<link>http://drj11.wordpress.com/2009/07/03/europython-2009/</link>
		<comments>http://drj11.wordpress.com/2009/07/03/europython-2009/#comments</comments>
		<pubDate>Fri, 03 Jul 2009 11:26:46 +0000</pubDate>
		<dc:creator>drj11</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[europython]]></category>
		<category><![CDATA[europython2009]]></category>

		<guid isPermaLink="false">http://drj11.wordpress.com/?p=609</guid>
		<description><![CDATA[Overall I had a really enjoyable time, met lots of interesting people, some new and some renewed friendships, and learned some good stuff.
Too many web frameworks.  Too many VMs.
What I like is the diverse range of applications to which people put Python.  The Guardian use it so we can inspect our MP&#8217;s expenses. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=drj11.wordpress.com&blog=258145&post=609&subd=drj11&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Overall I had a really enjoyable time, met lots of interesting people, some new and some renewed friendships, and learned some good stuff.</p>
<p>Too many web frameworks.  Too many VMs.</p>
<p>What I like is the diverse range of applications to which people put Python.  The Guardian use it <a href="http://mps-expenses.guardian.co.uk/">so we can inspect our MP&#8217;s expenses</a>.  Gregor Lindl uses it and <a href="http://docs.python.org/library/turtle.html">Papert&#8217;s turtle graphics</a> to teach.  The <a href="http://www.dlr.de/">Deutsches Zentrum für Luft- und Raumfahrt</a> use it to manage the extraction and shuffling of petabyte (that&#8217;s 10**15!) datasets across the supercomputing networks used by climate scientists.  The talented Stani Michiels creates the <a href="http://pythonide.blogspot.com/2008/10/how-to-make-money-with-free-software.html">new Dutch Euro coin designs using Python</a>.  Weather trading derivatives.  Video workflows.  Collaborative mathematics.  Games.  Academic document archives.  Billing.  System Administration.  The list goes on.</p>
<p>Naturally I managed not to go to most of those talks (apart from keynotes (like <a href="http://drj11.wordpress.com/2009/07/04/tony-hoare-man-of-science/">Tony Hoares</a>) and lightnings, I went to 2 talks).  That&#8217;s partly because going to 4 or 5 talks a day (which would easily have been possible given the <a href="http://www.europython.eu/talks/timetable/">packed schedule</a>) makes you dead tired and causes leaky brain; partly because there were timetable clashes! But mostly because I was writing the materials for my two talks.</p>
<p>Writing your talk at the conference itself is&#8230; exciting.  And intense.  And it probably gives the conference organisers the willies (as in, &#8220;where are your slides?&#8221;).  It did mean that I was able to include some stuff from the conference itself in my talks.  A war story I picked up in hallway chat about having to use 6 year old versions of Python on a government IT project made it into my &#8220;Loving Old Versions&#8221;" talk.  Dr Sue Black&#8217;s talk about Bletchley Park was in the timetable, and that prompted me to put a reference to Turing&#8217;s coffee mug into my &#8220;Python Sucks!&#8221; talk (Sue Black got there first with the same anecdote; ah well).  The &#8220;Python Sucks&#8221; talk also got a couple of references to Bruce Eckel&#8217;s keynote which covered some of the same ground (just using <a href="http://wordaligned.org/articles/europython-2009">a lot more space, according to Thomas Guest</a>).</p>
<p>But it&#8217;s way stressful (Rob Collins&#8217;s massage to raise PSF funds was very helpful in that regard).  Next year I&#8217;ll leave the laptop at home, and give a presentation using a marker pen (well, I will if they accept it!).</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/drj11.wordpress.com/609/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/drj11.wordpress.com/609/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/drj11.wordpress.com/609/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/drj11.wordpress.com/609/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/drj11.wordpress.com/609/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/drj11.wordpress.com/609/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/drj11.wordpress.com/609/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/drj11.wordpress.com/609/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/drj11.wordpress.com/609/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/drj11.wordpress.com/609/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=drj11.wordpress.com&blog=258145&post=609&subd=drj11&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://drj11.wordpress.com/2009/07/03/europython-2009/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9c6dfaac50b9c43815dd18081e87f3e3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">drj11</media:title>
		</media:content>
	</item>
		<item>
		<title>Convert Plan 9 images to PNG</title>
		<link>http://drj11.wordpress.com/2009/06/16/convert-plan-9-images-to-png/</link>
		<comments>http://drj11.wordpress.com/2009/06/16/convert-plan-9-images-to-png/#comments</comments>
		<pubDate>Tue, 16 Jun 2009 09:14:01 +0000</pubDate>
		<dc:creator>drj11</dc:creator>
				<category><![CDATA[PNG]]></category>

		<guid isPermaLink="false">http://drj11.wordpress.com/?p=591</guid>
		<description><![CDATA[Should anyone be in the slightly unusual position of wanting to convert a Plan 9 image to PNG format, they can now use the plan9topng utility in PyPNG:


curl http://plan9.bell-labs.com/sources/plan9/sys/games/lib/sokoban/images/right.bit &#124;
./plan9topng.py &#62; glenda.png

Glenda says &#8220;hi!&#8221;: 
       <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=drj11.wordpress.com&blog=258145&post=591&subd=drj11&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Should anyone be in the slightly unusual position of wanting to convert a <a href="http://plan9.bell-labs.com/plan9/">Plan 9</a> <a href="http://plan9.bell-labs.com/magic/man2html/6/image">image</a> to <a href="http://www.w3.org/TR/PNG/">PNG format</a>, they can now use the <a href="http://code.google.com/p/pypng/source/browse/trunk/code/plan9topng.py"><code>plan9topng</code></a> utility in <a href="http://code.google.com/p/pypng/">PyPNG</a>:</p>
<pre name="code" class="shell">

curl http://plan9.bell-labs.com/sources/plan9/sys/games/lib/sokoban/images/right.bit |
./plan9topng.py &gt; glenda.png
</pre>
<p><a href="http://plan9.bell-labs.com/plan9/glenda.html">Glenda</a> says &#8220;hi!&#8221;: <a href="http://drj11.files.wordpress.com/2009/06/glenda.png"><img src="http://drj11.files.wordpress.com/2009/06/glenda.png?w=49&#038;h=49" alt="glenda" title="glenda" width="49" height="49" class="alignnone size-full wp-image-592" /></a></p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/drj11.wordpress.com/591/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/drj11.wordpress.com/591/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/drj11.wordpress.com/591/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/drj11.wordpress.com/591/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/drj11.wordpress.com/591/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/drj11.wordpress.com/591/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/drj11.wordpress.com/591/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/drj11.wordpress.com/591/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/drj11.wordpress.com/591/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/drj11.wordpress.com/591/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=drj11.wordpress.com&blog=258145&post=591&subd=drj11&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://drj11.wordpress.com/2009/06/16/convert-plan-9-images-to-png/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9c6dfaac50b9c43815dd18081e87f3e3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">drj11</media:title>
		</media:content>

		<media:content url="http://drj11.files.wordpress.com/2009/06/glenda.png" medium="image">
			<media:title type="html">glenda</media:title>
		</media:content>
	</item>
		<item>
		<title>Python: Some Naughty Features</title>
		<link>http://drj11.wordpress.com/2009/06/03/python-some-naughty-features/</link>
		<comments>http://drj11.wordpress.com/2009/06/03/python-some-naughty-features/#comments</comments>
		<pubDate>Wed, 03 Jun 2009 12:13:48 +0000</pubDate>
		<dc:creator>drj11</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://drj11.wordpress.com/?p=568</guid>
		<description><![CDATA[Things in Python that I mostly like, but make me feel a bit naughty when I use them.  I air my smelly code as a sort of cleansing ritual and perhaps to make you feel a little bit better about your own dirty habits.
zip(*[iter(s)]*n)
I now tend to call this zip/iter thing group.  It [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=drj11.wordpress.com&blog=258145&post=568&subd=drj11&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Things in Python that I mostly like, but make me feel a bit naughty when I use them.  I air my smelly code as a sort of cleansing ritual and perhaps to make you feel a little bit better about your own dirty habits.</p>
<p><code>zip(*[iter(s)]*n)</code></p>
<p>I now tend to call this <code>zip</code>/<code>iter</code> thing <var>group</var>.  <a href="http://drj11.wordpress.com/2009/01/28/my-python-dream-about-groups/">It came to me in a dream</a>.</p>
<p><code>itertools.chain(*s)</code></p>
<p>The &#8220;inverse&#8221; of the above.  I only &#8220;discovered&#8221; this recently.  A good couple of months after the above discovery.</p>
<p>Bound methods of simple values.  Example:  In order to convert a sequence of image samples from perceptual space to linear space we need to raise each one to the power 2.2:</p>
<p><code>map((2.2).__rpow__, mylist)</code></p>
<p>«<code>(2.2).__rpow__</code>» is equivalent to the following function:</p>
<p><code>def f(x): return x**2.2</code></p>
<p>(as a lambda: «<code>lambda x: x**2.2</code>»)</p>
<p><code>__rpow__</code> is the swopped version of <code>__pow__</code>.  Using «<code>(2.2).__pow__</code>» would be equivalent to «<code>2.2**x</code>», which isn&#8217;t what I want at all.</p>
<p>Literal string concatenation.  To be honest, I&#8217;ve no idea why this feels a bit naughty to me, but it does.  C has it, so why not in Python?  Perhaps it&#8217;s because I often end up using it in cases where there are other problems with the code (this example from <a href="http://code.google.com/p/pypng/">PyPNG</a>):</p>
<pre name="code" class="python">

    raise FormatError(&quot;Illegal combination of bit depth (%d)&quot;
      &quot; and colour type (%d).&quot;
      &quot; See http://www.w3.org/TR/2003/REC-PNG-20031110/#table111 .&quot;
      % (self.bitdepth, self.color_type))
</pre>
<p>Sometimes I get a bit OCD about this and start removing extraneous string concatenation «+» operators from other people&#8217;s code.</p>
<p><a href="http://drj11.wordpress.com/2008/09/25/i-learn-python/">The <em>split</em> thing that I mentioned in an earlier article</a>.  Here&#8217;s an example from <a href="http://clearclimatecode.org/">Clear Climate Code</a>:</p>
<pre name="code" class="python">

noaa = &quot;&quot;&quot;
ftp://ftp.ncdc.noaa.gov/pub/data/ghcn/v2/v2.mean.Z
ftp://ftp.ncdc.noaa.gov/pub/data/ghcn/v2/v2.temperature.inv
ftp://ftp.ncdc.noaa.gov/pub/data/ushcn/hcn_doe_mean_data.Z
ftp://ftp.ncdc.noaa.gov/pub/data/ushcn/station.inventory.Z
&quot;&quot;&quot;.split()
</pre>
<p>It just seems so much nicer to be able to add a URL to the list without having to add any more syntax (and in this case, I think I originally created the list by pasting it in from a text file, so using <code>split</code> meant less reformatting).</p>
<p>Doing non-trivial things in a module&#8217;s top-level.  Another example from PyPNG:</p>
<p><code>_signature = struct.pack('8B', 137, 80, 78, 71, 13, 10, 26, 10)</code></p>
<p>This is just a complicated way of writing an 8-byte literal string.  It&#8217;s written this way so that the crucial 8 bytes are visible as decimal values, and can therefore be checked more easily against the <a href="http://www.w3.org/TR/PNG/#5PNG-file-signature">relevant part of the PNG standard</a> which also uses a decimal notation.</p>
<p>Passing a function with side-effects into <a href="http://docs.python.org/library/functions.html#map">map</a>.  <em>pipcolour</em> (an undocumented utility in PyPNG) counts the number of colours used in an image.  In the code below, <code>pixels</code> is an iterator that yields each row of a PNG image; the pixels from each row are added to a pre-existing set of colours.  Like this:</p>
<pre name="code" class="python">

    col = set()
    for row in pixels:
        # Ewgh, side effects on col
        map(col.add, png.group(row, planes))
</pre>
<p>(in this code <code>png.group</code> is the grouping function which is the first item in this article; for an RGB image, <var>planes</var> is 3)</p>
<p>Now, it strikes me that I could&#8217;ve gone:</p>
<p><code>set(group(itertools.chain(*pixels), planes))</code></p>
<p>but that has radically different memory usage.  The first form only loads one row at a time into its working set; the second form loads the entire decompressed PNG image into the working set (it&#8217;s all the fault of «<code>*pixels</code>»).  If I was daring enough to require Python 2.6 then I could&#8217;ve used <a href="http://docs.python.org/library/itertools.html#itertools.itertools.chain.from_iterable"><code>itertools.chain.from_iterable</code></a>, but that feels clumsy.  It one of those cases where I feel that Python is unnecessarily forcing me to choose between clarity and efficiency.</p>
<p>I think the above also illustrates a weakness with the sort of delayed evaluation programming that you get when using iterators: Apparently simple transformations to the code can result in huge changes to the space/time complexity.  This is not a weakness that is unique to Python, any style of delayed evaluation has this risk (Haskell&#8217;s lazy evaluation, for example).  When I was learning Prolog my chief criticism of it (and I think it&#8217;s still valid) was that it seemed to be fairly easy to be sure that your program would compute the correct answer, but very difficult to be sure that the answer would appear before the heat death of the universe.</p>
<p>Violating <a href="http://www.python.org/dev/peps/pep-0008/">PEP 8</a> so I can write one-line functions:</p>
<pre name="code" class="python">

    def test_gradient_horizontal_lr(x, y): return x
    def test_gradient_horizontal_rl(x, y): return 1-x
    def test_gradient_vertical_tb(x, y): return y
    def test_gradient_vertical_bt(x, y): return 1-y
</pre>
<p>Did you spot I did one of those earlier?  How naughty.  Of course PEP 8 kind of approves of this, because &#8220;a foolish consistency is the hobgoblin of little minds&#8221; which you probably know from PEP 8 but is of course from Ralph Waldo Emerson.  In a curious coincidence I just happen to have a copy of his &#8220;Self-Reliance&#8221; on my desk; and I checked, he really did say that.  In fact, the fuller quote is much more poetic than I was expecting: &#8220;A foolish consistency is the hobgoblin of little minds, adored by little statesmen and philosophers and divines.&#8221;.</p>
<p>Indexing with a boolean expression.  Yet another example comes from PyPNG and illustrates how well written it is.  Say you want to create an <a href="http://docs.python.org/library/array.html"><code>array</code></a> of pixel values or you want to pack them into a string (using <a href="http://docs.python.org/library/struct.html"><code>struct</code></a>).  If the number of bits per value (the <em>bitdepth</em>) is &lt;= 8 then you want to use «array(&#39;B&#39;)», otherwise you want «array(&#39;H&#39;)».  Select the format like this:</p>
<p><code>fmt = 'BH'[self.bitdepth &gt; 8]</code></p>
<p><a href="http://drj11.wordpress.com/2007/05/25/iversons-convention-or-what-is-the-value-of-x-y/">Iverson&#8217;s Convention</a> for the win! (and it&#8217;s better than <a href="http://docs.python.org/whatsnew/2.5.html#pep-308">PEP 308</a> because it works on Python 2.4)</p>
<p>Now, this is naughty:  Creating a naughty bit of Python code merely for purposes of this blog article.  Well, that&#8217;s not quite true, I got distracted by some Python code and that code and a stupid idea I had a few days ago clicked together.  The code is witty, but should never be used.  PyPNG allows the caller to specify the <a href="http://docs.python.org/library/zlib.html">zlib</a> compression level as an optional argument when creating a <code>png.Writer</code> object.  This is the argument to the <code>zlib.compressobj</code> constructor.  The default (for PyPNG) is to use zlib&#8217;s default.  Now it&#8217;s clearly not the business of PyPNG to know what zlib&#8217;s default is, so I don&#8217;t want that appearing in the code.  So the code looks like this:</p>
<pre name="code" class="python">

if self.compression is not None:
    compressor = zlib.compressobj(self.compression)
else:
    compressor = zlib.compressobj()
</pre>
<p>Now, what I really want is a way to say «pass in <var>x</var> as an argument, but only if it&#8217;s non <code>None</code>».  If I had some expression, <var>E</var>, that evaluated to a list that was either the singleton, «<code>[x]</code>», or the empty list «<code>[]</code>» then I could get rid of the <code>if</code> and go:</p>
<p><code>compressor = zlib.compressobj(*<var>E</var>)</code></p>
<p>«<code>*E</code>» would expand to either the empty argument list, or an argument list with just one argument.</p>
<p>I discovered an <var>E</var>: <code>[x for x in [self.compression] if x is not None]</code>.  So now the code is:</p>
<p><code>compressor = zlib.compressobj(*[x for x in [self.compression] if x is not None])</code></p>
<p>Well, the code isn&#8217;t really that, I&#8217;m not that much of a monster to check that into source control.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/drj11.wordpress.com/568/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/drj11.wordpress.com/568/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/drj11.wordpress.com/568/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/drj11.wordpress.com/568/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/drj11.wordpress.com/568/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/drj11.wordpress.com/568/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/drj11.wordpress.com/568/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/drj11.wordpress.com/568/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/drj11.wordpress.com/568/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/drj11.wordpress.com/568/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=drj11.wordpress.com&blog=258145&post=568&subd=drj11&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://drj11.wordpress.com/2009/06/03/python-some-naughty-features/feed/</wfw:commentRss>
		<slash:comments>27</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9c6dfaac50b9c43815dd18081e87f3e3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">drj11</media:title>
		</media:content>
	</item>
		<item>
		<title>Multiplication, Addition, Counting, and Python</title>
		<link>http://drj11.wordpress.com/2009/06/01/multiplication-addition-counting-and-python/</link>
		<comments>http://drj11.wordpress.com/2009/06/01/multiplication-addition-counting-and-python/#comments</comments>
		<pubDate>Mon, 01 Jun 2009 15:11:25 +0000</pubDate>
		<dc:creator>drj11</dc:creator>
				<category><![CDATA[computing]]></category>
		<category><![CDATA[education]]></category>
		<category><![CDATA[maths]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://drj11.wordpress.com/?p=564</guid>
		<description><![CDATA[Jason Dyer&#8217;s teasing post got me thinking.  About how Python could be used to give some insight into the meta-cognitive aspects of whole number multiplication.  Natch.
When children solve a multiplication problem by correspondence, the objects in the multiplier set are mapped over for each object in the multiplicand set (hmm, or is it [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=drj11.wordpress.com&blog=258145&post=564&subd=drj11&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p><a href="http://numberwarrior.wordpress.com/2009/05/22/the-multiplication-is-not-repeated-addition-research/">Jason Dyer&#8217;s teasing post</a> got me thinking.  About how Python could be used to give some insight into the meta-cognitive aspects of whole number multiplication.  Natch.</p>
<p>When children solve a multiplication problem by <em>correspondence</em>, the objects in the multiplier set are mapped over for each object in the multiplicand set (hmm, or is it the other way around?).  A typical procedure for multiplying 4 cakes by a price of £2 per cake might be to point to a cake with the left hand and then count up the 2 pounds using the right hand, then move the left hand to the next cake and repeat the count with the right hand, with the oral count continuing up; this is repeated until the left hand has exhausted all cakes.</p>
<p>We can model this in Python with the following program:</p>
<pre name="code" class="python">

def mul(multiplicand, multiplier):
    count = 0
    for i in range(multiplicand):
        for j in range(multiplier):
            count += 1
    return count
</pre>
<p>Whereas children using <em>repeated addition</em> do something more like this:</p>
<pre name="code" class="python">

def mul(multiplicand, multiplier):
    count = 0
    for i in range(multiplier):
        count = add(count, multiplicand)
    return count
</pre>
<p>In this case, <em>add</em> is a subroutine.  You could define one easily enough in Python or else you could go «<code><a href="http://docs.python.org/library/operator.html">from operator</a> import add</code>».</p>
<p>Clearly the second program is more efficient than the first, both as a Python program and as a manual procedure performed by children; it&#8217;s more the latter that I&#8217;m interested in, I&#8217;m using Python to describe the procedures.  However, the second procedure requires that <em>add</em> is already adopted as an internal procedure.  Of particular note is that, apart from <var>count</var>, the second procedure uses only one state variable, <var>i</var>; the first procedure uses two.</p>
<p>At the stage where multiplication is introduced, many children will not yet be performing addition accurately without counting.  Effectively <em>add</em> is not yet available to them as an internal procedure.  This is likely to be a problem because this type of learner is unlikely to be able to accurately add the multiplicand to the count without getting confused about where in the multiplication procedure they were.</p>
<p>As an example of what might go wrong, imagine that a learner starts by using the left hand to maintain the <var>i</var> variable in the second procedure (above); this hand will count from 1 to <var>multiplier</var> (hmm, there&#8217;s a small sleight of hand going on here, the Python counts from 0 to <em>n-1</em> whereas most learners will prefer count from 1 to <var>n</var>).  The count will be maintained orally (in other words by speaking out the successive multiples of the multiplicand).  Begin by raising one finger of the left hand and uttering the multiplicand (the initial count).  Now we need to add the multiplicand to the oral count.  Maybe the learner can do that without using their fingers, maybe they can&#8217;t; in any case depending on the parameters chosen, at some point some learners may need to use their hands to perform the addition.  So then the procedure for addition kicks in as the learner adds the multiplicand to the current count.  Many learners will have personal procedure for addition that requires both hands.  The addition may be performed accurately, but the <var>i</var> state variable will be lost.  We lose track of where we were in the multiply procedure.</p>
<p>If the learner can accurately add the multiplicand to the count mentally, then they stand a much better chance of performing the second procedure.  This is what I mean by having <em>add</em> available as an internal procedure.</p>
<p>The first procedure can be thought of as a way of simultaneously arranging to perform the multiplication and the additions required by the multiplication without having any state variables overlap.  Thereby minimising the chance that confusion will result.  Most learners will be capable of keeping track of the required state variables to perform the multiplication, but if left to their own devices may choose methods where the state variables overlap (in other words, they run out of hands).  Thus, they can benefit by being guided towards a procedure which they can manage.</p>
<p>Another way to think about this is that at the sort of age where children begin to learn to multiply, their procedure for addition is <em>leaky</em>.  It is not fully abstracted; performing addition may require the use of hands (for state variables), making the addition leak over any other use of the same hands to maintain state.</p>
<p>It seems to me that only when a learner can perform a typical <var>count</var> + <var>multiplicand</var> addition accurately in their head are they ready to <em>perform</em> multiplication as repeated addition.</p>
<p>Oh yeah, <a href="http://www.scribd.com/doc/5882775/The-development-of-the-concept-of-multiplication">the original research</a> on the whole &#8220;multiplication is/ain&#8217;t repeated addition&#8221; debate.  It sucks.  They test children at times <em>t0</em> and <em>t1</em> with a randomly chosen math related intervention in between.  It seems to me that a more carefully designed study should have also included a non math-related intervention such as giving the subjects fish-oil pills or teaching them to conga.  After all, if I was being tested on one day and then told that I was going to sit a similar test tomorrow, I would bone up on the material before the second test, regardless of what I was being taught.  Wouldn&#8217;t you?  They make no attempt to account for this effect.</p>
<h2>Appendix for Python hackers</h2>
<p>The first definition of <em>mul</em> that I give is of course completely worthless as a practical implementation in Python.  However, note the following: «10*10L» is <code>100L</code> but «mul(10,10L)» is <code>100</code>; in other words <em>mul</em> returns an <code>int</code> if it possibly can.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/drj11.wordpress.com/564/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/drj11.wordpress.com/564/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/drj11.wordpress.com/564/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/drj11.wordpress.com/564/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/drj11.wordpress.com/564/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/drj11.wordpress.com/564/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/drj11.wordpress.com/564/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/drj11.wordpress.com/564/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/drj11.wordpress.com/564/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/drj11.wordpress.com/564/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=drj11.wordpress.com&blog=258145&post=564&subd=drj11&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://drj11.wordpress.com/2009/06/01/multiplication-addition-counting-and-python/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9c6dfaac50b9c43815dd18081e87f3e3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">drj11</media:title>
		</media:content>
	</item>
		<item>
		<title>Anonymous things in Python</title>
		<link>http://drj11.wordpress.com/2009/05/22/anonymous-things-in-python/</link>
		<comments>http://drj11.wordpress.com/2009/05/22/anonymous-things-in-python/#comments</comments>
		<pubDate>Fri, 22 May 2009 15:20:12 +0000</pubDate>
		<dc:creator>drj11</dc:creator>
				<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://drj11.wordpress.com/?p=550</guid>
		<description><![CDATA[This post of pydanny prompted me to flesh this article out (previously it had just been floating around in my mind).  pydanny&#8217;s article reminded me of when I first learned about lambda and how scary it was.  That was in about 1988 when I was trying to get to grips with XLISP on [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=drj11.wordpress.com&blog=258145&post=550&subd=drj11&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>This <a href="http://pydanny.blogspot.com/2007/07/lambdas-no-more.html">post</a> of <a href="http://pydanny.blogspot.com/">pydanny</a> prompted me to flesh this article out (previously it had just been floating around in my mind).  pydanny&#8217;s article reminded me of when I first learned about lambda and how scary it was.  That was in about 1988 when I was trying to get to grips with <a href="http://www.xlisp.org/">XLISP</a> on the Atari ST (hah! a Lisp dialect so old it still spells its name with capital letters.  sweet).  Previous to XLISP I had been programming in (Sinclair) BASIC, z80 machine code, and 68000 assembler.  Oh, and FORTH.  Maybe you can imagine how scary and alien a concept like lambda was.  I can just barely remember how confusing it was.  I don&#8217;t think the confusion really passed until I started dabbling with ML, in 1991.  By the time I started dabbling with the lambda calculus in 1993 I was actually pretty comfortable with it.  So, only 5 years of exposure to get lambda to get comfy.</p>
<p>Of course now, I know that:</p>
<p>A lambda is a <em>simply</em> function with no name.</p>
<p>I emphasise the <em>simply</em> because I feel that that&#8217;s where the problem of understanding lay (for me).  In BASIC and FORTH it was impossible to have a function without a name.  In fact, the very thought was literally inconceivable.  At least for a few years.  Even in machine code where functions don&#8217;t have names they do have concrete locations, and they have names in the listing.</p>
<p>It wasn&#8217;t until many years later, in fact perhaps relatively recently, that I realised that there was a sort of hierarchy of things that must be named, and things that need not be named.  For example, an expression, like «a**2 + b**2 + c**2» is an anonymous value, a value that does not need a name.  More importantly, so are all the sub-expressions inside that expression.  What would a language look like if it didn&#8217;t have anonymous values?  Well, something like this:</p>
<pre name="code" class="python">

a2 = a**2
b2 = b**2
c2 = c**2
s = a2+b2
s = s + c2
</pre>
<p>Do you see?  Each expression must be given a name (by assigning it to a named variable), and no expression can contain a sub-expression, because that sub-expression would be anonymous.</p>
<p>Programming in assembler is a bit like this.  The names are the registers and every expression has to go in a register.  If you run out, you have to rename everything by spilling to the stack.  It&#8217;s partly why it can be so tedious.</p>
<p>So almost everything worthy of being called a language has anonymous values.  At least simple values, like numbers.  In the BASIC era it was common for strings to be named only.  The kinds of restrictions you had to deal with were things like not being able to create a string from part of an existing string.  You had to first create a named variable that was the target string, then copy part of the source string into the target string.  Maybe you had some string expressions but there were restrictions on where you could use them.  For example maybe you could only pass named strings to functions, so you could go «<code>T$=MID$(A$,12,16):PROCFOO(T$)</code>» but not pass the <code>MID$</code> expression directly: «<code>PROCFOO(MID$(A$,12,16))</code>» (bad bad bad).  Life was hard, and we licked coal for breakfast.</p>
<p>Arrays were another thing where you had to name them and couldn&#8217;t pass them to functions (for example).  AWK is still like this (arrays cannot be returned from a function, for example).</p>
<p>Speaking of arrays, an element of an array is a bit like an anonymous variable:</p>
<p><code>a[i] = x</code></p>
<p>Above, <code>a[i]</code> specifies the place where the computed value, <var>x</var> is this case, goes.  I say &#8220;a bit like&#8221; because you could equally well think of the element being named by the array name and an small integer as a pair.  A place where we can store a value that changes is called a <em>variable</em>.  You knew that right?  It&#8217;s just that most people think of <em>named variables</em> when you say variable.  The generalised term for &#8220;anything you can put on the left of an assignment&#8221; is <em>lvalue</em>.  A term you mostly see in the C programming language but often elsewhere too.  Common Lisp calls such things, <em><a href="http://www.ai.mit.edu/projects/iiip/doc/CommonLISP/HyperSpec/Body/glo_p.html#place">places</a></em>.  How sensible.</p>
<p>So the hierarchy of things you need not name starts something like:</p>
<li>simple numeric expressions</li>
<li>strings</li>
<li>arrays (including hash-tables, and so on)</li>
<li>objects (incuding structs, records, pairs, and so on)</li>
<li>functions</li>
<li>places (actually, not sure where this should go really)</li>
<p>As you go down the list, you see fewer and fewer languages that give you anonymous versions of the thing in question.  Although these days we probably all agree that any decent language will have anonymous versions of all of the above.  Python does, and that&#8217;s a Good Thing.</p>
<p>One reason for arranging things this way is that we can use it to both explain and motivate things like lambdas.  A lambda is an anonymous function, and you remember how useful it was when you realised you could have string expressions and you didn&#8217;t have to store intermediate strings in variables?  You could just manipulate strings without having to name them?  Well, the same is true of functions.</p>
<p>So what else could and should be anonymous?  Well we can look to other languages for inspiration.  ML has anonymous types.  Java has anonymous classes.  So perhaps the list can continue:</p>
<li>classes</li>
<li>types</li>
<p>(note that a class is a sort of type, so this order has a natural feel to it).</p>
<p>So everything I said about anonymous functions applies to anonymous classes and anonymous types.  They&#8217;re great, and every language needs them.  Basically my philosophy here is that if there&#8217;s a thing in a language and you can name it, then you should also be able to have anonymous versions of those things.  That will make the language better.</p>
<p>So what other sorts of things does Python have that we haven&#8217;t removed the names from yet?  <em>Modules</em>.  For a long while I wished that Python had anonymous modules, a way of getting hold of a module without it polluting your namespace.  Mostly I just wished for this because it would make Python more orthogonal, or <em>better</em> as I like to say.  Of course modules in Python are first class citizens.  Once you&#8217;ve imported the module <var>struct</var>, then <code>struct</code> refers to the module as a first class value, and it can be passed around and so on.  That&#8217;s how <code>help(struct)</code> works.  Recently I found both the way to get at modules anonymously (without importing them into a namespace) , and a reason why you might want to.</p>
<p>I&#8217;m a tidy sort of guy so when I program in Python I put my imports only in the functions that need them (a habit I picked up from <a href="http://commandline.org.uk/">Command Line Warriors</a> but buggered if I can find the original inspiration).  Well, sometimes I am.</p>
<p>I often find myself debugging my Python by the time honoured tradition of inserting <code>print</code> statements.  Because of my tidy habit, I quite often find that the <code>sys</code> module is not in scope (in fact, often the only function that has «<code>import sys</code>» is <code>main</code>.  So this fails:</p>
<p><code>print &gt;&gt;sys.stderr, "length:", l, "data:" x</code></p>
<p>And I&#8217;ll be damned if I&#8217;m going to add an <code>import sys</code> and forget to remove it later.  But this works:</p>
<p><code>print &gt;&gt;__import__('sys').stderr, "length:", l, "data:" x</code></p>
<p>Not a very nice syntax though, is it?</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/drj11.wordpress.com/550/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/drj11.wordpress.com/550/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/drj11.wordpress.com/550/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/drj11.wordpress.com/550/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/drj11.wordpress.com/550/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/drj11.wordpress.com/550/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/drj11.wordpress.com/550/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/drj11.wordpress.com/550/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/drj11.wordpress.com/550/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/drj11.wordpress.com/550/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=drj11.wordpress.com&blog=258145&post=550&subd=drj11&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://drj11.wordpress.com/2009/05/22/anonymous-things-in-python/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9c6dfaac50b9c43815dd18081e87f3e3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">drj11</media:title>
		</media:content>
	</item>
	</channel>
</rss>