decorator

How to create a synchronized function across all instances

巧了我就是萌 提交于 2019-12-11 12:48:56
问题 I want to create a synchronized method in python with respect to some CRUD functions on all instances of a class. For example while create is called and ongoing by a thread, delete needs to wait on the same object. Can someone please tell me if the code below is correct. I may have some syntax error but what I want to know is if the lock is going to be the same across calls to create ALL instances of this class and therefore if any instance create/delete is in progress delete/create on the

DI Interception vs. AOP

纵然是瞬间 提交于 2019-12-11 11:56:31
问题 From Unity documentation: Unity interception enables you to effectively capture calls to objects and add additional functionality to the target object. Interception is useful when you want to modify the behavior for individual objects but not the entire class, very much as you would do when using the Decorator pattern. It provides a flexible approach for adding new behaviors to an object at run time. Since the very same DP is used in Aspect Oriented Programming (see here) ...In the .NET

Why do the references in a decorator and in a proxy point to their subject's interface and concrete respectively?

点点圈 提交于 2019-12-11 10:58:55
问题 From Design Pattern by Gang of Four Why does the reference component of the decorator Decorator to the decorated point to the interface Component of the decorated, while the reference realSubject of the proxy Proxy point to the concrete RealSubject ? Thanks. 回答1: Proxy may point exclusively to its subject's interface. The GoF mentions, Proxy may refer to a Subject if the RealSubject and Subject interfaces are the same. But depending on implementation, a Proxy may instantiate its subject by

Passing arguments to decontext decorator

£可爱£侵袭症+ 提交于 2019-12-11 09:54:38
问题 I have a helper class Decontext that I am using to turn a context manager into a decorator (pyton 2.6). class Decontext(object): """ makes a context manager also act as decorator """ def __init__(self, context_manager): self._cm = context_manager def __enter__(self): return self._cm.__enter__() def __exit__(self, *args, **kwds): return self._cm.__exit__(*args, **kwds) def __call__(self, func): def wrapper(*args, **kwds): with self: return func(*args, **kwds) return wrapper My contextmanager

How do I call super in a method decorator in Python 3? [duplicate]

╄→гoц情女王★ 提交于 2019-12-11 09:19:49
问题 This question already has an answer here : Given a method, how do I return the class it belongs to in Python 3.3 onward? (1 answer) Closed 5 years ago . How do I fill in the ??? ? def ensure_finished(iterator): try: next(iterator) except StopIteration: return else: raise RuntimeError def derived_generator(method): def new_method(self, *args, **kwargs): x = method(self, *args, **kwargs) y = getattr(super(???, self), method.__name__)\ (*args, **kwargs) for a, b in zip(x, y): assert a is None

Structuremap3 DecorateAllWith

試著忘記壹切 提交于 2019-12-11 09:07:02
问题 I've been struggling getting DecorateAllWith working on generic interfaces. I've read some posts here where they solved it using interceptors but they seem to be using an older structure map version and it doesn't seem like a "clean" solution. I would really need some help to get it working with structure map 3 I have a generic repository which i would like to decorate with both logging and caching public interface IEntityRepository<T> where T : Entities.IEntity { } I have about 20 interfaces

generate a list of arguments in correct order from args and kwargs?

北战南征 提交于 2019-12-11 08:14:27
问题 following this topic: allow users to "extend" API functions class Inspector: def __init__(self, passedFunc): self.passedFunc = passedFunc def __call__(self, *args, **kwargs): # Inspector needs to generate a list of arguments of the passed function and print them in *correct* order # now how to generate a list of arguments using data from both ''args' and 'kwargs'? # the user might keep default argument values, in which case both will be None, # he might only use args, in which case it is easy

Unity Decorator Extension fails with multiple implementations

萝らか妹 提交于 2019-12-11 08:10:07
问题 I've been struggling with this problem for a couple days, and I still am not sure how to solve it. I've created a container extension for the Unity Container to enable me to easily register decorator classes in the container. This is the implementation I currently have, which is almost identical to the one in this article: public class DecoratorExtension : UnityContainerExtension { private int m_order; private Dictionary<Type, IList<DecoratorRegistration>> m_typeStacks; protected override

Decorator overwriting POST, GET and REQUEST in Django - doing it right?

ぃ、小莉子 提交于 2019-12-11 07:47:48
问题 In Django, I have created a function decorator which can - in this example - create a lowercase version of a supplied POST/GET argument, and it updates the REQUEST before the view handles it all. I have created the following decorator for this: def force_lowercase(*fields): assert isinstance(fields, tuple), "Fields must be of type tuple." def wrap_func(fn): def wrapper(request): post = request.POST.copy() get = request.GET.copy() for field in fields: if field in post: post[field] = post[field

Python class decorator converting element access to attribute access

↘锁芯ラ 提交于 2019-12-11 07:45:07
问题 I'm looking for a decorator for Python class that would convert any element access to attribute access, something like this: @DictAccess class foo(bar): x = 1 y = 2 myfoo = foo() print myfoo.x # gives 1 print myfoo['y'] # gives 2 myfoo['z'] = 3 print myfoo.z # gives 3 Does such decorator exist somewhere already? If not, what is the proper way to implement it? Should I wrap __new__ on class foo and add __getitem__ and __setitem__ properties to the instance? How make these properly bound to the