decorator

How to use decorator in observer pattern for Python 2.7

Deadly 提交于 2019-12-10 17:33:21
问题 The observer pattern in the very simplified code below works well. I would like to have have a decorator @on_event that does the registration in the Observable singleton. In class O2 below this does not work. The problem is of course that the decorator on_event get's called prior to the instance is created, and the registration will be to the unbound method event . In some way I have to delay the registration until the O2 object is initialized. Maybe needless to say but all I want to add in

Decorating all django admin views (1.4+)

北慕城南 提交于 2019-12-10 17:28:31
问题 There used to be a neat trick for Django versions prior to 1.4 to decorate all views within the admin: urlpatterns = patterns('', (r'^admin/(.*)', my_decorator(lambda *args: admin.site.root(*args))), ) This no longer works as root is deprecated. I have found some alternatives, but they seem pretty verbose compared to what I had. Is there still a hook to do this? 回答1: Decorate every view in a url tree http://djangosnippets.org/snippets/2607/ 来源: https://stackoverflow.com/questions/14537829

What design pattern would this be, and is it a good idea? (C#)

六月ゝ 毕业季﹏ 提交于 2019-12-10 17:02:22
问题 I've got a class that I want to extend functionality to in a decorator/adapter kind of a way, only I don't want my extending class to have to know anything about the kinds of classes it is extending, and I don't want to have to write a new class for each type of object I want to extend. All objects I would want to extend off of, though, share a common base class, Team . This sounds ripe for a use of generics, so here was my initial idea: public class TournamentTeam<T> : T where T : Team {

Is it possible to apply my own decorators to builtin methods in Python?

谁都会走 提交于 2019-12-10 16:35:23
问题 I've just come across Python decorators. Just out of interest, can you apply your own decorator to a built-in object method somehow? Say I wanted to apply this: def remove_empty(fn): def filtered(): return filter(lambda x: x != '', fn()) return filtered To this: some_string.split('\n') in order to remove empty strings. Is it possible? Or even a good idea? 回答1: It's possible in a sense; it depends on what exactly you mean. Decorator syntax like this... @dec def foo(): pass is really just sugar

Can I tell .net's Intellisense how to sort fields?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-10 16:13:36
问题 In Visual Studio, Intellisense sorts the fields of an object by alphabetical order. I am used to Delphi, where Intellisense sorts by (method, property, event) and then alphabetically. Is there a decoration once can add to a class/struct definition to tell Intellisense how to sort fields? 回答1: Unfortunately you cannot. It's a feature that's been requested but just hasn't made it in yet. 来源: https://stackoverflow.com/questions/4116688/can-i-tell-nets-intellisense-how-to-sort-fields

Using a Decorator, (rails) Could not infer a decorator for ActiveRecord::Base

牧云@^-^@ 提交于 2019-12-10 15:55:14
问题 I'm having trouble using a decorator. I've never used one before and I've been trying to use one with regards to something that I've been doing for breaking up some emails. However because I've never used one before, I've been having trouble even doing very simple things with my decorator and I'm thinking there is some form of setup issue with it. I do know that everything outside of my little feature (aka the gemfile and such) are all up to date and proper. The error I am getting is simply,

Is my in-class decorator not Pythonic enough or PyCharm not smart enough in lint warning?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-10 14:29:53
问题 I want to define a decorator within a class. I don't want to define it as a separated, independent function, for this decorator is specifically for this class and I want to keep the correlated methods together. The purpose of this decorator is to check some prerequisites, especially the database connection, SSH connection, etc., which are held by member variables, are still available. If not, the decorated function won't be called and some error reporting and clean-up works will be done. I

Python decorator function execution

自作多情 提交于 2019-12-10 14:28:00
问题 I have below decorator demonstration code. If I execute it without explicitly calling greet function, it is executing print statement inside decorator function and outputs Inside decorator . I am unable to understand this behavior of decorator. How the time_decorator is called even if I didn't call greet function? I am using Python 3. def time_decorator(original_func): print('Inside decorator') def wrapper(*args, **kwargs): start = time.clock() result = original_func(*args, **kwargs) end =

Checking in a unit test whether all methods are delegated

非 Y 不嫁゛ 提交于 2019-12-10 14:10:49
问题 Suppose I have the following class public abstract class Foo{ public int bar(){ //implementation } public abstract int bar2(); } and a base class to make it easier to write decorators for this class public class FooWrapper{ private final Foo delegate; protected FooWrapper( Foo delegate ){ this.delegate = delegate; } @Override public int bar(){ return delegate.bar() } @Override public int bar2(){ return delegate.bar2(); } } The class FooWrapper allows you to write a decorator for Foo where you

Java Decorator Pattern: Can I decorate a protected method?

假装没事ソ 提交于 2019-12-10 13:55:18
问题 I want to Decorate (Decorator design pattern) a common base class, but the method I need to Decorate is protected. See example: public class AbstractActor { public void act(){...} //Delegates its actions to doAct() based on some internal logic protected void doAct(){...} } Subclasses are meant to override doAct(), I need to inject some functionality there. I can override doAct, but my decorator class can't access the protected method doAct() on the instance being decorated. Example: public