From 2569cc5e59f81aa1337da88df22ce635d88c7597 Mon Sep 17 00:00:00 2001 From: gwen Date: Fri, 6 Jul 2012 09:57:35 +0200 Subject: [PATCH] polymorphism in autolib functions --- autolib.py | 44 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/autolib.py b/autolib.py index b4df145..c620344 100644 --- a/autolib.py +++ b/autolib.py @@ -19,7 +19,8 @@ # the whole pypy projet is under MIT licence # ____________________________________________________________ "enables us to carry out a calculation and return an option's value" -from tiramisu.error import DisabledOptionError, SpecialOwnersError +from tiramisu.error import DisabledOptionError, SpecialOwnersError, ConfigError +from creole import eosfunc # ____________________________________________________________ # automatic Option object special_owners = ['auto', 'fill'] @@ -30,8 +31,6 @@ def special_owner_factory(name, owner, value, # we have to carry out a calculation return calc_factory(name, callback, callback_params, config) -#g = globals() - def calc_factory(name, callback, callback_params, config): # FIXME we have to know the exact status of the config # not to disrupt it @@ -39,25 +38,50 @@ def calc_factory(name, callback, callback_params, config): if callback_params is None: callback_params = {} tcparams = {} + one_is_multi = False + len_multi = 0 for key, value in callback_params.items(): if type(value) == tuple: path, check_disabled = value try: opt_value = getattr(config, path) + opt = config.unwrap_from_path(path) except DisabledOptionError, e: if chek_disabled: continue raise DisabledOptionError(e) - tcparams[key] = opt_value + is_multi = opt.is_multi() + if is_multi: + len_value = len(opt_value) + if len_multi != 0 and len_multi != len_value: + raise ConfigError('unable to carry out a calculation, ' + 'option values with multi types must have same length for: ' + + name) + len_multi = len_value + one_is_multi = True + tcparams[key] = (opt_value, is_multi) else: - tcparams[key] = value + tcparams[key] = (value, False) + + if one_is_multi: + ret = [] + for incr in range(len_multi): + tcp = {} + for key, couple in tcparams.items(): + value, ismulti = couple + if ismulti: + tcp[key] = value[incr] + else: + tcp[key] = value + ret.append(calculate(callback, tcp)) + return ret + else: + return calculate(callback, tcparams) + +def calculate(callback, tcparams): try: - #return getattr(autolib, callback)(name, config) - #return g[callback](name, config, **callback_params) - from creole import eosfunc return getattr(eosfunc, callback)(**tcparams) except AttributeError: - import traceback - traceback.print_exc() raise SpecialOwnersError("callback: {0} not found for " "option: {1}".format(callback, name)) +