Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #33443
| Path | csiph.com!usenet.pasdenom.info!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <bruceg113355@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; 'args': 0.04; "'')": 0.07; 'args)': 0.07; 'arguments': 0.07; 'decorator': 0.07; 'used.': 0.07; 'wrapper': 0.07; '**kwargs)': 0.09; '**kwargs):': 0.09; 'arg': 0.09; 'subject:using': 0.09; 'to:addr:comp.lang.python': 0.09; 'unexpected': 0.09; 'wraps': 0.09; '~ethan~': 0.09; 'cc:addr :python-list': 0.10; 'def': 0.10; 'functools': 0.16; 'subject: \n ': 0.16; 'wrote:': 0.17; 'import': 0.21; 'cc:2**0': 0.23; '15,': 0.23; 'bruce': 0.23; 'cc:no real name:2**0': 0.24; 'tried': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header :User-Agent:1': 0.26; 'extend': 0.26; 'used,': 0.27; 'errors.': 0.27; 'subject:all': 0.29; 'van': 0.29; "skip:' 10": 0.30; 'becomes': 0.30; 'keyword': 0.30; 'thursday,': 0.30; 'error': 0.30; 'code': 0.31; '-----': 0.32; 'print': 0.32; 'handle': 0.33; 'code:': 0.33; 'received:google.com': 0.34; 'skip:k 20': 0.35; 'subject:?': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'add': 0.36; 'but': 0.36; 'received:209': 0.37; 'subject:: ': 0.38; 'from:no real name:2**0': 0.60; 'subject:there': 0.65; 'ethan,': 0.84; 'furman': 0.84; 'subject:before': 0.84; 'ethan': 0.91 |
| Newsgroups | comp.lang.python |
| Date | Fri, 16 Nov 2012 07:00:37 -0800 (PST) |
| In-Reply-To | <mailman.3731.1353039367.27098.python-list@python.org> |
| Complaints-To | groups-abuse@google.com |
| Injection-Info | glegroupsg2000goo.googlegroups.com; posting-host=173.49.62.250; posting-account=cxDMTgoAAAD0NMd7UaWriutT-PIoI910 |
| References | <18134e77-9b02-4aec-afb0-794ed900d194@googlegroups.com> <a8ed6936-0e85-421b-a2a4-96f255e7d76d@googlegroups.com> <mailman.3530.1352538537.27098.python-list@python.org> <k7ls7f$kvr$1@panix5.panix.com> <eee7cb66-b522-4f5c-bbf4-d33d9429f777@googlegroups.com> <k83vsf$nep$1@ger.gmane.org> <mailman.3731.1353039367.27098.python-list@python.org> |
| User-Agent | G2/1.0 |
| X-Google-Web-Client | true |
| X-Google-IP | 173.49.62.250 |
| MIME-Version | 1.0 |
| Subject | Re: Is there a simpler way to modify all arguments in a function before using the arguments? |
| From | bruceg113355@gmail.com |
| To | comp.lang.python@googlegroups.com |
| Content-Type | text/plain; charset=ISO-8859-1 |
| Cc | 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> |
| Message-ID | <mailman.3752.1353078041.27098.python-list@python.org> (permalink) |
| Lines | 97 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1353078041 news.xs4all.nl 6935 [2001:888:2000:d::a6]:43769 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:33443 |
Show key headers only | View raw
On Thursday, November 15, 2012 11:16:08 PM UTC-5, Ethan Furman wrote:
> Emile van Sebille wrote:
>
>
> >
>
> >> Using a decorator works when named arguments are not used. When named
>
> >> arguments are used, unexpected keyword error is reported. Is there a
>
> >> simple fix?
>
> >
>
> > Extend def wrapper(*args) to handle *kwargs as well
>
> >
>
> > Emile
>
> >
>
> >> Code:
>
> >> -----
>
> >>
>
> >> from functools import wraps
>
> >>
>
> >> def fix_args(fn):
>
> >> @wraps(fn)
>
> >> def wrapper(*args):
>
> so this line ^ becomes
>
> def wrapper(*args, **kwargs):
>
> >> args = (arg.replace('_', '') for arg in args)
>
> and add a line
>
> for k, v in kwargs:
>
> kwargs[k] = v.replace('_', '')
>
> >> return fn(*args)
>
> and this line ^ becomes
>
> return fn(*args, **kwargs)
>
> >> return wrapper
>
>
>
> ~Ethan~
Ethan,
I tried you code suggestions but got errors.
However, this works:
from functools import wraps
def fix_args(fn):
@wraps(fn)
def wrapper(*args, **kwargs):
args = (arg.replace('_', '') for arg in args)
for kv in kwargs:
kwargs[kv] = kwargs[kv].replace('_', '')
return fn(*args, **kwargs)
return wrapper
@fix_args
def foo(a1="", a2="", b1="", b2=""):
print(a1)
print(a2)
print(b1)
print(b2)
print ""
foo ('a1a1_x', 'a2a2_x', 'b1b1_x', 'b2b2_____x')
foo (a1='a1a1_x', a2='a2a2_x', b1='b1b1_x', b2='b2b2_____x')
foo ('a1a1_x', 'a2a2_x', b1='b1b1_x', b2='b2b2_____x')
Bruce
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Is there a simpler way to modify all arguments in a function before using the arguments? bruceg113355@gmail.com - 2012-11-09 16:48 -0800
Re: Is there a simpler way to modify all arguments in a function before using the arguments? Roy Smith <roy@panix.com> - 2012-11-09 20:05 -0500
Re: Is there a simpler way to modify all arguments in a function before using the arguments? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-11-10 01:16 +0000
Re: Is there a simpler way to modify all arguments in a function before using the arguments? bruceg113355@gmail.com - 2012-11-10 05:15 -0800
Re: Is there a simpler way to modify all arguments in a function before using the arguments? Chris Angelico <rosuav@gmail.com> - 2012-11-11 05:33 +1100
Re: Is there a simpler way to modify all arguments in a function before using the arguments? Paul Rubin <no.email@nospam.invalid> - 2012-11-09 18:52 -0800
Re: Is there a simpler way to modify all arguments in a function before using the arguments? Chris Angelico <rosuav@gmail.com> - 2012-11-10 14:56 +1100
Re: Is there a simpler way to modify all arguments in a function before using the arguments? Paul Rubin <no.email@nospam.invalid> - 2012-11-09 20:05 -0800
Re: Is there a simpler way to modify all arguments in a function before using the arguments? Chris Angelico <rosuav@gmail.com> - 2012-11-10 20:19 +1100
Re: Is there a simpler way to modify all arguments in a function before using the arguments? Miki Tebeka <miki.tebeka@gmail.com> - 2012-11-09 20:17 -0800
Re: Is there a simpler way to modify all arguments in a function before using the arguments? Peter Otten <__peter__@web.de> - 2012-11-10 10:09 +0100
Re: Is there a simpler way to modify all arguments in a function before using the arguments? aahz@pythoncraft.com (Aahz) - 2012-11-10 07:35 -0800
Re: Is there a simpler way to modify all arguments in a function before using the arguments? bruceg113355@gmail.com - 2012-11-10 09:56 -0800
Re: Is there a simpler way to modify all arguments in a function before using the arguments? Peter Otten <__peter__@web.de> - 2012-11-11 10:28 +0100
Re: Is there a simpler way to modify all arguments in a function before using the arguments? brucegoodstein@gmail.com - 2012-11-15 15:20 -0800
Re: Is there a simpler way to modify all arguments in a function before using the arguments? Emile van Sebille <emile@fenx.com> - 2012-11-15 16:03 -0800
Re: Is there a simpler way to modify all arguments in a function before using the arguments? Ethan Furman <ethan@stoneleaf.us> - 2012-11-15 20:00 -0800
Re: Is there a simpler way to modify all arguments in a function before using the arguments? bruceg113355@gmail.com - 2012-11-16 07:00 -0800
Re: Is there a simpler way to modify all arguments in a function before using the arguments? Ethan Furman <ethan@stoneleaf.us> - 2012-11-16 13:30 -0800
Re: Is there a simpler way to modify all arguments in a function before using the arguments? bruceg113355@gmail.com - 2012-11-16 07:00 -0800
Re: Is there a simpler way to modify all arguments in a function before using the arguments? Steve Howell <showell30@yahoo.com> - 2012-11-11 09:45 -0800
csiph-web