Skip to content

Instantly share code, notes, and snippets.

@txomon
Forked from cesarkawakami/gist:4580196
Last active December 11, 2015 09:28
Show Gist options
  • Save txomon/4580285 to your computer and use it in GitHub Desktop.
Save txomon/4580285 to your computer and use it in GitHub Desktop.
def _operations_list_to_string(self,operations):
""" (list | str) -> str
The function returns a plain string of the inner lists
>>> _operations_list_to_string(['a','+',[['b','+','d'],'*','c']])
'(a+((b+d)*c))'
"""
if type(operations) == list:
result = '('
for operation in operations:
result += self.operations_list_to_string(operation)
return result + ')'
elif type(operations) == str:
return operations
def operations_to_result(self, parameter_definition, dictionary_of_values):
""" (dict, dict) -> dict
Returns the result of doing the operations for each value in parameter_definition
with the values in dictionary_of_values in each index in a dictionary indexed
by the keys of parameter_definition.
>>> operations_to_result({"parameterA": ["a","+","b","+","c"]
"parameterB": ["a", "*", "c"]},
{"a": [3,2,3,4],
"b": [2,1,3,4],
"c": [1,3,5,2]})
{"parameterA": [6,6,11,10],
"parameterB": [3,6,15,8]}
"""
result = dict()
var_list = dict()
for index in range(len(max(dictionary_of_values.values())))
for operands,values in dictionary_of_values.keys():
var_list[operands] = values[index]
for parameter,definition in parameter_definition:
operations_string = _operations_list_to_string(definition)
result[parameter] = eval(operations_string, var_list)
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment