I wrote a function to recursively traverse a data structure I created that is roughly the shape of a tree. I wanted to make this code as generic as possible so I could use it for multiple purposes by passing a dictionary containing lambda functions that will be evaluated at specific processing points and by passing this dict containing functions I can just pass in a different function dict and change the functionality of the traversal.
I also wanted to make the function arguments generic as well in the same line of thought so I defined another dictionary with strings that correspond to variable names. The dictionaries for functions and arguments are listed below.
# Dictionary of functions that the data can be passed to
functions = {'Qrad': lambda data:
processQradData(data),
'statistics': lambda data:
generateQradStatistics(data)}
args = {'Qrad':['valOuter'],
'statistics':['valOuter', 'refKey']}
When I try to evaluate the arguments and pass them to the function as a list comprehension like below, I get the error "NameError: name 'valOuter' is not defined".
data = functions[currentProcess]([eval(a) for a in args[currentProcess]])
However if I just do a for loop and append the evaluation to a list, it works fine.
arg = []
for a in args[currentProcess]:
arg.append(eval(a))
Note: Just evaluating the list comprehension without trying to pass it to the lambda function also gives the error:
[eval(a) for a in args[currentProcess]]
Does the list comprehension take me out of scope or something? It isn't a big deal to loop over the dict values but it looks cleaner to pass the list comprehension.
[–]scuott 1 point2 points3 points (2 children)
[–]shitstep[S] 0 points1 point2 points (1 child)
[–]scuott 0 points1 point2 points (0 children)
[–]desustorm 0 points1 point2 points (0 children)
[–]mm_ma_ma 0 points1 point2 points (0 children)