decorator

How to implement a Decorator with non-local equality?

Greetings, currently I am refactoring one of my programs, and I found an interesting problem. I have Transitions in an automata. Transitions always have a start-state and an end-state. Some Transitions have a label, which encodes a certain Action that must be performed upon traversal. No label means no action. Some transitions have a co...

What is the difference between @staticmethod and @classmethod in Python?

What is the difference between a function decorated with @staticmethod and one decorated with @classmethod? ...

Preserving signatures of decorated functions

Suppose I have written a decorator that does something very generic. For example, it might convert all arguments to a specific type, perform logging, implement memoization, etc. Here is an example: def args_as_ints(f): def g(*args, **kwargs): args = [int(x) for x in args] kwargs = dict((k, int(v)) for k, v in kwarg...

Python - How do I write a decorator that restores the cwd?

How do I write a decorator that restores the current working directory to what it was before the decorated function was called? In other words, if I use the decorator on a function that does an os.chdir(), the cwd will not be changed after the function is called. ...

Flexible Decorator Pattern?

I was looking for a pattern to model something I'm thinking of doing in a personal project and I was wondering if a modified version of the decorator patter would work. Basicly I'm thinking of creating a game where the characters attributes are modified by what items they have equiped. The way that the decorator stacks it's modification...

improved collection Iterator

Hi, Personally, I find the range of functionality provided by java.util.Iterator to be fairly pathetic. At a minimum, I'd like to have methods such as: peek() returns next element without moving the iterator forward previous() returns the previous element Though there are lots of other possibilities such as first() and last(). Does...

In Django, how could one use Django's update_object generic view to edit forms of inherited models?

In Django, given excerpts from an application animals likeso: A animals/models.py with: from django.db import models from django.contrib.contenttypes.models import ContentType class Animal(models.Model): content_type = models.ForeignKey(ContentType,editable=False,null=True) name = models.CharField() class Dog(Animal): is_lucky...

Why Python decorators rather than closures?

I still haven't got my head around decorators in Python. I've already started using a lot of closures to do things like customize functions and classes in my coding. Eg. class Node : def __init__(self,val,children) : self.val = val self.children = children def makeRunner(f) : def run(node) : f(node) ...

Getting method parameter names in python

Given the python function: def aMethod(arg1, arg2): pass How can I extract the number and names of the arguments. Ie. given that I have a reference to func, I want the func.[something] to return ("arg1", "arg2") The usage scenario for this is that I have a decorator, and I wish to use the method arguments in the same order that t...

What naming convention do you use for the Decorator Pattern?

I just recently really truly groked dependency injection and the wonder of the Decorator design pattern and am using it all over the place. As wonderful as it is however, one thing that I've been having difficulty with is naming my decorator classes so I would just like to know what others do. Do you always append the word Decorator? ...

Question about decorator pattern

I'm a beginner when it comes to design patterns , and I have a question related to the decorator design pattern . Presuming I have a class named A , and I want to use the decorator design pattern . Correct me if I'm wrong , but for that to work , we'll need to create a decorator class ( ADecorator ) , which will hold a reference to an A ...

Python decorating functions before call.

I have a rather complex decorator written by someone else. What I want to do is call a decorated version of the function one time based on a descision or call the original function (not decorated) another time. Is this possible? ...

What does functools.wraps do?

In a comment on the answer to another question, someone said they weren't sure what functools.wraps was doing. So I'm asking this question so that there will be a record of it on StackOverflow for future reference: what does functools.wraps do, exactly? ...

getting redirect loop for admin_only decorator

I've made this decorator, which results in an infinite redirect loop. The problem is this: args[0].redirect(users.create_login_url(args[0].request.path)) It appears to be a perfectly valid URL. So why wouldn't it properly redirect? def admin_only(handler, *args): def redirect_to_login(*args, **kwargs): return args[0].r...

Python Decorators run before function it is decorating is called?

As an example, def get_booking(f=None): print "Calling get_booking Decorator" def wrapper(request, **kwargs): booking = _get_booking_from_session(request) if booking == None: # we don't have a booking in our session. return HttpRedirect('/') else: return f(request=reque...

c# windows-services - How do I handle logging exceptions?

I am creating a Windows service. When an exception occurrs, I handle it appropriately and create a log. I am using the decorator pattern, as there are many different ways people will be looking at these logs. I have an email logger, a file logger, and a windows event logger, all which inherit from LoggingDecorator, which implements ILogg...

What does the []-esque decorator syntax in Python mean?

Here's a snippet of code from within TurboGears 1.0.6: [dispatch.generic(MultiorderGenericFunction)] def run_with_transaction(func, *args, **kw): pass I can't figure out how putting a list before a function definition can possibly affect it. In dispatch.generic's docstring, it mentions: Note that when using older Python versi...

Is @measured a standard decorator? What library is it in?

In this blog article they use the construct: @measured def some_func(): #... # Presumably outputs something like "some_func() is finished in 121.333 s" somewhere This @measured directive doesn't seem to work with raw python. What is it? UPDATE: I see from Triptych that @something is valid, but is where can I find @measured,...

Why the name "Decorator" for the Decorator design pattern?

Can anybody explain why the name "Decorator" was chosen for the functionality conveyed by the Decorator design pattern? I've always found that name fairly misleading, because decorator and marking interface sound very similar to me in their purpose. However, whereas a marker does not really "do anything", decorators certainly do. But t...

Python - is there a list of decorators somewhere?

I know of @staticmethod, @classmethod, and @property, but only through scattered documentation. Is there a complete list of function decorators that are built into Python? ...