except for a doubly nested for on the above docstring (!), it all looks good to me;

i'm not that keen on the code around the unittesting, but -what the heck- it should pop up during testing ;-)