Created
August 15, 2018 11:35
-
-
Save itdxer/2ef2ea01a9848f1317e04ac9d9a049c0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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