python-decorators

How to Bypass Local Login Screen with Oauth2 and GAE

风流意气都作罢 提交于 2019-12-06 07:21:39
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 not appear to have any bearing on the Google login screen that comes after. I am wondering how to avoid

Pickling decorated callable class wrapper

怎甘沉沦 提交于 2019-12-06 05:25:18
问题 I'm struggling to pickle a wrapped function when I use a custom callable class as a wrapper. I have a callable class "Dependee" that keeps track of dependencies for a wrapped function with a member variable "depends_on". I'd like to use a decorator to wrap functions and also be able to pickle the resulting wrapped function. So I define my dependee class. Note the use of functools.update_wrapper. >>> class Dependee: ... ... def __init__(self, func, depends_on=None): ... self.func = func ...

Python: Monkeypatching a method of an object

我是研究僧i 提交于 2019-12-06 05:09:15
I'm hitting an API in python through requests' Session class. I'm making GET & POST method call using requests.Session(). On every call(GET/POST) failure, I want to notify another process. I can do this by creating a utility method as follows: s = request.Session() def post(): try: s.post(URL,data,headers) except: notify_another_process() And call this method instead of requests.Session().post directly. But, I want to monkeypatch this code to requests.Session().post and want the additional functionality of notifying the other process in the requests.Session().post method call itself. How can I

Decorators for Instance Methods

和自甴很熟 提交于 2019-12-06 04:49:27
Wrapping a class's method in a "boilerplate" Python decorator will treat that method as a regular function and make it lose its __self__ attribute that refers to the class instance object. Can this be avoided? Take the following class: class MyClass(object): def __init__(self, a=1, b=2): self.a = a self.b = b def meth(self): pass If meth() is undecorated, MyClass().meth.__self__ refers to an instance method and enables something like setattr(my_class_object.meth.__self__, 'a', 5) . But when wrapping anything in a decorator, only the function object is passed; the object to which it is actually

Python decorating class

我们两清 提交于 2019-12-06 04:40:58
问题 I'm trying to decorate a class with arguments but cannot get it to work: This is the decorator: def message(param1, param2): def get_message(func): func.__init__(param1,param2) return get_message class where I want to put the decorator @message(param1="testing1", param2="testing2") class SampleClass(object): def __init__(self): pass But this is not working , I am getting an error when running this. Does anyone know what the problem ?, I am trying to create a decorator to initialise classes

Does python allow me to pass dynamic variables to a decorator at runtime?

匆匆过客 提交于 2019-12-06 02:24:56
I am attempting to integrate a very old system and a newer system at work. The best I can do is to utilize an RSS firehouse type feed the system utilizes. The goal is to use this RSS feed to make the other system perform certain actions when certain people do things. My idea is to wrap a decorator around certain functions to check if the user (a user ID provided in the RSS feed) has permissions in the new system. My current solution has a lot of functions that look like this, which are called based on an action field in the feed: actions_dict = { ... 'action1': function1 } actions_dict[RSSFEED

Python: type checking decorator

风格不统一 提交于 2019-12-06 02:21:07
I've built a type checking decorator (with wraps): def accepts_func(*types): """ top-level decoration, consumes parameters """ def decorator(func): """ actual decorator function, consumes the input function """ @wraps(func) def check_accepts(*args): """ actual wrapper which does some magic type-checking """ # check if length of args matches length of specified types assert len(args) == len(types), "{} arguments were passed to func '{}', but only {} " \ "types were passed to decorator '@accepts_func'" \ .format(len(args), func.__name__, len(types)) # check types of arguments for i, arg,

How to use the user_passes_test decorator in class based views?

爱⌒轻易说出口 提交于 2019-12-05 23:30:42
问题 I am trying to check certain conditions before the user is allowed to see a particular user settings page. I am trying to achieve this using the user_passes_test decorator. The function sits in a class based view as follows. I am using method decorator to decorate the get_initial function in the view. class UserSettingsView(LoginRequiredMixin, FormView): success_url = '.' template_name = 'accts/usersettings.html' def get_form_class(self): if self.request.user.profile.is_student: return form1

Functools.update_wrapper() doesn't work properly

不打扰是莪最后的温柔 提交于 2019-12-05 22:17:13
问题 I use Functools.update_wrapper() in my decorator, but It seems like update_wrapper rewrites only function attributes (such as __doc__ , __name__ ), but does not affect on help() function. I aware of these answers, but they don't work with decorator-class. Here is my function. import functools class memoized(object): def __init__(self, func): self.func = func functools.update_wrapper(self, func) def __call__(self, *args): self.func(*args) @memoized def printer(arg): "This is my function" print

How can I send unknown list of arguments to a python decorator?

孤人 提交于 2019-12-05 19:43:00
I have created a python decorator as shown below. I want this decorator to accept an unknown list of arguments. But the code below doesn't work. #!/usr/bin/env python from functools import wraps def my_decorator(decorated_function, **kwargs): print "kwargs = {}".format(kwargs) @wraps(decorated_function) def inner_function(*args, **kwargs): print "Hello world" return decorated_function(*args, **kwargs) return inner_function @my_decorator(arg_1="Yolo", arg2="Bolo") def my_func(): print "Woo Hoo!" my_func() When I run it, I get this error: File "./decorator_test.py", line 14, in <module> @my