Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #48624
| 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 | <fabiosantosart@gmail.com> |
| 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 | <CAF_E5JYg39VdwmLFnyXNOMPRx1UD8ZNGiBRpqgU=A_9rU_ukpw@mail.gmail.com> |
| References | <CAF_E5JYg39VdwmLFnyXNOMPRx1UD8ZNGiBRpqgU=A_9rU_ukpw@mail.gmail.com> |
| Date | Tue, 18 Jun 2013 13:59:58 +0100 |
| Subject | Re: decorator to fetch arguments from global objects |
| From | Fábio Santos <fabiosantosart@gmail.com> |
| To | andrea crotti <andrea.crotti.0@gmail.com> |
| Content-Type | multipart/alternative; boundary=f46d04446ac158364704df6d4a18 |
| Cc | python-list <python-list@python.org> |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3529.1371560839.3114.python-list@python.org> (permalink) |
| 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 |
Show key headers only | View raw
[Multipart message — attachments visible in raw view] - view raw
On Tue, Jun 18, 2013 at 10:47 AM, andrea crotti <andrea.crotti.0@gmail.com>
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 = 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
>
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ábio Santos
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: decorator to fetch arguments from global objects Fábio Santos <fabiosantosart@gmail.com> - 2013-06-18 13:59 +0100
csiph-web