decorator

Calling Python instance methods in function decorators

Is there a clean way to have a decorator call an instance method on a class only at the time an instance of the class is instantiated? class C: def instance_method(self): print('Method called') def decorator(f): print('Locals in decorator %s ' % locals()) def wrap(f): print('Locals in wrapper ...

Python decorators and class inheritance

I'm trying to use decorators in order to manage the way users may or may not access resources within a web application (running on Google App Engine). Please note that I'm not allowing users to log in with their Google accounts, so setting specific access rights to specific routes within app.yaml is not an option. I used the following r...

Decorating a method

Hi In my Python app, I'm using events to communicate between different plugins. Now, instead of registering the methods to the events manually, I thought I might use decorators to do that for me. I would like to have it look like this: @events.listento('event.name') def myClassMethod(self, event) I have first tried to do it like thi...

Is there a workaround for Composition and Marker Interfaces?

I see myself regularly confronted with the following problem. I have some kind of Marker Interface (for simplicity let's use java.io.Serializable) and several wrappers (Adapter, Decorator, Proxy, ...). But when you wrap a Serializable instance in another instance (which is not serializable) you loose functionality. The same problem occur...

Attaching a decorator to all functions within a class

I don't really need to do this, but was just wondering, is there a way to bind a decorator to all functions within a class generically, rather than explicitly stating it for every function. I suppose it then becomes a kind of aspect, rather than a decorator and it does feel a bit odd, but was thinking for something like timing or auth i...

Python design question (Can/should decorators be used in this case?)

I have a problem that can be simplified as follows: I have a particular set of objects that I want to modify in a particular way. So, it's possible for me to write a function that modifies a single object and then create a decorator that applies that function to all of the objects in the set. So, let's suppose I have something like thi...

when do we need Decorator Pattern?

when do we need to go for Decorator pattern? If possible give me a real world example that suits that pattern... ...

__decorated__ for python decorators

As of 2.4 (2.6 for classes), python allows you to decorate a function with another function: def d(func): return func @d def test(first): pass It's a convenient syntactic sugar. You can do all sorts of neat stuff with decorators without making a mess. However, if you want to find out the original function that got decorated you hav...

AS3/Flex Decorator Pattern

I'm trying to create a decorator class in AS3/Flex in order to add some functionality to a UI element. The problem is that I don't know how to automatically "redirect" method and property calls to the object being decorated. I suppose I'm looking for something like the __call() "magic method" from PHP, which is called every time the appl...

Why does calling the security authentication property `principal.displayName` in a decorator throw an exception?

Is there a reason why calling the security authentication property principal.displayName in a decorator would cause a problem? I'm setting it as a variable in a sitemesh decorator: <c:set var="displayName"> <sec:authentication property="principal.displayName" /> </c:set> But it gene...

Flex switch item-renderer

Hi I was wondering if anyone had any luck with the following senario in flex. I'd like to be able to have a custom item renderer which delegates to another renderer inside. The reason for this would be in a datagrid for instance displaying a checkbox if the dataprovider for the row had a boolean value. Using the default item renderer ...

How much overhead do decorators add to Python function calls

I've been playing around with a timing decorator for my pylons app to provide on the fly timing info for specific functions. I've done this by creating a decorator & simply attaching it to any function in the controller I want timed. It's been pointed out however that decorators could add a fair amount of overhead to the call, and that ...

Using the decorator design pattern for a hierarchy of classes

Looking at the following (simplified) hierarchy of classes: > Email (base class) > SimpleEmail extends Email > HtmlEmail extends Email I need to decorate Email.send() to add throttling functionality. I need to instantiate SimpleEmail, HtmlEmail or other similar subclasses of Email. What should this pattern look like exac...

Alternatives to this development pattern

Usually I start with pure model objects, usually in a tree structure. Depending upon the application, I will end up adding functionality to the model heirarchy, such as attaching UI controls and other data depending upon the application. Usually I'll do this by walking through the heirarchy and placing additional functionality on the ...

Javascript Date decorator for accurate client side time

I'm currently working on some Javascript to synchronize the client side clock with our servers. I'm doing this by looking at the 'Date' header of any Ajax responses (it only has to be accurate to a few seconds). To make the rest of the code work seamlessly with this Date function I want to override the native Date function in a decorato...

Zend Form elements in div

Hello. I am creating a form, using Zend Form. I have 8 input elements. And I want to placing first 6 elements in one div, and others in second. How I can do this? I am tried to use addDisplayGroup(), but it was unsuccessful. Or may be you know how to create a link in form? ...

Is it bad practice to use self in decorators?

While I'm aware that you can't reference self directly in a decorator, I was wondering if it's bad practice to work around that by pulling it from args[0]. My hunch is that it is, but I want to be sure. To be more specific, I'm working on an API to a web service. About half the commands require a token to be passed that can be later use...

Does Lua support Decorators?

I come from a Python background and really like the power of Python Decorators. Does Lua support Decorators? I've read the following link but it's unclear to me: http://lua-users.org/wiki/DecoratorsAndDocstrings UPDATE Would you also mind given an example how how to implement it in Lua if it's possible. ...

What is the best way to do automatic attribute assignment in Python, and is it a good idea?

Instead of writing code like this every time I define a class: class Foo(object): def __init__(self, a, b, c, d, e, f, g): self.a = a self.b = b self.c = c self.d = d self.e = e self.f = f self.g = g I could use this recipe for automatic attribute assignment. class Foo(obj...

Python decorator to refresh cursor instance

Hello, I have a method to save data in DB, and a decorator to manage the connection, but I can't figure out how to make it work. method to save: class DA_Row(DABase): @DABase.connectAndDisconnect def save(self): """ Guarda el spin en la base de datos """ self.__cursor.callproc('sp_insert_row', (...