python-decorators

How to access decorator attributes?

亡梦爱人 提交于 2020-03-22 05:51:29
问题 Is it possible to access decorator attributes in Python 3? For example: is it possible to access self.misses after the call to the decorated fibonacci method? class Cache: def __init__(self, func): self.func = func self.cache = {} self.misses = 0 def __call__(self, *args): if not (args in self.cache): self.misses += 1 self.cache[args] = self.func(*args) return self.cache[args] @Cache def fibonacci(n): return n if n in (0, 1) else fibonacci(n - 1) + fibonacci(n - 2) fibonacci(20) ### now we

How can the class instance be accessed from a decorated method at runtime?

北慕城南 提交于 2020-03-06 09:36:06
问题 I am trying to write a python decorator, that allows tracking an integer property in a class (which is responsible for a model run). By tracking I mean adding every change to a local database my_db.db . This works well with the given code below, as you can also be seen in the screenshot of the database entries. The problem I have is that I cannot differentiate between the integer properties of two instances of the MockModel . I created the field instance_id in order to differentiate between

How to use pytest capsys on tests that have mocking decorators?

自闭症网瘾萝莉.ら 提交于 2020-02-24 14:40:13
问题 I have being trying to find a way to use mocking decorators and pytest capsys at the same time but I wasn't able to find the right way to do it. import pytest import requests_mock @requests_mock.mock() def test_with_mock(m): pass def test_with_capsys(capsys): pass # how to write a test that works with both? 回答1: As stated in the request-mock's docs: pytest has its own method of registering and loading custom fixtures. requests-mock provides an external fixture registered with pytest such that

Python subclass method to inherit decorator from superclass method

你说的曾经没有我的故事 提交于 2020-02-03 19:52:39
问题 I have a superclass that has a retrieve() method, and its subclasses each implement their own retrieve() method. I'd like every retrieve() method to be decorated to cache the return value when it receive the same args, without having to decorate the method in every subclass. Decorators don't seem to be inherited. I could probably call the superclass's method which would in turn set the cache, but currently my superclass raises a NotImplemented exception, which I like. import json import

Python subclass method to inherit decorator from superclass method

江枫思渺然 提交于 2020-02-03 19:50:03
问题 I have a superclass that has a retrieve() method, and its subclasses each implement their own retrieve() method. I'd like every retrieve() method to be decorated to cache the return value when it receive the same args, without having to decorate the method in every subclass. Decorators don't seem to be inherited. I could probably call the superclass's method which would in turn set the cache, but currently my superclass raises a NotImplemented exception, which I like. import json import

Python subclass method to inherit decorator from superclass method

旧时模样 提交于 2020-02-03 19:49:57
问题 I have a superclass that has a retrieve() method, and its subclasses each implement their own retrieve() method. I'd like every retrieve() method to be decorated to cache the return value when it receive the same args, without having to decorate the method in every subclass. Decorators don't seem to be inherited. I could probably call the superclass's method which would in turn set the cache, but currently my superclass raises a NotImplemented exception, which I like. import json import

How to Bypass Local Login Screen with Oauth2 and GAE

我怕爱的太早我们不能终老 提交于 2020-02-02 12:31:11
问题 I am working with the Oauth2 Decorator provided by Google. Right now I am just trying to do a very simple login via Oauth2 to Google using GAE. I am running locally for test purposes and have been successful in authenticating with Google; however, prior to the Google screen for authentication it always presents me with a local login screen running on localhost (//localhost:14080/_ah/login?continue=http%3A//localhost%3A14080/). I am not sure why I am getting this local login screen which does

Mocking render to response with Pyramid

 ̄綄美尐妖づ 提交于 2020-01-24 22:07:55
问题 I have a decorator that looks like so: def validate_something(func): def validate_s(request): if request.property: render_to_response('template.jinja', 'error' return func(request) return validate_something I'm trying to test it like so. I load the local WSGI stack as an app. from webtest import TestApp def setUp(self): self.app = TestApp(target_app()) self.config = testing.setUp(request=testing.DummyRequest) def test_something(self): def test_func(request): return 1 request = testing

Mocking render to response with Pyramid

本小妞迷上赌 提交于 2020-01-24 22:06:15
问题 I have a decorator that looks like so: def validate_something(func): def validate_s(request): if request.property: render_to_response('template.jinja', 'error' return func(request) return validate_something I'm trying to test it like so. I load the local WSGI stack as an app. from webtest import TestApp def setUp(self): self.app = TestApp(target_app()) self.config = testing.setUp(request=testing.DummyRequest) def test_something(self): def test_func(request): return 1 request = testing

Check if a function was called as a decorator

别说谁变了你拦得住时间么 提交于 2020-01-24 03:32:50
问题 In the following minimal example decorate is called two times. First using @decorate , second by normal function call decorate(bar) . def decorate(func): print(func.__name__) return func @decorate def bar(): pass decorate(bar) Is it possible to see inside of decorate if the call was invoked by using @decorate or as a normal function call? 回答1: The @decorator syntax is just syntactic sugar, thus both examples have identical behaviour. This also means whatever distinction you are doing between