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...
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...
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 ...
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
...
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...
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...
if(count($this->form->email->getMessages()) > 0)
{
$e = '<ul>';
$m = $this->form->email->getMessages();
foreach($m as $me)
{
$e .= '<li>';
...
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...
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)
...
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...
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):...
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...
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...
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 ...
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
...
Trying to find examples of when decorators might be really beneficial, and when not so much.
Sample code is appreciated.
...
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 ?
...
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() --...
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...
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:
...