<?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>Wed, 02 Nov 2011 13:25:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='rcjp.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.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&#039;s Weblog" />
	<atom:link rel='hub' href='http://rcjp.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Book: Does Anything Eat Wasps</title>
		<link>http://rcjp.wordpress.com/2009/12/29/book-does-anything-eat-wasps/</link>
		<comments>http://rcjp.wordpress.com/2009/12/29/book-does-anything-eat-wasps/#comments</comments>
		<pubDate>Tue, 29 Dec 2009 17:10:36 +0000</pubDate>
		<dc:creator>rcjp</dc:creator>
				<category><![CDATA[books]]></category>

		<guid isPermaLink="false">http://rcjp.wordpress.com/?p=154</guid>
		<description><![CDATA[Just finished a set of three books by New Scientist magazine assembled from a column devoted to reader questions along with a selection of reader replies (the first title in the series is the title of this post.) The appeal of the questions and quality of the answers varied a great deal, but I learnt [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rcjp.wordpress.com&amp;blog=1667523&amp;post=154&amp;subd=rcjp&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Just finished a set of three books by New Scientist magazine assembled from a column devoted to reader questions along with a selection of reader replies (the first title in the series is the title of this post.)  The appeal of the questions and quality of the answers varied a great deal, but I learnt a few interesting points which I’ll attempt to abridge below:</p>
<blockquote><p>The amount of water in a cloud is not different than the clear air around it,  its only a temperature difference which has caused the water to condense into droplets and become visible. </p></blockquote>
<blockquote><p>I’ve never tried photochromic lenses but apparently they are temperature dependent and work poorly in warm weather, which somewhat defeats the purpose unless you are skiing.</p></blockquote>
<blockquote><p>Aluminium is much more reactive than we usually think because it quickly forms a protective oxide layer.  Mercury can disrupt this, and then spread like wood rot, hence is banned on planes.</p></blockquote>
<p>I also found some of the biological explanations interesting, mostly because I know embarrassingly  little about this subject:</p>
<blockquote><p>About a quarter of humans have a tendency to sneeze when going into bright sunlight.  Its called a <em>photic sneeze</em> and happens because the protective reflexes of eyes and nose are closely linked.  It is a well known hazard for combat pilots but was noted as early as the 17th century by Francis Bacon.</p></blockquote>
<blockquote><p>We don’t normally notice the <em>Purkinje shadows</em> which appear on the retina caused by the light having to pass through blood vessels and fibres (unlike a squid’s eye) because we are not good at registering stationary images.  We only see stationary objects because are eyes are constantly jiggling about.  If we undo this using eye-tracking technology, we can make objects disappear &#8211; <em>Troxlers’s fading</em>.   We can only see the aforementioned shadows if we vary the angle of the light entering the eye via a pinhole in a card or shining light into the white part of the eye.</p></blockquote>
<blockquote><p>There is a physical section of the brain devoted to signals from different parts of the body called the <em>somatosensory homunculus</em> and fingers, for example, get much more brain to talk to than say the <a href="http://spinacare.wordpress.com/2007/02/05/where-is-that-pain-in-my-back/">back</a>. </p></blockquote>
<blockquote><p>The flu virus can live on a cash note for up to <a href="http://www.newscientist.com/article/dn12116">17 days</a>.</p></blockquote>
<blockquote><p>When a <em>platelet</em> (part of the bloodstream) encounters a wound it activates its <em>integrins</em> (surface receptors) enabling it to glue itself to the <em>fibrinogen</em> in the blood vessel.  The non-cellular tissue in the body (the extracellular matrix &#8211; teeth, cornea, tendons etc.) is mainly produced by localised <em>fibroblasts</em> using fibre forming proteins: <em>collagens</em> and <em>elastin</em> (structural) or <em>fibronectin</em> and <em>laminin</em> (adhesive)</p></blockquote>
<p>However in that last example I’m not really sure I learnt anything other than there were names for interesting things.  The prospect of me being able to performing any calculations seems unlikely &#8211; I’m guessing protein dynamics isn’t the sort of subject you can dabble in.  Maybe thats why I prefer basic physics: at least you get to play with the equations.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rcjp.wordpress.com/154/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rcjp.wordpress.com/154/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rcjp.wordpress.com/154/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rcjp.wordpress.com/154/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/rcjp.wordpress.com/154/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/rcjp.wordpress.com/154/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/rcjp.wordpress.com/154/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/rcjp.wordpress.com/154/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rcjp.wordpress.com/154/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rcjp.wordpress.com/154/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rcjp.wordpress.com/154/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rcjp.wordpress.com/154/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rcjp.wordpress.com/154/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rcjp.wordpress.com/154/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rcjp.wordpress.com&amp;blog=1667523&amp;post=154&amp;subd=rcjp&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://rcjp.wordpress.com/2009/12/29/book-does-anything-eat-wasps/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/53667c117e0fd86fddcd53194afeb6eb?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rcjp</media:title>
		</media:content>
	</item>
		<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 though: you [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rcjp.wordpress.com&amp;blog=1667523&amp;post=149&amp;subd=rcjp&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<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>
<br />  <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/gofacebook/rcjp.wordpress.com/149/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/rcjp.wordpress.com/149/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/rcjp.wordpress.com/149/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/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&amp;blog=1667523&amp;post=149&amp;subd=rcjp&amp;ref=&amp;feed=1" width="1" height="1" />]]></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://1.gravatar.com/avatar/53667c117e0fd86fddcd53194afeb6eb?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 Peter is [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rcjp.wordpress.com&amp;blog=1667523&amp;post=135&amp;subd=rcjp&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<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>
<br />  <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/gofacebook/rcjp.wordpress.com/135/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/rcjp.wordpress.com/135/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/rcjp.wordpress.com/135/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/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&amp;blog=1667523&amp;post=135&amp;subd=rcjp&amp;ref=&amp;feed=1" width="1" height="1" />]]></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://1.gravatar.com/avatar/53667c117e0fd86fddcd53194afeb6eb?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 I&#8217;m [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rcjp.wordpress.com&amp;blog=1667523&amp;post=130&amp;subd=rcjp&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<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>
<br />  <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/gofacebook/rcjp.wordpress.com/130/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/rcjp.wordpress.com/130/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/rcjp.wordpress.com/130/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/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&amp;blog=1667523&amp;post=130&amp;subd=rcjp&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://rcjp.wordpress.com/2009/03/24/redirecting-stdout-within-ipython/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/53667c117e0fd86fddcd53194afeb6eb?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; [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rcjp.wordpress.com&amp;blog=1667523&amp;post=115&amp;subd=rcjp&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<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>
<br /><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/gofacebook/rcjp.wordpress.com/115/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/rcjp.wordpress.com/115/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/rcjp.wordpress.com/115/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/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&amp;blog=1667523&amp;post=115&amp;subd=rcjp&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://rcjp.wordpress.com/2008/04/02/gaussian-pil-image-filter/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/53667c117e0fd86fddcd53194afeb6eb?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 [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rcjp.wordpress.com&amp;blog=1667523&amp;post=109&amp;subd=rcjp&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<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>
<br /><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/gofacebook/rcjp.wordpress.com/109/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/rcjp.wordpress.com/109/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/rcjp.wordpress.com/109/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/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&amp;blog=1667523&amp;post=109&amp;subd=rcjp&amp;ref=&amp;feed=1" width="1" height="1" />]]></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://1.gravatar.com/avatar/53667c117e0fd86fddcd53194afeb6eb?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 [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rcjp.wordpress.com&amp;blog=1667523&amp;post=108&amp;subd=rcjp&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<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>
<br /><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/gofacebook/rcjp.wordpress.com/108/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/rcjp.wordpress.com/108/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/rcjp.wordpress.com/108/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/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&amp;blog=1667523&amp;post=108&amp;subd=rcjp&amp;ref=&amp;feed=1" width="1" height="1" />]]></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://1.gravatar.com/avatar/53667c117e0fd86fddcd53194afeb6eb?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 this [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rcjp.wordpress.com&amp;blog=1667523&amp;post=96&amp;subd=rcjp&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<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>
<br /><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/gofacebook/rcjp.wordpress.com/96/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/rcjp.wordpress.com/96/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/rcjp.wordpress.com/96/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/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&amp;blog=1667523&amp;post=96&amp;subd=rcjp&amp;ref=&amp;feed=1" width="1" height="1" />]]></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://1.gravatar.com/avatar/53667c117e0fd86fddcd53194afeb6eb?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;% [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rcjp.wordpress.com&amp;blog=1667523&amp;post=114&amp;subd=rcjp&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<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>
<br /><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/gofacebook/rcjp.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/rcjp.wordpress.com/114/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/rcjp.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/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&amp;blog=1667523&amp;post=114&amp;subd=rcjp&amp;ref=&amp;feed=1" width="1" height="1" />]]></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://1.gravatar.com/avatar/53667c117e0fd86fddcd53194afeb6eb?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 ; int size ; }; symbol_list** [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rcjp.wordpress.com&amp;blog=1667523&amp;post=82&amp;subd=rcjp&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<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>
<br /><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/gofacebook/rcjp.wordpress.com/82/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/rcjp.wordpress.com/82/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/rcjp.wordpress.com/82/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/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&amp;blog=1667523&amp;post=82&amp;subd=rcjp&amp;ref=&amp;feed=1" width="1" height="1" />]]></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://1.gravatar.com/avatar/53667c117e0fd86fddcd53194afeb6eb?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rcjp</media:title>
		</media:content>
	</item>
	</channel>
</rss>
