python-decorators

Jython @property SyntaxError: mismatched input '' expecting CLASS

你离开我真会死。 提交于 2019-12-22 22:57:05
问题 I tried to run this example from the docs in the Jython interpreter: http://www.jython.org/docs/library/functions.html class C(object): def __init__(self): self._x = None @property def x(self): """I'm the 'x' property.""" return self._x @x.setter def x(self, value): self._x = value @x.deleter def x(self): del self._x Just entering the first 4 lines (up to and including @property ) yields a SyntaxError: >>> class C(object): ... def __init__(self): ... self._x = None ... @property File "<stdin>

Does python allow me to pass dynamic variables to a decorator at runtime?

廉价感情. 提交于 2019-12-22 10:48:21
问题 I am attempting to integrate a very old system and a newer system at work. The best I can do is to utilize an RSS firehouse type feed the system utilizes. The goal is to use this RSS feed to make the other system perform certain actions when certain people do things. My idea is to wrap a decorator around certain functions to check if the user (a user ID provided in the RSS feed) has permissions in the new system. My current solution has a lot of functions that look like this, which are called

Django - Correct way to pass arguments to CBV decorators?

匆匆过客 提交于 2019-12-22 10:38:52
问题 The docs feature nice options for applying decorators such as login_required to Class Based Views. However, I'm a little unclear about how to pass specific arguments along with the decorator, in this case I'd like to change the login_url of the decorator. Something like the following, only valid: @login_required(login_url="Accounts:account_login") @user_passes_test(profile_check) class AccountSelectView(TemplateView): template_name='select_account_type.html' 回答1: You should use @method

Python: Monkeypatching a method of an object

巧了我就是萌 提交于 2019-12-22 09:49:55
问题 I'm hitting an API in python through requests' Session class. I'm making GET & POST method call using requests.Session(). On every call(GET/POST) failure, I want to notify another process. I can do this by creating a utility method as follows: s = request.Session() def post(): try: s.post(URL,data,headers) except: notify_another_process() And call this method instead of requests.Session().post directly. But, I want to monkeypatch this code to requests.Session().post and want the additional

Flask decorator : Can't pass a parameter from URL

独自空忆成欢 提交于 2019-12-22 08:34:16
问题 i'm quite new with flask and i'm trying to use the migthy power of decorators :p I read lot of things and found tons of topics about python decorators here but nothing really helpful. @app.route('groups/<id_group>') @group_required(id_group) @login_required def groups_groupIndex(id_group): #do some stuff return render_template('index_group.html') This is the error i get : @group_required(id_group), NameError: name 'id_group' is not defined Ok, id_group is not defined yet, but I don't

descriptor '__init__' of 'super' object needs argument

こ雲淡風輕ζ 提交于 2019-12-22 04:13:15
问题 I'm trying my hand at making an Object-Oriented text-based game in Python, and attempting to implement my first properties and decorators. Using the chapter 5 in the book 'Python 3 Object Oriented Programming', I've tried to use the examples and concepts discussed to get the following code to set a Game-object's 'current_room' property upon instantiation: class Room(object): ''' An area of the game's map.''' def __init__(self): print("Accessing the Room __init__ method.") class FirstRoom(Room

Decorators for selective caching / memoization

喜你入骨 提交于 2019-12-21 17:42:17
问题 I am looking for a way of building a decorator @memoize that I can use in functions as follows: @memoize my_function(a, b, c): # Do stuff # result may not always be the same for fixed (a,b,c) return result Then, if I do: result1 = my_function(a=1,b=2,c=3) # The function f runs (slow). We cache the result for later result2 = my_function(a=1, b=2, c=3) # The decorator reads the cache and returns the result (fast) Now say that I want to force a cache update : result3 = my_function(a=1, b=2, c=3,

Python decorator to keep signature and user defined attribute

◇◆丶佛笑我妖孽 提交于 2019-12-21 10:18:26
问题 I have my simple decorator my_decorator which decorates the my_func . def my_decorator(func): def wrapper(*args, **kwargs): return func(*args, **kwargs) wrapper._decorator_name_ = 'my_decorator' return wrapper @my_decorator def my_func(x): print('hello %s'%x) my_func._decorator_name_ 'my_decorator' Till here things work, but I can't see the actual signature of the function. my_func? Signature: my_func(*args, **kwargs) Docstring: <no docstring> File: ~/<ipython-input-2-e4c91999ef66> Type:

wrapping class method in try / except using decorator

时间秒杀一切 提交于 2019-12-21 05:21:08
问题 I have a general purpose function that sends info about exceptions to an application log. I use the exception_handler function from within methods in classes. The app log handler that is passed into and called by the exception_handler creates a JSON string that is what actually gets sent to the logfile. This all works fine. def exception_handler(log, terminate=False): exc_type, exc_value, exc_tb = sys.exc_info() filename, line_num, func_name, text = traceback.extract_tb(exc_tb)[-1] log.error(

python decorator to display passed AND default kwargs

我是研究僧i 提交于 2019-12-21 05:03:10
问题 I am new to python and decorators and am stumped in writing a decorator which reports not only passed args and kwargs but ALSO the unchanged default kwargs. This is what I have so far. def document_call(fn): def wrapper(*args, **kwargs): print 'function %s called with positional args %s and keyword args %s' % (fn.__name__, args, kwargs) return fn(*args, **kwargs) return wrapper @document_call def square(n, trial=True, output=False): # kwargs are a bit of nonsense to test function if not