decorator

Decorator for determining HTTP response from a view

I want to create a decorator that will allow me to return a raw or "string" representation of a view if a GET parameter "raw" equals "1". The concept works, but I'm stuck on how to pass context to my renderer. Here's what I have so far: from django.shortcuts import render_to_response from django.http import HttpResponse from django.te...

What is the difference between a mixin and the decorator pattern?

The Decorator Pattern is dynamic extension-at-runtime of classes. It dynamically forms a is-a relationship. I started to wonder if I was over-complicating my API by using the Decorator Pattern after I got this answer about the difference between a mixin and an abstract class. ...

Is it an example of decorator pattern?

Hi, I've an example please tell me whether it is Decorator pattern or not? public abstract class ComputerComponent { String description ="Unknown Type"; public String getDescription() { return description; } public abstract double getCost(); } public abstract class AccessoryDecorator { Computer...

Can I call sitemesh struts tags from a decorator in freemarker?

I know if you are using JSPs, you can call struts tags defined the normal way from a decorator, like this: <html> <body> <decorator:body /> </body> </html> but in the examples I always see using freemarker decorators, they will instead say: <html> <body> ${body} </body> </html> Is this the only way to include the pieces of ...

Enum "copy" problem

Hi all! I have a class, let's call it A. It has a enum (E) and a method Foo(E e), with gets E in the arguments. I want to write a wrapper (decorator) W for A. So it'll have its own method Foo(A::E). But I want to have some kind of encapsulation, so this method should defined as Foo(F f), where F is another enum defined in W, that can be ...

A better python property decorator

I've inherited some python code that contains a rather cryptic decorator. This decorator sets properties in classes all over the project. The problem is that this I have traced my debugging problems to this decorator. Seems it "fubars" all debuggers I've tried and trying to speed up the code with psyco breaks everthing. (Seems psyco and ...

access django session from a decorator

I have a decorator that I use for my views @valid_session from django.http import Http404 def valid_session(the_func): """ function to check if the user has a valid session """ def _decorated(*args, **kwargs): if ## check if username is in the request.session: raise Http404('not logged in.') else: return...

Error with python decorator

I get this error object has no attribute 'im_func' with this class Test(object): def __init__(self, f): self.func = f def __call__( self, *args ): return self.func(*args) pylons code: class TestController(BaseController): @Test def index(self): return 'hello world' full error: File '/en...

Adding a method to a function object at runtime

I read a question earlier asking if there was a times method in Python, that would allow a function to be called n times in a row. Everyone suggested for _ in range(n): foo() but I wanted to try and code a different solution using a function decorator. Here's what I have: def times(self, n, *args, **kwargs): for _ in range(n): ...

sfWidgetFormSchemaFormatter: Format for name of embedded form

Hi, Im trying to create a custom formatter in Symfony 1.4. I have embedded form via $this->embedRelation('User','BasesfGuardUserAdminForm'); Is there a way to format the name of the embedded form 'User'? ...

Django: Staff Decorator

I'm trying to write a "staff only" decorator for Django, but I can't seem to get it to work: def staff_only(error='Only staff may view this page.'): def _dec(view_func): def _view(request, *args, **kwargs): u = request.user if u.is_authenticated() and u.is_staff: return view_func(reque...

Decorator Module Standard

I was wondering if it's frowned upon to use the decorator module that comes with python. Should I be creating decorators using the original means or is it considered okay practice to use the module? ...

How do I add a method with a decorator to a class in python?

How do I add a method with a decorator to a class? I tried def add_decorator( cls ): @dec def update(self): pass cls.update = update usage add_decorator( MyClass ) MyClass.update() but MyClass.update does not have the decorator @dec did not apply to update I'm trying to use this with orm.reconstructor in ...

Please help me understand the "Decorator Pattern" with real world example.

I was studying Decorator pattern of GOF. It seems to me somehow complecated. So, please help me understand "Decorator Pattern"? Could you give me an example of this is real world? ...

Simple check authentication decorator in Python + Pylons

I'd like to write a simple decorator that I can put above functions in my controller to check authentication and re-direct to the login page if the current user is not authenticated. What is the best way to do this? Where should the decorator go? How should I pass cookie info to the decorator? Sample code is greatly appreciated. Tha...

Help with authorization and redirection decorator in python (pylons)

I'm trying to write a simple decorator to check the authentication of a user, and to redirect to the login page if s/he is not authenticated: def authenticate(f): try: if user['authenticated'] is True: return f except: redirect_to(controller='login', action='index') class IndexController(BaseControll...

Sitemesh CookieDecoratorMapper xml markup example?

If anyone could show me how to mark up sitemesh.xml for the CookieDecoratorMapper I would appreciate it. Here's what I have now: 1) A cookie named "sitemesh" with a value of "green". 2) A decorator named "decorator-green.jsp". 3) A default decorator named "decorator.jsp". 4) All URLs mapped to decorator.jsp in decorators.xml. 5) The fol...

dynamically decorating objects in c#

is it possible to easily and dynamically decorate an object? for example, lets say I have a List<PointF>. this list is actually a plot of the sine function. I'd like to go through these points and add a flag to each PointF of whether it's a peak or not. but I don't want to create a new extended SpecialPointF or whatever, that has a boo...

Merge decorator function as class

Need to create a class that will do all things as the "merge" function. In class i will change, process and add new arguments. def merge(*arg, **kwarg): # get decorator args & kwargs def func(f): def tmp(*args, **kwargs): # get function args & kwargs kwargs.update(kwarg) # merge two dictionaries r...

Implementing and resolving many service decorators in Castle Windsor

Hi, I'm new to Castle Windsor, so I'm not sure what the best practise is in cases like the following: I have a service interface (ILoader), that is implemented by multiple Loader classes. I also have an implementation of ILoader that's a decorator: public interface ILoader { } public class LoaderImpl0 : ILoader { } public class LoaderI...