Skip to content

Category Archives: Python

Using signal.alarm() for a timeout

The Python signal module let’s you setup Unix signals, including SIGALRM. This can be used to setup a timeout : import signal def handler(signum, frame): print ‘received’, signum # Setup the handler signal.signal(signal.SIGALRM, handler) # Trigger the alarm after 60 seconds signal.alarm(60) This prints ‘received 14’ after 60 seconds.

Reliable file updates in Python

Machines can fail, so it’s important to make sure your files are saved. This article has some great ways of doing reliable file updates with python. Great stuff.

Stubbing out open() in Python

Here’s an example that stubs out the open() function in Python using the mock library. from StringIO import StringIO from mock import patch # We are testing these 2 functions : def read_sys(): f = open(‘/sys/something’) data = f.readline().strip() f.close() return data def read_dev(): f = open(‘/dev/something’) data = f.readline().strip() f.close() return data # Test […]

Cool Python

Check out the example in the 3rd comment : That is cool.