decorator

Finding a function's parameters in Python

时光毁灭记忆、已成空白 提交于 2019-12-30 02:26:08
问题 I want to be able to ask a class's __init__ method what it's parameters are. The straightforward approach is the following: cls.__init__.__func__.__code__.co_varnames[:code.co_argcount] However, that won't work if the class has any decorators. It will give the parameter list for the function returned by the decorator. I want to get down to the original __init__ method and get those original parameters. In the case of a decorator, the decorator function is going to be found in the closure of

Django: Tweaking @login_required decorator

我与影子孤独终老i 提交于 2019-12-29 19:24:18
问题 I want to begin a private Beta for my website. I have a splash page where a user can enter a code to then access the rest of the site. Currently, all the other site pages (except the splash page) consist of a series of redirects set up by requiring user login (via @login_required decorator). I want both logged in users and people who enter the Beta Tester code to be able to access the rest of the site. That means that I can't just use the decorator for all my views. Should I alter the @login

Django: Tweaking @login_required decorator

China☆狼群 提交于 2019-12-29 19:23:34
问题 I want to begin a private Beta for my website. I have a splash page where a user can enter a code to then access the rest of the site. Currently, all the other site pages (except the splash page) consist of a series of redirects set up by requiring user login (via @login_required decorator). I want both logged in users and people who enter the Beta Tester code to be able to access the rest of the site. That means that I can't just use the decorator for all my views. Should I alter the @login

Django: Tweaking @login_required decorator

瘦欲@ 提交于 2019-12-29 19:23:10
问题 I want to begin a private Beta for my website. I have a splash page where a user can enter a code to then access the rest of the site. Currently, all the other site pages (except the splash page) consist of a series of redirects set up by requiring user login (via @login_required decorator). I want both logged in users and people who enter the Beta Tester code to be able to access the rest of the site. That means that I can't just use the decorator for all my views. Should I alter the @login

Django: Tweaking @login_required decorator

随声附和 提交于 2019-12-29 19:23:02
问题 I want to begin a private Beta for my website. I have a splash page where a user can enter a code to then access the rest of the site. Currently, all the other site pages (except the splash page) consist of a series of redirects set up by requiring user login (via @login_required decorator). I want both logged in users and people who enter the Beta Tester code to be able to access the rest of the site. That means that I can't just use the decorator for all my views. Should I alter the @login

Making decorators with optional arguments [duplicate]

Deadly 提交于 2019-12-29 10:13:07
问题 This question already has answers here : How to create a Python decorator that can be used either with or without parameters? (11 answers) Closed 4 months ago . from functools import wraps def foo_register(method_name=None): """Does stuff.""" def decorator(method): if method_name is None: method.gw_method = method.__name__ else: method.gw_method = method_name @wraps(method) def wrapper(*args, **kwargs): method(*args, **kwargs) return wrapper return decorator Example: The following decorates

registering open generic decorators for typed implementations in castle windsor

那年仲夏 提交于 2019-12-29 09:24:22
问题 While trying to coerce Windsor into wrapping an implementation with a random number of decorators, i've stumbled upon the following: i have 3 decorators and an implementation all using the same interface. if you run this code, windsor resolves icommandhandler<stringcommand> as implementation , which, as far as i can tell, is expected behaviour, because the typed implementation can not be registered with the open typed decorators. However, if you uncomment the line container.Register(Component

Python dynamically add decorator to class' methods by decorating class

允我心安 提交于 2019-12-29 03:33:13
问题 say I have a class: class x: def first_x_method(self): print 'doing first_x_method stuff...' def second_x_method(self): print 'doing second_x_method stuff...' and this decorator class logger: @staticmethod def log(func): def wrapped(*args, **kwargs): try: print "Entering: [%s] with parameters %s" % (func.__name__, args) try: return func(*args, **kwargs) except Exception, e: print 'Exception in %s : %s' % (func.__name__, e) finally: print "Exiting: [%s]" % func.__name__ return wrapped how

celery task and customize decorator

拟墨画扇 提交于 2019-12-28 05:51:46
问题 I'm working on a project using django and celery(django-celery). Our team decided to wrap all data access code within (app-name)/manager.py (NOT wrap into Managers like the django way), and let code in (app-name)/task.py only dealing with assemble and perform tasks with celery(so we don't have django ORM dependency in this layer). In my manager.py , I have something like this: def get_tag(tag_name): ctype = ContentType.objects.get_for_model(Photo) try: tag = Tag.objects.get(name=tag_name)

Decorator pattern in C++

▼魔方 西西 提交于 2019-12-28 04:08:31
问题 Can someone give me an example of the Decorator design pattern in C++ ? I have come across the Java version of it, but found it difficult to understand the C++ version of it (from the examples I found). Thanks. 回答1: Vince Huston Design Patterns, even though its layout is poor, has C++ implementation for most design patterns in the Gang of Four book. Click for Decorator. There isn't much difference with Java, except the manual memory handling that you'd better wrap with smart pointers :) 回答2: