python-decorators

Can't call a decorator within the imported sub-class of a cherrpy application (site tree)

孤人 提交于 2020-01-24 00:27:09
问题 I am using cherrypy as a web server, and I want to check a user's logged-in status before returning the page. This works on methods in the main Application class (in site.py ) but gives an error when I call the same decorated function on method in a class that is one layer deeper in the webpage tree (in a separate file). validate_user() is the function used as a decorator. It either passes a user to the page or sends them to a 401 restricted page, as a cherrypy.Tool , like this: from user

Can't call a decorator within the imported sub-class of a cherrpy application (site tree)

只谈情不闲聊 提交于 2020-01-24 00:27:07
问题 I am using cherrypy as a web server, and I want to check a user's logged-in status before returning the page. This works on methods in the main Application class (in site.py ) but gives an error when I call the same decorated function on method in a class that is one layer deeper in the webpage tree (in a separate file). validate_user() is the function used as a decorator. It either passes a user to the page or sends them to a 401 restricted page, as a cherrypy.Tool , like this: from user

functools.wraps equivalent for class decorator

戏子无情 提交于 2020-01-22 14:15:32
问题 When we decorate function, we use functools.wraps to make decorated function look like original. Is there any wat to do same, when we want to decorate class? def some_class_decorator(cls_to_decorate): class Wrapper(cls_to_decorate): """Some Wrapper not important doc.""" pass return Wrapper @some_class_decorator class MainClass: """MainClass important doc.""" pass help(MainClass) Output: class Wrapper(MainClass) | Some Wrapper not important doc. | | Method resolution order: | Wrapper |

How would one decorate an inherited method in the child class?

余生长醉 提交于 2020-01-22 14:12:08
问题 I'm not quite sure how to use a decorator on an inherited method. Normally decorators are put before the definition but for an inherited function the definition is given in the parent and not the child class. I would like to avoid rewriting the definition of the method in the child class and simply specify to put the decorator around the inherited method. To make myself clearer, here is a working example of what I mean: class Person(): def __init__(self, age): self.age = age @classmethod def

How would one decorate an inherited method in the child class?

Deadly 提交于 2020-01-22 14:11:35
问题 I'm not quite sure how to use a decorator on an inherited method. Normally decorators are put before the definition but for an inherited function the definition is given in the parent and not the child class. I would like to avoid rewriting the definition of the method in the child class and simply specify to put the decorator around the inherited method. To make myself clearer, here is a working example of what I mean: class Person(): def __init__(self, age): self.age = age @classmethod def

Can someone explain how the source code of staticmethod works in python

懵懂的女人 提交于 2020-01-22 13:23:07
问题 First of all, I understand how, in general, a decorator work. And I know @staticmethod strips off the instance argument in the signature, making class C(object): @staticmethod def foo(): print 'foo' C.foo //<function foo at 0x10efd4050> C().foo //<function foo at 0x10efd4050> valid. However, I don't understand how the sourcec code of staticmethod make this happen. It seems to me that when wrapping method foo in staticmethod , an instance of staticmethod is instantiated, then some magic

Timing decorator is raising “'NoneType' object is not callable” exception

谁说胖子不能爱 提交于 2020-01-14 19:52:55
问题 I have i timing function and my main function. When i use only main function it runs fine, but when i use timing function as a decorator it raises an exception. Timing function code: def timing(function): import time t = time.time() function() t = time.time() - t print('Program has been running for {} seconds.'.format(t)) I use it like this: @timing def main(): #some code 回答1: The decorator needs to return the decorated function: def timing(function): def wrapped(): import time t = time.time(

Timing decorator is raising “'NoneType' object is not callable” exception

本秂侑毒 提交于 2020-01-14 19:52:18
问题 I have i timing function and my main function. When i use only main function it runs fine, but when i use timing function as a decorator it raises an exception. Timing function code: def timing(function): import time t = time.time() function() t = time.time() - t print('Program has been running for {} seconds.'.format(t)) I use it like this: @timing def main(): #some code 回答1: The decorator needs to return the decorated function: def timing(function): def wrapped(): import time t = time.time(

Celery task with multiple decorators not auto registering task name

给你一囗甜甜゛ 提交于 2020-01-14 07:29:12
问题 I'm having a task that looks like this from mybasetask_module import MyBaseTask @task(base=MyBaseTask) @my_custom_decorator def my_task(*args, **kwargs): pass and my base task looks like this from celery import task, Task class MyBaseTask(Task): abstract = True default_retry_delay = 10 max_retries = 3 acks_late = True The problem I'm running into is that the celery worker is registering the task with the name 'mybasetask_module.__inner' The task is registerd fine (which is the package+module

Python set docstring and get method name of dynamically generated classmethod

一笑奈何 提交于 2020-01-14 03:28:26
问题 I'm trying to get/set the name and docstring of dynamically created class methods as follows, but am having trouble figuring out exactly how to do it: import sys import inspect class test(object): pass @classmethod def genericFunc(cls, **kwargs): print "function:", (inspect.stack()[0][3]) print "kwargs:", kwargs function_list = ['myF1', 'myF2'] for func in function_list: setattr(test, func, genericFunc) #set docstring for func here? if __name__ == '__main__': x = test() print "docstring:", x