decorator

Validating Python Arguments in Subclasses

I'm trying to validate a few python arguments. Until we get the new static typing in Python 3.0, what is the best way of going about this. Here is an example of what I am attempting: class A(object): @accepts(int, int, int) def __init__(a, b, c): pass class B(A): @accepts(int, int, int, int) def __init__(a, b, ...

What are some common uses for Python decorators?

While I like to think of myself as a reasonably competent Python coder, one aspect of the language I've never been able to grok is decorators. I know what they are (superficially), I've read tutorials, examples, questions on Stack Overflow, and I understand the syntax, can write my own, occasionally use @classmethod and @staticmethod, b...

The Decorator Design Pattern

I am just starting to learn design patterns and I have two questions related to the Decorator... I was wondering why the decorator pattern suggests that the decorator implement all public methods of the component of which it decorates? Can't the decorator class just be used to provide the additional behaviors, and then the concrete com...

What is the shortest way to implement a proxy or decorator class in c#?

When you have a class car that implements IVehicle and you want to wrap it in a decorator that forwards all calls to car and counts them, how would you do it? In Ruby I could just build a decorator without any methods and use method_missing to forward all calls to the car object. In Java I could build a Proxy object that runs all code ...

Zend_Form_Decorator - How to add attrib to Zend_Form_Element_Hidden ?

I have 2 requirements: 1) All hidden input elements should be affected without removing standard decorators. 2) This should happen WITHOUT having to specify it on a per-element basis. All I need is for a CSS class to be attached to the DT & DD tags IF the element type is Zend_Form_Element_Hidden. I've tried creating custom HtmlTag, DtD...

Design OOP question on Decorator and Strategy pattern C#

Say for example you have a base abstract class public abstract Foo { IFlyable _fly; ISwimmable _swim; void performSwim() { _swim.swim(); } void performFly() { _fly.fly(); } } And have behaviors/algorithm that you will have in your system interface IFlyable { void fly(); } interface ISwimmable { void swim(); } in...

Using DynamicProxy as a decorator pattern in windsor container

I am looking for some info on using and configuring windsor to provide a dynamic proxy to intercept calls to an instance of another class. My class represents a resource that should be retained as a long lived instance by the container for performance reasons. However, sometimes this resource can transition into ununusable state, and...

Writing a TTL decorator in Python

Hi, I'm trying to write a TTL decorator in python. Basically I give it raise an exception if the function doesn't answer in the selected time. You can find the thead2 snippets on http://sebulba.wikispaces.com/recipe+thread2 from thread2 import Thread """ A TTL decorator. """ class Worker(Thread): def __init__(self, q, f, args, k...

Can I use a decorator to mutate the local scope of a function in Python?

Is there any way of writing a decorator such that the following would work? assert 'z' not in globals() @my_decorator def func(x, y): print z EDIT: moved from anwser In answer to hop's "why?": syntax sugar / DRY. It's not about caching, it's about calculating z (and z1, z2, z3, ...) based upon the values of x & y. I have lots...

Why does @foo.setter in Python not work for me?

So, I'm playing with decorators in python 2.6, and I'm having some trouble getting them to work. Here's by class file: class testDec: @property def x(self): print 'called getter' return self._x @x.setter def x(self, value): print 'called setter' self._x = value What I thought this me...

How can I pass all the parameters to a decorator?

I tried to trace the execution of some methods using a decorator. Here is the decorator code: def trace(func): def ofunc(*args): func_name = func.__name__ xargs = args print "entering %s with args %s" % (func_name,xargs) ret_val = func(args) print "return value %s" % ret_val print "exiting %s" % (...

How can I use named arguments in a decorator?

If I have the following function: def intercept(func): # do something here @intercept(arg1=20) def whatever(arg1,arg2): # do something here I would like for intercept to fire up only when arg1 is 20. I would like to be able to pass named parameters to the function. How could I accomplish this? Here's a little code sample : d...

Is it possible to replace a function/method decorator at runtime? [ python ]

If I have a function : @aDecorator def myfunc1(): # do something here if __name__ = "__main__": # this will call the function and will use the decorator @aDecorator myfunc1() # now I want the @aDecorator to be replaced with the decorator @otherDecorator # so that when this code executes, the function no longer goes through ...

In Python, is it possible to access the class which contains a method, given only a method object?

I'm pretty new to Python, and haven't been able to find an answer to this question from searching online. Here is an example decorator that does nothing (yet) def my_decorator(text): def wrap(f): # grab magic f.parent_class_object.my_var and append text def wrap_f(*args, **kwargs): f(*args, **kwargs) return wrap_f ...

How to create a Python decorator that can be used either with or without parameters?

I'd like to create a Python decorator that can be used either with parameters: @redirect_output("somewhere.log") def foo(): .... or without them (for instance to redirect the output to stderr by default): @redirect_output def foo(): .... Is that at all possible? Note that I'm not looking for a different solution to the pro...

A simple freeze behavior decorator

Hi, I'm trying to write a freeze decorator for Python. The idea is as follows : (In response to the two comments) I might be wrong but I think there is two main use of test case. One is the test-driven development : Ideally , developers are writing case before writing the code. It usually helps defining the architecture because t...

Python: decorator specific argument (unrelated to wrapped function)?

I'm looking to build a caching decorator that given a function caches the result of the function to a location specified in the decoration. Something like this: @cacheable('/path/to/cache/file') def my_function(a, b, c): return 'something' The argument to the decorator is completely separate from the argument to the function it's ...

Decorator classes in Python

I want to construct classes for use as decorators with the following principles intact: It should be possible to stack multiple such class decorators on top off 1 function. The resulting function name pointer should be indistinguishable from the same function without a decorator, save maybe for just which type/class it is. Ordering off...

Decorating A Method In ASP.NET?

Me again.. I hear the phrase 'decorating / decorate' a method being thrown about a lot in tutorials I have read / watched. But I just don't understand what it means AND what it actually does?? Can anyone point me in the direction of some information on beginning to use them (Very novice tutorial would be good) ...

Python Class Decorator

In Python 2.5, is there a way to create a decorator that decorates a class? Specifically, I want to use a decorator to add a member to a class and change the constructor to take a value for that member. Looking for something like the following (which has a syntax error on 'class Foo:': def getId(self): return self.__id class addID(or...