decorator

Can a django template know whether the view it is invoked from has the @login_required decorator?

Let's say that I have a system that has some pages that are public (both non-authenticated users and logged-in users can view) and others which only logged-in users can view. I want the template to show slightly different content for each of these two classes of pages. The @login_required view decorator is always used on views which on...

python decorators and methods

Hi. New here. Also I'm (very) new to python and trying to understand the following behavior. Can someone explain to me why the two methods in this example have different output? def map_children(method): def wrapper(self,*args,**kwargs): res = method(self,*args,**kwargs) for child in self._children: m...

How to put 2 buttons in a row in one Zend_Form

I assume it's a common requirement to have forms in your web apps that have Edit Delete buttons under them. But ZF puts one button under another, which is counter-intuitive. I guess ViewScript decorator could help me completely override button html. But how to do it across other forms, to avoid duplicating? May be I am overcomplicating ...

Decorators and in class

Is there any way to write decorators within a class structure that nest well? For example, this works fine without classes: def wrap1(func): def loc(*args,**kwargs): print 1 return func(*args,**kwargs) return loc def wrap2(func): def loc(*args,**kwargs): print 2 return func(*args,**kwargs) return lo...

What is your favorite python decorator?

Have you got a python decorator that you really love? Post it here! Here is my favorite, although since I have not known about the feature very long, it may change soon... def debug(f): """ Decorator. Function will enable debugging just for itself. Won't change anything if debugging is already enabled. """ def d...

Best External REST API Access Pattern?

I work on a couple of projects that connect with external services like Facebook and Netflix. At this moment most libraries I'm using to access these APIs ( including the ones I've written myself ) have single methods so call specific API functions yet always seem to call some sort of base method to make the request. Something like thi...

Understanding Python decorators

How can I make a decorator in Python that would do the following. @makebold @makeitalic def say(): return "Hello" which should return <b><i>Hello<i></b> I'm not trying to make HTML this way in a real application, just trying to understand how decorators and decorator chaining works. ...

Accessing the class that owns a decorated method from the decorator

Hi, I'm writing a decorator for methods that must inspect the parent methods (the methods of the same name in the parents of the class in which I'm decorating). Example (from the fourth example of PEP 318): def returns(rtype): def check_returns(f): def new_f(*args, **kwds): result = f(*args, **kwds) ...

Is there a decorator to simply cache function return values?

Consider the following: @property def name(self): if not hasattr(self, '_name'): # expensive calculation self._name = 1 + 1 return self._name I'm new, but I think the caching could be factored out into a decorator. Only I didn't find one like it ;) PS the real calculation doesn't depend on mutable values ...

Decorator Pattern Using Composition Instead of Inheritance

My previous understanding of the decorator pattern was that you inherit Window with WindowDecorator, then in the overridden methods, do some additional work before calling the Window's implementation of said methods. Similar to the following: public class Window { public virtual void Open() { // Open the window } } pu...

How would you write a @debuggable decorator in python?

When debugging, I like to print out all the inputs and outputs of a function (I know I need a better IDE, but humour me, this could be used for error reporting). So, I'd ideally like to have: @debuggable def myfunc(argA,argB,argC): return argB+1 and use a global variable to switch on or off debugging. No, you don't like globals ei...

Can't call base class method even though I have a pointer to it (Decorator)?

I have a template class that I've subclassed with a pointer to it (Decorator pattern). I added a getBase() call to return the pointer to the base class for any further subclasses. However, when I use that getBase() and call the base classes only method, I get a linker error that it can't find the symbol for that method in the interveni...

How to return a function value with decorator and thread

have this code import threading def Thread(f): def decorator(*args,**kargs): print(args) thread = threading.Thread(target=f, args=args) thread.start() thread.join() decorator.__name__ = f.__name__ return decorator @Thread def add_item(a, b): return a+b print(add_item(2,2)) but ...

Function Decorators

I like being able to measure performance of the python functions I code, so very often I do something similar to this... import time def some_function(arg1, arg2, ..., argN, verbose = True) : t = time.clock() # works best in Windows # t = time.time() # apparently works better in Linux # Function code goes here t = tim...

ASP.NET MVC Routing Via Method Attributes

In the StackOverflow Podcast #54, Jeff mentions they register their URL routes in the StackOverflow codebase via an attribute above the method that handles the route. Sounds like a good concept (with the caveat that Phil Haack brought up regarding route priorities). Could someone provide some sample to to make this happen? Also, any "b...

Design: Emailing System in Java

Hi everyone, I'm building an emailing system for a framework I'm developing. Thus far, I've got a generic MailMessage interface that every different type of email message will implement, something along the lines of public interface MailMessage { public String[] getTo(); public void setTo(String[] to); public String getFr...

Decorator Design Pattern Use With Service Objects (wSingleton)

I'm working on a project where I need to add some functionality to a service object and using a decorator to add it in seems like a good fit. However, I've only ever used decorators with simple beans, never on a singleton like a service object. Has anyone ever done this before and what are the pros and cons? In this case I don't think cr...

Django Contenttypes and decorator

Hi. The site makes use of 2 objects - articles and blogs. Every time an article or blog is viewed, a related counter should increase by one. The idea is to have a "top ten" application that measures the "popularity" of the articles and entries. Because I'm using more than one object, I would like the Tracker model to use a genericFo...

How to mark a global as deprecated in Python?

I've seen decorators that let you mark a function a deprecated so that a warning is given whenever that function is used. I'd like to do the same thing but for a global variable, but I can't think of a way to detect global variable accesses. I know about the globals() function, and I could check its contents, but that would just tell me ...

Cost decorators

Hello, For each product there are associated cost calculators like: discount, discount by merchant, bonus by merchant, monthly discount etc. In future, more cost calculators would be added. We have a concrete product class and many decorators for each cost calculation. All products should use all of the calculators, because the calcula...