I Might Be Wrong

thunkurry: a Python decorator for partial application

Posted in Python by Leif Ryge on September 30, 2010

Here is a decorator which allows you to curry named and/or positional arguments as many times as you like. The underlying function is finally called when the decorated function is called as a thunk (meaning, without any arguments).

def thunkurry(f_, *t_, **d_):
    """Currying thunk decorator
    Example usage:
    >>> prod = lambda *a: reduce(operator.mul, a)
    >>> sum_ = lambda *a: sum(a)
    >>> thunkurry(prod)(2)(3)(4)() == prod(2,3,4) == 24
    True
    >>> thunkurry(lambda a,b,c:a+b+c)(c=1)(2)(c=3)(4)()
    9
    >>> a = thunkurry(sum_)
    >>> a = a(1)(2)(3); a()
    6
    >>> a = a(1)(2)(3); a()
    12
    """
    def fn(*T_, **D_):
        return f_(*t_, **d_) if not (T_ or D_) \
                else thunkurry(f_, *t_ + T_, **dict(d_.items() + D_.items()))
    fn.__doc__ = f_.__doc__
    return fn

I’ve mostly found this useful as a class decorator.

(Note: the Talk:Currying page on wikipedia has some discussion about the terms currying vs. partial application; I’m using the terms interchangeably here and am not sure if that is wrong.)

Tagged with: , , , ,

Computing permutations with a recursive generator expression

Posted in Python by Leif Ryge on October 21, 2008
#!/usr/bin/python2.5
def permute(li):
    """Generate all permutations of a sequence
    >>> for i in permute([1,2,3]): print i
    (1, 2, 3)
    (1, 3, 2)
    (2, 1, 3)
    (2, 3, 1)
    (3, 1, 2)
    (3, 2, 1)"""
    return ((i,)+j for i in li for j in (permute([k for k in li if k is not i])
                                         if len(li) > 1 else [()] ) )

import doctest
doctest.testmod(verbose=True)

Preview of our new Maemo theme

Posted in Maemo, Python by Leif Ryge on September 21, 2008

Ian and I have been working on a new release of our LCARS packages, featuring a number of bug fixes as well as a new theme called “Okuda”:
"Okuda" theme running on N810

If all goes well, we should be ready to release this within the next 48 hours. All four of our themes now include icon templates, which we’re slicing with hildon-theme-slicer (the same way that the main theme template is processed). This is what the icon template for the Okuda theme looks like:
icon_template.png
Anyone else wishing to create a hildon-theme-tools (and ThemeMaker) compatible layout definition might find this Python script helpful:

#!/usr/bin/python2.5
"""Generate a hildon-theme-tools layout from variously sized images.
Edit script to specify desired margin, width, and origin (dx,dy).
usage: hildon-theme-layout-grid-generator.py `find icons|sort|grep png`
"""

__author__ = "Leif Ryge <leif @synthesize.us>"
__license__ = "no rights reserved / public domain"

from sys import argv
from PIL import Image

margin, width, dx,dy = 10, 600, 0,0 # margin, output width, starting coords
sizes = {}

for f in argv[1:]: sizes.setdefault(Image.open(f).size,[]).append(f)

print "[Main]\nTemplateWidth=%s\nTemplateHeight=%s\n\n[Elements]" % (width, \
    sum((h+margin)*(len(sizes[w,h])/(width/(w+margin))+1) for w,h in sizes) )

for w,h in sorted(sizes):
    column = width / (w+margin)
    row = 1 + len(sizes[w,h]) / column
    print "# %s images are %sx%s (%sx%s grid)" % (len(sizes[w,h]),w,h,column,row)
    for i, f in enumerate(sizes[w,h]):
        print "%s=%s,%s,%s,%s" % (f, dx+(i%column)*(w+margin),
                                     dy+(i/column)*(h+margin),w,h)
    dy = dy+row*(h+margin)

The actual template can then be created from existing images using Nokia’s hildon-theme-regenerator tool.

Memoization in one line

Posted in Python by Leif Ryge on August 27, 2008

The memoization decorator in the Python Decorator Library has served me well, but, after reading stupid lambda tricks, I started thinking about how it could be reduced to a single expression. This is the result:

memoize = lambda f: (lambda d={}: lambda *a: d.setdefault(a, a in d or f(*a)))()

Unlike the original, mine doesn’t catch the case of the memoized function being called with unhashable (mutable) arguments. It doesn’t seem like that is a likely scenario, though.

Here is another version which also works with functions taking named arguments:

memo_with_named_args = lambda f: (lambda d={}: lambda *a,**kw: \
    (lambda *k: d.setdefault(k, k in d or f(*a,**kw)))(tuple(kw.items()),a) )()
Follow

Get every new post delivered to your Inbox.