decorator

R decorator to change both input and output

五迷三道 提交于 2020-01-11 05:58:46
问题 I am trying to refactor this. In Python, I would use a decorator. What's the 'R'tful way to do this? Say, we have this pattern good_input <- format_input( bad_input ) bad_output <- use_this_func( good_input ) good_output <- format_output( bad_output ) And then again, good_input <- format_input( bad_input ) bad_output <- use_this_other_func( good_input ) good_output <- format_output( bad_output ) As you can imagine, this proliferates like wild mushroom. I want something close to this solution

Preserve default arguments of wrapped/decorated Python function in Sphinx documentation

♀尐吖头ヾ 提交于 2020-01-11 02:10:29
问题 How can I replace *args and **kwargs with the real signature in the documentation of decorated functions? Let's say I have the following decorator and decorated function: import functools def mywrapper(func): @functools.wraps(func) def new_func(*args, **kwargs): print('Wrapping Ho!') return func(*args, **kwargs) return new_func @mywrapper def myfunc(foo=42, bar=43): """Obscure Addition :param foo: bar! :param bar: bla bla :return: foo + bar """ return foo + bar Accordingly, calling print

Preferred way of defining properties in Python: property decorator or lambda?

那年仲夏 提交于 2020-01-10 07:39:08
问题 Which is the preferred way of defining class properties in Python and why? Is it Ok to use both in one class? @property def total(self): return self.field_1 + self.field_2 or total = property(lambda self: self.field_1 + self.field_2) 回答1: The decorator form is probably best in the case you've shown, where you want to turn the method into a read-only property. The second case is better when you want to provide a setter/deleter/docstring as well as the getter or if you want to add a property

In Django, how do I write a this for every function?

亡梦爱人 提交于 2020-01-07 09:03:13
问题 Whenever someone loads a page, I want to print "Hello" in console. I don't want to add a decorator to all my 100 views. That's silly. I sort of want this decorator to automatically be added to my views. Hmm... 回答1: Middleware: http://docs.djangoproject.com/en/dev/topics/http/middleware/#writing-your-own-middleware Define a middleware class, override one of the functions defined above, print "Hello", return the appropriate object (according to docs), and add the middleware class to your

Abstract base class should have or not have data members when using decorator pattern C++

狂风中的少年 提交于 2020-01-06 21:28:34
问题 Some posts have previously asked/discussed whether to include private data members in abstract base classes. I want to explore this here on a concrete example in combination with the decorator pattern. I came across this issue when I was implementing a Monte Carlo library to price financial derivatives. A random normal number generator is an essential part of a Monte Carlo pricing library. You have some options on how to define you normal variables starting from a random uniform number

How do I set a decorator for every one of my Django views?

▼魔方 西西 提交于 2020-01-06 05:43:47
问题 Suppose I have 20 views inside views.py How do I set a decorator on top of each function? Without manually pasting one on top? 回答1: I found a discussion regarding this here: http://groups.google.com/group/django-users/browse_thread/thread/2cff3374d365998d/24cd8b86b73d3832?lnk=raot&pli=1 The general conclusion is that you could write some code to auto-apply your decorator to all views, but that in general it is not worth the trouble and later confusion. 回答2: You can decorate all your views in

Flask-Login breaks when my decorator accepts parameters

丶灬走出姿态 提交于 2020-01-06 04:23:48
问题 Thanks to what I learned from this question, I've been able to make a Flask-Login process with a endpoint like this: @app.route('/top_secret') @authorize @login_required def top_secret(): return render_template("top_secret.html") and (for now) a completely pass-through "authorize" decorator: from functools import wraps def authorize(func): @wraps(func) def newfunc(*args, **kwargs): return func(*args, **kwargs) return newfunc The @wraps(func) call allows Flask to find the endpoint that it's

Python: Change class type with decorator and keep it's methods

时光总嘲笑我的痴心妄想 提交于 2020-01-05 10:10:14
问题 I want to create a class which could be used inside different Applications and their APIs to create UIs. Therefor I created a module called ui.py . Inside this module is the following: from PyQt4 import QtGui def CreateGui(uiFile, parent=None): print "Ui build.." def GetUiObject(uiClass): pUI = uiClass.PARENT class ParentUI(pUI): def __init__(self, uiFile): CreateGui(uiFile, self) def __call__(self, cls): for func in uiClass.__dict__: setattr(cls, func, uiClass.__dict__[func]) return ParentUI

Optionally use decorators on class methods

允我心安 提交于 2020-01-05 08:20:53
问题 Im new to Python, and im building a wrapper for an api. I would want to let the user decide if he/she wants to use a decorator on methods I expose from my module. For example: # create a new instance api = MyApi() # return a simple json response with all the data related to a timetable print api.time_table Now the user has all the data, and can do whatever he/she wants. I would want to add some kind of 'convenience' methods, for example; to let the user get a small part of the json instead of

ambiguous access when calling a decorated base class

与世无争的帅哥 提交于 2020-01-04 05:42:10
问题 I have a class that can be decorated with a set of add-on templates to provide additional functionality. Each add-on needs to be able to call the base class and the user needs to be able to call the base class (either directly or using the CMyClass as a proxy). Unfortunately, the compiler can't tell which base class I'm calling and I get ambiguous access errors. template< class T > class AddOn_A : public T { public: AddOn_A( int x ) : T( x ) {}; int AddOne() { T* pT = static_cast< T* >( this