decorator

Cache decorator for numpy arrays

邮差的信 提交于 2019-12-08 08:12:51
问题 I am trying to make a cache decorator for functions with numpy array input parameters from functools import lru_cache import numpy as np from time import sleep a = np.array([1,2,3,4]) @lru_cache() def square(array): sleep(1) return array * array square(a) But numpy arrays are not hashable, TypeError Traceback (most recent call last) <ipython-input-13-559f69d0dec3> in <module>() ----> 1 square(a) TypeError: unhashable type: 'numpy.ndarray' So they need to be converted to tuples. I have this

How do decorators in es6 work?

ⅰ亾dé卋堺 提交于 2019-12-08 07:06:37
问题 I'm learning redux and see that the examples in the doc use connect in this signature: const VisibleTodoList = connect( mapStateToProps, mapDispatchToProps )(TodoList) But in other places from other repos I've also seen this- @connect(mapStateToProps, mapDispatchToProps) I get that there the same thing but how does the signature of a decorator work? It doesn't look like it's setting a variable to the result of the connect so where does the function of @connect go to and get assigned to? 回答1:

Memoize decorators failing to memoize (when not using decorator syntax)

送分小仙女□ 提交于 2019-12-08 06:46:31
I've got a simple memoizer decorator: def funcmemo(f): memo = {} @wraps(f) def wrapper(*args): if args in memo: return memo[args] else: temp = f(*args) print "memoizing: ", args, temp memo[args] = temp return temp return wrapper Now, when I use it via the "@" token, @funcmemo def fib(n): print "fib called with:", n if n < 2: return n return fib(n-2) + fib(n-1) res = fib(3) print "result:", res it works correctly, as seen in the printed output: fib called with: 3 fib called with: 1 memoizing: (1,) 1 fib called with: 2 fib called with: 0 memoizing: (0,) 0 memoizing: (2,) 1 memoizing: (3,) 2

JavaScript Decorator Pattern

大城市里の小女人 提交于 2019-12-08 05:45:06
问题 I am trying to get a decorator pattern to work follwing http://www.joezimjs.com/javascript/javascript-design-patterns-decorator/ I might have missed some point but the pattern does not work as i expect. If i add two decorators and add them to a class the functions are not passed through as i thought. Only the last decorator's functions can be called or the basic Decorator is called. The chaining is broken. How do i fix this? I added a fiddle: http://jsfiddle.net/hbyLW/25/ UPDATE: Corrected

How to create a vector of all decorated functions from a specific module?

倖福魔咒の 提交于 2019-12-08 05:17:10
问题 I have a file main.rs and a file rule.rs . I want to define functions in rule.rs to be included in the Rules::rule vector without having to push them one by one. I'd prefer a loop to push them. main.rs : struct Rules { rule: Vec<fn(arg: &Arg) -> bool>, } impl Rules { fn validate_incomplete(self, arg: &Arg) -> bool { // iterate through all constraints and evaluate, if false return and stop for constraint in self.incomplete_rule_constraints.iter() { if !constraint(&arg) { return false; } } true

Attribute access on a decorated callable class

丶灬走出姿态 提交于 2019-12-08 05:15:54
问题 I have a callable class: class CallMeMaybe: __name__ = 'maybe' def __init__(self): self.n_calls = 0 def __call__(self): self.n_calls += 1 raise Exception That seems to work as advertised: >>> f = CallMeMaybe() >>> f.n_calls 0 >>> for i in range(7): ... try: ... f() ... except Exception: ... pass ... >>> f.n_calls 7 I want to decorate it with an exponential backoff: from backoff import on_exception, expo dec = on_exception(expo, Exception, max_tries=3, on_backoff=print) f = CallMeMaybe() f2 =

Memoize decorators failing to memoize (when not using decorator syntax)

為{幸葍}努か 提交于 2019-12-08 04:58:43
问题 I've got a simple memoizer decorator: def funcmemo(f): memo = {} @wraps(f) def wrapper(*args): if args in memo: return memo[args] else: temp = f(*args) print "memoizing: ", args, temp memo[args] = temp return temp return wrapper Now, when I use it via the "@" token, @funcmemo def fib(n): print "fib called with:", n if n < 2: return n return fib(n-2) + fib(n-1) res = fib(3) print "result:", res it works correctly, as seen in the printed output: fib called with: 3 fib called with: 1 memoizing:

How do decorators in es6 work?

一曲冷凌霜 提交于 2019-12-08 03:51:23
I'm learning redux and see that the examples in the doc use connect in this signature: const VisibleTodoList = connect( mapStateToProps, mapDispatchToProps )(TodoList) But in other places from other repos I've also seen this- @connect(mapStateToProps, mapDispatchToProps) I get that there the same thing but how does the signature of a decorator work? It doesn't look like it's setting a variable to the result of the connect so where does the function of @connect go to and get assigned to? Decorators are just higher-order functions that work on the next thing that they see. This is sort of

Making 'isinstance' work with decorators

只谈情不闲聊 提交于 2019-12-08 02:53:36
问题 How does the Python isinstance function work internally? Is there anything I can do to alter its results, like define a special function inside a class or something? Here's my use case: class Decorator: def __init__(self, decorated): self._decorated = decorated def __call__(self): return self._decorated() @Decorator class Foo: pass f = Foo() # How can I make this be true? isinstance(f, Foo) Decorator acts almost like a mixin, except a mixing wouldn't be appropriate here. Is there any way I

python setattr for dynamic method creator with decorator

大憨熊 提交于 2019-12-08 02:25:47
问题 I have a class which has multiple methods defined. import mat class Klass(object): @mat.sell(mat.CanSet): def method1(self): return None @mat.sell(mat.CanSet): def method2(self): return 'value2' Imagine I have 10 methods that I need to populate for this 'Klass'. I want to generate these methods without explicitely writing them all. So I want to do a factory that does setattr for each method. Problem is that I do following and the last method has the last value. Each do not get its related