Skip to content

Instantly share code, notes, and snippets.

@itdxer
Created August 15, 2018 11:35
Show Gist options
  • Save itdxer/2ef2ea01a9848f1317e04ac9d9a049c0 to your computer and use it in GitHub Desktop.
Save itdxer/2ef2ea01a9848f1317e04ac9d9a049c0 to your computer and use it in GitHub Desktop.
from collections import namedtuple
def flatten_tuples(values):
for value in values:
if isinstance(value, tuple):
for inner_value in flatten_tuples(value):
yield inner_value
else:
yield value
def fn(argument_names, function):
Argument = namedtuple("Argument", argument_names)
def wrapper(args):
args = Argument(*flatten_tuples(args))
function.__globals__['args'] = args
return function()
return wrapper
data = [(0, (1, 1)), (1, (2, 5)), (2, (11, 100))]
output = map(fn("key value1 value2", lambda: args.value1 + args.value2), data)
print(list(output)) # [2, 7, 111]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment