python-decorators

Google Style Guide properties for getters and setters

拥有回忆 提交于 2020-01-13 09:58:13
问题 I'm curious about one of the recommendations in the Google Python style guide concerning properties. In it, they give the following example: class Square(object): """A square with two properties: a writable area and a read-only perimeter. To use: >>> sq = Square(3) >>> sq.area 9 >>> sq.perimeter 12 >>> sq.area = 16 >>> sq.side 4 >>> sq.perimeter 16 """ def __init__(self, side): self.side = side def __get_area(self): """Calculates the 'area' property.""" return self.side ** 2 def ___get_area

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

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

Preserve default arguments of wrapped/decorated Python function in Sphinx documentation

♀尐吖头ヾ 提交于 2020-01-11 02:10:29
问题 How can I replace *args and **kwargs with the real signature in the documentation of decorated functions? Let's say I have the following decorator and decorated function: import functools def mywrapper(func): @functools.wraps(func) def new_func(*args, **kwargs): print('Wrapping Ho!') return func(*args, **kwargs) return new_func @mywrapper def myfunc(foo=42, bar=43): """Obscure Addition :param foo: bar! :param bar: bla bla :return: foo + bar """ return foo + bar Accordingly, calling print

Flask-Login breaks when my decorator accepts parameters

丶灬走出姿态 提交于 2020-01-06 04:23:48
问题 Thanks to what I learned from this question, I've been able to make a Flask-Login process with a endpoint like this: @app.route('/top_secret') @authorize @login_required def top_secret(): return render_template("top_secret.html") and (for now) a completely pass-through "authorize" decorator: from functools import wraps def authorize(func): @wraps(func) def newfunc(*args, **kwargs): return func(*args, **kwargs) return newfunc The @wraps(func) call allows Flask to find the endpoint that it's

how a function in python is getting called by just typing the name of function and not using brackets

瘦欲@ 提交于 2020-01-06 04:14:23
问题 First of all to find "lcm" of two numbers I made a function lcm(a, b) . Then I thought of finding "hcf" too so I made a decorator decor and defined a function hcf(a, b) in it. And then I returned this function by just typing the name of the function and I didn't put brackets with it but it is still working. I cant understand why this function is working even though I didn't used brackets. def decor(lcm_arg): # just to practice decorators def hcf(a, b): if a > b: a, b = b, a while True: if b %