Archive for March, 2010

Transparent javascript rpc

I’ve been writing a little ajaxish code using django as the server backend recently and have been finding it kind of irritating. The levels of indirection necessary for a simple function call seem excessive.

If you were on one machine and you wanted to call a function in response to a button click your code would look like this

function handleClick()
    // Perform actual operations that do things
} += handleClick

Whilst to do this using a client and a server you have to do the following in addition.

* Decide how you are going to serialize the arguments
* Serialize the arguments
* Decide on a url scheme for your function
* Be tormented by how to make an overall consistent scheme
* Be tormented by the principles of rest
* Define some urls
* Define a de-serialize
* Debug copiously

For most purposes this just seems silly. I’ve been feeling like what I want is something far simpler.

* I don’t want to have to worry about structuring urls
* I don’t want to worry about principles of url schemes
* I don’t want to worry about serialization
* I don’t want to have to debug things
* I don’t want to have to write boiler plate code

Also I don’t want to write java or compile any javascript (gwt or pyjamas). These approaches may indeed have large payoffs if you can live within the framework and once you have learnt it but this seems like a very large overhead.

I just want to be able to have a javascript function that I can on the client which corresponds to a function (rather than some crazy object/ class / data structure) which when called will deal with serialization and deciding on a url and calling this function.

In practice of course you mightn’t have a synchronous function in javascript, but something that takes a callback or similar but the principle remains the same.

The payoff to this approach seems large, everything suddenly becomes terribly simple – vast swathes of things that you would have had to thought of before disappear. But there are of course some potential costs:

* Defining a url scheme forces you to think about state and the structure of your data.
* Defining a url scheme forces you to separate out your client and server code somewhat – i.e define a generalish interface to your server. This allows other people to interact with your server and creates some structure, which no such separation you are more likely to generate a big ball of state
* The transparency can lead you to forget the difference between calling function. (I’m rather doubtful about this argument – I seem to be fairly aware of the implications of what I’m doing when I’m writing code. I might however agree that it can be an impediment when learning new code / sharing code between a large number of people). This argument reminds be of the ‘nice’ arguments one has bleated at you whilst in the education system.
* Url schemes make you think about state
* Debugging becomes more magical
* You don’t necessarily have the means to create more structure
* Eventually you will probably end up wanting to do some advanced serialization, there are going to be questions about this. Avoiding this for too long may lead to add hock structures permeating your code. There could be a mismatch between what is easy and what suddenly becomes terribly difficult.


March 29, 2010 at 11:20 pm Leave a comment

Drawing lines with matplotlib

Matplotlib’s pyplot library has the following functions for drawing lines:

axvline – A full vertical line (does not start or stop)
vlines – A vertical line segment (or multiple)
axhline – A full horizontal line
hlines – …

The relevant plot in the gallery is here:

March 25, 2010 at 3:55 pm Leave a comment

Sony Vaio VGN-NW20EF fixing sound

Out of the box on Ubuntu Karmic recording sound doesn’t work on this laptop (you just get noise when you attempt to records). However, upgrading the alsa drivers from the PPA here: fixes this thanks to the instructions here:

Making me slightly happier.

March 25, 2010 at 3:05 pm Leave a comment

Sony VAIO VGN-NW20EF Review (from the point of view of a linux user)

Hmm. Not terribly impressed:

* Recording didn’t work out of the box (I only tried playing audio before buying it an foolish decided that it didn’t work)
Fortunately this has been fixed
* Wireless is temperamental – has a tendency of breaking a requiring restarts after suspend. Also I’m suspiscious that it breaks after a large amount of traffic has been transfered (again tried the wireless in the shop)
* Screen has banding in grayscales. (Fortunately I don’t notice this that much – otherwise I would be very annoyed).
* No third mouse button for pasting (and pressing two buttons at the same time is difficult)

On the other hand:

* The keyboard is kind of nice

Next time I buy a computer I am going to:
* Bring my own gradient images to check the screen
* Play and record stuff on audio
* See if I can buy some good reviews
* Consider buying something from a sho that sells linux PCs (they will find out soon enough it there are issues)

I was quite surprising when buying this that I couldn’t really find things on the internet saying “this computer will work” or “this computer will not work”. And the machines which I could find that said this were either i) over $1200 dollars new or old.

The ASUS machines seemed fairly likely to work but their keyboards seemed lame.

March 25, 2010 at 3:01 pm Leave a comment

Comparing wikis

Very impressed by the wiki comparison questionnaire here:

This asks you a series of questions about and then gives you a details table comparing the different wikis.

March 14, 2010 at 6:27 pm Leave a comment

Python ticking clock – Periodically play a ticking sound

The following python script will periodically play a ticking sound after a given pause. This is can be useful for timing things when you are unable to start a timer – for example if you are practicing balancing a walking-stick on your nose on your own.

#!/usr/bin/env python
import ao
import ogg.vorbis
import os
import sys
from time import sleep, time

def switch_off_buffering():
    sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)

SOUND = "/usr/share/sounds/gnome/default/alerts/sonar.ogg"

filename = 'test.ogg'

class OggSample(object):
    def __init__(self, filename):
        id = ao.driver_id('esd')
        self.vf = ogg.vorbis.VorbisFile(filename)
        self.device = ao.AudioDevice(id)

    def play(self):
        while 1:
            buff, bytes, _ =
            if bytes != 0:
      , bytes)

def repeat(pause, period):
    sample = OggSample(SOUND)

    tick_time = time()
    while True:
        tick_time += period # avoid drift
        sleep(tick_time - time())


def usage(stream):
    print >>stream, "ticking-count [DELAY seconds] [PERIOD seconds]"
    print >>stream, "Default delay: %s seconds" % DEFAULT_DELAY
    print >>stream, "Default period: %s seconds" % DEFAULT_PERIOD

if __name__ == '__main__':
    args = sys.argv[1:]
    if '-h' in args or '--help' in args:

    if len(args) == 0:
        delay, period = DEFAULT_DELAY, DEFAULT_PERIOD
    elif len(args) == 1:
        delay, = args
        period = DEFAULT_PERIOD
    elif len(args) == 2:
        delay, period = args
    repeat(delay, period)

March 11, 2010 at 11:17 pm Leave a comment

Playing an ogg file on linux using python

The following code allows one to repeatedly play a sound sample using python on linux.

This code depends on python-ogg, python-pyvorbis and python-pyao.

import ogg.vorbis
import ao

filename = 'test.ogg'

class OggSample(object):
	def __init__(self, filename):
		id = ao.driver_id('esd')
		self.vf = ogg.vorbis.VorbisFile(filename)
		self.device = ao.AudioDevice(id)

	def play(self):
		while 1:
			buff, bytes, _ =
			if bytes != 0:, bytes)

sample = OggSample(filename)

There may well be better ways of doing this : in particular, I’m not sure how much this will cause one to talk to the disk.

March 11, 2010 at 11:02 pm Leave a comment

Older Posts

March 2010
« Feb   Apr »