decorator

PHP equivalent for a python decorator?

I want to be able to wrap a PHP function by another function, but leaving its original name/parameter list intact. For instance: function A() { print "inside A()\n"; } function Wrap_A() { print "Calling A()\n"; A(); print "Finished calling A()\n"; } // <--- Do some magic here (effectively "A = Wrap_A") A(); Output:...

How to determine the origin of a LocalJumpError?

How can I determine easily and programmatically whether a LocalJumpError arose from the caller's immediate failure to supply a needed block to a method, or from deeper within that method and others it invokes? By "easily," I mean I'd like to avoid string inspection/regexen on $!.backtrace. A solution applicable to 1.8 and 1.9 is also p...

Decorators should not have side effects?

Editing because the initial code was confusing. I would assume these two things to be same, #I would use either of these #Option 1 def bar(*args): pass foo = deco(bar) #Option2 @deco def foo(*args): pass However if the decorators deco has side effects, this is not guaranteed. In partcicular, this was my ecpectation form a de...

Zend_form rendering problem

Hi, I need a zend_form which will contain mostly checkboxes. I need to group them and also display a title for each group. e.g. Heading 1 Label1 Check1 Label2 Check2 Label3 Check3 Heading 2 Label4 Check4 Label5 Check5 Label6 Check6 First I don't know how to display the title ("headings")! Is there a way that you can add a label ...

Python Decorator for GAE Web-Service Security Check

In this post, Nick suggested a decoartor: http://stackoverflow.com/questions/1499832/python-webapp-google-app-engine-testing-for-user-pass-in-the-headers/1500047#1500047 I'm writing an API to expose potentially dozens of methods as web-services, so the decorator sounds like a great idea. I tried to start coding one based on this sam...

Is there a way to get the function a decorator has wrapped?

Suppose I have @someDecorator def func(): '''this function does something''' print 1 Now, the object func is an instance of someDecorator. Is there some way I can access the function it holds, i.e something like func.getInnerFunction(). For instance, if I need to retrieve the doc string of func(). ...

Prevent decorator from being used twice on the same function in python

I have a decorator: from functools import wraps def d(f): @wraps(f) def wrapper(*args,**kwargs): print 'Calling func' return f(*args,**kwargs) return wrapper And I want to prevent it from decorating the same function twice, e.g prevent things such as: @d @d def f(): print 2 Only possible solution I co...

extending a class that doesn't implement an interface

I'd like to override the Serialize methods of the ASP.NET JavaScriptSerializer class. Nothing too fancy, I just want to do some additional post processing to the serialized string returned from .NET. Unfortunately, none of the methods on this class are declared virtual and the class itself does not derive from an interface or abstract ...

decorator with a base that requires a constructor argument

I have a decorator-like pattern with a base that requires a constructor parameter. The decorator is constructed such that it can take an arbitrary number of add-on components as template parameters (up to 3 in this example). Unfortunately, I can't figure out how to pass the base's constructor parameter to it when more than one add-on is...

template meta-programming OR operation

I have a class that can be decorated with a set of add-on templates to provide additional functionality. Each add-on has an identifying addon_value that the base class needs to know. The code below is an example of what I would like to do. Obviously, the main() function fails to compile. The goal is for CBase::GetValueOfAddOns() to kno...

ambiguous access when calling a decorated base class

I have a class that can be decorated with a set of add-on templates to provide additional functionality. Each add-on needs to be able to call the base class and the user needs to be able to call the base class (either directly or using the CMyClass as a proxy). Unfortunately, the compiler can't tell which base class I'm calling and I ge...

Python: static variable decorator

I'd like to create a decorator like below, but I can't seem to think of an implementation that works. I'm starting to think it's not possible, but thought I would ask you guys first. I realize there's various other ways to create static variables in Python, but I find those ways ugly. I'd really like to use the below syntax, if possible...

How to change the layout of a form with Zend_Form decorators?

I'm totally confused about how decorators work. This is the html structure that I'm trying to achieve: <form id="" action="" method="post"> <fieldset><legend>Contact form</legend> <p> <label for="name">Name</label> <input type="text" name="name" id="name" size="30" /> </p> <p> <label for="email">Ema...

Java: List<E> decorator implementation with notification

Hi, I need to implement List decorator class, which notify registered listeners in case of any change in list. I have subclassed AbstractListDecorator from org.apache.commons.collections15 and override methods like add(), addAll(), remove(int), remove(E) and so on with my notifying. But there are some holes in -- for example when iter...

How to keep help strings the same when applying decorators?

How can I keep help strings in functions to be visible after applying a decorator? Right now the doc string is (partially) replaced with that of the inner function of the decorator. def deco(fn): def x(*args, **kwargs): return fn(*args, **kwargs) x.func_doc = fn.func_doc x.func_name = fn.func_name return x @dec...

Decorating Instance Methods in Python

Here's the gist of what I'm trying to do. I have a list of objects, and I know they have an instance method that looks like: def render(self, name, value, attrs) # Renders a widget... I want to (essentialy) decorate these functions at runtime, as I'm iterating over the list of objects. So that their render functions become this: d...

How to enforce unicode arguments for methods?

I have a model class with getter and setter methods, and the occasional static methods. I would like to enforce the usage of unicode strings as arguments for specific methods and using decorators was the first idea I had. Now I have something like this: import types class require_unicode(object): def __init__(self, function): ...

Python decorators on class members fail when decorator mechanism is a class

When creating decorators for use on class methods, I'm having trouble when the decorator mechanism is a class rather than a function/closure. When the class form is used, my decorator doesn't get treated as a bound method. Generally I prefer to use the function form for decorators but in this case I have to use an existing class to imp...

Is it possible to redefine reverse in a Django project?

I have some custom logic that needs to be executed every single time a URL is reversed, even for third-party apps. My project is a multitenant web app, and the tenant is identified based on the URL. There isn't a single valid URL that doesn't include a tenant identifier. I already have a wrapper function around reverse, but now I need a...

Use a Descriptor (EDIT: Not a single decorator) for multiple attributes?

Hi, Python 2.5.4. Fairly new to Python, brand new to decorators as of last night. If I have a class with multiple boolean attributes: class Foo(object): _bool1 = True _bool2 = True _bool3 = True #et cetera def __init__(): self._bool1 = True self._bool2 = False self._bool3 = True #et ...