Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder3.xlned.com!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; '"""': 0.07; 'none,': 0.07; 'none:': 0.07; '*args,': 0.09; 'none)': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'cheers': 0.12; '"can\'t': 0.16; '"default"': 0.16; '**kwargs)': 0.16; '**kwargs):': 0.16; 'argument,': 0.16; 'kwargs': 0.16; 'placeholder': 0.16; 'string)': 0.16; 'elements': 0.16; 'wrote:': 0.18; 'email addr:gmail.com>': 0.22; 'cc:addr:python.org': 0.22; '(such': 0.24; '(or': 0.24; 'cc:2**0': 0.24; '>': 0.26; 'push': 0.26; 'defined': 0.27; 'skip:_ 20': 0.27; 'header:In- Reply-To:1': 0.27; 'am,': 0.29; 'sets': 0.30; 'message- id:@mail.gmail.com': 0.30; 'assert': 0.31; 'checking': 0.33; 'skip:_ 10': 0.34; 'subject:from': 0.34; 'could': 0.34; 'received:google.com': 0.35; 'object,': 0.36; 'skip:& 10': 0.38; 'skip:& 20': 0.39; 'first': 0.61; 'kind': 0.63; 'to:addr:gmail.com': 0.65; 'andrea': 0.84; '2013': 0.98 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=V+xwTTdceZOsT+oMKIvQlcPrlzxz+9g9HbPSl6pZc4E=; b=ToXDCnDFhuzlG0KQMQapVZJjlpmVowbf+t4Gnl1QQ7YkbHeUkfoYgod233XCHw4x1K 5mshCwePHN4f+XMLjAHpxttHbJZX0jA8DPU5ZjC6/3hIuPGVc6ROjDzcDnlU7xURNE0Q K6Xv62nQA/WZPFc6v3bB/CBTzky6ROuPedFXbsQNOBJ3UE65hTeYoRiyBh+EgcGOvFgA 9uQ6WnceA0dDv/xy6eKTbZ0adOLe3veC6OUZeZP0pxh01qcRi4ag4gGIJCONyyP5Y1fF LiGdA2GKEWeOj8E3GgZhYLrU2t/GGq1Njl3z/06sZo4bFMscoDs3vGoSNRfD2LGfPZlp i9WQ== MIME-Version: 1.0 X-Received: by 10.229.165.207 with SMTP id j15mr4560742qcy.92.1371560398564; Tue, 18 Jun 2013 05:59:58 -0700 (PDT) In-Reply-To: References: Date: Tue, 18 Jun 2013 13:59:58 +0100 Subject: Re: decorator to fetch arguments from global objects From: =?ISO-8859-1?Q?F=E1bio_Santos?= To: andrea crotti Content-Type: multipart/alternative; boundary=f46d04446ac158364704df6d4a18 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: 88 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1371560839 news.xs4all.nl 15884 [2001:888:2000:d::a6]:44964 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:48624 --f46d04446ac158364704df6d4a18 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable On Tue, Jun 18, 2013 at 10:47 AM, andrea crotti wrote: > 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 =3D func.func_code.co_varnames > db =3D 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' =3D=3D func_args[0], "Needs to be the first defined" > else: > db =3D kwargs.get('db', None) > > if db is None: > kwargs['db'] =3D get_current_db() > > assert kwargs['db'] is not None, "Can't have a not defined database" > ret =3D func(*args, **kwargs) > return ret > > return _with_optional_db > If db is always the first argument, you could also use type (or hasattr) checking of the first argument, and push a db if it's not one. Or be specific about it and take a db or some kind of placeholder (such as an object, DEFAULT_DB, or the "default" string) Cheers -- F=E1bio Santos --f46d04446ac158364704df6d4a18 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable

On Tue, Jun 18, 2013 at 10:47 AM, andrea crotti <andrea.crotti.0@gmail.com> wro= te:
> def with_optional_db(func):
> """Decorator that sets the database to the global curre= nt one if
> not passed in or if passed in and None
> """
> @wraps(func)
> def _with_optional_db(*args, **kwargs):
> func_args =3D func.func_code.co_varnames
> db =3D 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' =3D=3D func_args[0], "Needs to be the first d= efined"
> else:
> db =3D kwargs.get('db', None)
>
> if db is None:
> kwargs['db'] =3D get_current_db()
>
> assert kwargs['db'] is not None, "Can't have a not de= fined database"
> ret =3D func(*args, **kwargs)
> return ret
>
> return _with_optional_db
>

If db is always the first argument, you could also use type = (or hasattr) checking of the first argument, and push a db if it's not = one.

Or be specific about it and take a db or some kind of placeh= older (such as an object, DEFAULT_DB, or the "default" string)

Cheers

--
F=E1bio Santos

--f46d04446ac158364704df6d4a18--