Decorators on abstract methods

I would code it as two different methods just like in standard method factory pattern description. https://www.oodesign.com/factory-method-pattern.html class Foo(object): __metaclass__ = abc.ABCMeta @abc.abstractmethod @some_decorator def my_method(self, x): self.child_method() class SubFoo(Foo): def child_method(self, x): print x

Decorator classes in Python

A do-nothing decorator class would look like this: class NullDecl (object): def __init__ (self, func): self.func = func for name in set(dir(func)) – set(dir(self)): setattr(self, name, getattr(func, name)) def __call__ (self, *args): return self.func (*args) And then you can apply it normally: @NullDecl def myFunc (x,y,z): return (x+y)/z

How do I use the Decorator Pattern with Unity without explicitly specifying every parameter in the InjectionConstructor

Another approach, thanks to a suggestion from @DarkSquirrel42, is to use an InjectionFactory. The downside is that the code still needs updating every time a new constructor parameter is added to something in the chain. The advantages are much easier to understand code, and only a single registration into the container. Func<IUnityContainer,object> createChain = container … Read more

How to retry urllib2.request when fails?

I would use a retry decorator. There are other ones out there, but this one works pretty well. Here’s how you can use it: @retry(urllib2.URLError, tries=4, delay=3, backoff=2) def urlopen_with_retry(): return urllib2.urlopen(“http://example.com”) This will retry the function if URLError is raised. Check the link above for documentation on the parameters, but basically it will retry … Read more

How do __enter__ and __exit__ work in Python decorator classes?

the __exit__() method should accept information about exceptions that come up in the with: block. See here. The following modification of your code works: def __exit__(self, exc_type, exc_value, tb): if exc_type is not None: traceback.print_exception(exc_type, exc_value, tb) # return False # uncomment to pass exception through return True Then you can try raising an exception … Read more

How to skip a pytest using an external fixture?

It seems py.test doesn’t use the test fixtures when evaluating the expression for skipif. By your example, test_ios is actually successful because it is comparing the function platform found in the module’s namespace to the “ios” string, which evaluates to False hence the test is executed and succeeds. If pytest was inserting the fixture for … Read more

Python-like C++ decorators

std::function provides most of the building blocks for my proposed solution. Here is my proposed solution. #include <iostream> #include <functional> //——————————- // BEGIN decorator implementation //——————————- template <class> struct Decorator; template <class R, class… Args> struct Decorator<R(Args …)> { Decorator(std::function<R(Args …)> f) : f_(f) {} R operator()(Args … args) { std::cout << “Calling the decorated … Read more

How can I get a Python decorator to run after the decorated function has completed?

Decorators usually return a wrapper function; just put your logic in the wrapper function after invoking the wrapped function. def audit_action(action): def decorator_func(func): def wrapper_func(*args, **kwargs): # Invoke the wrapped function first retval = func(*args, **kwargs) # Now do something here with retval and/or action print(‘In wrapper_func, handling action {!r} after wrapped function returned {!r}’.format(action, … Read more