Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #48692
| Path | csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!cs.uu.nl!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.001 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'static': 0.04; 'elif': 0.05; 'none:': 0.07; 'app,': 0.09; 'decorator': 0.09; 'method,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'api': 0.11; 'django': 0.11; 'def': 0.12; 'missed': 0.12; 'fetch': 0.16; 'message-id:@post.gmane.org': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'repeatedly.': 0.16; 'exception': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'basically': 0.19; 'written,': 0.19; 'header :User-Agent:1': 0.23; 'decorators': 0.24; 'instance,': 0.24; '(or': 0.24; 'class.': 0.26; 'this:': 0.26; 'pass': 0.26; 'header:X-Complaints-To:1': 0.27; 'function': 0.29; 'am,': 0.29; 'raise': 0.29; 'specified': 0.30; '(which': 0.31; 'code': 0.31; 'received:132': 0.31; 'writes:': 0.31; 'class': 0.32; 'there.': 0.32; 'regular': 0.32; 'subject:from': 0.34; 'could': 0.34; 'problem': 0.35; 'something': 0.35; 'but': 0.35; 'method': 0.36; 'effort': 0.37; 'turn': 0.37; 'easily': 0.37; 'server': 0.38; 'problems': 0.38; 'to:addr:python-list': 0.38; 'does': 0.39; 'itself': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'easy': 0.60; 'simple': 0.61; 'such': 0.63; 'our': 0.64; 'different': 0.65; 'within': 0.65; 'yes': 0.68; 'request.': 0.70; 'potentially': 0.81; 'andrea': 0.84; 'avoids': 0.84; 'clearer': 0.84; 'decorate': 0.84 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> |
| Subject | Re: decorator to fetch arguments from global objects |
| Date | Wed, 19 Jun 2013 07:39:39 +0000 (UTC) |
| References | <CAF_E5JYg39VdwmLFnyXNOMPRx1UD8ZNGiBRpqgU=A_9rU_ukpw@mail.gmail.com> <kppv2q$shk$1@ger.gmane.org> <CAF_E5JZ3afve8WxOS_vmL_i9vC863uVYc723fV4NPAS=xod7vQ@mail.gmail.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=utf-8 |
| Content-Transfer-Encoding | 8bit |
| X-Gmane-NNTP-Posting-Host | sea.gmane.org |
| User-Agent | Loom/3.14 (http://gmane.org/) |
| X-Loom-IP | 132.230.1.31 (Mozilla/5.0 (Windows NT 6.1; rv:21.0) Gecko/20100101 Firefox/21.0) |
| 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.3571.1371627599.3114.python-list@python.org> (permalink) |
| Lines | 62 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1371627599 news.xs4all.nl 15916 [2001:888:2000:d::a6]:33913 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:48692 |
Show key headers only | View raw
andrea crotti <andrea.crotti.0 <at> gmail.com> writes:
>
> 2013/6/18 Terry Reedy <tjreedy <at> 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..
>
The standard way of avoiding boilerplate like this is not to use a
decorator, but a regular function/method that you call from within your
code. Just encapsulate Terry's code into a module-level function (or a
static class method):
def guarantee_dbobject(db):
if db is None:
db = fetch_from_global()
elif not isinstance(db, dbclass):
raise TypeError('need dbobject')
return db
Then use it as a simple one-liner like this:
def save_doc(self, db=None):
db = guarantee_dbobject(db)
...
It's as much effort to use this as decorating the method, but I think it's
clearer and it avoids the potential problems that a decorator causes with
introspection (which might not be a problem now, but could turn into one at
some point).
Wolfgang
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: decorator to fetch arguments from global objects Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2013-06-19 07:39 +0000
csiph-web