From: W. Trevor King Date: Sun, 21 Dec 2008 07:40:44 +0000 (+0000) Subject: Importing NanoBlogger post "Splittable **kwargs" X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=418b192199d273bf683730a081050e3e69c8156d;p=blog.git Importing NanoBlogger post "Splittable **kwargs" --- diff --git a/posts/Splittable_**kwargs.mdwn b/posts/Splittable_**kwargs.mdwn new file mode 100644 index 0000000..4a6f77d --- /dev/null +++ b/posts/Splittable_**kwargs.mdwn @@ -0,0 +1,68 @@ +[[!meta title="Splittable **kwargs"]] +[[!meta date="2008-12-21 02:40:44"]] + +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](http://bugseverywhere.org/be/show/HomePage). My other +favorite is [Ditz](http://ditz.rubyforge.org/), 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](http://www.python.org/dev/peps/pep-0318/) (see [this +tutorial](http://adam.gomaa.us/blog/the-python-property-builtin/) for +some really neat examples) and [Bash +autocompletion](http://www.debian-administration.org/articles/317). + +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`](http://docs.python.org/tutorial/controlflow.html#keyword-arguments) +(see [wxPython's endorsement](http://wiki.wxpython.org/wxPython Style Guide#line-42)). +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 +module](/~wking/code/#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. + +[[!tag linux]] +[[!tag programming]]