decorator

Python LRU Cache Decorator Per Instance

吃可爱长大的小学妹 提交于 2019-12-17 22:26:50
问题 Using the LRU Cache decorator found here: http://code.activestate.com/recipes/578078-py26-and-py30-backport-of-python-33s-lru-cache/ from lru_cache import lru_cache class Test: @lru_cache(maxsize=16) def cached_method(self, x): return x + 5 I can create a decorated class method with this but it ends up creating a global cache that applies to all instances of class Test. However, my intent was to create a per instance cache. So if I were to instantiate 3 Tests, I would have 3 LRU caches rather

Python-like C++ decorators

我们两清 提交于 2019-12-17 22:06:22
问题 Are there ways to decorate functions or methods in C++ like in python style? @decorator def decorated(self, *args, **kwargs): pass Using macros for example: DECORATE(decorator_method) int decorated(int a, float b = 0) { return 0; } or DECORATOR_MACRO void decorated(mytype& a, mytype2* b) { } Is it possible? 回答1: std::function provides most of the building blocks for my proposed solution. Here is my proposed solution. #include <iostream> #include <functional> //-------------------------------

A decorator that profiles a method call and logs the profiling result

ε祈祈猫儿з 提交于 2019-12-17 21:57:04
问题 I want to create a decorator that profiles a method and logs the result. How can this be done? 回答1: The decorator would look something like: import time import logging def profile(func): def wrap(*args, **kwargs): started_at = time.time() result = func(*args, **kwargs) logging.info(time.time() - started_at) return result return wrap @profile def foo(): pass Anyway, if you want to do some serious profiling I would suggest you use the profile or cProfile packages. 回答2: If you want proper

Ruby on Rails patterns - decorator vs presenter

杀马特。学长 韩版系。学妹 提交于 2019-12-17 21:26:33
问题 There is all sorts of talk lately in the Ruby on Rails community about decorators and presenters. What is the essential difference between the two? If there is, what are the clues that tell me which one to use over the other? Or perhaps to use the two in conjunction? 回答1: A decorator is more of a "let's add some functionality to this entity". A presenter is more of a "let's build a bridge between the model/backend and view". The presenter pattern has several interpretations. Decorators are

list @property decorated methods in a python class

怎甘沉沦 提交于 2019-12-17 20:39:08
问题 Is it possible to obtain a list of all @property decorated methods in a class? If so how? Example: class MyClass(object): @property def foo(self): pass @property def bar(self): pass How would I obtain ['foo', 'bar'] from this class? 回答1: Anything decorated with property leaves a dedicated object in your class namespace. Look at the __dict__ of the class, or use the vars() function to obtain the same, and any value that is an instance of the property type is a match: [name for name, value in

Python Sphinx autodoc and decorated members

自古美人都是妖i 提交于 2019-12-17 19:07:59
问题 I am attempting to use Sphinx to document my Python class. I do so using autodoc: .. autoclass:: Bus :members: While it correctly fetches the docstrings for my methods, those that are decorated: @checkStale def open(self): """ Some docs. """ # Code with @checkStale being def checkStale(f): @wraps(f) def newf(self, *args, **kwargs): if self._stale: raise Exception return f(self, *args, **kwargs) return newf have an incorrect prototype, such as open(*args, **kwargs) . How can I fix this? I was

Decorating the ng-click directive in AngularJs

帅比萌擦擦* 提交于 2019-12-17 18:39:20
问题 I've been looking into modifying the AngularJS ng-click directive to add some additional features. I have a few different ideas of what to use it for, but a simple one is to add Google Analytics tracking to all ng-clicks, another is to prevent double clicking. To do this my first thought was to use a decorator. So something like this: app.config(['$provide', function($provide) { $provide.decorator('ngClickDirective', ['$delegate', function($delegate) { // Trigger Google Analytics tracking

Factory method for objects - best practice?

我的梦境 提交于 2019-12-17 18:05:10
问题 This is a question regarding the best practice for creating an instance of a class or type from different forms of the same data using python. Is it better to use a class method or is it better to use a separate function altogether? Let's say I have a class used to describe the size of a document. (Note: This is simply an example. I want to know the best way to create an instance of the class not the best way to describe the size of a document.) class Size(object): """ Utility object used to

Zend Framework forms, decorators and validation: should I go back to plain HTML?

情到浓时终转凉″ 提交于 2019-12-17 17:26:58
问题 I am currently working on a pretty large application which contains a lot of forms. Up to this moment, I have always been writing my forms by hand and writing my own validation logic, but I have decided it was about time I started using Zend_Form and it's built-in validation routines. However, I keep stumbling upon more and more problems concerning the (lack of) flexibility caused Zend_Form_Decorator. Simple tasks like adding an extra button to a single input-element become incredibly

Applying a decorator to every method in a class?

元气小坏坏 提交于 2019-12-17 15:44:59
问题 I have decorator @login_testuser applied to method test_1() : class TestCase(object): @login_testuser def test_1(self): print "test_1()" Is there a way I can apply @login_testuser on every method of the class prefixed with "test_" ? In other words, the decorator would apply to test_1() , test_2() methods below, but not on setUp() . class TestCase(object): def setUp(self): pass def test_1(self): print "test_1()" def test_2(self): print "test_2()" 回答1: In Python 2.6, a class decorator is