Skip to main content


Showing posts from December, 2014

Retrieve a single file from a git repo.

The answer to this was more difficult to find than it should have been. The best answer is on stackoverflow here.

In short, use the git archive command:

git archive --prefix=path/to/ HEAD:path/to/ | tar xvf -

For this to work the owner of the git server needs to have enabled upload-archive (git config daemon.uploadarch true)You need tar -xvf because the output is in tar form

Using user classes with Python's set operations

Python has an efficient, and built in, implementation of set operations. With a tiny bit of work you can make your user defined classes work nicely with set operations (as well as dictionaries).

Say you have a class:

class A: def __init__(self, x, s): self.x, self.s = x, s
Python actually doesn't have a way of knowing if the objects are the same or not:

a1 = A(1, 'Hi') a2 = A(1, 'Hi') a = set([a1, a2]) -> a1, a2
and by default assumes they are different.

All you have have to do, for things to work properly, is to define a hash function (__hash__()) and an equality (__eq__()) operator for your class. The two functions need to be congruent i.e. if the hash value for two objects is the same, they should also register as equal. It is possible to define the functions differently but this will lead to funny things happening with set operations.

The hash value is an integer. If your class has several attributes that you consider important for distinguishing betwe…

Sphinx + Cython

It's pretty awesome that Sphinx will work with Cython code files (.pyx) but there are currently a few wrinkles:
You need to rerun the cython compiler before the documentation changes take since sphinx reads this off the compiled libraryFunction signatures are omitted in the documentation and the compiler directive # cython: embedsignature=True does not work either. The workaround is to repeat the signature as the first line of the docstringdef merge_variants(c1, c2): """ merge_variants(c1, c2) ... rest of docstring ... """