decorator

TypeError: 'NoneType' object is not iterable when applying decorator to generator

随声附和 提交于 2019-12-11 01:27:58
问题 I have a decorator function which I want to apply to both normal function and a generator. When applied to the normal function, it works properly. However, when applied to the generator, the iteration loop inside the decorator is executed till the end, but after that the script throws an error: TypeError: 'NoneType' object is not iterable and exits the script. def decor(func): def wrapper(*args, **kwargs): func_name = func.__name__ is_generator = "_generator" in func_name if is_generator: for

Accessing function attribute created in a decorator outside that decorator

廉价感情. 提交于 2019-12-11 01:19:57
问题 I want to count the number of times a given function has been called. So, I made a countcalls decorator to give my functions a __callcount attribute which gets incremented on each call. Simple enough. My issue is getting the __callcount value back out later. Here's my code: import functools def countcalls(f): f.__callcount = 0 @functools.wraps(f) def _countcalls(*args, **kwds): f.__callcount += 1 print(' Called {0} time(s).'.format(f.__callcount)) return f(*args, **kwds) return _countcalls

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

点点圈 提交于 2019-12-11 00:27:13
问题 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 ILogger. So, no logger knows about any other logger. My question is: How should I handle logging exceptions? If writing to a file fails, or sending an email fails,

C function decorators (wrappers) at compile time

∥☆過路亽.° 提交于 2019-12-10 23:36:37
问题 I'm trying to change the behaviour of some functions in C with help of the preprocessor; and also add optional parameters that can be set on or off... The basic pattern for the optional parameters is easy: #ifdef OPT_PARAM #define my_func(a, b, opt) _my_func(a, b, opt) #else #define my_func(a, b, opt) _my_func(a, b) #endif /*the rest of the code always calls "my_func" with all the params and not the underscored version...*/ #ifdef OPT_PARAM void _my_func(int a, int b, int opt) #else void _my

decorator python library hide the kwargs inside args

こ雲淡風輕ζ 提交于 2019-12-10 22:50:49
问题 I got a pretty weird behaviour with the decorator library which is explained in the next code: from decorator import decorator @decorator def wrap(f, a, *args, **kwargs): print 'Decorator:', a, args, kwargs return f(a, *args, **kwargs) def mywrap(f): def new_f(a, *args, **kwargs): print 'Home made decorator:', a, args, kwargs return f(a, *args, **kwargs) return new_f @wrap def funcion(a, b, *args, **kwargs): pass @mywrap def myfuncion(a, b, *args, **kwargs): pass funcion(1, b=2) myfuncion(1,

What is the difference between using decorators and extending a sub class by inheritance?

▼魔方 西西 提交于 2019-12-10 21:18:10
问题 I was trying to wrap my brain around Decorators in python but can't understand why we cannot achieve the same thing by using sub classes? 回答1: You can achieve the same thing using subclasses, and in fact you don't even need subclasses - you can also achieve the same thing simply by wrapping a method in another method and reassigning it. There was a lot of discussion about whether or not the decorator syntax should be added to the language as it doesn't allow you to do anything new and

django class view with decorator and sessions

删除回忆录丶 提交于 2019-12-10 20:02:32
问题 I'm trying to convert some of my django views over from function based views to class based views and I've run into a small problem. My OO is kind of weak and I think the problem is that I've lost track of where things are going. I have a custom login decorator that I need on the views so I have... First I have the View class from this example http://www.djangosnippets.org/snippets/760/ Then my view class looks like this... class TopSecretPage(View): @custom_login def __call__(self, request,

What's the difference between f(arguments) to f.apply(this,arguments)?

六眼飞鱼酱① 提交于 2019-12-10 19:03:30
问题 I have not been studying JavaScript for a long time and now I'm trying to implement the Decorator pattern: function wrap(f, before, after){ return function(){ return before + f(arguments) + after; } } What I'm wondering about is that if we replace f(arguments) to f.apply(this,arguments) there is no obvious difference in output. Could you please clarify which case is preferable and why? UPD: I suppose I have understood what is the bottleneck :) If we decorate function with aforementioned code

Apply a single decorator to multiple functions

左心房为你撑大大i 提交于 2019-12-10 18:47:41
问题 I've searched for this, but the results I've seen involve the opposite: applying multiple decorators to a single function. I'd like to simplify this pattern. Is there a way to apply this single decorator to multiple functions? If not, how can I rewrite the above to be less repetitious? from mock import patch @patch('somelongmodulename.somelongmodulefunction') def test_a(patched): pass # test one behavior using the above function @patch('somelongmodulename.somelongmodulefunction') def test_b

Define filter for DecorateAllWith() method in structure-map 3

纵然是瞬间 提交于 2019-12-10 17:36:12
问题 I used following statement to decorate all my ICommandHandlers<> with Decorator1<> : ObjectFactory.Configure(x => { x.For(typeof(ICommandHandler<>)).DecorateAllWith(typeof(Decorator1<>)); }); But because Decorator1<> implements ICommandHandlers<> , the Decorator1<> class decorates itself too. So, the problem is that the Decorator1 registers inadvertently, when I register all the ICommandHandler<> 's. How can I filter DecorateWithAll() to decorate all ICommandHandler<> , except Decorator1<> ?