depth first search

“We can only see a short distance ahead, but we can see plenty there that needs to be done."

Category: python

In a Pickle

I like IPython, but I’m not sure I understand it, and my lack of understanding has caused me some pain recently when a pickle file (Python object serialization for the uninitiated) would not open. I’m not even sure I’ve figured out how to reproduce the problem, but I did find a very simple example of [...]

Reversing a Dictionary

For some reason the code I’m writing requires a lot of mappings between different kinds of values, and I find myself trying to manipulate dictionaries in many ways. I wrote a simple function that reverses a dictionary: Then I remembered the setdefault method and was able to create a more concise function: Of course if [...]

Worse than Failure

The shorthand “WTF” is common among developers. It does not actually stand for “worse than failure” but I run a clean ship here people. [Hint: let's just say that WTF is a somewhat crude interrogative. Sample usage: "Dude, WTF?" or, if you prefer, comic form.] I just spent two days looking for a bug in [...]

Computing All Shortest Paths in Python

A research problem I’m working on involves Isomap, a method of dimensionality reduction that requires computing all shortest paths over relatively large graphs. In interpreted languages like Matlab this is often done through the use of the Floyd-Warshall algorithm, a simple dynamic programming approach to computing all shortest paths. In the reference Isomap code this [...]

Metaclass Magic

So today I found myself trying to figure out how to pickle objects that are based on dynamically constructed classes. This is probably the simplest instance of metaclass magic in Python — the use of the ‘type’ builtin, a metaclass. A metaclass is a class whose objects are themselves class specifications. People use metaclasses to [...]

Python Segfaults

Python is an interpreted language, and the interpreter is solid enough that you wouldn’t expect to encounter the following: stober[~]{landmine}$ python test.py Segmentation fault Pretty scary stuff. So what was I running? #!/lusr/bin/python from opencv import highgui from opencv import cv   writer = highgui.cvCreateVideoWriter("test.mpg", highgui.CV_FOURCC(’m',’p',’g',’1′), 30.0, cv.cvSize(100,100), True) 99.9% of the time the Python [...]

OpenCV and Python

You may have come across the following issue with OpenCV Python bindings: image = cv.cvCreateImage(size, cv.IPL_DEPTH_8U, 3) image.imageData = data # set some data Trying to set the “data” of an OpenCV image directly fails. The problem is in the underlying SWIG code that attempts to make the imageData field available for writing at the [...]

On Python

I no longer choose to use Python. One day I decided to spend a couple of hours parallelizing some code for a new dual core machine. Than I discovered the GIL. From the Python list circa 2004: I’ve said it before. One day enough people will think that the GIL is a problem big enough [...]

Parallel Python

I once tried to parallelize some Python code to take advantage of multiple processors, so I learned about the Global Interpreter Lock the hard way. Threads are supposed to allow for concurrency without protection. If you want concurrency and protection, use processes. The GIL is just a half-baked half measure between these two easy to [...]

Continuation Passing Style – Twisted

Is deferred a use of continuation passing style? In a sense. continuation is a representation of some of the execution state of a program Adding deferred callbacks defines a sequence of continuations. At each step, the deferred process calls the next function in the callback sequence using the data returned from the previous call. It’s [...]