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.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>': 0.16; 'email name:<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: References: Date: Tue, 18 Jun 2013 18:21:40 +0100 Subject: Re: decorator to fetch arguments from global objects From: andrea crotti To: Terry Reedy Content-Type: multipart/alternative; boundary=f46d0442889642923c04df70f235 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: 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 --f46d0442889642923c04df70f235 Content-Type: text/plain; charset=ISO-8859-1 2013/6/18 Terry Reedy > 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.. --f46d0442889642923c04df70f235 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable



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:
=A0 =A0 =A0 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..
=A0


=A0 =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

Decorators are only worthwhile if used repeatedly. What you specified can e= asily be written, for instance, as

def save_doc(db=3DNone):
=A0 if db is None:
=A0 =A0 db =3D fetch_from_global()
=A0 if isinstance(db, dbclass):
=A0 =A0 save_it()
=A0 else:
=A0 =A0 raise ValueError('need dbobject')



Yes that's exa= ctly why I want a decorator, to avoid all this boilerplate for every functi= on method that uses a db object..=A0
--f46d0442889642923c04df70f235--