decorator

Passing default arguments to a decorator in python

廉价感情. 提交于 2019-12-12 07:28:06
问题 I am trying to find a way to pass my functions default arguments to the decorator. I have to say I am fairly new to the decorator business, so maybe I just don't understand it properly, but I have not found any answers yet. So here my modified example from the python functools.wraps manual page. from functools import wraps def my_decorator(f): @wraps(f) def wrapper(*args, **kwds): print 'Calling decorated function' print 'args:', args print 'kwargs:', kwds return f(*args, **kwds) return

Can I use the decorator pattern to wrap a method body?

江枫思渺然 提交于 2019-12-12 07:26:27
问题 I have a bunch of methods with varying signatures. These methods interact with a fragile data connection, so we often use a helper class to perform retries/reconnects, etc. Like so: MyHelper.PerformCall( () => { doStuffWithData(parameters...) }); And this works fine, but it can make the code a little cluttery. What I would prefer to do is decorate the methods that interact with the data connection like so: [InteractsWithData] protected string doStuffWithData(parameters...) { // do stuff... }

Python decorator with multiprocessing fails

丶灬走出姿态 提交于 2019-12-12 07:25:18
问题 I would like to use a decorator on a function that I will subsequently pass to a multiprocessing pool. However, the code fails with "PicklingError: Can't pickle : attribute lookup __builtin__ .function failed". I don't quite see why it fails here. I feel certain that it's something simple, but I can't find it. Below is a minimal "working" example. I thought that using the functools function would be enough to let this work. If I comment out the function decoration, it works without an issue.

Accessing original decorated function for test purposes

只愿长相守 提交于 2019-12-12 07:22:33
问题 I'm using a decorator( @render_to from the django_annoying package) in a view function. But the thing is that I wanted to get the original dict that is returned by the view function for test purposes, instead of the HttpResponse object that the decorator returns. The decorator uses @wraps (from functools ). If there is no way to access this, then do you have any idea of how to test this? 回答1: The wrapped function will be available as a function closure cell. Which cell exactly depends on how

How to include Draper Decorators in Rails Console?

为君一笑 提交于 2019-12-12 05:28:10
问题 I'd like to see the output of some of my Draper Decorators in Rails console (https://github.com/drapergem/draper and http://railscasts.com/episodes/286-draper). To do so, I was looking for a way to include a decorator and its methods similar to as we would an application helper: include ActionView::ApplicationHelper A draper decorator inherits from an ApplicationDecorator, which inherits from Draper::Base class ApplicationDecorator < Draper::Base end class MaterialDecorator <

How can I send unknown list of arguments to a python decorator — when the decorator is a method of a different class?

∥☆過路亽.° 提交于 2019-12-12 04:47:05
问题 This question is related to another question I just asked. I have created a python decorator as shown below. I want this decorator to accept an unknown list of arguments. But there is an added wrinkle. The decorator is an instance method of another class: #!/usr/bin/env python from functools import wraps class A: def my_decorator(self, func=None, **kwargs): def inner_function(decorated_function): def wrapped_func(*fargs, **fkwargs): print kwargs return decorated_function(*fargs, **fkwargs)

How to inherit a flask MethodView class without its decorators?

你说的曾经没有我的故事 提交于 2019-12-12 03:44:28
问题 For the reason of not rewriting the same API. I want to inherit a get method from an already created MethodView and ignore the login_required decorator . class DoStuffA(MethodView): decorators = [login_required] def get(self): return jsonify({"status":"ok"}) api.add_url_rule('/dostufa', view_func=DoStuffA.as_view("dostuffa"), methods=['GET']) class DoStuffB(DoStuffA): pass api.add_url_rule('/dostuffb', view_func=DoStuffB.as_view("dostuffb"), methods=['GET']) If I do a GET request to /dostuffb

c++ dynamic_cast over decorator instantiations fails

时间秒杀一切 提交于 2019-12-12 01:57:37
问题 I am trying to understand how decorator pattern works and how much I can "stretch" it to me needs. Following this example, I have extended classes XYZ. There exist derived classes "KLM" (from XYZ) Specifically, even though I have a decorator pattern, the derived decorator classes "KLM" have some functionality that does not show up in any of their base classes "XYZ", "D", "I" or "A". So while normally I would instantiate an object as I * inKLM = new L( new M( new K( new A ))); This would not

TypeScript : Using decorators for custom JSON serialization

独自空忆成欢 提交于 2019-12-12 01:57:10
问题 I'm trying to implement TypeScript decorators as seen in this, this and this link. I got my project running but I've got an issue when it comes to retrieve some values from the Decorator. First, the related classes : SerializeDecorator.ts let toSerialize = new WeakMap(); // for each target (Class object), add a map of property names export function serialize(target: any, key: string) { console.log("Target = " + target + " / Key = " + key); let map = toSerialize.get(target); if (!map) { map =

python decorators and wrappers

孤街浪徒 提交于 2019-12-11 23:22:08
问题 I am having a hard time grasping decorators, I initially thought decorators were syntactic sugar to do an operation such as: def decorator(x): return x*2 @decorator def plusone(x): return x+1 print plusone(5) # would print twelve, because: decorator(plusone(5)) as I have seen # in some tutorials but I've noticed that a wrapper function needs to be created in the decorator first. Why do we need to return a function, not an integer? 回答1: A (Python) decorator is syntactic sugar for a function