Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #48625
| Path | csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!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.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'argument': 0.05; '"""': 0.07; 'args': 0.07; 'none,': 0.07; 'none:': 0.07; 'problem?': 0.07; '*args,': 0.09; 'advice?': 0.09; 'app,': 0.09; 'arguments': 0.09; 'decorator': 0.09; 'logic': 0.09; 'none)': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'sure,': 0.09; 'api': 0.11; 'django': 0.11; 'def': 0.12; '"can\'t': 0.16; '**kwargs)': 0.16; '**kwargs):': 0.16; 'fetch': 0.16; 'kwargs': 0.16; 'magic': 0.16; 'message-id:@post.gmane.org': 0.16; 'positional': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'elements': 0.16; 'exception': 0.16; 'basically': 0.19; 'passing': 0.19; 'header:User-Agent:1': 0.23; '(or': 0.24; 'question': 0.24; 'pass': 0.26; 'defined': 0.27; 'skip:_ 20': 0.27; 'header:X-Complaints-To:1': 0.27; 'function': 0.29; 'raise': 0.29; 'sets': 0.30; "i'm": 0.30; 'work.': 0.31; 'code': 0.31; 'easier': 0.31; 'assert': 0.31; 'enforce': 0.31; 'received:132': 0.31; 'writes:': 0.31; 'class': 0.32; 'there.': 0.32; 'checking': 0.33; 'skip:_ 10': 0.34; 'maybe': 0.34; 'subject:from': 0.34; 'could': 0.34; 'problem': 0.35; 'something': 0.35; 'case,': 0.35; 'but': 0.35; 'there': 0.35; "didn't": 0.36; 'too': 0.37; 'server': 0.38; 'skip:. 20': 0.38; 'to:addr:python-list': 0.38; 'rather': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'easy': 0.60; 'removing': 0.60; 'first': 0.61; 'real': 0.63; 'such': 0.63; 'our': 0.64; 'different': 0.65; 'request.': 0.70; 'below.': 0.71; 'potentially': 0.81; 'andrea': 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 | Tue, 18 Jun 2013 13:25:52 +0000 (UTC) |
| References | <CAF_E5JYg39VdwmLFnyXNOMPRx1UD8ZNGiBRpqgU=A_9rU_ukpw@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.3530.1371561975.3114.python-list@python.org> (permalink) |
| Lines | 70 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1371561975 news.xs4all.nl 15943 [2001:888:2000:d::a6]:49980 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:48625 |
Show key headers only | View raw
andrea crotti <andrea.crotti.0 <at> gmail.com> writes:
>
>
> 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)
> ...
>
> 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
>
> Now it kinda of works already with the decorator below.
> The problem is that the argument is positional so I end up maybe passing
it twice.
> So I have to enforce that 'db' if there is passed as first argument..
>
> It would be a lot easier removing the db from the arguments but then it
would look too magic and I didn't want to change the signature.. any other
advice?
>
> 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
> """
> <at> 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
>
I'm not sure, whether your code would work. I get the logic for the db in
kwargs case, but why are you checking whether db is in func_args? Isn't the
real question whether it's in args ?? In general, I don't understand why you
want to use .func_code.co_varnames here. You know how you defined your
function (or rather method):
class Entity:
def save_doc(db):
...
Maybe I misunderstood the problem?
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-18 13:25 +0000
csiph-web