Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #48646

Re: decorator to fetch arguments from global objects

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 <andrea.crotti.0@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.006
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'none:': 0.07; 'app,': 0.09; 'api': 0.11; 'cc:addr:python-list': 0.11; 'django': 0.11; 'def': 0.12; 'missed': 0.12; 'email addr:udel.edu&gt;': 0.16; 'email name:&lt;tjreedy': 0.16; 'fetch': 0.16; 'reedy': 0.16; 'repeatedly.': 0.16; 'exception': 0.16; 'wrote:': 0.18; 'basically': 0.19; 'written,': 0.19; 'cc:addr:python.org': 0.22; 'decorators': 0.24; 'instance,': 0.24; 'cc:2**0': 0.24; 'class.': 0.26; 'pass': 0.26; 'header:In-Reply- To:1': 0.27; 'function': 0.29; 'am,': 0.29; 'raise': 0.29; 'specified': 0.30; 'message-id:@mail.gmail.com': 0.30; 'class': 0.32; 'there.': 0.32; 'subject:from': 0.34; 'could': 0.34; 'something': 0.35; 'received:google.com': 0.35; 'method': 0.36; 'easily': 0.37; 'server': 0.38; 'does': 0.39; 'itself': 0.39; 'easy': 0.60; 'such': 0.63; 'our': 0.64; 'different': 0.65; 'yes': 0.68; 'request.': 0.70; 'potentially': 0.81; 'andrea': 0.84; 'decorate': 0.84
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=xK1zEGXOH1IYMOT3JfeflrnvnaLZ9YKswZluPVqnRRw=; b=G9HK5oQwaSprWov/ufcoSy87rDwq3o/kqXakHPSco0680cKK9ZsZnizdemta6OQ7z/ a1ELJdVPN/ZFXydLMpNj0s4cIKPRrqitTI06yn+IlI0T2hrHv8hEsETQRFkAU/YD1J8X 7IMRpSRFmVi8lGiMztaklgC8I+CqrDADFWOQnj85osfJTGYpMKV8qKXDs9TZkZG7TLgj efN0clujwewix18u4zjPLsvSWubC1IYLwTLDmGC/nfdIGKuVsR7hNE2MfPSfwdJbYtzK +3i37uMzey5Xhh1/091F3UJsd+A5D7rqkVpwmmDE93mi+shN8Z8p/0+wmLhja1jtbyS8 ZJzw==
MIME-Version 1.0
X-Received by 10.180.98.233 with SMTP id el9mr8210257wib.54.1371576100620; Tue, 18 Jun 2013 10:21:40 -0700 (PDT)
In-Reply-To <kppv2q$shk$1@ger.gmane.org>
References <CAF_E5JYg39VdwmLFnyXNOMPRx1UD8ZNGiBRpqgU=A_9rU_ukpw@mail.gmail.com> <kppv2q$shk$1@ger.gmane.org>
Date Tue, 18 Jun 2013 18:21:40 +0100
Subject Re: decorator to fetch arguments from global objects
From andrea crotti <andrea.crotti.0@gmail.com>
To Terry Reedy <tjreedy@udel.edu>
Content-Type multipart/alternative; boundary=f46d0442889642923c04df70f235
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.3546.1371576102.3114.python-list@python.org> (permalink)
Lines 112
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1371576102 news.xs4all.nl 15953 [2001:888:2000:d::a6]:35631
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:48646

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

2013/6/18 Terry Reedy <tjreedy@udel.edu>

> On 6/18/2013 5:47 AM, andrea crotti wrote:
>
>> 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)
>>
>
> If save_doc does not use an instance of Entity (self) or Entity itself
> (cls), it need not be put in the class.


I missed a self it's a method actually..


>
>
>           ...
>>
>> 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
>>
>
> Decorators are only worthwhile if used repeatedly. What you specified can
> easily be written, for instance, as
>
> def save_doc(db=None):
>   if db is None:
>     db = fetch_from_global()
>   if isinstance(db, dbclass):
>     save_it()
>   else:
>     raise ValueError('need dbobject')
>
>
>
Yes that's exactly why I want a decorator, to avoid all this boilerplate
for every function method that uses a db object..

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: decorator to fetch arguments from global objects andrea crotti <andrea.crotti.0@gmail.com> - 2013-06-18 18:21 +0100

csiph-web