decorator

How can make Django permission_required decorator not to redirect already logged-in users to login page, but display some message

How can make Django permission_required decorator not to redirect already logged-in users to login page, but display some message like Insufficient permissions? Thank you. ...

Python decorators in classes

Hi, can one write sth like: class Test(object): def _decorator(self, foo): foo() @self._decorator def bar(self): pass This fails: self in @self is unknown I also tried: @Test._decorator(self) which also fails: Test unknown If would like to temp. change some instance variables in the decorator and th...

Zend ViewScript Decorator - Where to place my viewScript

Hey, so I was having trouble with decorators, and finally found the ViewScript decorator which seems to be what I'm looking for. I can't quite figure how to get all the files to point to eachother though, and was hoping you guys could help. In my form, at the bottom, I have my assignment of the viewScript decorator to all my elements: ...

Zend Framework forms, decorators and validation: should I go back to plain HTML?

I am currently working on a pretty large application which contains a lot of forms. Up to this moment, I have always been writing my forms by hand and writing my own validation logic, but I have decided it was about time I started using Zend_Form and it's built-in validation routines. However, I keep stumbling upon more and more proble...

Python lazy attributes that don't eval on hasattr()

Is it possible to make a decorator that makes attributes lazy that do not eval when you try to access it with hasattr()? I worked out how to make it lazy, but hasattr() makes it evaluate prematurely. E.g., class lazyattribute: # Magic. class A: @lazyattribute def bar(self): print("Computing") return 5 >>> a = A...

Using the same decorator (with arguments) with functions and methods.

I have been trying to create a decorator that can be used with both functions and methods in python. This on it's own is not that hard, but when creating a decorator that takes arguments, it seems to be. class methods(object): def __init__(self, *_methods): self.methods = _methods def __call__(self, func): def...

How to replace Python function while supporting all passed in parameters

I'm looking for a way to decorate an arbitrary python function, so that an alternate function is called instead of the original, with all parameters passed as a list or dict. More precisely, something like this (where f is any function, and replacement_f takes a list and a dict): def replace_func(f, replacement_f): def new_f(*args,...

Applying the decorator pattern polymorphically and coupling problems in C++

I am trying to make a C++ implementation of the board game Carcassonne. I am trying to make a tile object which has four sides and one of three basic terrains(field, road, city). The best interface for tile creation I could think of was of the form: City city; city_city_city_city = new Tile(city, city, city, city); Where a Tile clas...

Python - ambiguity with decorators receiving a single arg

I am trying to write a decorator that gets a single arg, i.e @Printer(1) def f(): print 3 So, naively, I tried: class Printer: def __init__(self,num): self.__num=num def __call__(self,func): def wrapped(*args,**kargs): print self.__num return func(*args,**kargs**) ret...

Code refactoring with python decorators?

Hi everyone, I'm actually struggling with some piece of code. I do know that it can be refactored, but I can't find the nice-smart-elegant solution. Here are two functions (much more functions of that kind are in my code): def fooA(param1, param2): if param2 == True: code_chunk_1 fooA_code #uses only param1 if pa...

Adding functionality: subclass vs decorator

I'm currently working on a legacy system using Oracle's ADF Faces JSF implementation for the presentation layer. The system relies on a rules engine to make certain form elements required, disabled, or even highlighted depending on the user's interaction with them or values entered. At it's current stage the application is "working", s...

Does decorator pattern need concrete class? (and to call base class from decorator?)

This is an example from: dofactory I reformatted it a bit for better fit (compressed): namespace DoFactory.GangOfFour.Decorator.Structural{ class MainApp{ static void Main(){ ConcreteComponent c = new ConcreteComponent(); ConcreteDecoratorA d1 = new ConcreteDecoratorA(); ConcreteDecorato...

C++ game and decorator pattern

I'm porting a 2D platformer and I need a good way of getting some extensibility out of my level tiles. I'm not sure if Decorator is correct here, but nothing else comes to mind. Maybe I need something unique. Anyway, say I have a basic Tile that has things like it's image, and whether the player can pass through it (background or foregr...

Zend_Form and decorators : how to insert html tags on a multi options radioform ?

I'm currently trying to use Zend_Form, and it's decorators mechanism, in order to output some well formated HTML, but I don't manage to have the wanted output. Concretely, I have a radio button form element (Zend_Form_Element_Radio), with a multiple option registred in it, like this: $codeHolder = new Zend_Form_Element_Radio('radiobutt...

How to decorate a method inside a class?

Am attempting to decorate a method inside a class but python is throwing an error on me. My class looks like this: from pageutils import formatHeader myPage(object): def __init__(self): self.PageName = '' def createPage(self): pageHeader = self.createHeader() @formatHeader #<----- decorator def createHeader(...

function decorators in c#

Is there a C# analog for Python's function decorators? It feels like it's doable with attributes and the reflection framework, but I don't see a way to replace functions at runtime. Python decorators generally work this way: class decorator(obj): def __init__(self, f): self.f = f def __call__(self, *args, **kwargs): ...

Nice Python Decorators

How do I nicely write a decorator? In particular issues include: compatibility with other decorators, preserving of signatures, ect. I would like to avoid dependency on the decorator module if possible, but if there were sufficient advantages, then I would consider it. Related Preserving signatures of decorated functions - much more...

Python decorate a class to change parent object type

Suppose you have two classes X & Y. You want to decorate those classes by adding attributes to the class to produce new classes X1 and Y1. For example: class X1(X): new_attribute = 'something' class Y1(Y): new_attribute = 'something' *new_attribute* will always be the same for both X1 and Y1. X & Y are not related in any meaning...

Validation with ASP.NET MVC Linq To SQL: how do I avoid editing a generated source?

I'm reading several docs on validation in ASP.NET MVC. Ignoring those that suggest to reinvent the wheel writing your own validation logic, most articles advocate the use of xVal or Data Annotation Validators, both of which allow declarative validation through decorating models' properties(*). I think I'll go for xVal, as it seems to be...

How can I add a decorator to an existing object method?

If I'm using a module/class I have no control over, how would I decorate one of the methods? I understand I can: my_decorate_method(target_method)() but I'm looking to have this happen wherever target_method is called without having to do a search/replace. Is it even possible? ...