how to find the last decorator of a chain

Aahz aahz at pythoncraft.com
Tue Jun 2 22:23:50 EDT 2009


In article <mailman.939.1243725387.8015.python-list at python.org>,
Gabriel  <gabriel at opensuse.org> wrote:
>
>I have something like this:
>
>@render(format="a")
>@render(format="b")
>@....
>def view(format, data):
>  return data
>
>Each render will do something with 'data' if format match, and nothing
>if not.
>
>But if there is no more renders to eval, the last one is the default,
>and must run even if the format doesn't match.

My inclination would be to make this explicit with something like this:

def make_render(func, format_list):
    def tmp(format, data):
        for f in format_list:
            if MATCH(format, f):
                render(data)
                break
        else:
            render(data)
    return tmp

def view(format, data):
    return data
view = make_render(view, ['a', 'b'])

IOW, just because we have decorators doesn't mean that they're the best
solution for all function-wrapping problems.
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

my-python-code-runs-5x-faster-this-month-thanks-to-dumping-$2K-
    on-a-new-machine-ly y'rs  - tim



More information about the Python-list mailing list