decorators

The Decorator Design Pattern

I am just starting to learn design patterns and I have two questions related to the Decorator... I was wondering why the decorator pattern suggests that the decorator implement all public methods of the component of which it decorates? Can't the decorator class just be used to provide the additional behaviors, and then the concrete com...

Python convert args to kwargs

I am writing a decorator that needs to call other functions prior to call of the function that it is decorating. The decorated function may have positional arguments, but the functions the decorator will call can only accept keyword arguments. Does anyone have a handy way of converting positional arguments into keyword arguments? I kn...

Zend_Form decorators ordering (Errors before form element issue)

Hello, I have the following Zend_Form code to apply to form element decorators: $decorators = array( 'ViewHelper', 'Description', array('break' => 'HtmlTag', array('tag' => 'br', 'openOnly'=>true)), array('Label', array('class' => 'formLabel', 'separator'=>' ', 'requiredSuffix' => ' <em class="requiredForm...

Python - Get original function arguments in decorator

I am trying to write a "login_required" decorator for the views in a WSGI+Werkzeug application. In order to do this, I need to get at the user's session, which is accessible via the Request object that is passed into the view methods. I can't figure out how to get at that instance of Request in the decorator, though. I looked at PEP318...

Decorators that are properties of decorated objects?

I want to create a decorator that allows me to refer back to the decorated object and grab another decorator from it, the same way you can use setter/deleter on properties: @property def x(self): return self._x @x.setter def x(self, y): self._x = y Specifically, I'd like it to act basically the same as property, but emulate a...

Creating a decorator in a class with access to the (current) class itself

Currently, I'm doing it in this fashion: class Spam(object): decorated = None @classmethod def decorate(cls, funct): if cls.decorated is None: cls.decorated = [] cls.decorated.append(funct) return funct class Eggs(Spam): pass @Eggs.decorate def foo(): print "spam and eggs" ...

C function decorators (wrappers) at compile time

I'm trying to change the behaviour of some functions in C with help of the preprocessor; and also add optional parameters that can be set on or off... The basic pattern for the optional parameters is easy: #ifdef OPT_PARAM #define my_func(a, b, opt) _my_func(a, b, opt) #else #define my_func(a, b, opt) _my_func(a, b) #endif /*the r...

Django options for making variables widely available

Every time I open a page I want to get the currently active project id. This will be done by chacking the subdomain and verifying the currently logged in user can view it. Once I reach my view I want to be able to do tasks = Task.objects.filter(project = current_project) WHere current_project (or CURRENT_PROJECT or current_project...

django class view with decorator and sessions

I'm trying to convert some of my django views over from function based views to class based views and I've run into a small problem. My OO is kind of weak and I think the problem is that I've lost track of where things are going. I have a custom login decorator that I need on the views so I have... First I have the View class from thi...

dynamically adding functions to a Python module

Our framework requires wrapping certain functions in some ugly boilerplate code: def prefix_myname_suffix(obj): def actual(): print 'hello world' obj.register(actual) return obj I figured this might be simplified with a decorator: @register def myname(): print 'hello world' However, that turned out to be rat...

Python logging using a decorator

This is the first example we meet when we face with decorators. But I'm not able to realize what exactly I would like. A simple decorator named LOG. It should work like this: @LOG def f(a, b=2, *c, **d): pass And the result should be something like: f(1, pippo=4, paperino='luca') ===== Enter f ===== a = 1 b = 2 pippo = 4 paperin...

Strange decorator result on related object comparison

On views that allow updating/deleting objects, I need a decorator that verifies that the object to be edited belongs to a group(model "loja). Both defined in the url: /[slug model loja--s_loja]/[viewname-ex:addmenu]/[object id--obj_id] Because the model of the object can vary, the decorator the model of the object as an argument. Every...

Python: Very confused about decorators...

I thought I understood decorators but not anymore. Do decorators only work when the function is created? I wanted to create a series of functions that all have a required argument called 'ticket_params' that is a dictionary. and then decorate them with something like @param_checker(['req_param_1', 'req_param_2']) and then if 'req_param_...

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

Django + decorators: Adding context to the template based on a criteria.

Hi there, I'm not sure if decorators are the best way to do this, but I've removed the idea of using context processors and I'm not sure if a middleware is what I'd like. My situation is as follows: We process sales and other data daily. Every month, we close the month off just like any other business. We do this on paper, but I would ...

Zend Form decorators trouble

How do I achieve the following with form decorators for form elements: <dt> <ul> <li>The errors</li> <li>The errors</li> </ul> <label>The label</label> </dt> <dd> <input type="text" value="The input field"> </dd> In other words, in stead of Errors appended after the input field, I want them prepended before the Label. ...

Change HTML output of Zend_Form

Hi, I'm trying to change the html outputted by Zend_Form using decorators. I want the outputted HTML to look like this: <form> <fieldset> <legend>Your Details</legend> <dl> <dt>label etc</dt> <dd>input etc</dd> <dt>label etc</dt> <dd>input etc</dd> </dl> </fieldset> <fieldset> <legend>Addre...

Zend Form Decoration Issues

I've got a few random issues with decorator related stuff with Zend Form. Firstly, // THIS WORKS AND REMOVES THE DECORATORS $hidden = new Zend_Form_Element_Hidden('hiddenfield'); $hidden->setRequired(TRUE) ->removeDecorator('label') ->removeDecorator('HtmlTag') ->addErrorMessage('Please upl...

zend form decorators

Having (more) issues with zend form decorators. I've got this so far: Reset overall form decorator: $this->clearDecorators(); $this->setDecorators(array('FormElements', 'Form')); I'm adding all my elements to a display group that i want to be inside a fieldset, within a DL $group->setDecorators(array( 'FormEle...