python-decorators

Python: Decorators: How does the following code work?

Are the comments in the following code correct? Particularly the "instance =..." one? # This does nothing. class donothing(object): def __init__(self, func): """ The 'func' argument is the function being decorated because in this case, we're not instantiating the decorator class. Instead we are just ...

Python: Why can't I get my decorator to work?

This works now for those new to this question: class ensureparams(object): """ Used as a decorator with an iterable passed in, this will look for each item in the iterable given as a key in the params argument of the function being decorated. It was built for a series of PayPal methods that require different params,...

Caching non-view returns

I have a dozen or so permission lookups on views that make sure users have the right permissions to do something on the system (ie make sure they're in the right group, if they can edit their profile, if they're group administrators, etc). A check might look like this: from django.contrib.auth.decorators import user_passes_test test_c...

Python: How do I access an decorated class's instance from inside a class decorator?

Here's an example of what I mean: class MyDecorator(object): def __call__(self, func): # At which point would I be able to access the decorated method's parent class's instance? # In the below example, I would want to access from here: myinstance def wrapper(*args, **kwargs): return func(*args...

How can I pack serveral decorators into one?

I have several decorators on each function, is there a way to pack them in to one instead? @fun1 @fun2 @fun3 def do_stuf(): pass change to: @all_funs #runs fun1 fun2 and fun3, how should all_funs look like? def do_stuf(): pass ...

In Python, what are some examples of when decorators greatly simplify a task?

Trying to find examples of when decorators might be really beneficial, and when not so much. Sample code is appreciated. ...

Class Decorators, Inheritance, super(), and maximum recursion

I'm trying to figure out how to use decorators on subclasses that use super(). Since my class decorator creates another subclass a decorated class seems to prevent the use of super() when it changes the className passed to super(className, self). Below is an example: def class_decorator(cls): class _DecoratedClass(cls): def ...

Using python decorator functions from a different module

I want to use a function from another module as a decorator, but I need it to manipulate the current module's global namespace. For example, I want to be able to go from this: class SomeClass: pass root = SomeClass to this: from othermodule import decorator @decorator class Someclass: pass Any ideas? ...

Custom function decorator for views that modified the request path

Could someone show me how i could write a login decorator like @redirect_to_home for my views so that it modifies the request.PATH variable to a new a value like / whenever it is applied to a view. I've seen people do quite complex stuff with decorators: I'm yet to figure them out thoroughly. Thanks ...

Disabling Django CSRF for views that do not always have a response

I have a Django view that receives POSTs which do not need to have the CSRF token. Therefore I used the @csrf_exempt decorator on the view. The problem is that sometimes I do not issue a response from the view (it's a Twitter bot, it receives an HTTP POST for every tweet and I do not want to respond to every tweet). When I don't issue a ...

Finding a function's parameters in Python

I want to be able to ask a class's __init__ method what it's parameters are. The straightforward approach is the following: cls.__init__.__func__.__code__.co_varnames[:code.co_argcount] However, that won't work if the class has any decorators. It will give the parameter list for the function returned by the decorator. I want to get...

Is it bad practice to use self in decorators?

While I'm aware that you can't reference self directly in a decorator, I was wondering if it's bad practice to work around that by pulling it from args[0]. My hunch is that it is, but I want to be sure. To be more specific, I'm working on an API to a web service. About half the commands require a token to be passed that can be later use...

Python - correct order of the application of decorators

I'm decorating a function as such: def some_abstract_decorator(func): @another_lower_level_decorator def wrapper(*args, **kwargs): # ... details omitted return func(*args, **kwargs) return wrapper This does what you'd expect (applies a low level decorator and then does some more stuff. My problem is that I ...

Python - decorator - trying to access the parent class of a method

This doesn't work: def register_method(name=None): def decorator(method): # The next line assumes the decorated method is bound (which of course it isn't at this point) cls = method.im_class cls.my_attr = 'FOO BAR' def wrapper(*args, **kwargs): method(*args, **kwargs) return wrappe...

Python - making decorators with optional arguments

from functools import wraps def foo_register(method_name=None): """Does stuff.""" def decorator(method): if method_name is None: method.gw_method = method.__name__ else: method.gw_method = method_name @wraps(method) def wrapper(*args, **kwargs): method(*args, **...