Port to Python (from Ruby) with all tests passing.