how to find the last decorator of a chain

G

Gabriel

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Hi,

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.

In my understanding this equivalent to:

render('a',
render('b',
view(***)))

Is there any way to know, in this case, that 'a' is the 'default' format?

PS: the number of renders could be 1 or more.

- --
Kind regards.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.9 (GNU/Linux)

iEYEAREIAAYFAkohvjoACgkQNHr4BkRe3pJZQgCgqxL7Qq7/vqDQMLkGQs5emWgH
nbMAn2vzY0xGjG2xhOkxAf8hmERc8R5r
=wWbB
-----END PGP SIGNATURE-----
 
S

samwyse

I have something like this:

@render(format="a")
@render(format="b")
@....
def view(format, data):
  return data
In my understanding this equivalent to:

render('a',
 render('b',
  view(***)))

Not quite. 'render' is a function of one argument that returns a
decorator. So, the equivalent is more like this:
view = render('a')(render('b')(view))
or more simply:
fb = render('b')
view = fb(view)
fa = render('a')
view = fa(view)
Is there any way to know, in this case, that 'a' is the 'default' format?

Will this do? (In case the formatting gets messed up, I've also
posted the code to http://python.pastebin.com/f7f229d9d)

##from functools import wraps

def render(c):
def decorator(f):
## @wraps(f)
def wrapper(*args, **kwds):
if getattr(wrapper, 'outermost', False):
print('outer wrapper', c)
else:
print('inner wrapper', c)
return f(*args, **kwds)
return wrapper
return decorator

def mark_as_top(f):
print('marking', f)
f.outermost = True
## @wraps(f)
return f

@mark_as_top
@render('a')
@render('b')
@render('c')
@render('d')
def f():
pass

f()
 
A

Aahz

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,290
Messages
2,571,453
Members
48,131
Latest member
AntoniaSep

Latest Threads

Top