Posts tagged ‘programming’

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

Hardcoding parameters into function names

Code seems to sometimes be slightly easier to read if information is pushed out of the arguments and into the function name.

I.e: Set(‘Color’, red) —> SetColor(red)

This has the nasty effects of making one’s APIs rather large, but if used sparingly it:

  1. Makes APIs more discoverable – the naming of function now contains information which would otherwise be in the usage of functions
  2. Makes code slightly easier to parse – Mostly just because we have removed an argument from the argument list
  3. Make code using the API slightly easier to write

Bear in mind that this statement is heuristic and general… so is probably wrong most of the time. Also I kind of suspect that all of this may immediately become false when two arguments rather than on are pulled out of the argument list and into the functional name.

This statement is, however, particularly true when your different operations take slightly different data as one of the other arguments vary.

July 30, 2008 at 10:47 pm Leave a comment

Reduce (fold) for the imperatively minded

Decorative PictureThe reduce or fold function is a concept in functional programming languages which can be used to simulate looping behaviour without the need for a real loop, and in particularly without the need for any explicit variables. I believe it originated in the field of functional programming languages, where getting rid of any form of ‘state’ seems to be an overriding aim.

I suspect that, with suitable training, one may be able to grasp the meaning of a (more…)

March 16, 2008 at 10:12 pm Leave a comment

A python caching decorator for referential transparent functions

The following is quite a nice way of automating caching in python functions using decorators. This code will work whenever a function depends on only its paramters (this is called referential transparency.)

def cached(func):
    cache = {}
    def new_func(*args):
        if args in cache:
             return cache[args]
             temp = cache[args] = func(*args)
             return temp
    return new_func

def non_cachedFunc(blah)

def cacheFunc(blah):

This makes turning caching on and off very easy and saves quite a lot of typing. The only overhead added to each call to a cached function should be one extra function call and a dictionary membership test.

March 12, 2008 at 9:42 pm Leave a comment

February 2019
« Nov