[[!meta title="Splittable **kwargs"]] [[!meta date="2008-12-21 02:40:44"]] [[!template id=gitrepo repo=splittable_kwargs]] 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][BE]. 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][decorators] (see [this tutorial][AG] for some really neat examples) and [Bash autocompletion][bash]. 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`][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 "", line 1, in File "", 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) 7 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. [BE]: http://bugseverywhere.org/ [Ditz]: http://ditz.rubyforge.org/ [decorators]: http://www.python.org/dev/peps/pep-0318/ [AG]: http://adam.gomaa.us/blog/the-python-property-builtin/ [bash]: http://www.debian-administration.org/articles/317 [kwargs]: http://docs.python.org/tutorial/controlflow.html#keyword-arguments [wxPython]: http://wiki.wxpython.org/wxPython Style Guide#line-42 [[!tag tags/linux]] [[!tag tags/programming]]