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