Allow for missing keys in dictionaries of combined behaviors
This commit is contained in:
parent
0e276006a1
commit
ba2a6484f9
28
ui/config.py
28
ui/config.py
|
|
@ -25,7 +25,7 @@ seed = {
|
||||||
# return (y, x)
|
# return (y, x)
|
||||||
|
|
||||||
# Behaviors per Mechanism
|
# Behaviors per Mechanism
|
||||||
# Different return types per mechanism ??
|
# Different return types per mechanism ?? *** No ***
|
||||||
def b1m1(step, sL, s):
|
def b1m1(step, sL, s):
|
||||||
return {'param1': 1}
|
return {'param1': 1}
|
||||||
def b2m1(step, sL, s):
|
def b2m1(step, sL, s):
|
||||||
|
|
@ -131,7 +131,7 @@ def print_fwd(x):
|
||||||
return x
|
return x
|
||||||
|
|
||||||
def behavior_to_dict(v):
|
def behavior_to_dict(v):
|
||||||
return dict(list(zip(map(lambda n: 'b' + str(n), list(range(len(v)))), v)))
|
return dict(list(zip(map(lambda n: 'b' + str(n+1), list(range(len(v)))), v)))
|
||||||
|
|
||||||
@curried
|
@curried
|
||||||
def foldr_dict_vals(f, d):
|
def foldr_dict_vals(f, d):
|
||||||
|
|
@ -140,7 +140,7 @@ def foldr_dict_vals(f, d):
|
||||||
def sum_dict_values(f = _ + _):
|
def sum_dict_values(f = _ + _):
|
||||||
return foldr_dict_vals(f)
|
return foldr_dict_vals(f)
|
||||||
|
|
||||||
def get_neutral_element(datatype):
|
def get_base_value(datatype):
|
||||||
if datatype is str:
|
if datatype is str:
|
||||||
return ''
|
return ''
|
||||||
elif datatype is int:
|
elif datatype is int:
|
||||||
|
|
@ -152,18 +152,16 @@ def get_neutral_element(datatype):
|
||||||
|
|
||||||
@curried
|
@curried
|
||||||
def dict_op(f, d1, d2):
|
def dict_op(f, d1, d2):
|
||||||
res = {}
|
|
||||||
for k in set(list(d1.keys())+list(d2.keys())):
|
def set_base_value(target_dict, source_dict, key):
|
||||||
try:
|
if key not in target_dict:
|
||||||
a = d1[k]
|
return get_base_value(type(source_dict[key]))
|
||||||
except KeyError:
|
else:
|
||||||
a = get_neutral_element(type(d2[k]))
|
return target_dict[key]
|
||||||
try:
|
|
||||||
b = d2[k]
|
key_set = set(list(d1.keys())+list(d2.keys()))
|
||||||
except KeyError:
|
|
||||||
b = get_neutral_element(type(d1[k]))
|
return {k: f(set_base_value(d1, d2, k), set_base_value(d2, d1, k)) for k in key_set}
|
||||||
res.update({k: f(a,b)})
|
|
||||||
return res
|
|
||||||
|
|
||||||
def dict_elemwise_sum(f = _ + _):
|
def dict_elemwise_sum(f = _ + _):
|
||||||
return dict_op(f)
|
return dict_op(f)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue