Is there a simpler way to modify all arguments in a function beforeusing the arguments?

B

bruceg113355

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
# -----------------
# …
 
R

Roy Smith

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.
 
S

Steven D'Aprano

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)]
...
 
P

Paul Rubin

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])
 
C

Chris Angelico

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
 
P

Paul Rubin

Chris Angelico said:
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.
 
M

Miki Tebeka

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)
 
P

Peter Otten

Miki said:
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/ ;)
 
C

Chris Angelico

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
 
B

bruceg113355

(e-mail address removed) wrote:


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)]

...


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
 
A

Aahz

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.
 
B

bruceg113355

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
 
C

Chris Angelico

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
 
P

Peter Otten

Aahz said:
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.
 
S

Steve Howell

Is there a simpler way to modify all arguments in a function before usingthe 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
       # -----------------
       # …

I would couch this problem in a little more specific terms than trying
to make this "simpler."

The word "simple" is a dangerous term, because it's so broad and
subjective. By my mind, the code is already simple, but that's just
my own two cents.

The real problem with the code that it's a maintenance trap, because a
careless developer could add the ii parameter and forget to clean the
output. So the problem statement here might be more like "How do I
make sure future developers don't forget to fix the underscores in
future args?". That's still a controversial question, but at least
it's a little more specific.

The other problem with the current code is that all the boilerplate
distracts from the real logic of the function. That's a valid
concern, although it's likely that most maintainers of the code would
simply page down past the boilerplate without too much complaint.
 
B

brucegoodstein

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 ([email protected]) <*> 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'
 
E

Emile van Sebille

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
 
E

Ethan Furman

Emile said:
Extend def wrapper(*args) to handle *kwargs as well

Emile
so this line ^ becomes
def wrapper(*args, **kwargs):and add a line
for k, v in kwargs:
kwargs[k] = v.replace('_', '')and this line ^ becomes
return fn(*args, **kwargs)
~Ethan~
 
B

bruceg113355

Emile van Sebille wrote:

Extend def wrapper(*args) to handle *kwargs as well

so this line ^ becomes

def wrapper(*args, **kwargs):

and add a line

for k, v in kwargs:

kwargs[k] = v.replace('_', '')

and this line ^ becomes

return fn(*args, **kwargs)



~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
 
B

bruceg113355

Emile van Sebille wrote:

Extend def wrapper(*args) to handle *kwargs as well

so this line ^ becomes

def wrapper(*args, **kwargs):

and add a line

for k, v in kwargs:

kwargs[k] = v.replace('_', '')

and this line ^ becomes

return fn(*args, **kwargs)



~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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top