<?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:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Rcjp's Weblog</title>
	<atom:link href="http://rcjp.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://rcjp.wordpress.com</link>
	<description>A mostly Coding and Physics weblog</description>
	<lastBuildDate>Sun, 22 Nov 2009 17:22:34 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='rcjp.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/7496d7538daef292b22e82b7d66d6564?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>Rcjp's Weblog</title>
		<link>http://rcjp.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://rcjp.wordpress.com/osd.xml" title="Rcjp&#8217;s Weblog" />
		<item>
		<title>Book: &#8220;Things Snowball&#8221; by Rich Hall</title>
		<link>http://rcjp.wordpress.com/2009/10/31/book-things-snowball-by-rich-hall/</link>
		<comments>http://rcjp.wordpress.com/2009/10/31/book-things-snowball-by-rich-hall/#comments</comments>
		<pubDate>Sat, 31 Oct 2009 17:18:07 +0000</pubDate>
		<dc:creator>rcjp</dc:creator>
				<category><![CDATA[books]]></category>

		<guid isPermaLink="false">http://rcjp.wordpress.com/?p=149</guid>
		<description><![CDATA[Just finished this paperback of short rants and tall tales warped with Rich Hall&#8217;s wry humour.  Best voiced in your head with his elastic American drawl stretching the words and snapping them back.  Picking it up a few times a day and reading through it like any other book isn&#8217;t a good approach [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rcjp.wordpress.com&blog=1667523&post=149&subd=rcjp&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Just finished this paperback of short rants and tall tales warped with Rich Hall&#8217;s wry humour.  Best voiced in your head with his elastic American drawl stretching the words and snapping them back.  Picking it up a few times a day and reading through it like any other book isn&#8217;t a good approach though: you have to be in the right mood to laugh at his jarring nonsense.  But when you are, his best work like stories of his grandfolks small nuclear plant, are head shakingly comical.</p>
<p>The cutting rants are always funny and sharply observed &#8220;Las Vegas is what America deserves &#8230; two thousand years of civilization and the planet&#8217;s most visible creation is a lit-up string of clip joints in the Mojave Desert&#8221;.   After recently having to spent a few mostly vile days there I was left wondering if Vegas is the ultimate destiny of cities all over the world; the product of limitless money pandering to what the majority of ordinary people really desire.  But I couldn&#8217;t quite capture a description of the objectless mass wanderings up and down the Vegas strip until I read Rich Hall&#8217;s description of visitors standing on the &#8216;people movers&#8217; &#8211; those horizontal metal escalators walkways as being &#8220;&#8230;perfectly content as they creep along like God&#8217;s unclaimed luggage&#8221;.  Genius.   </p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rcjp.wordpress.com/149/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rcjp.wordpress.com/149/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rcjp.wordpress.com/149/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rcjp.wordpress.com/149/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rcjp.wordpress.com/149/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rcjp.wordpress.com/149/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rcjp.wordpress.com/149/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rcjp.wordpress.com/149/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rcjp.wordpress.com/149/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rcjp.wordpress.com/149/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rcjp.wordpress.com&blog=1667523&post=149&subd=rcjp&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://rcjp.wordpress.com/2009/10/31/book-things-snowball-by-rich-hall/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a54c0b4049118721decd42bd4a29abd8?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rcjp</media:title>
		</media:content>
	</item>
		<item>
		<title>Book: “Coders at Work” by Peter Seibel</title>
		<link>http://rcjp.wordpress.com/2009/10/28/review-of-the-book-%e2%80%9ccoders-at-work%e2%80%9d-by-peter-seibel/</link>
		<comments>http://rcjp.wordpress.com/2009/10/28/review-of-the-book-%e2%80%9ccoders-at-work%e2%80%9d-by-peter-seibel/#comments</comments>
		<pubDate>Wed, 28 Oct 2009 23:04:28 +0000</pubDate>
		<dc:creator>rcjp</dc:creator>
				<category><![CDATA[books]]></category>

		<guid isPermaLink="false">http://rcjp.wordpress.com/?p=135</guid>
		<description><![CDATA[Over the weekend I read Peter Seibel’s latest book in which he interviews fifteen big name programmers using a set of similar questions about their programming approach and background.  
Apart from a few slow sections it is a worthwhile read.  Most interviews I’ve read before of famous coders gloss over interesting answers, but [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rcjp.wordpress.com&blog=1667523&post=135&subd=rcjp&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Over the weekend I read Peter Seibel’s latest book in which he interviews fifteen big name programmers using a set of similar questions about their programming approach and background.  </p>
<p>Apart from a few slow sections it is a worthwhile read.  Most interviews I’ve read before of famous coders gloss over interesting answers, but Peter is a practicing programmer and his personal interest shows through in attempting to dig a little deeper.  Having said that, unfortunately I don’t think his probing turned up much gold.  If you are hoping, as I was, to glean some productive techniques from these computing gods you are likely to be disappointed.  </p>
<p>Anyway, on a first reading at least, I pulled out some quotes I thought insightful:</p>
<blockquote><p> “<em>The physical properties of matter are such that 99.9 percent of the time you can understand it in aggregate.  And everything you have to know about it, you can understand from dealing with it in aggregate.  To a great extent, that is not true in the world of software.</em>” &#8230; L Peter Deutsch
</p></blockquote>
<p>And later talking about advice from Jerry Elkind </p>
<blockquote><p>“<em>&#8230;measure things.  Even sometimes measure things you don’t think you need to measure.  That [advice]  had a profound effect on me.</em>”
</p></blockquote>
<p>Ken Thompson who, it is rather surprisingly revealed, doesn&#8217;t have code commit privileges where he works at Google said</p>
<blockquote><p> “I’ve never been a lover of existing code.  Code by itself almost rots and it’s gotta be rewritten.  Even when nothing has changed, for some reason it rots.”</p></blockquote>
<p>Grouping the interviews collectively is perhaps a wrong of me to attempt, but the book as a whole gives the impression of these hackers with sharp minds, primitive tools and not much interest in the software development literature.  Quick to rewrite the code they don’t understand, mulling over problems in their head a few weeks, before jotting down a few data structures and debugging the resulting code with print statements.  </p>
<p>I guess the book could have been titled “Greybeards Still A Work” as almost all are old-timers as evidenced by the answers to the usual opening question of “When did you learn to program”.  The answer is often a lengthy description of some antique paper tape hardware for which they rewrote the operating system over a long weekend.  So I&#8217;m not sure this book is a picture of an average &#8220;coder at work&#8221; and there is also a definite west-coast lisp slant to the book, not surprising really as Peter is a west-coast lisper!</p>
<p>There were a few lines of questioning that didn’t seem to lead anywhere like asking all of them if they’d read Knuths ‘The Art of Programming’ (mostly a little bit) and if they’d used literate programming (nope) and what they thought of C++ (they hated it).  But, even with these dead ends, its definitely worth a read if only to confirm there isn’t much in the way of programming magic out there &#8211; just persistence and obstinacy and little inhibitions about rewriting code.</p>
<p>Finally, I can’t resist a few of quotes that made me laugh:</p>
<blockquote><p>“<em>The whole Web is built on one mistake after another.  We have this big pile of accidents.</em>”&#8230;Douglas Crockford </p></blockquote>
<blockquote><p>Seibel:  <em>I think Larry Wall described [lisp] as a bowl of oatmeal with fingernail clippings in it.</em><br />
Deutsch: <em>Well, my description of Perl is something that looks like it came out of the wrong end of a dog.</em>
</p></blockquote>
<p>and my favourite quote from Joe</p>
<blockquote><p>“<em>&#8230;the problem with object-oriented languages is that they’ve got all this implicit environment that they carry around with them.  You wanted a banana but what you got was a gorilla holding the banana and the entire jungle.</em>&#8230; Joe Armstrong ”
</p></blockquote>
<p>Right, now I’m off to go and measure something I don’t think I need to measure&#8230;</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rcjp.wordpress.com/135/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rcjp.wordpress.com/135/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rcjp.wordpress.com/135/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rcjp.wordpress.com/135/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rcjp.wordpress.com/135/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rcjp.wordpress.com/135/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rcjp.wordpress.com/135/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rcjp.wordpress.com/135/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rcjp.wordpress.com/135/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rcjp.wordpress.com/135/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rcjp.wordpress.com&blog=1667523&post=135&subd=rcjp&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://rcjp.wordpress.com/2009/10/28/review-of-the-book-%e2%80%9ccoders-at-work%e2%80%9d-by-peter-seibel/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a54c0b4049118721decd42bd4a29abd8?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rcjp</media:title>
		</media:content>
	</item>
		<item>
		<title>Redirecting stdout within ipython</title>
		<link>http://rcjp.wordpress.com/2009/03/24/redirecting-stdout-within-ipython/</link>
		<comments>http://rcjp.wordpress.com/2009/03/24/redirecting-stdout-within-ipython/#comments</comments>
		<pubDate>Tue, 24 Mar 2009 00:09:58 +0000</pubDate>
		<dc:creator>rcjp</dc:creator>
				<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://rcjp.wordpress.com/?p=130</guid>
		<description><![CDATA[A friend was asking if I knew a way of grabbing the output from a python function within ipython.  I couldn&#8217;t see an easy way (the normal shell way of &#8216;&#62;&#8217; redirection won&#8217;t work).  So the following is a quick hack on IPython&#8217;s OutputTrap:
Sample session, just defining a function that outputs something and [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rcjp.wordpress.com&blog=1667523&post=130&subd=rcjp&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>A friend was asking if I knew a way of grabbing the output from a python function within ipython.  I couldn&#8217;t see an easy way (the normal shell way of &#8216;&gt;&#8217; redirection won&#8217;t work).  So the following is a quick hack on IPython&#8217;s OutputTrap:</p>
<p>Sample session, just defining a function that outputs something and I&#8217;m also using the ipython trick of preceding the command with a comma to make the args into strings&#8230;</p>
<p><code>
<pre>In [1]: <font color="#000000"><b>def</b></font>&nbsp;<font color="#660000">myfunc</font>():
&nbsp;&nbsp; ...:&nbsp;&nbsp;&nbsp;&nbsp; <font color="#000000"><b>print</b></font>&nbsp;<span style="background-color:#ffffcc;"><font color="#000000">&#39;</font></span><span style="background-color:#ffff00;"><font color="#006666">hello</font></span><span style="background-color:#ffffcc;"><font color="#000000">&#39;</font></span>
&nbsp;&nbsp; ...:&nbsp;&nbsp;&nbsp;&nbsp; 
&nbsp;&nbsp; ...:&nbsp;&nbsp;&nbsp;&nbsp; 

In [2]: run grab

In [3]: ,grab dumpfile myfunc()</pre>
<p></code></p>
<p>and grab.py is&#8230;</p>
<p><code>
<pre><font color="#808040">import</font>&nbsp;sys
<font color="#808040">from</font>&nbsp;IPython <font color="#808040">import</font>&nbsp;OutputTrap


<font color="#000000"><b>def</b></font>&nbsp;<font color="#660000">grab</font>(fname, cmd):
&nbsp;&nbsp;&nbsp;&nbsp;dump = OutputTrap.OutputTrap(<span style="background-color:#ffffcc;"><font color="#000000">&#39;</font></span><span style="background-color:#ffff00;"><font color="#006666">dump</font></span><span style="background-color:#ffffcc;"><font color="#000000">&#39;</font></span>,<span style="background-color:#ffffcc;"><font color="#000000">&#39;&#39;</font></span>,<span style="background-color:#ffffcc;"><font color="#000000">&#39;&#39;</font></span>,trap_out=1,quiet_out=1)
&nbsp;&nbsp;&nbsp;&nbsp;dump.trap_all()
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#000000"><b>try</b></font>:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;eval(cmd, sys._getframe(1).f_globals, sys._getframe(1).f_locals)
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#000000"><b>except</b></font>:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#000000"><b>print</b></font>&nbsp;sys.exc_info()

&nbsp;&nbsp;&nbsp;&nbsp;dump.release_all()
&nbsp;&nbsp;&nbsp;&nbsp;file(fname,<span style="background-color:#ffffcc;"><font color="#000000">&#39;</font></span><span style="background-color:#ffff00;"><font color="#006666">w</font></span><span style="background-color:#ffffcc;"><font color="#000000">&#39;</font></span>).writelines( dump.summary() )
&nbsp;&nbsp;&nbsp;&nbsp;dump.flush_all()
</pre>
<p></code></p>
<p>&#8230; and the output of myfunc() appears in the file &#8216;dumpfile&#8217; so it seems to work, but note I haven&#8217;t done much testing.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rcjp.wordpress.com/130/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rcjp.wordpress.com/130/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rcjp.wordpress.com/130/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rcjp.wordpress.com/130/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rcjp.wordpress.com/130/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rcjp.wordpress.com/130/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rcjp.wordpress.com/130/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rcjp.wordpress.com/130/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rcjp.wordpress.com/130/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rcjp.wordpress.com/130/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rcjp.wordpress.com&blog=1667523&post=130&subd=rcjp&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://rcjp.wordpress.com/2009/03/24/redirecting-stdout-within-ipython/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a54c0b4049118721decd42bd4a29abd8?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rcjp</media:title>
		</media:content>
	</item>
		<item>
		<title>Gaussian PIL Image Filter</title>
		<link>http://rcjp.wordpress.com/2008/04/02/gaussian-pil-image-filter/</link>
		<comments>http://rcjp.wordpress.com/2008/04/02/gaussian-pil-image-filter/#comments</comments>
		<pubDate>Wed, 02 Apr 2008 18:54:39 +0000</pubDate>
		<dc:creator>rcjp</dc:creator>
				<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://rcjp.wordpress.com/2008/04/02/gaussian-pil-image-filter/</guid>
		<description><![CDATA[I could not see a gaussian filter in the python imaging library, but its simple enough to write one&#8230;

import&#160;ImageFilter
from&#160;PIL import&#160;Image
from&#160;numpy import&#160;*

def&#160;gaussian_grid(size = 5):
&#160;&#160;&#160;&#160;&#34;&#34;&#34;
&#160;&#160;&#160;&#160;Create a square grid of integers of gaussian shape
&#160;&#160;&#160;&#160;e.g. gaussian_grid() returns
&#160;&#160;&#160;&#160;array([[ 1,&#160;&#160;4,&#160;&#160;7,&#160;&#160;4,&#160;&#160;1],
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [ 4, 20, 33, 20,&#160;&#160;4],
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [ 7, 33, 55, 33,&#160;&#160;7],
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [ 4, 20, 33, 20,&#160;&#160;4],
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [ 1,&#160;&#160;4,&#160;&#160;7,&#160;&#160;4,&#160;&#160;1]])
&#160;&#160;&#160;&#160;&#34;&#34;&#34;
&#160;&#160;&#160;&#160;m = size/2
&#160;&#160;&#160;&#160;n = m+1&#160;&#160;# [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rcjp.wordpress.com&blog=1667523&post=115&subd=rcjp&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>I could not see a gaussian filter in the python imaging library, but its simple enough to write one&#8230;</p>
<p><code>
<pre><font color="#808040">import</font>&nbsp;ImageFilter
<font color="#808040">from</font>&nbsp;PIL <font color="#808040">import</font>&nbsp;Image
<font color="#808040">from</font>&nbsp;numpy <font color="#808040">import</font>&nbsp;*

<font color="#000000"><b>def</b></font>&nbsp;<font color="#660000">gaussian_grid</font>(size = 5):
&nbsp;&nbsp;&nbsp;&nbsp;<span style="background-color:#ffffcc;"><font color="#000000">&quot;&quot;&quot;</font></span>
<span style="background-color:#ffff00;"><font color="#006666">&nbsp;&nbsp;&nbsp;&nbsp;Create a square grid of integers of gaussian shape</font></span>
<span style="background-color:#ffff00;"><font color="#006666">&nbsp;&nbsp;&nbsp;&nbsp;e.g. gaussian_grid() returns</font></span>
<span style="background-color:#ffff00;"><font color="#006666">&nbsp;&nbsp;&nbsp;&nbsp;array([[ 1,&nbsp;&nbsp;4,&nbsp;&nbsp;7,&nbsp;&nbsp;4,&nbsp;&nbsp;1],</font></span>
<span style="background-color:#ffff00;"><font color="#006666">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [ 4, 20, 33, 20,&nbsp;&nbsp;4],</font></span>
<span style="background-color:#ffff00;"><font color="#006666">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [ 7, 33, 55, 33,&nbsp;&nbsp;7],</font></span>
<span style="background-color:#ffff00;"><font color="#006666">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [ 4, 20, 33, 20,&nbsp;&nbsp;4],</font></span>
<span style="background-color:#ffff00;"><font color="#006666">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [ 1,&nbsp;&nbsp;4,&nbsp;&nbsp;7,&nbsp;&nbsp;4,&nbsp;&nbsp;1]])</font></span>
<span style="background-color:#ffff00;"><font color="#006666">&nbsp;&nbsp;&nbsp;&nbsp;</font></span><span style="background-color:#ffffcc;"><font color="#000000">&quot;&quot;&quot;</font></span>
&nbsp;&nbsp;&nbsp;&nbsp;m = size/2
&nbsp;&nbsp;&nbsp;&nbsp;n = m+1&nbsp;&nbsp;<font color="#b0b0b0"><i># remember python is &#39;upto&#39; n in the range below</i></font>
&nbsp;&nbsp;&nbsp;&nbsp;x, y = mgrid[-m:n,-m:n]
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#b0b0b0"><i># multiply by a factor to get 1 in the corner of the grid</i></font>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#b0b0b0"><i># ie for a 5x5 grid&nbsp;&nbsp; fac*exp(-0.5*(2**2 + 2**2)) = 1</i></font>
&nbsp;&nbsp;&nbsp;&nbsp;fac = exp(m**2)
&nbsp;&nbsp;&nbsp;&nbsp;g = fac*exp(-0.5*(x**2 + y**2))
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#000000"><b>return</b></font>&nbsp;g.round().astype(int)

<font color="#000000"><b>class</b></font>&nbsp;<font color="#660000">GAUSSIAN</font>(ImageFilter.BuiltinFilter):
&nbsp;&nbsp;&nbsp;&nbsp;name = <span style="background-color:#ffffcc;"><font color="#000000">&quot;</font></span><span style="background-color:#ffff00;"><font color="#006666">Gaussian</font></span><span style="background-color:#ffffcc;"><font color="#000000">&quot;</font></span>
&nbsp;&nbsp;&nbsp;&nbsp;gg = gaussian_grid().flatten().tolist()
&nbsp;&nbsp;&nbsp;&nbsp;filterargs = (5,5), sum(gg), 0, tuple(gg)


im = Image.open(<span style="background-color:#ffffcc;"><font color="#000000">&#39;</font></span><span style="background-color:#ffff00;"><font color="#006666">/home/rcjp/tmp/test.png</font></span><span style="background-color:#ffffcc;"><font color="#000000">&#39;</font></span>)
im1 = im.filter(GAUSSIAN)
im1.save(<span style="background-color:#ffffcc;"><font color="#000000">&#39;</font></span><span style="background-color:#ffff00;"><font color="#006666">/home/rcjp/tmp/testfiltered.png</font></span><span style="background-color:#ffffcc;"><font color="#000000">&#39;</font></span>)</pre>
<p></code></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/rcjp.wordpress.com/115/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/rcjp.wordpress.com/115/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rcjp.wordpress.com/115/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rcjp.wordpress.com/115/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rcjp.wordpress.com/115/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rcjp.wordpress.com/115/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rcjp.wordpress.com/115/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rcjp.wordpress.com/115/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rcjp.wordpress.com/115/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rcjp.wordpress.com/115/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rcjp.wordpress.com/115/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rcjp.wordpress.com/115/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rcjp.wordpress.com&blog=1667523&post=115&subd=rcjp&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://rcjp.wordpress.com/2008/04/02/gaussian-pil-image-filter/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a54c0b4049118721decd42bd4a29abd8?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rcjp</media:title>
		</media:content>
	</item>
		<item>
		<title>K language &#8211; ultimate coding brevity?</title>
		<link>http://rcjp.wordpress.com/2008/02/25/k-language-ultimate-coding-brevity/</link>
		<comments>http://rcjp.wordpress.com/2008/02/25/k-language-ultimate-coding-brevity/#comments</comments>
		<pubDate>Mon, 25 Feb 2008 17:56:01 +0000</pubDate>
		<dc:creator>rcjp</dc:creator>
				<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://rcjp.wordpress.com/?p=109</guid>
		<description><![CDATA[After reading a thread on c.l.l and, over the last week, more than I care to on arc&#8217;s emphasis on brevity, I can&#8217;t decide if I&#8217;m impressed or appalled by the K language.
As K seems to be a proprietary language I wouldn&#8217;t normally look at it, but thankfully the Wikipedia entry has a link to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rcjp.wordpress.com&blog=1667523&post=109&subd=rcjp&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>After reading a <a href="http://groups.google.co.uk/group/comp.lang.lisp/browse_thread/thread/5743277182ac6678/caa0f2a6813ca73b#caa0f2a6813ca73b">thread on c.l.l</a> and, over the last week, more than I care to on arc&#8217;s emphasis on brevity, I can&#8217;t decide if I&#8217;m impressed or appalled by the <a href="http://en.wikipedia.org/wiki/K_(programming_language)">K language</a>.</p>
<p>As K seems to be a proprietary language I wouldn&#8217;t normally look at it, but thankfully the Wikipedia entry has a link to a screencast by Michael Schidlowsky solving the <a href="http://en.wikipedia.org/wiki/Birthday_paradox">birthday problem</a> &#8211; how many people do you need in a room to, more often than not, have two or more persons with the same birthday.  In K, to simulate 1000 rooms with 10 people in them its<br />
<code><br />
#+/{~(#x)=#?x}' {(1000,x) _draw 365} 10<br />
</code><br />
using ipython I&#8217;d do:<br />
<code>
<pre>In [1]: <font color="#808040">from</font>&nbsp;random <font color="#808040">import</font>&nbsp;randint
In [2]: <font color="#000000"><b>def</b></font>&nbsp;<font color="#660000">samebday</font>(n): <font color="#000000"><b>return</b></font>&nbsp;n != len(set(randint(1,365) <font color="#000000"><b>for</b></font>&nbsp;i in&nbsp;range(n)))
In [3]: <font color="#000000"><b>def</b></font>&nbsp;<font color="#660000">bday1000</font>(n): <font color="#000000"><b>return</b></font>&nbsp;[samebday(n) <font color="#000000"><b>for</b></font>&nbsp;i in&nbsp;range(1000)].count(True)
In [4]: bday1000(23)
Out[4]: 496
In [5]: %timeit bday1000(23)
10 loops, best of 3: 367 ms per loop</pre>
<p></code><br />
Casting the list of random &#8216;birthdays&#8217; (just integers from 1..365) to a python <code>set</code> removes any duplicates, so just checking if the length has changed will show if two or more were the same.  The tipping point is 23 according to wikipedia, and the bday1000 function simulating 1000 rooms shows 496 of those rooms had common birthdays &#8211; which is roughly half of them.  Not as short as K, but concise enough for me I think.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/rcjp.wordpress.com/109/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/rcjp.wordpress.com/109/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rcjp.wordpress.com/109/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rcjp.wordpress.com/109/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rcjp.wordpress.com/109/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rcjp.wordpress.com/109/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rcjp.wordpress.com/109/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rcjp.wordpress.com/109/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rcjp.wordpress.com/109/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rcjp.wordpress.com/109/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rcjp.wordpress.com/109/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rcjp.wordpress.com/109/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rcjp.wordpress.com&blog=1667523&post=109&subd=rcjp&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://rcjp.wordpress.com/2008/02/25/k-language-ultimate-coding-brevity/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a54c0b4049118721decd42bd4a29abd8?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rcjp</media:title>
		</media:content>
	</item>
		<item>
		<title>Playing with Arc</title>
		<link>http://rcjp.wordpress.com/2008/01/31/playing-with-arc/</link>
		<comments>http://rcjp.wordpress.com/2008/01/31/playing-with-arc/#comments</comments>
		<pubDate>Thu, 31 Jan 2008 12:12:14 +0000</pubDate>
		<dc:creator>rcjp</dc:creator>
				<category><![CDATA[lisp]]></category>

		<guid isPermaLink="false">http://rcjp.wordpress.com/?p=108</guid>
		<description><![CDATA[Paul Graham has made available a tarball prerelease of his new arc language.  I&#8217;m currently running Ubuntu gutsy on my laptop so it needs a quick patch

~/lisp&#62; tar -xvf ~/tmp/arc0.tar 
~/lisp&#62; cd arc0
~/lisp/arc0&#62; wget http://blakeley.com/linux-date.patch
~/lisp/arc0&#62; patch arc.arc linux-date.patch 
~/lisp/arc0&#62; rlwrap -C arc mzscheme -m -f as.scm
Use (quit) to quit, (tl) to return here after [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rcjp.wordpress.com&blog=1667523&post=108&subd=rcjp&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Paul Graham has made available a tarball prerelease of his new <a href="http://www.arclanguage.org/">arc language</a>.  I&#8217;m currently running Ubuntu gutsy on my laptop so it needs a quick patch</p>
<p><code>
<pre>~/lisp&gt; tar -xvf ~/tmp/arc0.tar 
~/lisp&gt; cd arc0
~/lisp/arc0&gt; wget <a href="http://blakeley.com/linux-date.patch">http://blakeley.com/linux-date.patch</a>
~/lisp/arc0&gt; patch arc.arc linux-date.patch 
~/lisp/arc0&gt; rlwrap -C arc mzscheme -m -f as.scm
Use (quit) to quit, (tl) to return here after an interrupt.
arc&gt; </pre>
<p></code></p>
<p>then you can go through the tutorial, or play with the webserver, which we&#8217;ll start in a new thread</p>
<p><code>
<pre>arc&gt; (defop hello req (pr &quot;hello world&quot;))
#&lt;procedure:gs1431&gt;
arc&gt; (= webtest (thread asv))
#&lt;thread: asv&gt;
arc&gt; ready to serve port 8080</pre>
<p></code></p>
<p>and then look at http://localhost:8080/hello </p>
<p>The prompt doesn&#8217;t quite come back correctly here, but you can carry on typing</p>
<p><code>
<pre>(defop hello req (pr &quot;hello world changed&quot;))
#&lt;procedure:gs1438&gt;</pre>
<p></code></p>
<p>and refresh the browser.  Kill the thread and quit with</p>
<p><code>
<pre>arc&gt; (break-thread webtest) 
arc&gt; (quit)</pre>
<p></code></p>
<p>Though initially disappointed that there was no native compiler (written in arc of course!), I&#8217;ve warmed to the syntax &#8211; I didn&#8217;t find myself in a fighting the language, or frustrated that I had to reinvent stuff I didn&#8217;t want to e.g., here&#8217;s how to split up a string using spaces or commas</p>
<p><code>
<pre>arc&gt; (tokens &quot;abc def,ghi&quot; [in _ #\ #\,])
(&quot;abc&quot; &quot;def&quot; &quot;ghi&quot;)</pre>
<p></code></p>
<p>There&#8217;s plenty to play with by reading through the source.  So far, it feels fun.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/rcjp.wordpress.com/108/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/rcjp.wordpress.com/108/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rcjp.wordpress.com/108/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rcjp.wordpress.com/108/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rcjp.wordpress.com/108/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rcjp.wordpress.com/108/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rcjp.wordpress.com/108/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rcjp.wordpress.com/108/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rcjp.wordpress.com/108/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rcjp.wordpress.com/108/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rcjp.wordpress.com/108/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rcjp.wordpress.com/108/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rcjp.wordpress.com&blog=1667523&post=108&subd=rcjp&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://rcjp.wordpress.com/2008/01/31/playing-with-arc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a54c0b4049118721decd42bd4a29abd8?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rcjp</media:title>
		</media:content>
	</item>
		<item>
		<title>Show Nearest X11 System Colours</title>
		<link>http://rcjp.wordpress.com/2007/10/17/nearest-rgb-colours/</link>
		<comments>http://rcjp.wordpress.com/2007/10/17/nearest-rgb-colours/#comments</comments>
		<pubDate>Wed, 17 Oct 2007 13:16:45 +0000</pubDate>
		<dc:creator>rcjp</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[utils]]></category>

		<guid isPermaLink="false">http://rcjp.wordpress.com/2007/10/17/nearest-rgb-colours/</guid>
		<description><![CDATA[
Just a little utility to show the nearest system rgb colours (taken from rgb.txt) on an X11 system compared to the one under the cursor.  
I couldn&#8217;t work out how to listen to mouse messages when I don&#8217;t own the window under the mouse pointer (on X Windows anyway) &#8211; so I cheated and [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rcjp.wordpress.com&blog=1667523&post=96&subd=rcjp&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p><a href='http://rcjp.files.wordpress.com/2007/10/colourinfo.png' title='ColourInfo'><img src='http://rcjp.files.wordpress.com/2007/10/colourinfo.png' alt='ColourInfo' align="right" /></a><br />
Just a little utility to show the nearest system rgb colours (taken from rgb.txt) on an X11 system compared to the one under the cursor.  </p>
<p>I couldn&#8217;t work out how to listen to mouse messages when I don&#8217;t own the window under the mouse pointer (on X Windows anyway) &#8211; so I cheated and this code takes a snapshot of the screen and displays it in the background.  Obviously you can&#8217;t move/click on any windows when this thing is running! quit with &#8216;q&#8217; or &#8216;ESC&#8217; or just close the window.</p>
<p><code>
<pre>
<font color="#c0c0c0"><i>#!/usr/bin/env python</i></font>
<font color="#c0c0c0"><i>#</i></font>
<font color="#c0c0c0"><i># Takes a screenshot image of the root window and display a table of </i></font>
<font color="#c0c0c0"><i># nearest system colours compared to that under the mouse pointer</i></font>
<font color="#c0c0c0"><i># can specify an argument 1..29 to show more colours (default 6)</i></font>
<font color="#c0c0c0"><i># The window title shows the exact rgb values of pointer in hex</i></font>
<font color="#c0c0c0"><i>#</i></font>
<font color="#808040">import</font> gtk, re
<font color="#808040">from</font> sys <font color="#808040">import</font> argv

RGBCOLOURS = <span style="background-color:#ffffcc;"><font color="#000000">&#39;</font></span><span style="background-color:#ffff00;"><font color="#006666">/etc/X11/rgb.txt</font></span><span style="background-color:#ffffcc;"><font color="#000000">&#39;</font></span>   <font color="#c0c0c0"><i># rgb colour data</i></font>

<font color="#000000"><b>class</b></font> <font color="#660000">ScreenColour</font>(object):

    syscolours = {}  <font color="#c0c0c0"><i># hold system rgb.txt relating colours to names</i></font>

    <font color="#000000"><b>def</b></font> <font color="#660000">__init__</font>(self, rgbfile=RGBCOLOURS):
        <font color="#c0c0c0"><i># 1 pixel buffer for pixel under mouse pointer</i></font>
        self.pix = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB,False, 8, 1, 1)
        <font color="#000000"><b>for</b></font> line in file(rgbfile).readlines():
            <font color="#000000"><b>if</b></font> not line.startswith(<span style="background-color:#ffffcc;"><font color="#000000">&#39;</font></span><span style="background-color:#ffff00;"><font color="#006666">!</font></span><span style="background-color:#ffffcc;"><font color="#000000">&#39;</font></span>):
                rgbname = re.compile(<span style="background-color:#ffffcc;"><font color="#000000">r&#39;</font></span><span style="background-color:#ffff00;"><font color="#006666">\s*(\d+)\s*(\d+)\s*(\d+)\s*(.+)</font></span><span style="background-color:#ffffcc;"><font color="#000000">&#39;</font></span>)
                (r,g,b,name) = rgbname.match(line).groups()
                self.syscolours[name.rstrip()] = (int(r),int(g),int(b))

    <font color="#000000"><b>def</b></font> <font color="#660000">cmp_screencolour</font>(self,col,basecol):
        <span style="background-color:#ffffcc;"><font color="#000000">&quot;&quot;&quot;</font></span><span style="background-color:#ffff00;"><font color="#006666">Numerical difference between colours col(name) and basecol(r,g,b)</font></span><span style="background-color:#ffffcc;"><font color="#000000">&quot;&quot;&quot;</font></span>
        <font color="#000000"><b>return</b></font> sum(abs(a-b) <font color="#000000"><b>for</b></font> a, b in zip(basecol, ScreenColour.syscolours[col]))

    <font color="#000000"><b>def</b></font> <font color="#660000">pixelinfo</font>(self):
        <span style="background-color:#ffffcc;"><font color="#000000">&quot;&quot;&quot;</font></span><span style="background-color:#ffff00;"><font color="#006666">Returns the (r,g,b) value of colour under mouse pointer</font></span><span style="background-color:#ffffcc;"><font color="#000000">&quot;&quot;&quot;</font></span>
        (_, x, y, _) = gtk.gdk.display_get_default().get_pointer()
        self.pix.get_from_drawable(gtk.gdk.screen_get_default().get_root_window(),
                                   gtk.gdk.colormap_get_system(),
                                   x,y, 0,0, 1,1)
        col = self.pix.get_pixels_array()
        <font color="#000000"><b>return</b></font> (int(col[0,0,0]), int(col[0,0,1]), int(col[0,0,2]))

    <font color="#000000"><b>def</b></font> <font color="#660000">nearest_colours</font>(self, n, basecol):
        <span style="background-color:#ffffcc;"><font color="#000000">&quot;&quot;&quot;</font></span><span style="background-color:#ffff00;"><font color="#006666">Return the nearest n system colours compared to basecol</font></span><span style="background-color:#ffffcc;"><font color="#000000">&quot;&quot;&quot;</font></span>
        nearest = self.syscolours.keys()
        nearest.sort(key=<font color="#000000"><b>lambda</b></font> (c): self.cmp_screencolour(c, basecol))
        <font color="#000000"><b>return</b></font> nearest[:n]

<font color="#000000"><b>class</b></font> <font color="#660000">ColourInfoWindow</font>(ScreenColour):

    <span style="background-color:#ffffcc;"><font color="#000000">&quot;&quot;&quot;</font></span><span style="background-color:#ffff00;"><font color="#006666">Draw a table of colours nearest to that under mouse pointer</font></span><span style="background-color:#ffffcc;"><font color="#000000">&quot;&quot;&quot;</font></span>
    label = []
    eb = []
    oldrgb = (-1,-1,-1)

    <font color="#000000"><b>def</b></font> <font color="#660000">delete_event</font>(self, widget, event, data=None):
        gtk.main_quit()
        <font color="#000000"><b>return</b></font> False

    <font color="#000000"><b>def</b></font> <font color="#660000">__init__</font>(self, tablesize):
        self.tablesize = tablesize
        self.image = gtk.Window()
        self.image.set_decorated(False)
        self.image.add_events(gtk.gdk.POINTER_MOTION_MASK)
        self.image.connect(<span style="background-color:#ffffcc;"><font color="#000000">&quot;</font></span><span style="background-color:#ffff00;"><font color="#006666">motion_notify_event</font></span><span style="background-color:#ffffcc;"><font color="#000000">&quot;</font></span>, self.event_handler)
        <font color="#c0c0c0"><i>#</i></font>
        <font color="#c0c0c0"><i># set background from a screen shot of the root window</i></font>
        <font color="#c0c0c0"><i>#</i></font>
        screen = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8,
                                gtk.gdk.screen_width(), gtk.gdk.screen_height())
        screen.get_from_drawable(gtk.gdk.get_default_root_window(),
                                 gtk.gdk.colormap_get_system(), 0, 0, 0, 0,
                                 gtk.gdk.screen_width(), gtk.gdk.screen_height())
        pixmap, mask = screen.render_pixmap_and_mask()
        self.image.set_app_paintable(True)
        self.image.realize()
        self.image.window.set_back_pixmap(pixmap, False)
        <font color="#000000"><b>del</b></font> pixmap
        self.image.fullscreen()
        self.image.show()

        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.set_size_request(200, 40*self.tablesize)
        self.window.set_resizable(False)
        self.window.set_transient_for(self.image)  <font color="#c0c0c0"><i># stay above screen image</i></font>
        self.window.set_title(<span style="background-color:#ffffcc;"><font color="#000000">&quot;</font></span><span style="background-color:#ffff00;"><font color="#006666">Colour Info</font></span><span style="background-color:#ffffcc;"><font color="#000000">&quot;</font></span>)
        self.window.connect(<span style="background-color:#ffffcc;"><font color="#000000">&quot;</font></span><span style="background-color:#ffff00;"><font color="#006666">delete_event</font></span><span style="background-color:#ffffcc;"><font color="#000000">&quot;</font></span>, self.delete_event)
        self.window.add_events(gtk.gdk.KEY_PRESS_MASK)
        self.window.connect(<span style="background-color:#ffffcc;"><font color="#000000">&quot;</font></span><span style="background-color:#ffff00;"><font color="#006666">key-press-event</font></span><span style="background-color:#ffffcc;"><font color="#000000">&quot;</font></span>, self.event_keys)

        self.Colour = ScreenColour()
        vbox = gtk.VBox(True,2)
        <font color="#000000"><b>for</b></font> i in range(self.tablesize):
            self.label.append(gtk.Label(<span style="background-color:#ffffcc;"><font color="#000000">&quot;</font></span><span style="background-color:#ffff00;"><font color="#006666">Unknown</font></span><span style="background-color:#ffffcc;"><font color="#000000">&quot;</font></span>))
            self.eb.append(gtk.EventBox())
            self.eb[i].add(self.label[i])
            vbox.add(self.eb[i])
        self.window.add(vbox)
        self.window.show_all()
        self.update_colours()

    <font color="#000000"><b>def</b></font> <font color="#660000">update_colours</font>(self):
        rgb = self.Colour.pixelinfo()
        <font color="#000000"><b>if</b></font> self.oldrgb != rgb:
            self.oldrgb = rgb
            nearest = self.Colour.nearest_colours(self.tablesize, rgb)
            self.window.set_title(<span style="background-color:#ffffcc;"><font color="#000000">&quot;</font></span><span style="background-color:#ffff00;"><font color="#006666">%02X %02X %02X</font></span><span style="background-color:#ffffcc;"><font color="#000000">&quot;</font></span> % rgb)
            <font color="#000000"><b>for</b></font> i in range(self.tablesize):
                <font color="#000000"><b>try</b></font>:
                    self.eb[i].modify_bg(gtk.STATE_NORMAL,
                                         gtk.gdk.color_parse(nearest[i]))
                    <font color="#c0c0c0"><i>#</i></font>
                    <font color="#c0c0c0"><i># if brighter than 128,128,128 switch to </i></font>
                    <font color="#c0c0c0"><i># a black background so text is visable</i></font>
                    <font color="#c0c0c0"><i>#</i></font>
                    <font color="#000000"><b>if</b></font> (sum(self.syscolours[nearest[i]]) &gt; 384):
                        w = <span style="background-color:#ffffcc;"><font color="#000000">&#39;</font></span><span style="background-color:#ffff00;"><font color="#006666">&lt;span foreground=&quot;black&quot;&gt;%s&lt;/span&gt;</font></span><span style="background-color:#ffffcc;"><font color="#000000">&#39;</font></span> % nearest[i]
                    <font color="#000000"><b>else</b></font>:
                        w = <span style="background-color:#ffffcc;"><font color="#000000">&#39;</font></span><span style="background-color:#ffff00;"><font color="#006666">&lt;span foreground=&quot;white&quot;&gt;%s&lt;/span&gt;</font></span><span style="background-color:#ffffcc;"><font color="#000000">&#39;</font></span> % nearest[i]
                    self.label[i].set_markup(w)
                <font color="#000000"><b>except</b></font> ValueError:
                    <font color="#000000"><b>print</b></font> <span style="background-color:#ffffcc;"><font color="#000000">&#39;</font></span><span style="background-color:#ffff00;"><font color="#006666">unknown colour... </font></span><span style="background-color:#ffffcc;"><font color="#000000">&#39;</font></span>, nearest[i]

    <font color="#000000"><b>def</b></font> <font color="#660000">event_handler</font>(self, widget, event=None):
        <font color="#000000"><b>if</b></font> event and event.type == gtk.gdk.MOTION_NOTIFY:
            self.update_colours()

    <font color="#000000"><b>def</b></font> <font color="#660000">event_keys</font>(self, widget, event=None):
        <font color="#000000"><b>if</b></font> event:
            <font color="#000000"><b>if</b></font> event.keyval == gtk.gdk.keyval_from_name(<span style="background-color:#ffffcc;"><font color="#000000">&quot;</font></span><span style="background-color:#ffff00;"><font color="#006666">Escape</font></span><span style="background-color:#ffffcc;"><font color="#000000">&quot;</font></span>) <font color="#6a5acd">\</font>
               or event.keyval == gtk.gdk.keyval_from_name(<span style="background-color:#ffffcc;"><font color="#000000">&quot;</font></span><span style="background-color:#ffff00;"><font color="#006666">q</font></span><span style="background-color:#ffffcc;"><font color="#000000">&quot;</font></span>):
                  self.window.destroy()
                  self.image.destroy()
                  gtk.main_quit()

<font color="#000000"><b>if</b></font> __name__ == <span style="background-color:#ffffcc;"><font color="#000000">&quot;</font></span><span style="background-color:#ffff00;"><font color="#006666">__main__</font></span><span style="background-color:#ffffcc;"><font color="#000000">&quot;</font></span>:
    <font color="#000000"><b>if</b></font> len(argv) == 2 and (0&lt;int(argv[1])&lt;30):
        ColourInfoWindow(int(argv[1]))
    <font color="#000000"><b>else</b></font>:
        ColourInfoWindow(6)
    gtk.main()
</pre>
<p></code></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/rcjp.wordpress.com/96/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/rcjp.wordpress.com/96/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rcjp.wordpress.com/96/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rcjp.wordpress.com/96/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rcjp.wordpress.com/96/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rcjp.wordpress.com/96/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rcjp.wordpress.com/96/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rcjp.wordpress.com/96/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rcjp.wordpress.com/96/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rcjp.wordpress.com/96/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rcjp.wordpress.com/96/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rcjp.wordpress.com/96/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rcjp.wordpress.com&blog=1667523&post=96&subd=rcjp&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://rcjp.wordpress.com/2007/10/17/nearest-rgb-colours/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a54c0b4049118721decd42bd4a29abd8?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rcjp</media:title>
		</media:content>

		<media:content url="http://rcjp.files.wordpress.com/2007/10/colourinfo.png" medium="image">
			<media:title type="html">ColourInfo</media:title>
		</media:content>
	</item>
		<item>
		<title>Number reversing game</title>
		<link>http://rcjp.wordpress.com/2007/10/10/number-reversing-game/</link>
		<comments>http://rcjp.wordpress.com/2007/10/10/number-reversing-game/#comments</comments>
		<pubDate>Wed, 10 Oct 2007 15:10:56 +0000</pubDate>
		<dc:creator>rcjp</dc:creator>
				<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://rcjp.wordpress.com/2007/10/10/number-reversing-game/</guid>
		<description><![CDATA[Here is the reverse game where the object is to flip the initial numbers in a random sequence of 1..9 repeatedly until they are in order.

import&#160;random

numbers = random.sample(range(1,10), 9)
steps = 0

while&#160;numbers != sorted(numbers):
&#160;&#160;&#160;&#160;print&#160;&#34;&#160;&#34;.join(map(str, numbers))
&#160;&#160;&#160;&#160;nflip = int(raw_input(&#34;Flip how many? &#34;))
&#160;&#160;&#160;&#160;numbers[:nflip] = reversed(numbers[:nflip])
&#160;&#160;&#160;&#160;steps += 1

print&#160;&#34;Finished in %d steps.&#34;&#160;% steps

&#34;&#34;&#34;&#160;sample run...

In [6]: run finished/game.py
2 3 4 5 9 [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rcjp.wordpress.com&blog=1667523&post=114&subd=rcjp&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Here is the <a href="http://www.atariarchives.org/basicgames/showpage.php?page=135">reverse game</a> where the object is to flip the initial numbers in a random sequence of 1..9 repeatedly until they are in order.</p>
<p><code>
<pre><font color="#808040">import</font>&nbsp;random

numbers = random.sample(range(1,10), 9)
steps = 0

<font color="#000000"><b>while</b></font>&nbsp;numbers != sorted(numbers):
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#000000"><b>print</b></font>&nbsp;<span style="background-color:#ffffcc;"><font color="#000000">&quot;</font></span><span style="background-color:#ffff00;"><font color="#006666">&nbsp;</font></span><span style="background-color:#ffffcc;"><font color="#000000">&quot;</font></span>.join(map(str, numbers))
&nbsp;&nbsp;&nbsp;&nbsp;nflip = int(raw_input(<span style="background-color:#ffffcc;"><font color="#000000">&quot;</font></span><span style="background-color:#ffff00;"><font color="#006666">Flip how many? </font></span><span style="background-color:#ffffcc;"><font color="#000000">&quot;</font></span>))
&nbsp;&nbsp;&nbsp;&nbsp;numbers[:nflip] = reversed(numbers[:nflip])
&nbsp;&nbsp;&nbsp;&nbsp;steps += 1

<font color="#000000"><b>print</b></font>&nbsp;<span style="background-color:#ffffcc;"><font color="#000000">&quot;</font></span><span style="background-color:#ffff00;"><font color="#006666">Finished in %d steps.</font></span><span style="background-color:#ffffcc;"><font color="#000000">&quot;</font></span>&nbsp;% steps

<span style="background-color:#ffffcc;"><font color="#000000">&quot;&quot;&quot;</font></span><span style="background-color:#ffff00;"><font color="#006666">&nbsp;sample run...</font></span>

<span style="background-color:#ffff00;"><font color="#006666">In [6]: run finished/game.py</font></span>
<span style="background-color:#ffff00;"><font color="#006666">2 3 4 5 9 7 6 1 8</font></span>
<span style="background-color:#ffff00;"><font color="#006666">Flip how many? 4</font></span>
<span style="background-color:#ffff00;"><font color="#006666">5 4 3 2 9 7 6 1 8</font></span>
<span style="background-color:#ffff00;"><font color="#006666">Flip how many? 6</font></span>
<span style="background-color:#ffff00;"><font color="#006666">7 9 2 3 4 5 6 1 8</font></span>
<span style="background-color:#ffff00;"><font color="#006666">Flip how many? 8</font></span>
<span style="background-color:#ffff00;"><font color="#006666">1 6 5 4 3 2 9 7 8</font></span>
<span style="background-color:#ffff00;"><font color="#006666">Flip how many? 5</font></span>
<span style="background-color:#ffff00;"><font color="#006666">3 4 5 6 1 2 9 7 8</font></span>
<span style="background-color:#ffff00;"><font color="#006666">Flip how many? 7</font></span>
<span style="background-color:#ffff00;"><font color="#006666">9 2 1 6 5 4 3 7 8</font></span>
<span style="background-color:#ffff00;"><font color="#006666">Flip how many? 9</font></span>
<span style="background-color:#ffff00;"><font color="#006666">8 7 3 4 5 6 1 2 9</font></span>
<span style="background-color:#ffff00;"><font color="#006666">Flip how many? 8</font></span>
<span style="background-color:#ffff00;"><font color="#006666">2 1 6 5 4 3 7 8 9</font></span>
<span style="background-color:#ffff00;"><font color="#006666">Flip how many? 3</font></span>
<span style="background-color:#ffff00;"><font color="#006666">6 1 2 5 4 3 7 8 9</font></span>
<span style="background-color:#ffff00;"><font color="#006666">Flip how many? 6</font></span>
<span style="background-color:#ffff00;"><font color="#006666">3 4 5 2 1 6 7 8 9</font></span>
<span style="background-color:#ffff00;"><font color="#006666">Flip how many? 3</font></span>
<span style="background-color:#ffff00;"><font color="#006666">5 4 3 2 1 6 7 8 9</font></span>
<span style="background-color:#ffff00;"><font color="#006666">Flip how many? 5</font></span>
<span style="background-color:#ffff00;"><font color="#006666">Finished in 11 steps.</font></span>
<span style="background-color:#ffffcc;"><font color="#000000">&quot;&quot;&quot;</font></span></pre>
<p></code></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/rcjp.wordpress.com/114/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/rcjp.wordpress.com/114/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rcjp.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rcjp.wordpress.com/114/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rcjp.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rcjp.wordpress.com/114/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rcjp.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rcjp.wordpress.com/114/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rcjp.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rcjp.wordpress.com/114/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rcjp.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rcjp.wordpress.com/114/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rcjp.wordpress.com&blog=1667523&post=114&subd=rcjp&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://rcjp.wordpress.com/2007/10/10/number-reversing-game/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a54c0b4049118721decd42bd4a29abd8?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rcjp</media:title>
		</media:content>
	</item>
		<item>
		<title>Hash Tables</title>
		<link>http://rcjp.wordpress.com/2007/10/06/hash-tables/</link>
		<comments>http://rcjp.wordpress.com/2007/10/06/hash-tables/#comments</comments>
		<pubDate>Sat, 06 Oct 2007 11:21:36 +0000</pubDate>
		<dc:creator>rcjp</dc:creator>
				<category><![CDATA[c]]></category>

		<guid isPermaLink="false">http://rcjp.wordpress.com/?p=82</guid>
		<description><![CDATA[Just a quick note on how we used to use hash tables for the compiler code at Salford:


#include &#60;stdio.h&#62;
#include &#60;stdlib.h&#62;

/* point this at a debugging malloc if required */
#define newm_array(T,N) (T*)malloc(sizeof(T)*(N))
#define HASHSIZE    1024

struct symbol_list {
   symbol_list * next;
   int symbol ;
   char* name ;
   [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rcjp.wordpress.com&blog=1667523&post=82&subd=rcjp&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Just a quick note on how we used to use hash tables for the compiler code at Salford:</p>
<p><code>
<pre>
<font color="#808040">#include </font><font color="#663333">&lt;stdio.h&gt;</font>
<font color="#808040">#include </font><font color="#663333">&lt;stdlib.h&gt;</font>

<font color="#c0c0c0"><i>/*</i></font><font color="#c0c0c0"><i> point this at a debugging malloc if required </i></font><font color="#c0c0c0"><i>*/</i></font>
<font color="#808040">#define newm_array(T,N) (T*)malloc(</font>sizeof<font color="#808040">(T)*(N))</font>
<font color="#808040">#define HASHSIZE    </font><font color="#808880">1024</font>

<font color="#0000c8">struct</font> symbol_list {
   symbol_list * next;
   <font color="#0000c8">int</font> symbol ;
   <font color="#0000c8">char</font>* name ;
   <font color="#0000c8">int</font> size ;
};

symbol_list** base;

<font color="#0000c8">int</font> main()
{
    ... we have some name we want to hash...

    base=newm_array(symbol_list*, HASHSIZE);

    <font color="#c0c0c0"><i>/*</i></font><font color="#c0c0c0"><i> clear hash table </i></font><font color="#c0c0c0"><i>*/</i></font>
    i=<font color="#808880">0</font>;
    <font color="#000000"><b>while</b></font>(i&lt;HASHSIZE) {
        base[i]=<font color="#888080">NULL</font>;
        i++;
    }

    <font color="#0000c8">int</font> ih=<font color="#808880">0</font>;
    <font color="#0000c8">char</font>* p=name;
    <font color="#000000"><b>while</b></font> (*p) {       <font color="#c0c0c0"><i>/*</i></font><font color="#c0c0c0"><i> compute hash </i></font><font color="#c0c0c0"><i>*/</i></font>
        ih=(ih&lt;&lt;<font color="#808880">1</font>)+*p;
        p++;
    }
    ih=and(ih,HASHSIZE-<font color="#808880">1</font>);
    symbol_list* x=base[ih];
    <font color="#000000"><b>while</b></font> (x != <font color="#888080">NULL</font>) x=x-&gt;next; <font color="#c0c0c0"><i>/*</i></font><font color="#c0c0c0"><i> find empty chain </i></font><font color="#c0c0c0"><i>*/</i></font>

    ... set x = name...
}
</pre>
<p></code></p>
<p>though might be better to make some functions</p>
<p><code>
<pre>
<font color="#0000c8">static</font> <font color="#0000c8">unsigned</font> hash(<font color="#0000c8">char</font> *s)
{
    <font color="#0000c8">unsigned</font> hashval;

    <font color="#000000"><b>for</b></font> (hashval = <font color="#808880">0</font>; *s != <span style="background-color:#ffff00;"><font color="#006666">&#39;&#39;</font></span>; s++)
        hashval = *s + <font color="#808880">31</font>*hashval;

    <font color="#000000"><b>return</b></font> hashval % HASHSIZE;
}

<font color="#0000c8">static</font> entry *lookup(<font color="#0000c8">char</font> *s)
{
    entry *np;

    <font color="#000000"><b>for</b></font> (np = hashtab[hash(s)]; np != null; np = np-&gt;next)
        <font color="#000000"><b>if</b></font> (wstrcmp(s, np-&gt;name) == <font color="#808880">0</font>)
            <font color="#000000"><b>return</b></font> np;
    <font color="#000000"><b>return</b></font> null;
}
</pre>
<p></code></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/rcjp.wordpress.com/82/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/rcjp.wordpress.com/82/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rcjp.wordpress.com/82/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rcjp.wordpress.com/82/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rcjp.wordpress.com/82/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rcjp.wordpress.com/82/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rcjp.wordpress.com/82/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rcjp.wordpress.com/82/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rcjp.wordpress.com/82/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rcjp.wordpress.com/82/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rcjp.wordpress.com/82/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rcjp.wordpress.com/82/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rcjp.wordpress.com&blog=1667523&post=82&subd=rcjp&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://rcjp.wordpress.com/2007/10/06/hash-tables/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a54c0b4049118721decd42bd4a29abd8?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rcjp</media:title>
		</media:content>
	</item>
		<item>
		<title>Simple diff of 2 files</title>
		<link>http://rcjp.wordpress.com/2007/10/01/simple-diff-of-2-files/</link>
		<comments>http://rcjp.wordpress.com/2007/10/01/simple-diff-of-2-files/#comments</comments>
		<pubDate>Mon, 01 Oct 2007 19:59:56 +0000</pubDate>
		<dc:creator>rcjp</dc:creator>
				<category><![CDATA[c]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://rcjp.wordpress.com/2007/10/01/simple-diff-of-2-files/</guid>
		<description><![CDATA[A few days ago I wanted to compare to files each of which had one word per line (they were completion files for the rlwrap utility incidentally) thats easy enough with the unix shell commands, infact you can do it in one line 
diff -iyw --suppress-common-lines &#60;(sort -f file1) &#60;(sort -f file2)
but I thought as [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rcjp.wordpress.com&blog=1667523&post=74&subd=rcjp&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>A few days ago I wanted to compare to files each of which had one word per line (they were completion files for the <a href="http://freshmeat.net/projects/rlwrap/">rlwrap</a> utility incidentally) thats easy enough with the unix shell commands, infact you can do it in one line </p>
<p><code>diff -iyw --suppress-common-lines &lt;(sort -f file1) &lt;(sort -f file2)</code></p>
<p>but I thought as a quick programming exercise I&#8217;d do it in C++/C, python and Common Lisp&#8230;</p>
<p>Calculating the difference is very easy using C++&#8217;s <code>set_difference</code> etc. but it is a surprise, I think for most programmers anyway, that you have to write your own case insensitive string comparison function.  C++ sure is a peculiar mix of high and low level programming.<br />
<code>
<pre>
<font color="#808040">#include </font><font color="#663333">&lt;iostream&gt;</font>
<font color="#808040">#include </font><font color="#663333">&lt;fstream&gt;</font>
<font color="#808040">#include </font><font color="#663333">&lt;string&gt;</font>
<font color="#808040">#include </font><font color="#663333">&lt;vector&gt;</font>
<font color="#808040">#include </font><font color="#663333">&lt;iterator&gt;</font>
<font color="#808040">#include </font><font color="#663333">&lt;functional&gt;</font>

<font color="#c0c0c0"><i>// </i></font>
<font color="#c0c0c0"><i>// not sure there is any advantage to inherit from binary_function here?</i></font>
<font color="#c0c0c0"><i>// (infact we could just define a function rather than a struct and operator)</i></font>
<font color="#c0c0c0"><i>//</i></font>
<font color="#0000c8">struct</font> lessthan_nocase :
  <font color="#000000"><b>public</b></font> std::binary_function&lt;<font color="#0000c8">const</font> std::string&amp;, <font color="#0000c8">const</font> std::string&amp;, <font color="#0000c8">bool</font>&gt;
{
    <font color="#0000c8">bool</font> operator()(<font color="#0000c8">const</font> std::string&amp; s1, <font color="#0000c8">const</font> std::string&amp; s2) <font color="#0000c8">const</font>
    {
        std::string::const_iterator p1 = s1.begin();
        std::string::const_iterator p2 = s2.begin();

        <font color="#000000"><b>while</b></font>(p1 != s1.end() &amp;&amp; p2 != s2.end()) {
            <font color="#000000"><b>if</b></font> (toupper(*p1) != toupper(*p2)) <font color="#000000"><b>return</b></font> toupper(*p1) &lt; toupper(*p2);
            ++p1;
            ++p2;
        }
        <font color="#000000"><b>return</b></font> s1.size() &lt; s2.size();
    }
};

<font color="#c0c0c0"><i>//</i></font>
<font color="#c0c0c0"><i>// set_difference only work on sorted containers</i></font>
<font color="#c0c0c0"><i>//</i></font>
<font color="#0000c8">void</font> sorted_readfile(<font color="#0000c8">const</font> <font color="#0000c8">char</font>* file, std::vector&lt;std::string&gt;&amp; fvec)
{
    std::ifstream f(file);
    std::istream_iterator&lt;std::string&gt; finput(f), fend;

    copy(finput, fend, back_inserter(fvec));
    sort(fvec.begin(), fvec.end(), lessthan_nocase());
}

<font color="#c0c0c0"><i>//</i></font>
<font color="#c0c0c0"><i>// dump results (show words if &lt; MAXSHOW)</i></font>
<font color="#c0c0c0"><i>//</i></font>
<font color="#0000c8">const</font> <font color="#0000c8">unsigned</font> <font color="#0000c8">int</font> MAXSHOW = <font color="#808880">10</font>;

<font color="#0000c8">void</font> display_diff(std::string title, std::vector&lt;std::string&gt; v)
{
    std::cout &lt;&lt; title;
    <font color="#000000"><b>if</b></font> (v.size() &lt; MAXSHOW) {
        std::cout &lt;&lt; std::endl &lt;&lt; std::string(title.size(), <font color="#888080">&#39;-&#39;</font>) &lt;&lt; std::endl;
        copy(v.begin(), v.end(),
             std::ostream_iterator&lt;std::string&gt;(std::cout, <span style="background-color:#ffff00;"><font color="#006666">&quot;</font></span><span style="background-color:#ffff00;"><font color="#006666">\n</font></span><span style="background-color:#ffff00;"><font color="#006666">&quot;</font></span>));
    } <font color="#000000"><b>else</b></font> {
        std::cout &lt;&lt; <span style="background-color:#ffff00;"><font color="#006666">&quot; =  &quot;</font></span> &lt;&lt; v.size() &lt;&lt; <span style="background-color:#ffff00;"><font color="#006666">&quot; words&quot;</font></span> &lt;&lt; std::endl;
    }
    std::cout &lt;&lt; std::endl;
}

<font color="#0000c8">int</font> main(<font color="#0000c8">int</font> argc, <font color="#0000c8">char</font>* argv[])
{
    <font color="#000000"><b>if</b></font> (argc != <font color="#808880">3</font>) {
        std::cerr &lt;&lt; <span style="background-color:#ffff00;"><font color="#006666">&quot;Usage: tdiff filename1 filename2&quot;</font></span> &lt;&lt; std::endl;
        exit(<font color="#808880">1</font>);
    }

    std::vector&lt;std::string&gt; f1;
    sorted_readfile(argv[<font color="#808880">1</font>], f1);

    std::vector&lt;std::string&gt; f2;
    sorted_readfile(argv[<font color="#808880">2</font>], f2);

    std::vector&lt;std::string&gt; notinf1;
    set_difference(f1.begin(), f1.end(), f2.begin(), f2.end(),
            back_inserter(notinf1), lessthan_nocase());
    display_diff(<span style="background-color:#ffff00;"><font color="#006666">&quot;words in 1st but not in 2nd&quot;</font></span>, notinf1);

    std::vector&lt;std::string&gt; notinf2;
    set_difference(f2.begin(), f2.end(), f1.begin(), f1.end(),
            back_inserter(notinf2), lessthan_nocase());
    display_diff(<span style="background-color:#ffff00;"><font color="#006666">&quot;words in 2nd but not in 1st&quot;</font></span>, notinf2);

    std::vector&lt;std::string&gt; symdiff;
    set_symmetric_difference(f2.begin(), f2.end(), f1.begin(), f1.end(),
            back_inserter(symdiff), lessthan_nocase());
    display_diff(<span style="background-color:#ffff00;"><font color="#006666">&quot;symmetric difference&quot;</font></span>, symdiff);

    std::vector&lt;std::string&gt; inter;
    set_intersection(f2.begin(), f2.end(), f1.begin(), f1.end(),
            back_inserter(inter), lessthan_nocase());
    display_diff(<span style="background-color:#ffff00;"><font color="#006666">&quot;intersection&quot;</font></span>, inter);

    <font color="#000000"><b>return</b></font> <font color="#808880">0</font>;
}
</pre>
<p></code></p>
<p>In C, the only thing to trip you up is getting the casting of the <code>void * </code> pointers in <code>strcmp_nocase</code> correct before trying to dereference them.  Also I&#8217;ve cheated and used the non-ansi <code>strcasecmp</code> (sometimes called stricmp).</p>
<p><code>
<pre>
<font color="#808040">#include </font><font color="#663333">&lt;stdio.h&gt;</font>
<font color="#808040">#include </font><font color="#663333">&lt;stdlib.h&gt;</font>
<font color="#808040">#include </font><font color="#663333">&lt;string.h&gt;</font>
<font color="#808040">#include </font><font color="#663333">&lt;ctype.h&gt;</font>

<font color="#808040">#define MAXLINE  </font><font color="#808880">256</font>
<font color="#808040">#define MAXWORDS </font><font color="#808880">10000</font>
<font color="#808040">#define MAXSHOW  </font><font color="#808880">10</font>

<font color="#0000c8">typedef</font> <font color="#0000c8">enum</font> {<font color="#888080">false</font>, <font color="#888080">true</font>} <font color="#0000c8">bool</font>;

<font color="#0000c8">bool</font> strempty(<font color="#0000c8">char</font> *str)
{
    <font color="#0000c8">char</font> *p = str;
    <font color="#000000"><b>while</b></font> (*p) <font color="#000000"><b>if</b></font> (!isspace(*p++)) <font color="#000000"><b>return</b></font> <font color="#888080">false</font>;
    <font color="#000000"><b>return</b></font> <font color="#888080">true</font>;
}

<font color="#0000c8">int</font> strcmp_nocase(<font color="#0000c8">const</font> <font color="#0000c8">void</font> *s1, <font color="#0000c8">const</font> <font color="#0000c8">void</font> *s2)
{
    <font color="#c0c0c0"><i>/*</i></font><font color="#c0c0c0"><i> The actual arguments to this function are &quot;pointers to</i></font>
<font color="#c0c0c0"><i>       pointers to char&quot;, but strcmp() arguments are &quot;pointers</i></font>
<font color="#c0c0c0"><i>       to char&quot;, hence the following cast plus dereference </i></font><font color="#c0c0c0"><i>*/</i></font>
    <font color="#000000"><b>return</b></font> strcasecmp( *(<font color="#0000c8">char</font> * <font color="#0000c8">const</font> *)s1, * (<font color="#0000c8">char</font> * <font color="#0000c8">const</font> *) s2);
}

<font color="#0000c8">int</font> sorted_readfile(<font color="#0000c8">char</font> *filename, <font color="#0000c8">char</font> **words)
{
    <font color="#0000c8">FILE</font> *f = fopen(filename, <span style="background-color:#ffff00;"><font color="#006666">&quot;r&quot;</font></span>);

    <font color="#000000"><b>if</b></font> (!f) {
        fprintf(<font color="#888080">stderr</font>, <span style="background-color:#ffff00;"><font color="#006666">&quot;can&#39;t open </font></span><span style="background-color:#ffff00;"><font color="#006666">%s</font></span><span style="background-color:#ffff00;"><font color="#006666">\n</font></span><span style="background-color:#ffff00;"><font color="#006666">&quot;</font></span>, filename);
        exit(<font color="#808880">1</font>);
    }

    <font color="#0000c8">int</font> count = <font color="#808880">0</font>;
    <font color="#0000c8">char</font> line[MAXLINE], *q;
    <font color="#000000"><b>while</b></font> (fgets(line, MAXLINE, f) &amp;&amp; (count &lt; MAXWORDS)) {
        <font color="#000000"><b>if</b></font> ((q=strpbrk(line,<span style="background-color:#ffff00;"><font color="#006666">&quot;</font></span><span style="background-color:#ffff00;"><font color="#006666">\r\n</font></span><span style="background-color:#ffff00;"><font color="#006666">&quot;</font></span>))) *q=<font color="#808880">0</font>;     <font color="#c0c0c0"><i>/*</i></font><font color="#c0c0c0"><i> words dont include newlines </i></font><font color="#c0c0c0"><i>*/</i></font>
        <font color="#000000"><b>if</b></font> (strempty(line)) <font color="#000000"><b>continue</b></font>;           <font color="#c0c0c0"><i>/*</i></font><font color="#c0c0c0"><i> skip blank lines </i></font><font color="#c0c0c0"><i>*/</i></font>
        words[count] = (<font color="#0000c8">char</font> *) malloc(strlen(line)+1);
        <font color="#c0c0c0"><i>/*</i></font><font color="#c0c0c0"><i> or words[count++] = strdup(line)</i></font><font color="#c0c0c0"><i>*/</i></font>
        strcpy(words[count], line);
        count++;
    }

    fclose(f);

    <font color="#000000"><b>if</b></font> (count == MAXWORDS) {
        fprintf(<font color="#888080">stderr</font>, <span style="background-color:#ffff00;"><font color="#006666">&quot;increase MAXWORDS buffer size</font></span><span style="background-color:#ffff00;"><font color="#006666">\n</font></span><span style="background-color:#ffff00;"><font color="#006666">&quot;</font></span>);
        exit(<font color="#808880">1</font>);
    }
    qsort(words, count, sizeof(<font color="#0000c8">char</font> *), strcmp_nocase);
    <font color="#000000"><b>return</b></font> count;
}

<font color="#0000c8">void</font> display_diff(<font color="#0000c8">char</font> *title, <font color="#0000c8">char</font> **v, <font color="#0000c8">int</font> vsize)
{
    <font color="#0000c8">int</font> i;
    printf(<span style="background-color:#ffff00;"><font color="#006666">&quot;</font></span><span style="background-color:#ffff00;"><font color="#006666">%s</font></span><span style="background-color:#ffff00;"><font color="#006666">&quot;</font></span>, title);

    <font color="#000000"><b>if</b></font> (vsize &lt; MAXSHOW) {
        printf(<span style="background-color:#ffff00;"><font color="#006666">&quot;</font></span><span style="background-color:#ffff00;"><font color="#006666">\n</font></span><span style="background-color:#ffff00;"><font color="#006666">&quot;</font></span>);
        <font color="#000000"><b>for</b></font> (i=<font color="#808880">0</font>; i&lt;strlen(title); i++) putchar(<font color="#888080">&#39;-&#39;</font>);
        printf(<span style="background-color:#ffff00;"><font color="#006666">&quot;</font></span><span style="background-color:#ffff00;"><font color="#006666">\n</font></span><span style="background-color:#ffff00;"><font color="#006666">&quot;</font></span>);
        <font color="#000000"><b>for</b></font> (i=<font color="#808880">0</font>; i&lt;vsize; i++)
            printf(<span style="background-color:#ffff00;"><font color="#006666">&quot;</font></span><span style="background-color:#ffff00;"><font color="#006666">%s</font></span><span style="background-color:#ffff00;"><font color="#006666">\n</font></span><span style="background-color:#ffff00;"><font color="#006666">&quot;</font></span>, v[i]);
    } <font color="#000000"><b>else</b></font> {
        printf(<span style="background-color:#ffff00;"><font color="#006666">&quot; = </font></span><span style="background-color:#ffff00;"><font color="#006666">%d</font></span><span style="background-color:#ffff00;"><font color="#006666"> words</font></span><span style="background-color:#ffff00;"><font color="#006666">\n</font></span><span style="background-color:#ffff00;"><font color="#006666">&quot;</font></span>, vsize);
    }
    printf(<span style="background-color:#ffff00;"><font color="#006666">&quot;</font></span><span style="background-color:#ffff00;"><font color="#006666">\n</font></span><span style="background-color:#ffff00;"><font color="#006666">&quot;</font></span>);
}

<font color="#0000c8">int</font> main(<font color="#0000c8">int</font> argc, <font color="#0000c8">char</font> *argv[])
{
    <font color="#000000"><b>if</b></font> (argc != <font color="#808880">3</font>) {
        fprintf(<font color="#888080">stderr</font>, <span style="background-color:#ffff00;"><font color="#006666">&quot;Usage: tdiff filename1 filename2</font></span><span style="background-color:#ffff00;"><font color="#006666">\n</font></span><span style="background-color:#ffff00;"><font color="#006666">&quot;</font></span>);
        exit(<font color="#808880">1</font>);
    }
    <font color="#0000c8">int</font> i;
    <font color="#0000c8">char</font> **f1words;
    f1words = malloc(MAXWORDS*sizeof(<font color="#0000c8">char</font>*));
    <font color="#0000c8">int</font> nf1words = sorted_readfile(argv[<font color="#808880">1</font>], f1words);

    <font color="#0000c8">char</font> **f2words;
    f2words = malloc(MAXWORDS*sizeof(<font color="#0000c8">char</font>*));
    <font color="#0000c8">int</font> nf2words = sorted_readfile(argv[<font color="#808880">2</font>], f2words);

    <font color="#0000c8">char</font> **diff;
    <font color="#0000c8">int</font> ndiff = <font color="#808880">0</font>;
    diff = malloc(MAXWORDS*sizeof(<font color="#0000c8">char</font>*));
    <font color="#000000"><b>for</b></font> (i=<font color="#808880">0</font>; i &lt; nf1words; i++) {
        <font color="#000000"><b>if</b></font> (!bsearch(&amp;f1words[i], f2words, nf2words,
                     sizeof(<font color="#0000c8">char</font>*), strcmp_nocase)) {
            <font color="#c0c0c0"><i>/*</i></font><font color="#c0c0c0"><i> could just point into f1words instead </i></font><font color="#c0c0c0"><i>*/</i></font>
            diff[ndiff++] = strdup(f1words[i]);
        }
    }
    display_diff(<span style="background-color:#ffff00;"><font color="#006666">&quot;words in 1st but not in 2nd&quot;</font></span>, diff, ndiff);

    <font color="#000000"><b>return</b></font> <font color="#808880">0</font>;
}
</pre>
<p></code></p>
<p>In python, most of it is easy, though its perhaps not very pythonesque to cram some things on one line like I&#8217;ve done here.  Making <code>set</code> case independent requires the <code>__hash__</code> and <code>__eq__</code> functions (I think that is all we need in this case) get overridden to use a saved lowercase version of the supplied string (see the <a>python reference</a>) .</p>
<p><code>
<pre>
<font color="#808040">import</font> string

<font color="#000000"><b>class</b></font> <font color="#660000">NoCaseStr</font>(str):
    <font color="#000000"><b>def</b></font> <font color="#660000">__init__</font>(self, s):
        str.__init__(self, s)
        <font color="#c0c0c0"><i># keep a copy of the lower case string</i></font>
        self.loweredstr = s.lower()

    <font color="#000000"><b>def</b></font> <font color="#660000">__eq__</font>(self, s):
        <font color="#000000"><b>return</b></font> self.loweredstr == s.lower()

    <font color="#000000"><b>def</b></font> <font color="#660000">__hash__</font>(self):
        <font color="#000000"><b>return</b></font> hash(self.loweredstr)

<font color="#000000"><b>def</b></font> <font color="#660000">display_diff</font>(title, dset, Maxshow=10):
    <font color="#000000"><b>if</b></font> len(dset) &lt; Maxshow:
        <font color="#000000"><b>print</b></font> title
        <font color="#000000"><b>print</b></font> len(title) * <span style="background-color:#ffffcc;"><font color="#000000">&#39;</font></span><span style="background-color:#ffff00;"><font color="#006666">-</font></span><span style="background-color:#ffffcc;"><font color="#000000">&#39;</font></span>
        <font color="#000000"><b>print</b></font> <span style="background-color:#ffffcc;"><font color="#000000">&#39;</font></span><font color="#6a5acd">\n</font><span style="background-color:#ffffcc;"><font color="#000000">&#39;</font></span>.join(dset)
    <font color="#000000"><b>else</b></font>:
        <font color="#000000"><b>print</b></font> title, <span style="background-color:#ffffcc;"><font color="#000000">&#39;</font></span><span style="background-color:#ffff00;"><font color="#006666">=</font></span><span style="background-color:#ffffcc;"><font color="#000000">&#39;</font></span>, len(dset)
    <font color="#000000"><b>print</b></font>

<font color="#000000"><b>def</b></font> <font color="#660000">read_words</font>(f):
    w = set(NoCaseStr(string.strip(word)) <font color="#000000"><b>for</b></font> word in open(f).readlines())
    w.discard(<span style="background-color:#ffffcc;"><font color="#000000">&#39;&#39;</font></span>)<font color="#c0c0c0"><i># slightly clumsy... &#39;  \n&#39; gets stripped to &#39;&#39; so discard it</i></font>
    <font color="#000000"><b>return</b></font> w

<font color="#000000"><b>def</b></font> <font color="#660000">tdiff</font>(f1, f2):
    w1 = read_words(f1)
    w2 = read_words(f2)
    display_diff(<span style="background-color:#ffffcc;"><font color="#000000">&#39;</font></span><span style="background-color:#ffff00;"><font color="#006666">in 1st but not in 2nd</font></span><span style="background-color:#ffffcc;"><font color="#000000">&#39;</font></span>, w1.difference(w2))
    display_diff(<span style="background-color:#ffffcc;"><font color="#000000">&#39;</font></span><span style="background-color:#ffff00;"><font color="#006666">in 2nd but not in 1st</font></span><span style="background-color:#ffffcc;"><font color="#000000">&#39;</font></span>, w2.difference(w1))
    display_diff(<span style="background-color:#ffffcc;"><font color="#000000">&#39;</font></span><span style="background-color:#ffff00;"><font color="#006666">symmetric difference</font></span><span style="background-color:#ffffcc;"><font color="#000000">&#39;</font></span>,  w1.symmetric_difference(w2))
    display_diff(<span style="background-color:#ffffcc;"><font color="#000000">&#39;</font></span><span style="background-color:#ffff00;"><font color="#006666">intersection</font></span><span style="background-color:#ffffcc;"><font color="#000000">&#39;</font></span>,          w1.intersection(w2))

<font color="#000000"><b>if</b></font> __name__ == <span style="background-color:#ffffcc;"><font color="#000000">&#39;</font></span><span style="background-color:#ffff00;"><font color="#006666">__main__</font></span><span style="background-color:#ffffcc;"><font color="#000000">&#39;</font></span>:
    <font color="#808040">import</font> sys
    <font color="#000000"><b>if</b></font> len(sys.argv) &lt; 3:
        <font color="#000000"><b>print</b></font> <span style="background-color:#ffffcc;"><font color="#000000">&#39;</font></span><span style="background-color:#ffff00;"><font color="#006666">Usage: %s file1 file2</font></span><span style="background-color:#ffffcc;"><font color="#000000">&#39;</font></span> % sys.argv[0]
        sys.exit(1)

    tdiff(sys.argv[1], sys.argv[2])
</pre>
<p></code></p>
<p>My favourite language &#8211; Common Lisp has everything you need to do with without overriding anything.  <code>string-equal</code> is case insensitive (as opposed to <code>string=</code>)</p>
<p><code>
<pre>
(defun read-words (file)
  (with-open-file (str file)
    (loop for line = (read-line str nil nil)
       for word = (string-trim &quot; &quot; line)
       while line
       unless (string= word &quot;&quot;)
       collect word)))

(defun display-diff (title diff &amp;optional (max-show 10))
  (format t &quot;~A: ~V{~A~^ ~}~%&quot; title max-show diff))

(defun tdiff (f1 f2)
  (let ((w1 (read-words f1))
        (w2 (read-words f2)))
    (display-diff &quot;in 1st but not in 2nd&quot; (set-difference w1 w2 :test #&#39;string-equal))
    (display-diff &quot;in 2nd but not in 1st&quot; (set-difference w2 w1 :test #&#39;string-equal))
    (display-diff &quot;intersection&quot; (intersection w1 w2 :test #&#39;string-equal))))

(tdiff &quot;/home/r/tmp/f1&quot; &quot;/home/r/tmp/f2&quot;)
</pre>
<p></code></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/rcjp.wordpress.com/74/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/rcjp.wordpress.com/74/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rcjp.wordpress.com/74/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rcjp.wordpress.com/74/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rcjp.wordpress.com/74/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rcjp.wordpress.com/74/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rcjp.wordpress.com/74/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rcjp.wordpress.com/74/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rcjp.wordpress.com/74/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rcjp.wordpress.com/74/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rcjp.wordpress.com/74/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rcjp.wordpress.com/74/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rcjp.wordpress.com&blog=1667523&post=74&subd=rcjp&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://rcjp.wordpress.com/2007/10/01/simple-diff-of-2-files/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a54c0b4049118721decd42bd4a29abd8?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rcjp</media:title>
		</media:content>
	</item>
	</channel>
</rss>