Posts tagged ‘python’
Reading mail using python
There may be a better way to go about this… however this works without requiring too much code.
* Use imapclient to fetch messages.
* Use fetch to get the full message using ‘BODY[]’
* Parse this with email.parser.Parser
* Use walk to pull out the message part (emails contain multiparts)
Victory.
Improved python dir
The results of dir are too long and writing comprehensions for filtering is slightly too painful. However there is a solution: replace it!
The following function is an improved version of dir that
* Pretty prints values
* Has an additional ‘filter’ argument that allows you to only search for names containing a particular string or matching a particular regular expression (this is case insensitive by default)
class PrettyList(list): def __repr__(self): return pprint.pformat(list(self)) UNSET = object() def magic_dir(object=UNSET, filter='', flags=re.I): if object is UNSET: results = sys._getframe(1).f_locals.keys() else: results = dir(object) if isinstance(filter, str): regexp = re.compile(filter, flags) else: regexp = filter return PrettyList([x for x in results if regexp.search(x)])
I then smuggle this dir into my context replacing the builtin dir by setting PYTHONSTARTUP and doing the patching here. There are some slight issues with getting this into pdb, by it otherwise works wonderfully.
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
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.
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
Asynchronous file io in eventlet (aka non-thread blocking read)
I spent a little while trying to get this to work. I ended up using the following approach that worked quite well
i) Create a thread pool
ii) Use this thread pool to to perform blocking file io
and return this result to your thread
iii) Do something with the result
I do quite like the run-this-on-another-thread-without-blocking-this-thread-and-wait-for-the-result” operation.
Below is some (tested) code for eventlet 0.9.4.
import sys import eventlet.tpool def controller(): while True: line = eventlet.tpool.execute(sys.stdin.readline) eventlet.spawn(talker, line.strip()) def talker(msg): while True: eventlet.sleep(1) print msg eventlet.spawn(controller).wait()