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 } button.click += 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.
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:
http://matplotlib.sourceforge.net/examples/pylab_examples/axhspan_demo.html
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:
https://launchpad.net/~ubuntu-audio-dev/+archive/ppa fixes this thanks to the instructions here:
https://wiki.ubuntu.com/DebuggingSoundProblems/KarmicCaveats.
Making me slightly happier.
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.
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.
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) switch_off_buffering() PERIOD = 2 PAUSE = 5 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, _ = self.vf.read(4096) if bytes != 0: self.device.play(buff, bytes) else: self.vf.time_seek(0) return def repeat(pause, period): sample = OggSample(SOUND) sleep(pause) tick_time = time() while True: sys.stdout.write(".") sample.play() tick_time += period # avoid drift sleep(tick_time - time()) DEFAULT_DELAY = 5 DEFAULT_PERIOD = 2 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: usage(sys.stdout) 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 else: usage(sys.stderr) repeat(delay, period)
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, _ = self.vf.read(4096) if bytes != 0: self.device.play(buff, bytes) else: self.vf.time_seek(0) return sample = OggSample(filename) sample.play() sample.play()
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.
Postgres sql basic querying based on datetime
For the benefit of google (and me when I’m using google)
In postgres and probably most other SQLs the following
SELECT * FROM table where creation_date > timestamp 'now' - interval '2 hours';
finds all records that were created in the last 2 hours.
See also:
http://www.postgresql.org/docs/8.0/static/datatype-datetime.html
Python switching off buffering to stdout
For the benefit of google.
To switch off buffering of stdout in python one can use the following code:
def switch_off_buffering(): sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0) switch_off_buffering()
Note that some buffering occurs by default.
Alternatively one can use the -u option to python.
See also:
http://stackoverflow.com/questions/107705/python-output-buffering
Human readable datetimes in python’s django
For the benefit of google (and me when I’m searching). To create human readable dates, datetimes and timestamps in django’s templating language one can use the timesince filter like so
This happened {{ object.creation_date|timesince }} ago