Category Archives: Snippet

The forgotten art of error checking

I was just rambling randomly yesterday, in the usual microblogging platforms, about how result checking seems to be ignored or done badly. The precise wording was: It’s really amazing how little attention error handling receives in most software development. Even … Continue reading

Posted in Architecture, Go, Java, Lua, Perl, Python, Ruby, Snippet, Test | 10 Comments

Mocker 0.10 and trivial patch-mocking of existing objects

Mocker 0.10 is out, with a number of improvements! While we’re talking about Mocker, here is another interesting use case, exploring a pretty unique feature it offers. Suppose we want to test that a method hello() on an object will … Continue reading

Posted in Project, Python, Snippet, Test | 2 Comments

Partial stubbing of os.path.isfile() with Mocker

One neat feature which Mocker offers is the ability to very easily implement custom behavior on specific functions or methods. Take for instance the case where you want to pretend to some code that a given file exists, but you … Continue reading

Posted in Project, Python, Snippet, Test | Leave a comment

Python’s os.environ

As Chris Armstrong pointed out yesterday, os.environ.pop() is broken in Python versions at least up to 2.5. The method will simply remove the entry from the in-memory dictionary which holds a copy of the environment: >>> import os >>> os.system(“echo … Continue reading

Posted in Python, Snippet | 2 Comments

Mandelbrot Set

I’ve finally posted the Mandelbrot Set code snippet that was sitting on my disk for a while. It computes and draws the Mandelbrot Set fractal using pygame, and also Python for Series 60. Three different screenshots are provided. They show … Continue reading

Posted in Fractal, Python, Snippet | Leave a comment

Labyrinth Solver

What about a labyrinth solver function in 16 lines of Python? :-)

Posted in Python, Snippet | 4 Comments

Textual watermarks with Python Imaging Library

I’ve added a new snippet about doing watermarks with PIL.

Posted in Python, Snippet | 2 Comments

Decorator for automatic inclusion of function definition in __doc__

Python 2.4 included support for function decorators. Decorators are callable objects that may modify a given function or class method arbitrarily when they’re prepended with specific syntax to the function/method definition. The following code implements a decorator that will automatically … Continue reading

Posted in Python, Snippet | Leave a comment

All combinations of N elements

Here’s a snippet to compute all combinations of N elements over K positions.

Posted in Math, Python, Snippet | 3 Comments

Permutations of all subsets

Another snippet to compute the permutations of all subsets.

Posted in Math, Python, Snippet | 3 Comments