decorator

How to strip decorators from a function in Python

旧城冷巷雨未停 提交于 2019-12-27 19:17:12
问题 Let's say I have the following: def with_connection(f): def decorated(*args, **kwargs): f(get_connection(...), *args, **kwargs) return decorated @with_connection def spam(connection): # Do something I want to test the spam function without going through the hassle of setting up a connection (or whatever the decorator is doing). Given spam , how do I strip the decorator from it and get the underlying "undecorated" function? 回答1: In the general case, you can't, because @with_connection def spam

How to strip decorators from a function in Python

前提是你 提交于 2019-12-27 19:15:48
问题 Let's say I have the following: def with_connection(f): def decorated(*args, **kwargs): f(get_connection(...), *args, **kwargs) return decorated @with_connection def spam(connection): # Do something I want to test the spam function without going through the hassle of setting up a connection (or whatever the decorator is doing). Given spam , how do I strip the decorator from it and get the underlying "undecorated" function? 回答1: In the general case, you can't, because @with_connection def spam

How to interface Output() and Input() decorators?

扶醉桌前 提交于 2019-12-25 18:55:32
问题 I want to create an interface for components that generate JSON. I want to force each implementing component to accept a type as Input and produce an Output: import { EventEmitter, Output, Input } from '@angular/core'; import { Foo, FooConfiguration } from '../../interfaces'; interface FooConfigurator { @Output() fooWasConfigured: EventEmitter<FooConfiguration>; @Input() fooInstance: Foo; } Then, components implementing FooConfigurator would ensure the following: import { EventEmitter, Output

How can I get the same/to pass parameters between decorator and decorated function?

99封情书 提交于 2019-12-25 17:18:54
问题 In this case I want to pass _source_dir_abs:str into decorator. I tried to mimic the same process that Flask has for routing to pass parameter from decorator to function it is decorated. But this makes the parameter interpreted as a literal string and not as a variable. @dec_check_abs("<_source_dir_abs>") def walk_return_dir_nofolder(_source_dir_abs:str) -> list: w = walk(_source_dir_abs) d = [d for d, fol, fil in w if len(fol) == 0] return d I tried with @dec_check_abs(_source_dir_abs) it

How can I get the same/to pass parameters between decorator and decorated function?

被刻印的时光 ゝ 提交于 2019-12-25 17:18:24
问题 In this case I want to pass _source_dir_abs:str into decorator. I tried to mimic the same process that Flask has for routing to pass parameter from decorator to function it is decorated. But this makes the parameter interpreted as a literal string and not as a variable. @dec_check_abs("<_source_dir_abs>") def walk_return_dir_nofolder(_source_dir_abs:str) -> list: w = walk(_source_dir_abs) d = [d for d, fol, fil in w if len(fol) == 0] return d I tried with @dec_check_abs(_source_dir_abs) it

Java Decorative Pattern Pizza Topping

人走茶凉 提交于 2019-12-25 17:17:33
问题 i have to implement Pizza(American and Neapolitan) decoration pattern with 4 different toppings(Salami,Soudjouk,Onion,Pepper) which extends "TopingDecorator" class and out of them 3 will be added to pizza by "Add Pizza" command. However, the code does not add it to Pizza's TopingDecorator ArrayList. It should be something like below(I am trying to add Salami and Soudjouk to AmericanPan pizza(which extends PlainPizza class)): AmericanPan a = new American(); Salami s = new Salami(a); Soudjouk

Python decorator for attribute and method?

柔情痞子 提交于 2019-12-25 06:37:37
问题 Is it possible to have a decorator that makes a method work like an attribute if values are assigned to it using class.something = 2 and work like a method if it is called like class.something(2, True) ? What I currently have To be more concrete, I currently have the following class attributeSetter(object): ''' Makes functions appear as attributes. Takes care of autologging.''' def __init__(self, func): self.func = func def __set__(self, obj, value): return self.func(obj, value) def __repr__

Decorated class looses acces to its attributes

孤人 提交于 2019-12-25 05:35:24
问题 I implemented a decorator that worked like a charm until I added attributes to the decorated class. When I instantiate the class, it cannot acces the calss attributes. Take the following minimal working example : from module import specialfunction class NumericalMathFunctionDecorator: def __init__(self, enableCache=True): self.enableCache = enableCache def __call__(self, wrapper): def numericalmathfunction(*args, **kwargs): func = specialfunction(wrapper(*args, **kwargs)) """ Do some setup to

Decorator Pattern Using Composition Instead of Inheritance

两盒软妹~` 提交于 2019-12-25 01:43:46
问题 My previous understanding of the decorator pattern was that you inherit Window with WindowDecorator , then in the overridden methods, do some additional work before calling the Window 's implementation of said methods. Similar to the following: public class Window { public virtual void Open() { // Open the window } } public class LockableWindow : Window // Decorator { public virtual void Open() { // Unlock the window base.Open(); } } However this essentially hardcodes the decoration, so how

Why do I see SVN's uncommitted changes decorator on fully synced directories?

主宰稳场 提交于 2019-12-25 01:33:05
问题 I'm accessing a Subversion repository with Subclipse. Some of the folders in the Package Explorer window of MyEclipse show up with the uncommitted changes decorator: Usually, this means that one or more files in the directory on my machine have local, uncommitted changes. Right now, though, some of the folders appear to contain no files or subfolders with uncommitted changes. (I.e. I don't see any of these: ) This inconsistency is supported by Team Synchronization mode. When I tell Subclipse