decorator

Jython @property SyntaxError: mismatched input '' expecting CLASS

China☆狼群 提交于 2019-12-22 22:57:42
问题 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>

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>

Skipping all unit tests but one in Python by using decorators and metaclasses

徘徊边缘 提交于 2019-12-22 18:05:47
问题 I am writing unit tests for an MCU that communicates commands through the USB port and checks their response. If one unit test fails it makes sense for me to do some debugging in the MCU. Therefore I would like to disable all unittests except for the one that I would like to debug on the MCU side because if I set a breakpoint somewhere it might get triggered by another unittest with different commands. I went to the python docs and found this code which is a decorator that will skip all

How do I catch an error raised in a decorator?

主宰稳场 提交于 2019-12-22 13:54:00
问题 I am using flask-auth, which provides some helper decorators. I've added all the various methods below, but the question I want to ask is how to catch any issues thrown by the authorized_handler decorator. It's a general question about decorators, but I thought a real example might help. If the decorator blows up, how could I catch it? import os import flask import flask_oauth CONSUMER_KEY = os.environ['CONSUMER_KEY'] CONSUMER_SECRET = os.environ['CONSUMER_SECRET'] oauth = flask_oauth.OAuth()

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

Decorators and in class

谁说胖子不能爱 提交于 2019-12-22 10:33:21
问题 Is there any way to write decorators within a class structure that nest well? For example, this works fine without classes: def wrap1(func): def loc(*args,**kwargs): print 1 return func(*args,**kwargs) return loc def wrap2(func): def loc(*args,**kwargs): print 2 return func(*args,**kwargs) return loc def wrap3(func): def loc(*args,**kwargs): print 3 return func(*args,**kwargs) return loc def merger(func): return wrap1(wrap2(wrap3(func))) @merger def merged(): print "merged" @wrap1 @wrap2

Decorating a method

痞子三分冷 提交于 2019-12-22 09:13:20
问题 In my Python app, I'm using events to communicate between different plugins. Now, instead of registering the methods to the events manually, I thought I might use decorators to do that for me. I would like to have it look like this: @events.listento('event.name') def myClassMethod(self, event): ... I have first tried to do it like this: def listento(to): def listen_(func): myEventManager.listen(to, func) def wrapper(*args, **kwargs): return func(*args, **kwargs) return func return listen_

How to decorate interfaces bound to more than one concrete type with Ninject

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-22 08:35:01
问题 So, I have a message bus that instantiates message handlers through Ninject. I'd like to decorate my handlers with cross cutting concerns such as logging, transaction management, etc. I setup my bindings like so: kernel.Bind<IMessageHandler<int>>().To<IntHandlerOne>() .WhenInjectedInto(typeof(HandlerDecorator<>)); kernel.Bind(typeof(IMessageHandler<>)).To(typeof(HandlerDecorator<>)); Which works fantastically whenever I have a single handler of a specific message type. However, when I have

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

Python decorator with Flask

一个人想着一个人 提交于 2019-12-22 04:13:50
问题 I need to add a python decorator to Flask route functions, (basically I edited the code from here) def requires_admin(f): def wrapper(f): @wraps(f) def wrapped(*args, **kwargs): #if not admin: #return render_template('error.html') return f(*args, **kwargs) return wrapped return wrapper and use it like this will be OK: @app.route('/admin/action') @requires_admin def AdminAction(): #NO error if NO parameter But use it like this will have error: @app.route('/admin/action/<int:id>') @requires