tags:

views:

52

answers:

1

I am being passed lists of strings that I would like to convert to method calls using functools.partial:

[ 'object1.method1(arg1, arg2, 4, key=value)', 'object2.method2(6, arg1)' ...]

It's easy to use a regexp to generate the necessary 'func' argument to functools.partial, but I'm having a devil of a time easily converting the string within the parens into valid *args and **kwargs to pass to functools.partial.

Every list item is guaranteed to be valid Python. I'm just unable to come up with a quick, easy way to convert strings such as 'arg1, arg2, 4, key=value' into somehthing functools.partial can use. What am I missing?

Update:

My appologies. I did forget vital information. The args are not valid identifiers within the scope of this procedure, hence 'eval' doesn't work. They are, however, correct in the scope in which the resulting partial objects will be used, so they can be "copied" as literals. My current procedure returns a string 'arg1, arg2, 4, this=that'. If passed directly to functools.partial, functools.partial "wraps" it as a single string argument.

Hmmm.. the more I describe this, the more I realize that there's no way to do this unless these identifiers are valid within this scope...

+1  A: 

I'm writing another answer, because if I change the old one, the comments won't reflect the the content at all. Instead, if this turns out to be a better answer, I will withdraw the other. The following should work.

Theory of Operation:

  1. Obtain your func
  2. dissect string for arguments
  3. put every argument together with its own eval in a partial
  4. put a wrapper, func and the partial'd arguments in a partial
  5. execute in the scope needed

The following code concentrates on 2.-4. Imports are assumed. Of course there is an additional problem of names colliding, because of the additional code in the scope


def wrapper(func_, *args, **kw):
    new_args = []
    new_kw = {}
    for arg in args:
        if type(arg)==functools.partial:
            arg = arg()
        new_args.append(arg)
    for key in kw:
        value = kw[key]
        if type(value)==functools.partial:
            value = value()
        new_kw[key]=value
     return func_(*new_args, **new_kw)

orig_str = 'object1.method1(arg1, arg2, 4, key=value)'
argstr = re.search('.+\((.+)\)', orig_str).group(1)
args = []
kw = {}
for x in argstr.split(','):
    if '=' in x:
        key, value = x.strip().split('=')
    else:
        value = x.strip()
        key = None            
    value = functools.partial(eval, value)
    if key:
        kw[key]=value
    else:
        args.append(value)

what_you_want = functools.partial(wrapper, func, *args, **kw)
knitti
I like your concept here, put a wrapper around the args that evaluates them at the point of execution -- assuming they'll be defined there. I'll mark this as the answer, but I recommend to future readers that they treat the above as pseudo-code. Thanks, Knitti. Great idea!
JS