decorator

Having trouble making a custom django view decorator (with args)

做~自己de王妃 提交于 2020-01-12 10:19:45
问题 So I've read all the similar questions and copied what they wrote but I still keep having issues. So I want something like this # Yes, I know django has one but I want to make my own @rate_limit(seconds=10) myview(request, somearg, *args, **kwargs): # Return a response ... def rate_limit(seconds=10): def decorator(view): def wrapper(request, *args, **kwargs): # Do some stuff return view(request, *args, **kwargs) return wrapper return decorator When I run it I get the error decorator() got an

Having trouble making a custom django view decorator (with args)

痴心易碎 提交于 2020-01-12 10:18:08
问题 So I've read all the similar questions and copied what they wrote but I still keep having issues. So I want something like this # Yes, I know django has one but I want to make my own @rate_limit(seconds=10) myview(request, somearg, *args, **kwargs): # Return a response ... def rate_limit(seconds=10): def decorator(view): def wrapper(request, *args, **kwargs): # Do some stuff return view(request, *args, **kwargs) return wrapper return decorator When I run it I get the error decorator() got an

Check if a function uses @classmethod

十年热恋 提交于 2020-01-12 03:26:34
问题 TL;DR How do I find out whether a function was defined using @classmethod or something with the same effect? My problem For implementing a class decorator I would like to check if a method takes the class as its first argument, for example as achieved via @classmethod def function(cls, ...): I found a solution to check for @staticmethod via the types module ( isinstance(foo, types.UnboundMethodType) is False if the foo is static, see here), but did not find anything on how to do so for

How to create a Python class decorator that is able to wrap instance, class and static methods?

回眸只為那壹抹淺笑 提交于 2020-01-11 19:42:06
问题 I'd like to create a Python class decorator (*) that would be able to seamlessly wrap all method types the class might have: instance, class and static. This is the code I have for now, with the parts that break it commented: def wrapItUp(method): def wrapped(*args, **kwargs): print "This method call was wrapped!" return method(*args, **kwargs) return wrapped dundersICareAbout = ["__init__", "__str__", "__repr__"]#, "__new__"] def doICareAboutThisOne(cls, methodName): return (callable(getattr

Implementing a class property that preserves the docstring

為{幸葍}努か 提交于 2020-01-11 09:42:10
问题 I have a descriptor that turns a method into a property on the class level: class classproperty(object): def __init__(self, getter): self.getter = getter self.__doc__ = getter.__doc__ def __get__(self, instance, owner): return self.getter(owner) Used like this: class A(object): @classproperty def test(cls): "docstring" return "Test" However, I now can't access the __doc__ attribute (which is logical, because accessing A.test.__doc__ will fetch the __doc__ of str , because A.test already

Implementing a class property that preserves the docstring

穿精又带淫゛_ 提交于 2020-01-11 09:42:08
问题 I have a descriptor that turns a method into a property on the class level: class classproperty(object): def __init__(self, getter): self.getter = getter self.__doc__ = getter.__doc__ def __get__(self, instance, owner): return self.getter(owner) Used like this: class A(object): @classproperty def test(cls): "docstring" return "Test" However, I now can't access the __doc__ attribute (which is logical, because accessing A.test.__doc__ will fetch the __doc__ of str , because A.test already

Implementing a class property that preserves the docstring

懵懂的女人 提交于 2020-01-11 09:42:08
问题 I have a descriptor that turns a method into a property on the class level: class classproperty(object): def __init__(self, getter): self.getter = getter self.__doc__ = getter.__doc__ def __get__(self, instance, owner): return self.getter(owner) Used like this: class A(object): @classproperty def test(cls): "docstring" return "Test" However, I now can't access the __doc__ attribute (which is logical, because accessing A.test.__doc__ will fetch the __doc__ of str , because A.test already

Python: Passing the default values of function' arguments to *args or **kwargs

耗尽温柔 提交于 2020-01-11 09:32:42
问题 Consider example: def decorator(func): def wrapper(*args, **kwargs): print(args, kwargs) func(*args, **kwargs) return wrapper @decorator def foo(x, y, z=0): pass foo(5, 5) Output: (5, 5) {} Why not (5, 5) {'z': 0} ? How to pass all default values of the function foo to *args or **kwargs using only decorator (for functions) or metaclass (for class methods, e.g. __init__ )? 回答1: The wrapper is just a normal function. It does not have "access" to the internals of the wrapped function. You would

How to reference static method from class variable [duplicate]

人盡茶涼 提交于 2020-01-11 09:08:13
问题 This question already has answers here : Accessing class variables from a list comprehension in the class definition (5 answers) Closed 16 days ago . Given the class from __future__ import annotations from typing import ClassVar, Dict, Final import abc class Cipher(abc.ABC): @abc.abstractmethod def encrypt(self, plaintext: str) -> str: pass @abc.abstractmethod def decrypt(self, ciphertext: str) -> str: pass class VigenereCipher(Cipher): @staticmethod def rotate(n: int) -> str: return string

How to reference static method from class variable [duplicate]

核能气质少年 提交于 2020-01-11 09:07:33
问题 This question already has answers here : Accessing class variables from a list comprehension in the class definition (5 answers) Closed 16 days ago . Given the class from __future__ import annotations from typing import ClassVar, Dict, Final import abc class Cipher(abc.ABC): @abc.abstractmethod def encrypt(self, plaintext: str) -> str: pass @abc.abstractmethod def decrypt(self, ciphertext: str) -> str: pass class VigenereCipher(Cipher): @staticmethod def rotate(n: int) -> str: return string