Convert to tags from "*" to "tags/*".
[blog.git] / posts / Splittable_kwargs.mdwn
1 [[!meta  title="Splittable **kwargs"]]
2 [[!meta  date="2008-12-21 02:40:44"]]
3
4 Ah, it has been a while, but I am excited about a new python module I
5 wrote so I shall post again ;).  Actually, there has been a lot of
6 Python learning over the past two months.  It started out when I felt
7 the need to setup a bug-tracker to keep track of all the things going
8 wrong with my cantilever calibration software :p.  Since I'm using git
9 for versioning, I naturally wanted a 'distributed bugtracker'.
10 Luckily there are a number of nice ones out there.  I've been using
11 [Bugs Everywhere](http://bugseverywhere.org/be/show/HomePage).  My other
12 favorite is [Ditz](http://ditz.rubyforge.org/), but I like being able to
13 hack away on BE in my familiar Python.
14
15 Besides helping keep track of my bugs, I've enjoyed hacking away at
16 BE.  In the process I've learned some cool tricks such as [Python
17 decorators](http://www.python.org/dev/peps/pep-0318/) (see [this
18 tutorial](http://adam.gomaa.us/blog/the-python-property-builtin/) for
19 some really neat examples) and [Bash
20 autocompletion](http://www.debian-administration.org/articles/317).
21
22 Meanwhile, the calibration code developed, and I was getting tired of
23 keeping assorted keyword arguments default in sync (how many times do
24 I have to repeat minFreq=500?).  The way to avoid repeating yourself
25 in this situation is to use [`*args` or
26 `**kwargs`](http://docs.python.org/tutorial/controlflow.html#keyword-arguments)
27 (see [wxPython's endorsement](http://wiki.wxpython.org/wxPython Style Guide#line-42)).
28 I don't trust myself to keep my code in sync enough to use *args, so
29 lets focus on **kwargs.  Consider the following example:
30
31     >>> def A(a=1):
32     ...     return a
33     >>> def B(b=2):
34     ...     return b
35     >>> def C(**kwargs):
36     ...     return A(**kwargs)+B(**kwargs)
37     >>> C(a=3,b=4)
38     Traceback (most recent call last):
39       File "<stdin>", line 1, in <module>
40       File "<stdin>", line 2, in C
41     TypeError: A() got an unexpected keyword argument 'b'
42
43 Hmm, how to split the kwargs into those for `A` and those for `B`?  So
44 I wrote the [splittable_kwargs
45 module](/~wking/code/#splittable_kwargs).  See the docstrings
46 for some usage examples and more info.  With my module the above
47 example becomes
48
49     >>> from splittable_kwargs import splittableKwargsFunction
50     >>> @splittableKwargsFunction()
51     ... def A(a=1):
52     ...     return a
53     >>> @splittableKwargsFunction()
54     ... def B(b=2):
55     ...     return b
56     >>> @splittableKwargsFunction(A,B)
57     >>> def C(**kwargs):
58     ...     akw,bkw = C._splitargs(C, kwargs)
59     ...     return A(**bkw)+B(**akw)
60     >>> C(a=3,b=4)
61     7
62
63 In other news, my current side project is a MySQL-based, multi-user,
64 archiving, command-line grade database.  Down with web-forms ;).
65 More information to come as the project progresses.
66
67 [[!tag tags/linux]]
68 [[!tag tags/programming]]