decorator

Applying python decorators to methods in a class

I have decorator @login_testuser applied against method test_1: class TestCase(object): @login_testuser def test_1(self): print "test_1()" Is there a way I can apply @login_testuser on every method of the class prefixed with "test_"? In other words, the decorator would apply to test_1, test_2 methods below, but not o...

Python 2.6.4 property decorators not working

I've seen many examples online and in this forum of how to create properties in Python with special getters and setters. However, I can't get the special getter and setter methods to execute, nor can I use the @property decorator to transform a property as readonly. I'm using Python 2.6.4 and here is my code. Different methods to use p...

Potential use of Python decorator or other refactorization: iterative optimization

Forgive me for yet another question on Python decorators. I did read through many of them, but I wonder what the best solution to the specific following problem is. I have written several functions that do some form of gradient descent in numpy/scipy. Given a matrix X, I try to iteratively minimize some distance, d(X, AS), as functions ...

Is it possible to decorate include(...) in djano's urls with login_required?

I have a few restricted areas on the site, for which I would like to specify login_required decorator. However I would like to do that once per inclusion in main urls.py, not per individual url in included urls.py So instead of: /private/urls.py: (r'^profile/$', login_required(profile)), I'd do something along the lines: /urls.py ...

Django Signal via Decorator on Model Method?

Hi, I'm trying to do something like these proposed signal decorators. In addition to having a decorator that connects the decorated method to a signal (with the signal's sender as an argument to the decorator), I would like to use the decorator on class methods. I'd like to use the decorator like so: class ModelA(Model): @connec...

How does this decorator make a call to the 'register' method?

I'm trying to understand what is going on in the decorator @not_authenticated. The next step in the TraceRoute is to the method 'register' which is also located in django_authopenid/views.py which I just don't understand because I don't see anywhere that register is even mentioned in signin() How is the method 'register' called? def n...

Zend Framework configuring decorator

if(count($this->form->email->getMessages()) > 0) { $e = '<ul>'; $m = $this->form->email->getMessages(); foreach($m as $me) { $e .= '<li>'; ...

Can a Python Decorator of an Instance Method Access the Class?

Hi I have something roughly like the following. Basically I need to access the class of an instance method from a decorator used upon the instance method in its definition. def decorator(view): # do something that requires view's class print view.im_class return view class ModelA(object): @decorator def a_method(se...

Preferred way of defining properties in Python: property decorator or lambda?

Which is the preferred way of defining class properties in Python and why? Is it Ok to use both in one class? @property def total(self): return self.field_1 + self.field_2 or total = property(lambda self: self.field_1 + self.field_2) ...

Decorator class to test for required class variables

First of all I don't know if this is the right approach. I want to write a decorator class that will be used with methods of other class. Before running the method I'd like to check if all required class variables are initialized. The ideal case would be something similar to this: class Test(object): def __init__(self, f): s...

confused about python decorators

I have a class that has an output() method which returns a matplotlib Figure instance. I have a decorator I wrote that takes that fig instance and turns it into a Django response object. My decorator looks like this: class plot_svg(object): def __init__(self, view): self.view = view def __call__(self, *args, **kwargs):...

How to differentiate between method and function in a decorator?

I want to write a decorator that acts differently depending on whether it is applied to a function or to a method. def some_decorator(func): if the_magic_happens_here(func): # <---- Point of interest print 'Yay, found a method ^_^ (unbound jet)' else: print 'Meh, just an ordinary function :/' return func cla...

Graph limitations - Should I use Decorator?

I have a functional AdjacencyListGraph class that adheres to a defined interface GraphStructure. In order to layer limitations on this (eg. acyclic, non-null, unique vertex data etc.), I can see two possible routes, each making use of the GraphStructure interface: Create a single class ("ControlledGraph") that has a set of bitflags spe...

Refresh decorator

I'm trying to write a decorator that 'refreshes' after being called, but where the refreshing only occurs once after the last function exits. Here is an example: @auto_refresh def a(): print "In a" @auto_refresh def b(): print "In b" a() If a() is called, I want the refresh function to be run after exiting a(). If b() is ...

Is WPF Decorator class useful?

I need to create control to draw border around its child. So, I have created class and derived it from Decorator: class RoundedBoxDecorator : Decorator { protected override Size ArrangeOverride(Size arrangeSize) { //some source } protected override void OnRender(DrawingContext dc) { //some source ...

In Python, what are some examples of when decorators greatly simplify a task?

Trying to find examples of when decorators might be really beneficial, and when not so much. Sample code is appreciated. ...

decorators in the python standard lib (@deprecated specifically)

I need to mark routines as deprecated, but apparently there's no standard library decorator for deprecation. I am aware of recipes for it and the warnings module, but my question is: why is there no standard library decorator for this (common) task ? Additional question: are there standard decorators in the standard library at all ? ...

Java method missing (ala Ruby) for decorating?

Is there any technique available in Java for intercepting messages (method calls) like the method_missing technique in Ruby? This would allow coding decorators and proxies very easily, like in Ruby: :Client p:Proxy im:Implementation ------- ---------- ----------------- p.foo() --...

How to handle 'self' argument with Python decorators

I am trying to setup some decorators so that I can do something like: class Ball(object): def __init__(self, owner): self.owner = owner class Example(CommandSource): @command @when(lambda self, ball: ball.owner == self) def throwBall(self, ball): # code to throw the ball pass e = Example() ball...

Applying the Decorator Pattern to Forms

I am trying to apply the Decorator Design Pattern to the following situation: I have 3 different kind of forms: Green, Yellow, Red. Now, each of those forms can have different set of attributes. They can have a minimize box disabled, a maximized box disabled and they can be always on top. I tried to model this the following way: ...