decorator

Entity Framework Decorator Pattern

In my line of business we have Products. These products can be modified by a user by adding Modifications to them. Modifications can do things such as alter the price and alter properties of the Product. This, to me, seems to fit the Decorator pattern perfectly. Now, envision a database in which Products exist in one table and Modificat...

is a decorator in python exactly the same as calling a function on a function?

I thought that doing @f def g(): print 'hello' is exactly the same as def g(): print 'hello' g=f(g) But, I had this code, that uses contextlib.contextmanager: @contextlib.contextmanager def f(): print 1 yield print 2 with f: print 3 which works and yields 1 3 2 And when I tried to change it into def f()...

Using class/static methods as default parameter values within methods of the same class

I'd like to do something like this: class SillyWalk(object): @staticmethod def is_silly_enough(walk): return (False, "It's never silly enough") def walk(self, appraisal_method=is_silly_enough): self.do_stuff() (was_good_enough, reason) = appraisal_method(self) if not was_good_enough: ...

Collections as decorators in C#

Hey there! We've been hit with a pretty "obvious" problem while designing a collections infrastructure: suppose you need to implement many (sub)types of collections. One of the aspects is storage-related: list, array etc, while the other is behavior-related: ordered, remove only, observable (the one that fires an event upon every change...

How to pass a specific argument to a decorator in python

I want to write a python function decorator that tests that certain arguments to a function pass some criterion. For eg, Suppose I want to test that some arguments are always even, then I want to be able to do something like this (not valid python code) def ensure_even( n ) : def decorator( function ) : @functools.wraps( functio...

Java Decorating The Easy Way

Say you have an API that is not accessible to change: List<LegacyObject> getImportantThingFromDatabase(Criteria c); Imaging Legacy Object has a ton of fields and you want to extend it to make getting at certain information easier: class ImprovedLegacyObject extends LegacyObject { String getSomeFieldThatUsuallyRequiresIteratorsAndA...

Decorate a whole library in Python

I'm new to the ideas of decorators (and still trying to wrap my head around them), but I think I've come across a problem that would be well suited for them. I'd like to have class that is decorated across all of the functions in the math library. More specifically my class has two members, x and flag. When flag is true, I'd like the ori...

Assign std::string from an object using operator=

I'd really like to be able to assign a std::string object from a DecoratedString object that I'm writing. class DecoratedString { private: std::string m_String; public: DecoratedString(const std::string& initvalue) : m_String(initvalue) { } const std::string& ToString() const { return m_String...

Introspection to get decorator names on a method?

I am trying to figure out how to get the names of all decorators on a method. I can already get the method name and docstring, but cannot figure out how to get a list of decorators. ...

How to use decorator?

In SharpDevelop, I want to create a dll which contains a static methon, void Main(string[] args). Some one said I should use decorator to restrict the function in IronPython. I found "@staticmethod", but others, "void", "string[] args", how to restrict them? class MyClass: def __init__(self): pass @staticmethod ...

How to create decorator for lazy initialization of a property

I want to create a decorator that works like a property, only it calls the decorated function only once, and on subsequent calls always return the result of the first call. An example: def SomeClass(object): @LazilyInitializedProperty def foo(self): print "Now initializing" return 5 >>> x = SomeClass() >>> x.foo...

How to use palette window decorators in Java/Swing?

Hi, is there an open solution to use palette window decorators in Java/Swing like in the following screenshot? JNIWrapper can do this but it is neither open nor free. ...

GWT serialization and decorator pattern

I use the decorator pattern to describe Actions, and I would like to use those Actions in RPC calls public abstract class Action implement Serializable { boolean isDecorated = false; public Action() {} // default constructor for Serialization } public abstract class ActionDecorator extends Action { private Action _decora...

Python decorator class to transform methods to dictionary items

I wonder if there is a reasonable easy way to allow for this code (with minor modifications) to work. class Info(object): @attr("Version") def version(self): return 3 info = Info() assert info.version == 3 assert info["Version"] == 3 Ideally, the code would do some caching/memoising as well, e.g. employ lazy attribute...

@StaticMethod or @ClassMethod decoration on magic methods

I am trying to decorate the magic method __getitem__ to be a classmethod on the class. Here is a sample of what I tried. I don't mind using either classmethod or staticmethod decoration, but I am not too sure how to do it. Here is what I tried: import ConfigParser class Settings(object): _env = None _config = None def __init_...

Using Python property() inside a method.

Assuming you know about Python builtin property: http://docs.python.org/library/functions.html#property I want to re-set a object property in this way but, I need to do it inside a method to be able to pass to it some arguments, currently all the web examples of property() are defining the property outside the methods, and trying the ob...

Writing a CherryPy Decorator for Authorization

I have a cherrypy application and on some of the views I want to start only allowing certain users to view them, and sending anyone else to an authorization required page. Is there a way I can do this with a custom decorator? I think that would be the most elegant option. Here's a basic example of what I want to do: class MyApp: ...

Change TextDecoration programmatically

Hi, I want to underline a text element programmatically in C# (I'm using WPF). I have a DependencyProperty: public TextDecorationCollection TextDecoration { get { return (TextDecorationCollection)GetValue(TextDecorationProperty); } set { SetValue(TextDecorationProperty, value); } } public stat...

How can I use decorators in Python to specify and document repeated used arguments of methods?

One of my classes has a logical numpy array as parameter in many methods repeated (idx_vector=None). How can I use a decorator to: automatically specify idx_vector automatically insert the description into the docstring Example without decorator: import numpy as np class myarray(object): def __init__(self, data): self.data = ...

apply decorator to all django error views

Hi I've got a Http BasicAuth decorator wrapped around most of my views in django on my testing site and no BasicAuth on my production site. If debug = false it simply returns the view, otherwise it does a BasicAuth password check. I just discovered that the nice debug error pages are still visible to any little hacker, though. (Obvious...