I have several decorators on each function, is there a way to pack them in to one instead?
@fun1
@fun2
@fun3
def do_stuf():
pass
change to:
@all_funs #runs fun1 fun2 and fun3, how should all_funs look like?
def do_stuf():
pass
...
My brain feels slow today.
I'm writing pre/post/invariants in Python using decorators. Currently, I need each call to specify the locals and globals for context, and this feels ugly. Is there a way to get the locals and globals from the decorator application level even though it's an arbitrary depth.
That is, I'm trying to make ...
I'm creating a decorator for Django views that will check permissions in a non-Django-managed database. Here is the decorator:
def check_ownership(failure_redirect_url='/', *args, **kwargs):
def _check_ownership(view):
def _wrapper(request, csi=None):
try:
opb_id=request.user.get_profile().opb_id
...
if We apply any custom decorator on radio button then it will be display on next line so how
we create such custom decorator that will display inline also same for file
...
I'm writing some code that needs to change function signatures. Right now, I'm using Simionato's FunctionMaker class, which uses the (hacky) inspect module, and does a compile. Unfortunately, this still loses the line and file metadata.
Does anyone know:
If it is possible to overwrite these values in some odd way?
If hacking up a c...
I have a decorated function (simplified version):
class Memoize:
def __init__(self, function):
self.function = function
self.memoized = {}
def __call__(self, *args, **kwds):
hash = args
try:
return self.memoized[hash]
except KeyError:
self.memoized[hash] = self.func...
Hi, I am currently using zend_decorators to add styles to my form. I was wondering if there is an alternative way of doing it? It is a bit difficult to write decorators. I would love the casual one using divs and css style :
<input type="submit" class="colorfulButton" >
It is much simpler rather than set a decorator for a certain con...
Goal: Make it possible to decorate class methods. When a class method gets decorated, it gets stored in a dictionary so that other class methods can reference it by a string name.
Motivation: I want to implement the equivalent of ASP.Net's WebMethods. I am building this on top of google app engine, but that does not affect the point...
Goal: Make a decorator which can modify the scope that it is used in.
If it worked:
class Blah(): # or perhaps class Blah(ParentClassWhichMakesThisPossible)
def one(self):
pass
@decorated
def two(self):
pass
>>> Blah.decorated
["two"]
Why? I essentially want to write classes which can maintain specifi...
I have the following Zend_Form_Element:
$imginstructions = "Some description";
$img = $this->createElement('select','img');
$img->setAttrib('class', 'image-select');
$imgdecorator = $img->getDecorator('Description');
$imgdecorator->setOption('escape', false);
$img->setLabel('Image:')
->setRequired(true)
...
I'm writing some unit tests for a Django project, and I was wondering if its possible (or necessary?) to test some of the decorators that I wrote for it.
Here is an example of a decorator that I wrote:
class login_required(object):
def __init__(self, f):
self.f = f
def __call__(self, *args):
request = args[0...
This is a very specific instance where I'm having difficulty getting Zend Form to produce the correct output and supply the correct validation. I may have to go create a composite element but thought I'd ask here first. Here is the HTML I'm trying to get Zend Form to produce. I'd like this to be able to work where if the validation doesn...
I was looking at some lazy loading property decorators in Python and happened across this example (http://code.activestate.com/recipes/363602-lazy-property-evaluation/):
class Lazy(object):
def __init__(self, calculate_function):
self._calculate = calculate_function
def __get__(self, obj, _=None):
if obj is None...
I want to be able to ask a class's __init__ method what it's parameters are. The straightforward approach is the following:
cls.__init__.__func__.__code__.co_varnames[:code.co_argcount]
However, that won't work if the class has any decorators. It will give the parameter list for the function returned by the decorator. I want to get...
Hi, how does one go about accessing a decorator from a base class in a child?
I assumed (wrongly) that the ffg. would work:
class baseclass(object):
def __init__(self):
print 'hey this is the base'
def _deco(func):
def wrapper(*arg):
res = func(*arg)
print 'I\'m a decorator. This is fabu...
[Updated]: Answer inline below question
I have an inspecting program and one objective is for logic in a decorator to know whether the function it is decorating is a class method or regular function. This is failing in a strange way. Below is code run in Python 2.6:
def decorate(f):
print 'decorator thinks function is', f
ret...
Hi, I'm reaching back to my CLOS (Common Lisp Object System) days for this abstract question.
I'm augmenting the question to clarify:
It appears to me that a Python decorator is sort of like an "around" method in CLOS.
From what I remember, an "around" method in CLOS is a method/function that wraps around the primary method/functi...
Is it possible to decorate a function based on a condition?
a'la:
if she.weight() == duck.weight():
@burn
def witch():
pass
I'm just wondering if logic could be used (when witch is called?) to figure out whether or not to decorate witch with @burn?
If not, is it possible to create a condition within the decorator to the sam...
I have this URL in my project:
url(r'^alerts/inbox/$', 'inbox', {'template_name': 'inbox.xhtml' }, name = 'inbox'),
The inbox view is exactly this:
@login_required()
@ownsBid
def inbox(request, template_name):
return render_to_response(template_name, context_instance=RequestContext(request))
My ownsBid decorator is:
def ownsBi...
Compared to decorators applied to a function, it's not easy to understand the decorators applied to a class.
@foo
class Bar(object):
def __init__(self, x):
self.x = x
def spam(self):
statements
What's the use case of decorators to a class? How to use it?
...