decorator

how to implement a decorator in PHP?

Suppose there is a class called "Class_A", it has a member function called "func". I want the "func" to do some extra work by wrapping Class_A in a decorator class. $worker = new Decorator(new Original()); Can someone give an example? I've never used OO with PHP. ======================================================= Thank you g...

How do I infer the class to which a @staticmethod belongs?

I am trying to implement infer_class function that, given a method, figures out the class to which the method belongs. So far I have something like this: import inspect def infer_class(f): if inspect.ismethod(f): return f.im_self if f.im_class == type else f.im_class # elif ... what about staticmethod-s? else: ...

How to access the parent class during initialisation in python?

How do I find out which class I am initialising a decorator in? It makes sense that I wouldn't be able to find this out as the decorator is not yet bound to the class, but is there a way of getting round this? class A(object): def dec(f): # I am in class 'A' def func(cls): f(cls) return func @dec...

Does it matter when a decorator gets called (at the beginning or at the end of the method) in the decorator pattern?

I'm implementing a decorator pattern in Javascript. I've seen the example here in Wikipedia // Class to be decorated function Coffee() { this.cost = function() { return 1; }; } // Decorator A function Milk(coffee) { this.cost = function() { return coffee.cost() + 0.5; }; } // Decorator B function Whip(coffee) { ...

In Zend Framework how do you wrap form elements in a label using decorators?

I would like to wrap form elements with labels as such <label for="email">E-mail <input type="text" name="email" id="email" /></label> The only option I could find is to alter the placement; however it only accepts 'prepend' and 'append': <!-- prepend --> <label for="email">E-mail</label> <input type="text" name="email" id="email" />...

Is it wrong to use the decorator pattern like this?

I've noticed that on all the examples that illustrate the decorator pattern, there is a base class/interface and there is a decorator class that inherits/implements it, after which, all the decorated classes extend the decorator class. Something along the following lines : Interface Window > WindowDecorator > WindowDecorator > VerticalS...

Servlet page decoration: Do people use Tiles, Sitemesh, or something else?

I've used Tiles and Sitemesh for a number of years and while I personally prefer the Sitemesh style page decoration, I generally don't see a lot of mention of Sitemesh or Tiles on the Internet. Do people use Tiles and/or Sitemesh actively, or are there other libraries that have taken over in this capacity? ...

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

How frequently should Python decorators be used?

I recently started experimenting with Python decorators (and higher-order functions) because it looked like they might make my Django unit tests more concise. e.g., instead of writing: def visit1(): login() do_stuff() logout() I could instead do @handle_login def visit1(): do_stuff() However, after some experimentin...

Python Decorator 3.0 and arguments to the decorator

I'm excited to see the latest version of the decorator python module (3.0). It looks a lot cleaner (e.g. the syntax is more sugary than ever) than previous iterations. However, it seems to have lousy support (e.g. "sour" syntax, to horribly stretch the metaphor) for decorators that take arguments themselves. Does anyone have a good ex...

Decorator to mark a method to be executed no more than once even if called several times

I will go straight to the example: class Foo: @execonce def initialize(self): print 'Called' >>> f1 = Foo() >>> f1.initialize() Called >>> f1.initialize() >>> f2 = Foo() >>> f2.initialize() Called >>> f2.initialize() >>> I tried to define execonce but could not write one that works with methods. PS: I cannot define the code ...

How are these type of python decorators written?

I'd like to write a decorator that would limit the number of times a function can be executed, something along the following syntax : @max_execs(5) def my_method(*a,**k): # do something here pass I think it's possible to write this type of decorator, but I don't know how. I think a function won't be this decorator's first argum...

How to use C#-like attributes in C++

I'm considering the use of C++ for a personal project. I would like to make it platform independent (no Mono please, since some platforms don't yet support it), and that's why I considered C++. I have one doubt, however. I've grown quite fond of C#'s attributes, and I would like to know if I can use something similar in C++. Also, is i...

Python Function Decorators in Google App Engine

I'm having trouble using python function decorators in Google's AppEngine. I'm not that familiar with decorators, but they seem useful in web programming when you might want to force a user to login before executing certain functions. Anyway, I was following along with a flickr login example here that uses django and decorates a func...

defining functions in decorator

Why does this not work? How can I make it work? That is, how can I make gu accessible inside my decorated function? def decorate(f): def new_f(): def gu(): pass f() return new_f @decorate def fu(): gu() fu() Do I need to add gu to a dictionary of defined functions somehow? Or can I add gu t...

How to strip decorators from a function in python

Let's say I have the following: def with_connection(f): def decorated(*args, **kwargs): f(get_connection(...), *args, **kwargs) return decorated @with_connection def spam(connection): # Do something I want to test the spam function without going through the hassle of setting up a connection (or whatever the decora...

Struts2 Sitemesh not working in WAS 6 server

I have a struts2-spring application which works fine in jetty server but when i try migrating it to WAS 6 the decorator(sitemesh) is not getting applied. The server logs shows no error.Is this a known issue ? my web.xml looks like this <filter> <filter-name>action2-cleanup</filter-name> <filter-class>org.apache.struts2.dispatch...

Python: How to get rid of circular dependency involving decorator?

I had got a following case of circular import (here severly simplified): array2image.py conversion module: import tuti @tuti.log_exec_time # can't do that, evaluated at definition time def convert(arr): '''Convert array to image.''' return image.fromarray(arr) tuti.py test utils module: import array2image def log_exec_time...

How can I use a class instance variable as an argument for a method decorator in Python?

How can I use a class instance variable as an argument for a method decorator in Python? The following is a minimal example shows what I'm trying to do. It obviously fails as the decorator function does not have access to the reference to the instance and I have no idea how to get access to the reference from the decorator. def decorato...

Decorating a class whose constructor has parameters?

I am using the decorator pattern and am decorating a class with a constructor that has parameters. Below is the constructor of the class decorating; Public Sub New(ByVal repository As ISchedulingRespository) Me.repository = repository End Sub Because my decorator class inherits from the decorated class I need to declare it's co...