Conditional decoration

J

Jeremiah Dodds

Is there any way to conditionally apply a decorator to a function?
For example, in django, I want to be able to control, via a run-time
config flag, if a view gets decorated with @login_required().

@login_required()
def my_view(request):
pass

You could write a decorator that takes the value of the flag and either
does nothing or decorates it's target function.
 
R

Roy Smith

Is there any way to conditionally apply a decorator to a function?
For example, in django, I want to be able to control, via a run-time
config flag, if a view gets decorated with @login_required().

@login_required()
def my_view(request):
pass
 
M

MRAB

Is there any way to conditionally apply a decorator to a function?
For example, in django, I want to be able to control, via a run-time
config flag, if a view gets decorated with @login_required().

@login_required()
def my_view(request):
pass
A decorator is just syntactic sugar for function application after the
definition.

This:

@deco
def func():
pass

is just another way of writing:

def func():
pass
func = deco(func)

Not as neat, but you can make it conditional.
 
E

Emile van Sebille

On 6/18/2012 3:16 PM Roy Smith said...
Is there any way to conditionally apply a decorator to a function?
For example, in django, I want to be able to control, via a run-time
config flag, if a view gets decorated with @login_required().

@login_required()
def my_view(request):
pass


class myDecorator(object):
def __init__(self, f):
self.f = f
def __call__(self):
print "Entering", self.f.__name__
self.f()
print "Exited", self.f.__name__


def null(a): return a


#if condition:
myDecorator = null


@myDecorator
def aFunction():
print "aFunction running"

aFunction()


Cobbled together from http://www.chrisevans3d.com/pub_blog/?p=530 and
http://stackoverflow.com/questions/3687046/python-sphinx-autodoc-and-decorated-members

HTH

Emile
 
D

Devin Jeanpierre

On 6/18/2012 3:16 PM Roy Smith said...
class myDecorator(object):
   def __init__(self, f):
       self.f = f
   def __call__(self):
       print "Entering", self.f.__name__
       self.f()
       print "Exited", self.f.__name__

I dunno about other people, but I generally avoid class decorators
because they do not behave the way functions do when applied as a
decorator for a method.

This is a decorator taken out of some of my own source code. I was
mostly dicking around with things, as opposed to actually coding. The
docstring was even more ridiculous.

from functools import reduce

def rapply(*args):
"""apply(f, ...) <-> rapply(..., f)"""
return args[-1](*args[:-1])

def condecorator(condition, *decorators):
if condition:
return lambda f: reduce(rapply, reversed(decorators), f)
else:
return lambda f: f

Example usage:

@condecorator(condition,
decorator1,
decorator2)
def decorated(...):
...

equivalent to:

def decorated(...):
...

if condition:
decorated = decorator1(decorator2(decorated))

-- Devin
 
R

Rob Williscroft

Roy Smith wrote in in
gmane.comp.python.general:
Is there any way to conditionally apply a decorator to a function?
For example, in django, I want to be able to control, via a run-time
config flag, if a view gets decorated with @login_required().

@login_required()
def my_view(request):
pass

You need to create a decorator that calls either the original
function or the decorated funtion, depending on your condition,
Something like (untested):

def conditional_login_required( f ):
_login_required = login_required()(f)

def decorated( request ):
if condition == "use-login":
return _login_required( request )
else:
return f( request )

return decorated

@conditional_login_required
def my_view(request):
pass

Replace (condition == "use-login") with whatever your "run-time
control flag" is.
 
G

Gelonida N

Roy Smith wrote in in
gmane.comp.python.general:


You need to create a decorator that calls either the original
function or the decorated funtion, depending on your condition,
Something like (untested):

def conditional_login_required( f ):
_login_required = login_required()(f)

def decorated( request ):
if condition == "use-login":
return _login_required( request )
else:
return f( request )

return decorated

@conditional_login_required
def my_view(request):
pass

Replace (condition == "use-login") with whatever your "run-time
control flag" is.

Your suggestion will unconditionally decorate a function and depending
on the condition call the original function or not.

However if you want to evaluate the condition only once at decoration
time, then you had probably to do something like (not tested)
 
J

Jean-Michel Pichavant

Roy said:
Is there any way to conditionally apply a decorator to a function?
For example, in django, I want to be able to control, via a run-time
config flag, if a view gets decorated with @login_required().

@login_required()
def my_view(request):
pass
Hi,


def my_view(request):
pass

if flag: my_view = login_required(my_view)

Regards,

JM
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top