Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'argument': 0.05; 'position,': 0.05; '"""': 0.07; 'args': 0.07; 'none,': 0.07; 'none:': 0.07; 'problem?': 0.07; '*args,': 0.09; 'advice?': 0.09; 'app,': 0.09; 'arguments': 0.09; 'decorator': 0.09; 'logic': 0.09; 'none)': 0.09; 'sanity': 0.09; 'sure,': 0.09; 'api': 0.11; 'cc:addr:python-list': 0.11; 'django': 0.11; 'def': 0.12; '"can\'t': 0.16; '**kwargs)': 0.16; '**kwargs):': 0.16; 'check.': 0.16; 'fetch': 0.16; 'kwargs': 0.16; 'magic': 0.16; 'positional': 0.16; '\xa0def': 0.16; 'elements': 0.16; 'exception': 0.16; 'bit': 0.19; 'basically': 0.19; 'passing': 0.19; 'cc:addr:python.org': 0.22; 'simpler': 0.24; '(or': 0.24; 'question': 0.24; 'cc:2**0': 0.24; '>': 0.26; 'pass': 0.26; 'least': 0.26; 'defined': 0.27; 'skip:_ 20': 0.27; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'function': 0.29; 'raise': 0.29; 'sets': 0.30; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; 'url:mailman': 0.30; 'work.': 0.31; 'code': 0.31; 'easier': 0.31; 'assert': 0.31; 'enforce': 0.31; 'writes:': 0.31; 'class': 0.32; 'there.': 0.32; 'another': 0.32; 'url:python': 0.33; 'checking': 0.33; 'skip:_ 10': 0.34; 'maybe': 0.34; 'subject:from': 0.34; 'could': 0.34; 'problem': 0.35; 'something': 0.35; 'case,': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'url:listinfo': 0.36; "didn't": 0.36; 'url:org': 0.36; 'too': 0.37; 'server': 0.38; 'skip:& 10': 0.38; 'skip:. 20': 0.38; 'rather': 0.38; 'skip:& 20': 0.39; 'though,': 0.39; 'url:mail': 0.40; 'how': 0.40; 'even': 0.60; 'easy': 0.60; 'removing': 0.60; 'first': 0.61; 'real': 0.63; 'such': 0.63; 'our': 0.64; 'different': 0.65; 'default': 0.69; 'request.': 0.70; 'below.': 0.71; 'potentially': 0.81; 'andrea': 0.84; 'decorate': 0.84; 'good,': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=zWvrPqE4hvuwB3hi6XZ59ivgkoulqiOIvDpIXM5XD5U=; b=gM/wBgpzZNsO1eldI96yDg6nX+ax05w4S9y4rYhIJCXSmK+QTobvuuF2cVFJ1gOktD 2soeZzGm+dCzE2HxBWxjoEMEhEl7YyxC0KzyCBUgHcZSwbx+gdz4o1mSeJKYGMmHJebr raB+bsnYHL5jJWgd9f4923sZaEmxqqgYcZdpz0o6Yk1ASJRnD/QoIsCO69zxo6oZoXSK sECBRr5+aeqKeVqXZfrpkZqaoAKIJYDA7txCdiG0Fza3IxJXMq0N9ErdLQ0NsSI5UGds JVND7iApPtxWdaKYs6FSP6qDKYCDkEWHnhn7L71iUqjVAiPbd8G0MNioL84cvEC46vAy UUrg== MIME-Version: 1.0 X-Received: by 10.194.173.71 with SMTP id bi7mr10903956wjc.2.1371562898067; Tue, 18 Jun 2013 06:41:38 -0700 (PDT) In-Reply-To: References: Date: Tue, 18 Jun 2013 14:41:37 +0100 Subject: Re: decorator to fetch arguments from global objects From: andrea crotti To: Wolfgang Maier Content-Type: multipart/alternative; boundary=089e0112cf9a5396e104df6ddf79 Cc: python-list X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 207 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1371562900 news.xs4all.nl 15882 [2001:888:2000:d::a6]:54271 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:48627 --089e0112cf9a5396e104df6ddf79 Content-Type: text/plain; charset=ISO-8859-1 2013/6/18 Wolfgang Maier > andrea crotti gmail.com> writes: > > > > > > > Using a CouchDB server we have a different database object potentially > for > every request. > > > > We already set that db in the request object to make it easy to pass it > around form our django app, however it would be nice if I could set it once > in the API and automatically fetch it from there. > > > > Basically I have something like > > > > class Entity: > > def save_doc(db) > > ... > > > > I would like basically to decorate this function in such a way that: > > - if I pass a db object use it > > - if I don't pass it in try to fetch it from a global object > > - if both don't exist raise an exception > > > > Now it kinda of works already with the decorator below. > > The problem is that the argument is positional so I end up maybe passing > it twice. > > So I have to enforce that 'db' if there is passed as first argument.. > > > > It would be a lot easier removing the db from the arguments but then it > would look too magic and I didn't want to change the signature.. any other > advice? > > > > def with_optional_db(func): > > """Decorator that sets the database to the global current one if > > not passed in or if passed in and None > > """ > > wraps(func) > > def _with_optional_db(*args, **kwargs): > > func_args = func.func_code.co_varnames > > db = None > > # if it's defined in the first elements it needs to be > > # assigned to *args, otherwise to kwargs > > if 'db' in func_args: > > assert 'db' == func_args[0], "Needs to be the first defined" > > else: > > db = kwargs.get('db', None) > > > > if db is None: > > kwargs['db'] = get_current_db() > > > > assert kwargs['db'] is not None, "Can't have a not defined > database" > > ret = func(*args, **kwargs) > > return ret > > > > return _with_optional_db > > > > I'm not sure, whether your code would work. I get the logic for the db in > kwargs case, but why are you checking whether db is in func_args? Isn't the > real question whether it's in args ?? In general, I don't understand why > you > want to use .func_code.co_varnames here. You know how you defined your > function (or rather method): > class Entity: > def save_doc(db): > ... > Maybe I misunderstood the problem? > Wolfgang > > > > > -- > http://mail.python.org/mailman/listinfo/python-list > Well the point is that I could allow someone to not use "db" as argument of the function if he only wants to use the global db object.. Or at least I want to check that it's the first argument and not in another position, just as a sanity check. I might drop some magic and make it a bit simpler though, even the default argument DEFAULT_DB could be actually good, and I would not even need the decorator at that point.. --089e0112cf9a5396e104df6ddf79 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
2013/6/18 Wolfgang Maier <wolfgang.= maier@biologie.uni-freiburg.de>
andrea crotti <andrea.c= rotti.0 <at> gmail.com= > writes:

>
>
> Using a CouchDB server we have a different database object potentially= for
every request.
>
> We already set that db in the request object to make it easy to pass i= t
around form our django app, however it would be nice if I could set it once=
in the API and automatically fetch it from there.
>
> Basically I have something like
>
> class Entity:
> =A0 =A0 =A0def save_doc(db)
> =A0 =A0 =A0 =A0 ...
>
> I would like basically to decorate this function in such a way that: > - if I pass a db object use it
> - if I don't pass it in try to fetch it from a global object
> - if both don't exist raise an exception
>
> Now it kinda of works already with the decorator below.
> The problem is that the argument is positional so I end up maybe passi= ng
it twice.
> So I have to enforce that 'db' if there is passed as first arg= ument..
>
> It would be a lot easier removing the db from the arguments but then i= t
would look too magic and I didn't want to change the signature.. any ot= her
advice?
>
> def with_optional_db(func):
> =A0 =A0 """Decorator that sets the database to the glob= al current one if
> =A0 =A0 not passed in or if passed in and None
> =A0 =A0 """
> =A0 =A0 =A0<at> wraps(func)
> =A0 =A0 def _with_optional_db(*args, **kwargs):
> =A0 =A0 =A0 =A0 func_args =3D func.func_code.co_varnames
> =A0 =A0 =A0 =A0 db =3D None
> =A0 =A0 =A0 =A0 # if it's defined in the first elements it needs t= o be
> =A0 =A0 =A0 =A0 # assigned to *args, otherwise to kwargs
> =A0 =A0 =A0 =A0 if 'db' in func_args:
> =A0 =A0 =A0 =A0 =A0 =A0 assert 'db' =3D=3D func_args[0], "= ;Needs to be the first defined"
> =A0 =A0 =A0 =A0 else:
> =A0 =A0 =A0 =A0 =A0 =A0 db =3D kwargs.get('db', None)
>
> =A0 =A0 =A0 =A0 if db is None:
> =A0 =A0 =A0 =A0 =A0 =A0 kwargs['db'] =3D get_current_db()
>
> =A0 =A0 =A0 =A0 assert kwargs['db'] is not None, "Can'= ;t have a not defined database"
> =A0 =A0 =A0 =A0 ret =3D func(*args, **kwargs)
> =A0 =A0 =A0 =A0 return ret
>
> =A0 =A0 return _with_optional_db
>

I'm not sure, whether your code would work. I get the logic for t= he db in
kwargs case, but why are you checking whether db is in func_args? Isn't= the
real question whether it's in args ?? In general, I don't understan= d why you
want to use .func_code.co_varnames here. You know how you defined your
function (or rather method):
class Entity:
=A0 =A0 def save_doc(db):
=A0 =A0 =A0 =A0 ...
Maybe I misunderstood the problem?
Wolfgang




--
http://mail.python.org/mailman/listinfo/python-list


<= /div>
Well the point is that I could allow = someone to not use "db" as argument of the function if he only wa= nts to use the global db object..

Or at least I want to check that it's the first argument and not in an= other position, just as a sanity check.

I might drop some magic and make= it a bit simpler though, even the default argument DEFAULT_DB could be act= ually good, and I would not even need the decorator at that point..
--089e0112cf9a5396e104df6ddf79--