Function to apply superset of arguments to a function

Andrey Fedorov anfedorov at gmail.com
Wed Sep 9 20:07:22 CEST 2009


When a web request is made, my Django views are called with argument
`user_id' present if someone is logged in, and set to None if the request is
anonymous. The response varies based on this argument - someone pulling a
team's information will get their relationship to the team if they are
logged in, but not if they are anonymous.

I'd like the views to remain agnostic to this functionality, because most of
them don't care if a particular user is calling them. Hence, I don't want to
include `user_id=None' in all of their definitions. However, to support the
views that *do* differentiate based on user, and to minimize effort required
to go from being user-agnostic to user-specific, I'd like my authentication
mechanism to pass `user_id' (among other things) only if they are present.

I suppose this is an IoC principle or something - where methods declare what
information they need in their argument list. Another example of this
application is in accepting POST variables - I use a decorator @acceptsPOST
which uses `apply_some' to pass all POST variables into a view, and let it
ignore the ones it doesn't need. For example:

@acceptsPOST
> def login(request, user_name, password):
>    ...
>

Instead of:

def login(request):
>     user_name = request.POST['user_name'] if 'user_name' in request.POST
> else None
>     password = request.POST['password'] if 'password' in request.POST else
> None
>     ...
>

This is especially useful both when adding/removing POST variables, and when
there end up being a lot of them.

Cheers,
Andrey


On Wed, Sep 9, 2009 at 1:40 PM, David Stanek <dstanek at dstanek.com> wrote:

> On Wed, Sep 9, 2009 at 12:45 PM, Andrey Fedorov<anfedorov at gmail.com>
> wrote:
> > Hi all,
> >
> > I've written a function [1] called apply_some which takes a set of
> > keywords arguments, filters only those a function is expecting, and
> > calls the function with only those arguments. This is meant to
> > suppress TypeErrors - a way to abstract the logic which checks what
> > arguments a passed-in function accepts.
> >
> > For example:
> >
> >> def foo(x=1, y=2):
> >>    return (x,y)
> >>
> >> apply_some(foo, y=0, z="hi") // calls foo(y=0)
> >> -> (1,0)
> >
> > I'd like to expand this to fill undefined arguments with None, but
> > before I do, does anyone know of any packages/libraries which either
> > do something similar or would make this code cleaner?
> >
> > Cheers,
> > Andrey
> >
> > 1. http://gist.github.com/183375
> > --
> > http://mail.python.org/mailman/listinfo/python-list
> >
>
> What is your use-case for using this? It seems really odd to me.
>
> --
> David
> blog: http://www.traceback.org
> twitter: http://twitter.com/dstanek
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090909/f7d512de/attachment.html>


More information about the Python-list mailing list