Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #33067 > unrolled thread
| Started by | bruceg113355@gmail.com |
|---|---|
| First post | 2012-11-09 16:48 -0800 |
| Last post | 2012-11-11 09:45 -0800 |
| Articles | 20 on this page of 21 — 12 participants |
Back to article view | Back to comp.lang.python
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
Page 1 of 2 [1] 2 Next page →
| From | bruceg113355@gmail.com |
|---|---|
| Date | 2012-11-09 16:48 -0800 |
| Subject | Is there a simpler way to modify all arguments in a function before using the arguments? |
| Message-ID | <18134e77-9b02-4aec-afb0-794ed900d194@googlegroups.com> |
Is there a simpler way to modify all arguments in a function before using the arguments?
For example, can the below code, in the modify arguments section be made into a few statements?
def someComputation (aa, bb, cc, dd, ee, ff, gg, hh):
# modify arguments
# ----------------------
aa = aa.replace (“_” , “”)
bb= bb.replace (“_” , “”)
cc = cc.replace (“_” , “”)
dd = dd.replace (“_” , “”)
ee = ee.replace (“_” , “”)
ff = ff.replace (“_” , “”)
gg = gg.replace (“_” , “”)
hh = hh.replace (“_” , “”)
# use the arguments
# -----------------
# …
[toc] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2012-11-09 20:05 -0500 |
| Message-ID | <roy-209335.20052609112012@news.panix.com> |
| In reply to | #33067 |
In article <18134e77-9b02-4aec-afb0-794ed900d194@googlegroups.com>,
bruceg113355@gmail.com wrote:
> Is there a simpler way to modify all arguments in a function before using the
> arguments?
>
> For example, can the below code, in the modify arguments section be made into
> a few statements?
>
> def someComputation (aa, bb, cc, dd, ee, ff, gg, hh):
> # modify arguments
> # ----------------------
> aa = aa.replace (і_І , іІ)
> bb= bb.replace (і_І , іІ)
> cc = cc.replace (і_І , іІ)
> dd = dd.replace (і_І , іІ)
> ee = ee.replace (і_І , іІ)
> ff = ff.replace (і_І , іІ)
> gg = gg.replace (і_І , іІ)
> hh = hh.replace (і_І , іІ)
>
> # use the arguments
> # -----------------
> # Љ
You could do something like (not error checked)...
def someComputation(*args):
new_args = [arg.replace("_", "") for arg in args]
aa, bb, cc, dd, ee, ff, gg, hh = new_args
but that's pretty weird. I suspect you just want to pass a list instead
of a bunch of discrete arguments.
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2012-11-10 01:16 +0000 |
| Message-ID | <509daadc$0$29980$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #33069 |
On Fri, 09 Nov 2012 20:05:26 -0500, Roy Smith wrote:
> In article <18134e77-9b02-4aec-afb0-794ed900d194@googlegroups.com>,
> bruceg113355@gmail.com wrote:
>
>> Is there a simpler way to modify all arguments in a function before
>> using the arguments?
>>
>> For example, can the below code, in the modify arguments section be
>> made into a few statements?
>>
>> def someComputation (aa, bb, cc, dd, ee, ff, gg, hh):
>> # modify arguments
>> # ----------------------
>> aa = aa.replace (³_² , ³²)
>> bb= bb.replace (³_² , ³²)
>> cc = cc.replace (³_² , ³²)
>> dd = dd.replace (³_² , ³²)
>> ee = ee.replace (³_² , ³²)
>> ff = ff.replace (³_² , ³²)
>> gg = gg.replace (³_² , ³²)
>> hh = hh.replace (³_² , ³²)
>>
>> # use the arguments
>> # -----------------
>> #
>
> You could do something like (not error checked)...
>
> def someComputation(*args):
> new_args = [arg.replace("_", "") for arg in args] aa, bb, cc, dd,
> ee, ff, gg, hh = new_args
>
> but that's pretty weird. I suspect you just want to pass a list instead
> of a bunch of discrete arguments.
I agree with everything you say except that it is pretty weird. As far as
I am concerned, it isn't weird at all.
If you need named parameters:
def someComputation(aa, bb, cc, dd, ee, ff, gg, hh):
aa, bb, cc, dd, ee, ff, gg, hh = [arg.replace("_", "")
for arg in (aa. bb, cc, dd, ee, ff, gg, hh)]
...
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | bruceg113355@gmail.com |
|---|---|
| Date | 2012-11-10 05:15 -0800 |
| Message-ID | <8e17124b-63ee-4166-8964-80f47f5716d5@googlegroups.com> |
| In reply to | #33070 |
On Friday, November 9, 2012 8:16:12 PM UTC-5, Steven D'Aprano wrote:
> On Fri, 09 Nov 2012 20:05:26 -0500, Roy Smith wrote:
>
>
>
> > In article <18134e77-9b02-4aec-afb0-794ed900d194@googlegroups.com>,
>
> > bruceg113355@gmail.com wrote:
>
> >
>
> >> Is there a simpler way to modify all arguments in a function before
>
> >> using the arguments?
>
> >>
>
> >> For example, can the below code, in the modify arguments section be
>
> >> made into a few statements?
>
> >>
>
> >> def someComputation (aa, bb, cc, dd, ee, ff, gg, hh):
>
> >> # modify arguments
>
> >> # ----------------------
>
> >> aa = aa.replace (³_² , ³²)
>
> >> bb= bb.replace (³_² , ³²)
>
> >> cc = cc.replace (³_² , ³²)
>
> >> dd = dd.replace (³_² , ³²)
>
> >> ee = ee.replace (³_² , ³²)
>
> >> ff = ff.replace (³_² , ³²)
>
> >> gg = gg.replace (³_² , ³²)
>
> >> hh = hh.replace (³_² , ³²)
>
> >>
>
> >> # use the arguments
>
> >> # -----------------
>
> >> # Š
>
> >
>
> > You could do something like (not error checked)...
>
> >
>
> > def someComputation(*args):
>
> > new_args = [arg.replace("_", "") for arg in args] aa, bb, cc, dd,
>
> > ee, ff, gg, hh = new_args
>
> >
>
> > but that's pretty weird. I suspect you just want to pass a list instead
>
> > of a bunch of discrete arguments.
>
>
>
>
>
> I agree with everything you say except that it is pretty weird. As far as
>
> I am concerned, it isn't weird at all.
>
>
>
> If you need named parameters:
>
>
>
> def someComputation(aa, bb, cc, dd, ee, ff, gg, hh):
>
> aa, bb, cc, dd, ee, ff, gg, hh = [arg.replace("_", "")
>
> for arg in (aa. bb, cc, dd, ee, ff, gg, hh)]
>
> ...
>
>
>
>
>
>
>
> --
>
> Steven
Thanks to all.
Steve's example is the one I will try next week.
Passing in lists, will work but it requires extra coding from the calling routines to build the list.
Discrete arguments make sense.
Also, what is the problem passing in 7 or more arguments?
Thanks,
Bruce
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2012-11-11 05:33 +1100 |
| Message-ID | <mailman.3538.1352572887.27098.python-list@python.org> |
| In reply to | #33086 |
On Sun, Nov 11, 2012 at 12:15 AM, <bruceg113355@gmail.com> wrote:
> Thanks to all.
> Steve's example is the one I will try next week.
> Passing in lists, will work but it requires extra coding from the calling routines to build the list.
Not necessarily! Watch:
def foo(*args):
print(repr(args))
foo("Hello","world","!")
('Hello', 'world', '!')
Okay, that's not technically a list, it's a tuple, but same diff. Your
callers still see you as taking separate arguments, but you take them
as a single collection.
ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Paul Rubin <no.email@nospam.invalid> |
|---|---|
| Date | 2012-11-09 18:52 -0800 |
| Message-ID | <7xlieadv9o.fsf@ruckus.brouhaha.com> |
| In reply to | #33067 |
bruceg113355@gmail.com writes:
> Is there a simpler way to modify all arguments in a function before
> using the arguments?
Why do you want to do that?
> For example, can the below code, in the modify arguments section be
> made into a few statements?
Whenever someone uses that many variables one always has to ask whether
a table would be better. But, for
> def someComputation (aa, bb, cc, dd, ee, ff, gg, hh):
> # modify arguments
> # ----------------------
> aa = aa.replace (“_” , “”)
> bb= bb.replace (“_” , “”)
> cc = cc.replace (“_” , “”)
> dd = dd.replace (“_” , “”)
> ee = ee.replace (“_” , “”)
> ff = ff.replace (“_” , “”)
> gg = gg.replace (“_” , “”)
> hh = hh.replace (“_” , “”)
you could write (untested):
def someComputation (aa, bb, cc, dd, ee, ff, gg, hh):
def modify(s): return s.replace('_', '')
aa,bb,cc,dd,ee,ff,gg,hh = \
map(modify,[aa,bb,cc,dd,ee,ff,gg,hh])
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2012-11-10 14:56 +1100 |
| Message-ID | <mailman.3529.1352519803.27098.python-list@python.org> |
| In reply to | #33071 |
On Sat, Nov 10, 2012 at 1:52 PM, Paul Rubin <no.email@nospam.invalid> wrote:
> bruceg113355@gmail.com writes:
>> Is there a simpler way to modify all arguments in a function before
>> using the arguments?
>
> Why do you want to do that?
>
Contrived example:
def send_email(from, to, subj, body, whatever, other, headers, you, like):
# Okay, now translate all those into the appropriate encoding and
with special characters escaped
# We need to translate each one separately so that, for instance,
a newline in the subject won't let you create additional headers
ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Paul Rubin <no.email@nospam.invalid> |
|---|---|
| Date | 2012-11-09 20:05 -0800 |
| Message-ID | <7x390iw19i.fsf@ruckus.brouhaha.com> |
| In reply to | #33072 |
Chris Angelico <rosuav@gmail.com> writes: > Contrived example: > def send_email(from, to, subj, body, whatever, other, headers, you, like): That should be a dictionary with the header names as indexes. In fact there are already some email handling modules in the stdlib that represent headers that way.
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2012-11-10 20:19 +1100 |
| Message-ID | <mailman.3532.1352539154.27098.python-list@python.org> |
| In reply to | #33073 |
On Sat, Nov 10, 2012 at 3:05 PM, Paul Rubin <no.email@nospam.invalid> wrote: > Chris Angelico <rosuav@gmail.com> writes: >> Contrived example: >> def send_email(from, to, subj, body, whatever, other, headers, you, like): > > That should be a dictionary with the header names as indexes. In fact > there are already some email handling modules in the stdlib that > represent headers that way. That's also plausible, but keyword arguments do make sense. And this was a top-of-the-head contrived example; I'm sure there are plenty of good use-cases. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Miki Tebeka <miki.tebeka@gmail.com> |
|---|---|
| Date | 2012-11-09 20:17 -0800 |
| Message-ID | <a8ed6936-0e85-421b-a2a4-96f255e7d76d@googlegroups.com> |
| In reply to | #33067 |
> Is there a simpler way to modify all arguments in a function before using the arguments?
You can use a decorator:
from functools import wraps
def fix_args(fn):
@wraps(fn)
def wrapper(*args):
args = (arg.replace('_', '') for arg in args)
return fn(*args)
return wrapper
@fix_args
def foo(x, y):
print(x)
print(y)
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2012-11-10 10:09 +0100 |
| Message-ID | <mailman.3530.1352538537.27098.python-list@python.org> |
| In reply to | #33074 |
Miki Tebeka wrote:
>> Is there a simpler way to modify all arguments in a function before using
>> the arguments?
> You can use a decorator:
>
> from functools import wraps
>
> def fix_args(fn):
> @wraps(fn)
> def wrapper(*args):
> args = (arg.replace('_', '') for arg in args)
> return fn(*args)
>
> return wrapper
>
> @fix_args
> def foo(x, y):
> print(x)
> print(y)
I was tempted to post that myself, but he said /simpler/ ;)
[toc] | [prev] | [next] | [standalone]
| From | aahz@pythoncraft.com (Aahz) |
|---|---|
| Date | 2012-11-10 07:35 -0800 |
| Message-ID | <k7ls7f$kvr$1@panix5.panix.com> |
| In reply to | #33080 |
In article <mailman.3530.1352538537.27098.python-list@python.org>,
Peter Otten <__peter__@web.de> wrote:
>Miki Tebeka wrote:
>
>>> Is there a simpler way to modify all arguments in a function before using
>>> the arguments?
>>
>> You can use a decorator:
>>
>> from functools import wraps
>>
>> def fix_args(fn):
>> @wraps(fn)
>> def wrapper(*args):
>> args = (arg.replace('_', '') for arg in args)
>> return fn(*args)
>>
>> return wrapper
>>
>> @fix_args
>> def foo(x, y):
>> print(x)
>> print(y)
>
>I was tempted to post that myself, but he said /simpler/ ;)
From my POV, that *is* simpler. When you change the parameters for foo,
you don't need to change the arg pre-processing. Also allows code reuse,
probably any program needing this kind of processing once will need it
again.
--
Aahz (aahz@pythoncraft.com) <*> http://www.pythoncraft.com/
"....Normal is what cuts off your sixth finger and your tail..." --Siobhan
[toc] | [prev] | [next] | [standalone]
| From | bruceg113355@gmail.com |
|---|---|
| Date | 2012-11-10 09:56 -0800 |
| Message-ID | <1f3e6587-3b7d-4e30-bbaa-42c30df83475@googlegroups.com> |
| In reply to | #33088 |
All,
I never used decorators before. I saw Miki Tebeka's sample code and your rationale (Aahz) and I like it. For my application problem, decorators seem like a good solution.
Thanks to all,
Bruce
On Saturday, November 10, 2012 10:35:12 AM UTC-5, Aahz wrote:
> In article <mailman.3530.1352538537.27098.python-list@python.org>,
>
> Peter Otten <__peter__@web.de> wrote:
>
> >Miki Tebeka wrote:
>
> >
>
> >>> Is there a simpler way to modify all arguments in a function before using
>
> >>> the arguments?
>
> >>
>
> >> You can use a decorator:
>
> >>
>
> >> from functools import wraps
>
> >>
>
> >> def fix_args(fn):
>
> >> @wraps(fn)
>
> >> def wrapper(*args):
>
> >> args = (arg.replace('_', '') for arg in args)
>
> >> return fn(*args)
>
> >>
>
> >> return wrapper
>
> >>
>
> >> @fix_args
>
> >> def foo(x, y):
>
> >> print(x)
>
> >> print(y)
>
> >
>
> >I was tempted to post that myself, but he said /simpler/ ;)
>
>
>
> From my POV, that *is* simpler. When you change the parameters for foo,
>
> you don't need to change the arg pre-processing. Also allows code reuse,
>
> probably any program needing this kind of processing once will need it
>
> again.
>
> --
>
> Aahz (aahz@pythoncraft.com) <*> http://www.pythoncraft.com/
>
>
>
> "....Normal is what cuts off your sixth finger and your tail..." --Siobhan
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2012-11-11 10:28 +0100 |
| Message-ID | <mailman.3556.1352626145.27098.python-list@python.org> |
| In reply to | #33088 |
Aahz wrote:
> In article <mailman.3530.1352538537.27098.python-list@python.org>,
> Peter Otten <__peter__@web.de> wrote:
>>Miki Tebeka wrote:
>>
>>>> Is there a simpler way to modify all arguments in a function before
>>>> using the arguments?
>>>
>>> You can use a decorator:
>>>
>>> from functools import wraps
>>>
>>> def fix_args(fn):
>>> @wraps(fn)
>>> def wrapper(*args):
>>> args = (arg.replace('_', '') for arg in args)
>>> return fn(*args)
>>>
>>> return wrapper
>>>
>>> @fix_args
>>> def foo(x, y):
>>> print(x)
>>> print(y)
>>
>>I was tempted to post that myself, but he said /simpler/ ;)
>
> From my POV, that *is* simpler. When you change the parameters for foo,
> you don't need to change the arg pre-processing. Also allows code reuse,
> probably any program needing this kind of processing once will need it
> again.
Typical changes would be
@fix_args
def bar(x, y=None):
print(x)
print(y)
@fix_args
def baz(file, x, y):
print(s, file=file)
Do you find it obvious what
bar("a_b")
bar("a_b", y="c_d")
print? Do you find the traceback produced by the latter helpful?
Moving complexity into a helper function often makes client code simpler
because if the helper is well-tested and preferrably maintained by someone
else the part that you have to deal with becomes simpler, but the overall
complexity still increases.
A fix_args() decorator is worthwhile only if you need it more than once or
twice, and because it is hard to generalise I expect that yagni.
[toc] | [prev] | [next] | [standalone]
| From | brucegoodstein@gmail.com |
|---|---|
| Date | 2012-11-15 15:20 -0800 |
| Message-ID | <eee7cb66-b522-4f5c-bbf4-d33d9429f777@googlegroups.com> |
| In reply to | #33088 |
On Saturday, November 10, 2012 10:35:12 AM UTC-5, Aahz wrote:
> In article <mailman.3530.1352538537.27098.python-list@python.org>,
>
> Peter Otten <__peter__@web.de> wrote:
>
> >Miki Tebeka wrote:
>
> >
>
> >>> Is there a simpler way to modify all arguments in a function before using
>
> >>> the arguments?
>
> >>
>
> >> You can use a decorator:
>
> >>
>
> >> from functools import wraps
>
> >>
>
> >> def fix_args(fn):
>
> >> @wraps(fn)
>
> >> def wrapper(*args):
>
> >> args = (arg.replace('_', '') for arg in args)
>
> >> return fn(*args)
>
> >>
>
> >> return wrapper
>
> >>
>
> >> @fix_args
>
> >> def foo(x, y):
>
> >> print(x)
>
> >> print(y)
>
> >
>
> >I was tempted to post that myself, but he said /simpler/ ;)
>
>
>
> From my POV, that *is* simpler. When you change the parameters for foo,
>
> you don't need to change the arg pre-processing. Also allows code reuse,
>
> probably any program needing this kind of processing once will need it
>
> again.
>
> --
>
> Aahz (aahz@pythoncraft.com) <*> http://www.pythoncraft.com/
>
>
>
> "....Normal is what cuts off your sixth finger and your tail..." --Siobhan
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?
Thanks to all,
Bruce
Code:
-----
from functools import wraps
def fix_args(fn):
@wraps(fn)
def wrapper(*args):
args = (arg.replace('_', '') for arg in args)
return fn(*args)
return wrapper
@fix_args
def foo(a1="", a2="", b1="", b2=""):
print(a1)
print(a2)
print(b1)
print(b2)
foo ('a1a1_x', 'a2a2_x', 'b1b1_x', 'b2b2_____x')
foo (a1='a1a1_x', a2='a2a2_x', b1='b1b1_x', b2='b2b2_____x')
Results:
--------
a1a1x
a2a2x
b1b1x
b2b2x
Traceback (most recent call last):
File "C:\WORK\masterDB_Update\argtest.py", line 19, in <module>
foo (a1='a1a1_x', a2='a2a2_x', b1='b1b1_x', b2='b2b2_____x')
TypeError: wrapper() got an unexpected keyword argument 'a1'
[toc] | [prev] | [next] | [standalone]
| From | Emile van Sebille <emile@fenx.com> |
|---|---|
| Date | 2012-11-15 16:03 -0800 |
| Message-ID | <mailman.3730.1353024223.27098.python-list@python.org> |
| In reply to | #33405 |
brucegoodstein@gmail.com 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):
> args = (arg.replace('_', '') for arg in args)
> return fn(*args)
> return wrapper
>
> @fix_args
> def foo(a1="", a2="", b1="", b2=""):
> print(a1)
> print(a2)
> print(b1)
> print(b2)
>
> foo ('a1a1_x', 'a2a2_x', 'b1b1_x', 'b2b2_____x')
> foo (a1='a1a1_x', a2='a2a2_x', b1='b1b1_x', b2='b2b2_____x')
>
> Results:
> --------
> a1a1x
> a2a2x
> b1b1x
> b2b2x
> Traceback (most recent call last):
> File "C:\WORK\masterDB_Update\argtest.py", line 19, in <module>
> foo (a1='a1a1_x', a2='a2a2_x', b1='b1b1_x', b2='b2b2_____x')
> TypeError: wrapper() got an unexpected keyword argument 'a1'
[toc] | [prev] | [next] | [standalone]
| From | Ethan Furman <ethan@stoneleaf.us> |
|---|---|
| Date | 2012-11-15 20:00 -0800 |
| Message-ID | <mailman.3731.1353039367.27098.python-list@python.org> |
| In reply to | #33405 |
Emile van Sebille wrote:
> brucegoodstein@gmail.com 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~
[toc] | [prev] | [next] | [standalone]
| From | bruceg113355@gmail.com |
|---|---|
| Date | 2012-11-16 07:00 -0800 |
| Message-ID | <211b323b-1fd4-4018-a7e5-34c6fbda01a5@googlegroups.com> |
| In reply to | #33411 |
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
[toc] | [prev] | [next] | [standalone]
| From | Ethan Furman <ethan@stoneleaf.us> |
|---|---|
| Date | 2012-11-16 13:30 -0800 |
| Message-ID | <mailman.3760.1353101765.27098.python-list@python.org> |
| In reply to | #33442 |
bruceg113355@gmail.com wrote:
> 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.
Right, my 'for k, v in kwargs' should have been 'for k, v in kwargs.items()'
Glad you were able to make it work!
~Ethan~
[toc] | [prev] | [next] | [standalone]
| From | bruceg113355@gmail.com |
|---|---|
| Date | 2012-11-16 07:00 -0800 |
| Message-ID | <mailman.3752.1353078041.27098.python-list@python.org> |
| In reply to | #33411 |
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
[toc] | [prev] | [next] | [standalone]
Page 1 of 2 [1] 2 Next page →
Back to top | Article view | comp.lang.python
csiph-web