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.<br>

<br>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.<br>

<br>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:<br>

<br><blockquote style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;" class="gmail_quote">@acceptsPOST<br>def login(request, user_name, password):<br>   ...<br></blockquote><br>
Instead of:<br>
<br><blockquote style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;" class="gmail_quote">def login(request):<br>    user_name = request.POST['user_name'] if 'user_name' in request.POST else None<br>

    password = request.POST['password'] if 'password' in request.POST else None<br>    ...<br></blockquote><br>This is especially useful both when adding/removing POST variables, and when there end up being a lot of them.<br>

<br>Cheers,<br>Andrey<br><br><br><div class="gmail_quote">On Wed, Sep 9, 2009 at 1:40 PM, David Stanek <span dir="ltr"><<a href="mailto:dstanek@dstanek.com">dstanek@dstanek.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">

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