Rcjp's Weblog

December 29, 2009

Book: Does Anything Eat Wasps

Filed under: books — rcjp @ 5:10 pm

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:

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.

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.

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.

I also found some of the biological explanations interesting, mostly because I know embarrassingly little about this subject:

About a quarter of humans have a tendency to sneeze when going into bright sunlight. Its called a photic sneeze 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.

We don’t normally notice the Purkinje shadows 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 – Troxlers’s fading. 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.

There is a physical section of the brain devoted to signals from different parts of the body called the somatosensory homunculus and fingers, for example, get much more brain to talk to than say the back.

The flu virus can live on a cash note for up to 17 days.

When a platelet (part of the bloodstream) encounters a wound it activates its integrins (surface receptors) enabling it to glue itself to the fibrinogen in the blood vessel. The non-cellular tissue in the body (the extracellular matrix – teeth, cornea, tendons etc.) is mainly produced by localised fibroblasts using fibre forming proteins: collagens and elastin (structural) or fibronectin and laminin (adhesive)

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 – 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.

October 31, 2009

Book: “Things Snowball” by Rich Hall

Filed under: books — rcjp @ 5:18 pm

Just finished this paperback of short rants and tall tales warped with Rich Hall’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’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.

The cutting rants are always funny and sharply observed “Las Vegas is what America deserves … two thousand years of civilization and the planet’s most visible creation is a lit-up string of clip joints in the Mojave Desert”. 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’t quite capture a description of the objectless mass wanderings up and down the Vegas strip until I read Rich Hall’s description of visitors standing on the ‘people movers’ – those horizontal metal escalators walkways as being “…perfectly content as they creep along like God’s unclaimed luggage”. Genius.

October 28, 2009

Book: “Coders at Work” by Peter Seibel

Filed under: books — rcjp @ 11:04 pm

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 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.

Anyway, on a first reading at least, I pulled out some quotes I thought insightful:

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.” … L Peter Deutsch

And later talking about advice from Jerry Elkind

…measure things. Even sometimes measure things you don’t think you need to measure. That [advice] had a profound effect on me.

Ken Thompson who, it is rather surprisingly revealed, doesn’t have code commit privileges where he works at Google said

“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.”

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.

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’m not sure this book is a picture of an average “coder at work” and there is also a definite west-coast lisp slant to the book, not surprising really as Peter is a west-coast lisper!

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 – just persistence and obstinacy and little inhibitions about rewriting code.

Finally, I can’t resist a few of quotes that made me laugh:

The whole Web is built on one mistake after another. We have this big pile of accidents.”…Douglas Crockford

Seibel: I think Larry Wall described [lisp] as a bowl of oatmeal with fingernail clippings in it.
Deutsch: Well, my description of Perl is something that looks like it came out of the wrong end of a dog.

and my favourite quote from Joe

…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.… Joe Armstrong ”

Right, now I’m off to go and measure something I don’t think I need to measure…

March 24, 2009

Redirecting stdout within ipython

Filed under: python — rcjp @ 12:09 am

A friend was asking if I knew a way of grabbing the output from a python function within ipython. I couldn’t see an easy way (the normal shell way of ‘>’ redirection won’t work). So the following is a quick hack on IPython’s OutputTrap:

Sample session, just defining a function that outputs something and I’m also using the ipython trick of preceding the command with a comma to make the args into strings…

In [1]: def myfunc():
   ...:     print 'hello'

In [2]: run grab

In [3]: ,grab dumpfile myfunc()

and grab.py is…

import sys
from IPython import OutputTrap

def grab(fname, cmd):
    dump = OutputTrap.OutputTrap('dump','','',trap_out=1,quiet_out=1)
        eval(cmd, sys._getframe(1).f_globals, sys._getframe(1).f_locals)
        print sys.exc_info()

    file(fname,'w').writelines( dump.summary() )

… and the output of myfunc() appears in the file ‘dumpfile’ so it seems to work, but note I haven’t done much testing.

April 2, 2008

Gaussian PIL Image Filter

Filed under: python — rcjp @ 6:54 pm

I could not see a gaussian filter in the python imaging library, but its simple enough to write one…

import ImageFilter
from PIL import Image
from numpy import *

def gaussian_grid(size = 5):
    Create a square grid of integers of gaussian shape
    e.g. gaussian_grid() returns
    array([[ 1,  4,  7,  4,  1],
           [ 4, 20, 33, 20,  4],
           [ 7, 33, 55, 33,  7],
           [ 4, 20, 33, 20,  4],
           [ 1,  4,  7,  4,  1]])
    m = size/2
    n = m+1  # remember python is 'upto' n in the range below
    x, y = mgrid[-m:n,-m:n]
    # multiply by a factor to get 1 in the corner of the grid
    # ie for a 5x5 grid   fac*exp(-0.5*(2**2 + 2**2)) = 1
    fac = exp(m**2)
    g = fac*exp(-0.5*(x**2 + y**2))
    return g.round().astype(int)

class GAUSSIAN(ImageFilter.BuiltinFilter):
    name = "Gaussian"
    gg = gaussian_grid().flatten().tolist()
    filterargs = (5,5), sum(gg), 0, tuple(gg)

im = Image.open('/home/rcjp/tmp/test.png')
im1 = im.filter(GAUSSIAN)

February 25, 2008

K language – ultimate coding brevity?

Filed under: python — rcjp @ 5:56 pm

After reading a thread on c.l.l and, over the last week, more than I care to on arc’s emphasis on brevity, I can’t decide if I’m impressed or appalled by the K language.

As K seems to be a proprietary language I wouldn’t normally look at it, but thankfully the Wikipedia entry has a link to a screencast by Michael Schidlowsky solving the birthday problem – 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

#+/{~(#x)=#?x}' {(1000,x) _draw 365} 10

using ipython I’d do:

In [1]: from random import randint
In [2]: def samebday(n): return n != len(set(randint(1,365) for i in range(n)))
In [3]: def bday1000(n): return [samebday(n) for i in 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

Casting the list of random ‘birthdays’ (just integers from 1..365) to a python set 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 – which is roughly half of them. Not as short as K, but concise enough for me I think.

January 31, 2008

Playing with Arc

Filed under: lisp — rcjp @ 12:12 pm

Paul Graham has made available a tarball prerelease of his new arc language. I’m currently running Ubuntu gutsy on my laptop so it needs a quick patch

~/lisp> tar -xvf ~/tmp/arc0.tar 
~/lisp> cd arc0
~/lisp/arc0> wget http://blakeley.com/linux-date.patch
~/lisp/arc0> patch arc.arc linux-date.patch
~/lisp/arc0> rlwrap -C arc mzscheme -m -f as.scm
Use (quit) to quit, (tl) to return here after an interrupt.

then you can go through the tutorial, or play with the webserver, which we’ll start in a new thread

arc> (defop hello req (pr "hello world"))
arc> (= webtest (thread asv))
#<thread: asv>
arc> ready to serve port 8080

and then look at http://localhost:8080/hello

The prompt doesn’t quite come back correctly here, but you can carry on typing

(defop hello req (pr "hello world changed"))

and refresh the browser. Kill the thread and quit with

arc> (break-thread webtest) 
arc> (quit)

Though initially disappointed that there was no native compiler (written in arc of course!), I’ve warmed to the syntax – I didn’t find myself in a fighting the language, or frustrated that I had to reinvent stuff I didn’t want to e.g., here’s how to split up a string using spaces or commas

arc> (tokens "abc def,ghi" [in _ #\ #\,])
("abc" "def" "ghi")

There’s plenty to play with by reading through the source. So far, it feels fun.

October 17, 2007

Show Nearest X11 System Colours

Filed under: python, utils — rcjp @ 1:16 pm

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’t work out how to listen to mouse messages when I don’t own the window under the mouse pointer (on X Windows anyway) – so I cheated and this code takes a snapshot of the screen and displays it in the background. Obviously you can’t move/click on any windows when this thing is running! quit with ‘q’ or ‘ESC’ or just close the window.

#!/usr/bin/env python
# Takes a screenshot image of the root window and display a table of 
# nearest system colours compared to that under the mouse pointer
# can specify an argument 1..29 to show more colours (default 6)
# The window title shows the exact rgb values of pointer in hex
import gtk, re
from sys import argv

RGBCOLOURS = '/etc/X11/rgb.txt'   # rgb colour data

class ScreenColour(object):

    syscolours = {}  # hold system rgb.txt relating colours to names

    def __init__(self, rgbfile=RGBCOLOURS):
        # 1 pixel buffer for pixel under mouse pointer
        self.pix = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB,False, 8, 1, 1)
        for line in file(rgbfile).readlines():
            if not line.startswith('!'):
                rgbname = re.compile(r'\s*(\d+)\s*(\d+)\s*(\d+)\s*(.+)')
                (r,g,b,name) = rgbname.match(line).groups()
                self.syscolours[name.rstrip()] = (int(r),int(g),int(b))

    def cmp_screencolour(self,col,basecol):
        """Numerical difference between colours col(name) and basecol(r,g,b)"""
        return sum(abs(a-b) for a, b in zip(basecol, ScreenColour.syscolours[col]))

    def pixelinfo(self):
        """Returns the (r,g,b) value of colour under mouse pointer"""
        (_, x, y, _) = gtk.gdk.display_get_default().get_pointer()
                                   x,y, 0,0, 1,1)
        col = self.pix.get_pixels_array()
        return (int(col[0,0,0]), int(col[0,0,1]), int(col[0,0,2]))

    def nearest_colours(self, n, basecol):
        """Return the nearest n system colours compared to basecol"""
        nearest = self.syscolours.keys()
        nearest.sort(key=lambda (c): self.cmp_screencolour(c, basecol))
        return nearest[:n]

class ColourInfoWindow(ScreenColour):

    """Draw a table of colours nearest to that under mouse pointer"""
    label = []
    eb = []
    oldrgb = (-1,-1,-1)

    def delete_event(self, widget, event, data=None):
        return False

    def __init__(self, tablesize):
        self.tablesize = tablesize
        self.image = gtk.Window()
        self.image.connect("motion_notify_event", self.event_handler)
        # set background from a screen shot of the root window
        screen = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8,
                                gtk.gdk.screen_width(), gtk.gdk.screen_height())
                                 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.window.set_back_pixmap(pixmap, False)
        del pixmap

        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.set_size_request(200, 40*self.tablesize)
        self.window.set_transient_for(self.image)  # stay above screen image
        self.window.set_title("Colour Info")
        self.window.connect("delete_event", self.delete_event)
        self.window.connect("key-press-event", self.event_keys)

        self.Colour = ScreenColour()
        vbox = gtk.VBox(True,2)
        for i in range(self.tablesize):

    def update_colours(self):
        rgb = self.Colour.pixelinfo()
        if self.oldrgb != rgb:
            self.oldrgb = rgb
            nearest = self.Colour.nearest_colours(self.tablesize, rgb)
            self.window.set_title("%02X %02X %02X" % rgb)
            for i in range(self.tablesize):
                    # if brighter than 128,128,128 switch to 
                    # a black background so text is visable
                    if (sum(self.syscolours[nearest[i]]) > 384):
                        w = '<span foreground="black">%s</span>' % nearest[i]
                        w = '<span foreground="white">%s</span>' % nearest[i]
                except ValueError:
                    print 'unknown colour... ', nearest[i]

    def event_handler(self, widget, event=None):
        if event and event.type == gtk.gdk.MOTION_NOTIFY:

    def event_keys(self, widget, event=None):
        if event:
            if event.keyval == gtk.gdk.keyval_from_name("Escape") \
               or event.keyval == gtk.gdk.keyval_from_name("q"):

if __name__ == "__main__":
    if len(argv) == 2 and (0<int(argv[1])<30):

October 10, 2007

Number reversing game

Filed under: python — rcjp @ 3:10 pm

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 random

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

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

print "Finished in %d steps." % steps

""" sample run...

In [6]: run finished/game.py
2 3 4 5 9 7 6 1 8
Flip how many? 4
5 4 3 2 9 7 6 1 8
Flip how many? 6
7 9 2 3 4 5 6 1 8
Flip how many? 8
1 6 5 4 3 2 9 7 8
Flip how many? 5
3 4 5 6 1 2 9 7 8
Flip how many? 7
9 2 1 6 5 4 3 7 8
Flip how many? 9
8 7 3 4 5 6 1 2 9
Flip how many? 8
2 1 6 5 4 3 7 8 9
Flip how many? 3
6 1 2 5 4 3 7 8 9
Flip how many? 6
3 4 5 2 1 6 7 8 9
Flip how many? 3
5 4 3 2 1 6 7 8 9
Flip how many? 5
Finished in 11 steps.

October 6, 2007

Hash Tables

Filed under: c — rcjp @ 11:21 am

Just a quick note on how we used to use hash tables for the compiler code at Salford:

#include <stdio.h>
#include <stdlib.h>

/* 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** base;

int main()
    ... we have some name we want to hash...

    base=newm_array(symbol_list*, HASHSIZE);

    /* clear hash table */
    while(i<HASHSIZE) {

    int ih=0;
    char* p=name;
    while (*p) {       /* compute hash */
    symbol_list* x=base[ih];
    while (x != NULL) x=x->next; /* find empty chain */

    ... set x = name...

though might be better to make some functions

static unsigned hash(char *s)
    unsigned hashval;

    for (hashval = 0; *s != ''; s++)
        hashval = *s + 31*hashval;

    return hashval % HASHSIZE;

static entry *lookup(char *s)
    entry *np;

    for (np = hashtab[hash(s)]; np != null; np = np->next)
        if (wstrcmp(s, np->name) == 0)
            return np;
    return null;

Older Posts »

Blog at WordPress.com.


Get every new post delivered to your Inbox.