Available in a git repository.
Repository: splittable_kwargs
Browsable repository: splittable_kwargs
Author: W. Trevor King

Ah, it has been a while, but I am excited about a new python module I wrote so I shall post again ;). Actually, there has been a lot of Python learning over the past two months. It started out when I felt the need to setup a bug-tracker to keep track of all the things going wrong with my cantilever calibration software :p. Since I'm using git for versioning, I naturally wanted a 'distributed bugtracker'. Luckily there are a number of nice ones out there. I've been using Bugs Everywhere. My other favorite is Ditz, but I like being able to hack away on BE in my familiar Python.

Besides helping keep track of my bugs, I've enjoyed hacking away at BE. In the process I've learned some cool tricks such as Python decorators (see this tutorial for some really neat examples) and Bash autocompletion.

Meanwhile, the calibration code developed, and I was getting tired of keeping assorted keyword arguments default in sync (how many times do I have to repeat minFreq=500?). The way to avoid repeating yourself in this situation is to use *args or **kwargs (see [wxPython's endorsement][wxPython]). I don't trust myself to keep my code in sync enough to use args, so lets focus on *kwargs. Consider the following example:

>>> def A(a=1):
...     return a
>>> def B(b=2):
...     return b
>>> def C(**kwargs):
...     return A(**kwargs)+B(**kwargs)
>>> C(a=3,b=4)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in C
TypeError: A() got an unexpected keyword argument 'b'

Hmm, how to split the kwargs into those for A and those for B? So I wrote the splittable_kwargs. See the docstrings for some usage examples and more info. With my module the above example becomes

>>> from splittable_kwargs import splittableKwargsFunction
>>> @splittableKwargsFunction()
... def A(a=1):
...     return a
>>> @splittableKwargsFunction()
... def B(b=2):
...     return b
>>> @splittableKwargsFunction(A,B)
>>> def C(**kwargs):
...     akw,bkw = C._splitargs(C, kwargs)
...     return A(**bkw)+B(**akw)
>>> C(a=3,b=4)

In other news, my current side project is a MySQL-based, multi-user, archiving, command-line grade database. Down with web-forms ;). More information to come as the project progresses.

[wxPython]: http://wiki.wxpython.org/wxPython Style Guide#line-42