Mocking __iter__ with a magic mock in Python

Posted on Thu 29 March 2012 in Coding • Tagged with iteration, list comprehension, magic mock, Python

I just ran into a tiny little problem and thought I’d share the solution.

I was writing some tests in which I was mocking a dependency using Mock. My test code was roughly equivalent to the following (based on an example in the Mock documentation on magic methods):

>>> sub1 ...

Continue reading

Generator combined with with/using statement - Python vs C#

Posted on Fri 30 December 2011 in Coding • Tagged with c#, generator, Python

The other day, I tried to return a generator expression from within a with statement in Python, like so:

#!/usr/bin/python

def readlines(fn):
    with open(fn) as fd: # <-- the with statement
        return (line for line in fd) # <-- the generator expression

if __name__ == "__main__":
    for line in readlines("/proc ...

Continue reading

Getting network interfaces in Python

Posted on Fri 30 December 2011 in Sysadmin • Tagged with linux, network interfaces, Python

Here’s a small Python script/module I put together to list all network interfaces on the current server (tested on Linux only!) in a format that easily lets me convert between name, index and address of an interface.

The code is based on getifaddrs.py from pydlnadms, but I ...


Continue reading

Mocking iteration over file in Python

Posted on Sat 24 December 2011 in Coding • Tagged with file, mock, test, Python

Let’s say that I want to mock reading from a file using file’s iterator capability, i.e. (simplified version):

def readfile(f):
    using open(f) as fd:
        for line in fd:
            yield line

I could mock the readfile function, but then my mock would have to replicate the ...


Continue reading

Split on separator but keep the separator, in Python

Posted on Fri 23 December 2011 in Coding • Tagged with reduce, split, string, Python

I have a function that reads from a file using file’s iterator capability, i.e. (simplified version, in reality there’s going on more within the generator):

def readfile(f):
    using open(f) as fd:
        for line in fd:
            yield line

If the file contains something like a\nb ...


Continue reading

Serializing non-qualified attributes with ElementTree

Posted on Mon 21 November 2011 in Coding • Tagged with elementtree, hack, python

The other day, I had a piece of Python code that deserialized an XML text containing a UPnP service description to an ElementTree instance using ElementTree.parse, then at a later stage serialized it back to XML using ElementTree.write.

Now, the service description happened to contain non-qualified attributes (like ...


Continue reading

Test data provider using Python metaclass

Posted on Sat 01 October 2011 in Coding • Tagged with metaclass, Python, test

In my airpnp project, I had to write a binary plist parser in order to parse certain data posted by iDevices. To support my unit tests, I created a number of binary plist files using an existing tool. I ended up with 17 files, and quickly realized that I wanted ...


Continue reading

Mocking Zope interfaces

Posted on Wed 28 September 2011 in Coding • Tagged with mock, test, python

In one of my pet projects I use Twisted for handling asynchronous network events. While browsing the Twisted source code, I encountered Zope interfaces. In a dynamically typed language like Python, what good are interfaces?

It turns out that Zope interfaces are slightly different from interfaces found in a statically ...


Continue reading