python-decorators

Using decorators to implement Observer Pattern in Python3

允我心安 提交于 2019-12-24 19:52:56
问题 This question is not in general about the observer pattern. It is focused on the use of decorators in that pattern. The question is based on the answer of a similar question. #!/usr/bin/env python3 class Observable: """ The object that need to be observed. Alternative names are 'Subject'. In the most cases it is a data object. """ def __init__(self): self._observers = [] def register_observer(self, callback): self._observers.append(callback) return callback def _broadcast_observers(self,

Decorator with parameters

南笙酒味 提交于 2019-12-24 02:40:50
问题 Can you explain me how the following decorator works: def set_ev_cls(ev_cls, dispatchers=None): def _set_ev_cls_dec(handler): if 'callers' not in dir(handler): handler.callers = {} for e in _listify(ev_cls): handler.callers[e] = _Caller(_listify(dispatchers), e.__module__) return handler return _set_ev_cls_dec @set_ev_cls(ofp_event.EventOFPSwitchFeatures, CONFIG_DISPATCHER) def _switch_features_handler(self, ev): datapath = ev.msg.datapath .... Please, don't go into details on what's going on

Is there a way to run a method automatically on the initialization of an instance without using __init__?

ぐ巨炮叔叔 提交于 2019-12-24 02:33:37
问题 I am writing some unit tests with Pytest. If I want them to be collected automatically, I have to avoid the __init__ constructor. (If there's a way to make Pytest collect tests with the __init__ constructor I'd take that as an alternate useful answer.) My unit tests have some variables and methods in common. Right now I have base test class TestFoo, and child test class TestBar(TestFoo), and grandchild test class TestBaz(TestBar). Since I can't have an init method, right now I'm calling a

How to configure a decorator in Python

对着背影说爱祢 提交于 2019-12-23 22:15:46
问题 I'm trying to use Thespian (https://thespianpy.com/doc/), a Python library for the actor model, and in particular I'm trying to use the "troupe" functionality. As I understand it, the troupe decorator acts as a scheduler to run multiple actors up to the max_count specified, with each actor running in parallel. The troupe functionality is applied as a decorator on my actor class: @troupe(max_count = 4, idle_count = 2) class Calculation(ActorTypeDispatcher): def receiveMsg_CalcMsg(self, msg,

Signature-changing decorator: properly documenting additional argument

我的未来我决定 提交于 2019-12-23 17:31:19
问题 Let's say I have a custom decorator, and I want it to properly handle docstring of decorated function. Problem is: my decorator adds an argument. from functools import wraps def custom_decorator(f): @wraps(f) def wrapper(arg, need_to_do_more): ''' :param need_to_do_more: if True: do more ''' args = do_something(arg) if need_to_do_more: args = do_more(args) return f(args) return wrapper You can see the argument is not actually passed to decorated function, but used by the wrapper - which may

Which way is ideal for Python factory registration?

给你一囗甜甜゛ 提交于 2019-12-23 17:25:27
问题 This is a question about which of these methods would be considered as the most Pythonic . I'm not looking for personal opinions, but, instead, what is idiomatic. My background is not in Python, so this will help me. I'm working on a Python 3 project which is extensible. The idea is similar to the factory pattern, except it is based on functions. Essentially, users will be able to create a custom function (across packages and projects) which my tool can locate and dynamically invoke. It will

Composable C++ Function Decorators

南笙酒味 提交于 2019-12-23 14:56:56
问题 Python has a very useful feature of function decorators, which, moreover, allows composition. For example, if write a function foo , then you can state that you would like foo to be memoized, but also retried more than a single time in case of a cache miss in which foo also raises an exception, by: @lru_cache @retry def foo(...): Decorator composability allows developing functions like foo and individual function decorators independently, and then mixing them as needed. It would be nice if we

Composable C++ Function Decorators

自作多情 提交于 2019-12-23 14:56:46
问题 Python has a very useful feature of function decorators, which, moreover, allows composition. For example, if write a function foo , then you can state that you would like foo to be memoized, but also retried more than a single time in case of a cache miss in which foo also raises an exception, by: @lru_cache @retry def foo(...): Decorator composability allows developing functions like foo and individual function decorators independently, and then mixing them as needed. It would be nice if we

Dynamically add a decorator to class

三世轮回 提交于 2019-12-23 08:53:20
问题 I have a rather large and involved decorator to debug PyQt signals that I want to dynamically add to a class. Is there a way to add a decorator to a class dynamically? I might be approaching this problem from the wrong angle, so here is what I want to accomplish. Goal I have a decorator that will discover/attach to all pyqt signals in a class and print debug when those signals are emitted. This decorator is great for debugging a single class' signals. However, there might be a time when I

Jython @property SyntaxError: mismatched input '' expecting CLASS

China☆狼群 提交于 2019-12-22 22:57:42
问题 I tried to run this example from the docs in the Jython interpreter: http://www.jython.org/docs/library/functions.html class C(object): def __init__(self): self._x = None @property def x(self): """I'm the 'x' property.""" return self._x @x.setter def x(self, value): self._x = value @x.deleter def x(self): del self._x Just entering the first 4 lines (up to and including @property ) yields a SyntaxError: >>> class C(object): ... def __init__(self): ... self._x = None ... @property File "<stdin>