Monday, December 22, 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 --remote=git@github.com:foo/bar.git --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

Thursday, December 4, 2014

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 between objects you can hash each attribute separately and then XOR the individual hashes.

class A:
  def __init__(self, x, s):
    self.x, self.s = x, s

  def __hash__(self):
    return self.x ^ self.s.__hash__()


  def __eq__(self, other):
    return self.__hash__() == other.__hash__()

And now

a1 = A(1, 'Hi')
a2 = A(1, 'Hi')
a = set([a1, a2])
-> just a1 as expected

Tuesday, December 2, 2014

Sphinx + Cython

It's pretty awesome that Sphinx will work with Cython code files (.pyx) but there are currently a few wrinkles:
  1. You need to rerun the cython compiler before the documentation changes take since sphinx reads this off the compiled library
  2. Function 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 docstring
def merge_variants(c1, c2):
  """
  merge_variants(c1, c2)
  ... rest of docstring ...
  """