Swedish to not-so-Swedish through diacritics removal

Posted on Fri 06 April 2012 in Coding • Tagged with diacritics, Swedish, unicode

According to Merriam-Webster, a diacritic is:

a mark near or through an orthographic or phonetic character or combination of characters indicating a phonetic value different from that given the unmarked or otherwise marked element

Right! :-) In the Swedish language, there are three common letters with diacritics, namely å, ä and …

Continue reading

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

Nothing surprises me in VB.NET

Posted on Tue 27 March 2012 in Coding • Tagged with nothing, null, vb.net

I currently work in a VB.NET project with a customer, and what amazes me is that I over and over again get the feeling that the language is designed to be easy to work with, but at the same time there are so many aspects of it that behave …

Continue reading

NSubstitute vs Moq - a quick comparison

Posted on Mon 27 February 2012 in Coding • Tagged with mock, moq, nsubstitute, test

When you write unit tests, you occasionally need to mock away dependencies of the subject under test (SUT). By far the easiest way to do this is to use a mocking library, which has the added benefit that it allows you to verify the behavior of the SUT by inspecting …

Continue reading

Node.js experimentation - a GitHub clone script

Posted on Thu 19 January 2012 in Coding • Tagged with async, git, node.js

Recently I wrote my first Node.js program! Yeah, yeah, I know! Late to the game and all that! But I thought it was time to catch up with the rest of the world. Better late than never, right? (Also, the program needed to parse JSON data, so JavaScript felt …

Continue reading

Strongly typed properties in Java vs C#

Posted on Thu 19 January 2012 in Coding • Tagged with delegate, generics, method group, csharp, java

The background for this post was that I needed to implement strongly typed properties in a C# project. With strongly typed property, I specifically mean a strongly typed version of public object GetProperty(string propertyName), where the property name is a constant (or name) of some sort, and the return …

Continue reading

Generic extension method with multiple constraints

Posted on Wed 18 January 2012 in Coding • Tagged with constraints, extension methods, generics

In a .NET project, I was coding an extension method for forms, and for various reasons I needed to call a form method which came from an interface. Instead of coding an ugly type check and subsequent type cast, I was happy to discover that I could simply create a …

Continue reading

Playing with Java annotation processing

Posted on Fri 06 January 2012 in Coding • Tagged with annotations, apt, Java, spi

I found Project Lombok via a Tweet, and was intrigued when I saw that a single annotation could trigger automatic code generation transparently during compilation of a source file. How do they do that?

Some digging lead me to discover Java’s annotation processing API, and how one can use …

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:


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

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