decorators

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 ...

How can I preserve or identify a caller's stack frame?

My brain feels slow today. I'm writing pre/post/invariants in Python using decorators. Currently, I need each call to specify the locals and globals for context, and this feels ugly. Is there a way to get the locals and globals from the decorator application level even though it's an arbitrary depth. That is, I'm trying to make ...

Django decorator getting a WSGIRequest and not the expected function argument

I'm creating a decorator for Django views that will check permissions in a non-Django-managed database. Here is the decorator: def check_ownership(failure_redirect_url='/', *args, **kwargs): def _check_ownership(view): def _wrapper(request, csi=None): try: opb_id=request.user.get_profile().opb_id ...

How we Create Custom Decorator that can apply on file field and radio button selection

if We apply any custom decorator on radio button then it will be display on next line so how we create such custom decorator that will display inline also same for file ...

Any way to set or overwrite the __line__ and __file__ metadata?

I'm writing some code that needs to change function signatures. Right now, I'm using Simionato's FunctionMaker class, which uses the (hacky) inspect module, and does a compile. Unfortunately, this still loses the line and file metadata. Does anyone know: If it is possible to overwrite these values in some odd way? If hacking up a c...

add a decorate function to a class

I have a decorated function (simplified version): class Memoize: def __init__(self, function): self.function = function self.memoized = {} def __call__(self, *args, **kwds): hash = args try: return self.memoized[hash] except KeyError: self.memoized[hash] = self.func...

Is there a better way of designing zend_forms rather than using decorators?

Hi, I am currently using zend_decorators to add styles to my form. I was wondering if there is an alternative way of doing it? It is a bit difficult to write decorators. I would love the casual one using divs and css style : <input type="submit" class="colorfulButton" > It is much simpler rather than set a decorator for a certain con...

Use python decorators on class methods and subclass methods

Goal: Make it possible to decorate class methods. When a class method gets decorated, it gets stored in a dictionary so that other class methods can reference it by a string name. Motivation: I want to implement the equivalent of ASP.Net's WebMethods. I am building this on top of google app engine, but that does not affect the point...

python decorator to modify variable in current scope

Goal: Make a decorator which can modify the scope that it is used in. If it worked: class Blah(): # or perhaps class Blah(ParentClassWhichMakesThisPossible) def one(self): pass @decorated def two(self): pass >>> Blah.decorated ["two"] Why? I essentially want to write classes which can maintain specifi...

How can I append a description to a Zend_Form_Element?

I have the following Zend_Form_Element: $imginstructions = "Some description"; $img = $this->createElement('select','img'); $img->setAttrib('class', 'image-select'); $imgdecorator = $img->getDecorator('Description'); $imgdecorator->setOption('escape', false); $img->setLabel('Image:') ->setRequired(true) ...

Testing Python Decorators?

I'm writing some unit tests for a Django project, and I was wondering if its possible (or necessary?) to test some of the decorators that I wrote for it. Here is an example of a decorator that I wrote: class login_required(object): def __init__(self, f): self.f = f def __call__(self, *args): request = args[0...

Zend Form - how do I create these custom form elements?

This is a very specific instance where I'm having difficulty getting Zend Form to produce the correct output and supply the correct validation. I may have to go create a composite element but thought I'd ask here first. Here is the HTML I'm trying to get Zend Form to produce. I'd like this to be able to work where if the validation doesn...

How does this Python decorator work?

I was looking at some lazy loading property decorators in Python and happened across this example (http://code.activestate.com/recipes/363602-lazy-property-evaluation/): class Lazy(object): def __init__(self, calculate_function): self._calculate = calculate_function def __get__(self, obj, _=None): if obj is None...

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...

Accessing a decorator in a parent class from the child in Python

Hi, how does one go about accessing a decorator from a base class in a child? I assumed (wrongly) that the ffg. would work: class baseclass(object): def __init__(self): print 'hey this is the base' def _deco(func): def wrapper(*arg): res = func(*arg) print 'I\'m a decorator. This is fabu...

Decorator changing function status from method to function

[Updated]: Answer inline below question I have an inspecting program and one objective is for logic in a decorator to know whether the function it is decorating is a class method or regular function. This is failing in a strange way. Below is code run in Python 2.6: def decorate(f): print 'decorator thinks function is', f ret...

Python decorators compared to CLOS "around" method.

Hi, I'm reaching back to my CLOS (Common Lisp Object System) days for this abstract question. I'm augmenting the question to clarify: It appears to me that a Python decorator is sort of like an "around" method in CLOS. From what I remember, an "around" method in CLOS is a method/function that wraps around the primary method/functi...

Python3 decorating conditionally?

Is it possible to decorate a function based on a condition? a'la: if she.weight() == duck.weight(): @burn def witch(): pass I'm just wondering if logic could be used (when witch is called?) to figure out whether or not to decorate witch with @burn? If not, is it possible to create a condition within the decorator to the sam...

problems rendering a template when using a decorator in Django

I have this URL in my project: url(r'^alerts/inbox/$', 'inbox', {'template_name': 'inbox.xhtml' }, name = 'inbox'), The inbox view is exactly this: @login_required() @ownsBid def inbox(request, template_name): return render_to_response(template_name, context_instance=RequestContext(request)) My ownsBid decorator is: def ownsBi...

Decorators applied to class definition with Python

Compared to decorators applied to a function, it's not easy to understand the decorators applied to a class. @foo class Bar(object): def __init__(self, x): self.x = x def spam(self): statements What's the use case of decorators to a class? How to use it? ...