12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- import builtins
- from federatedml.util import consts
- class ParamExtract(object):
- def __init__(self):
- self.builtin_types = dir(builtins)
- def parse_param_from_config(
- self, param, config_json, valid_check=False, module=None, cpn=None
- ):
- if config_json is None or type(config_json).__name__ != "dict":
- raise Exception(
- "config file is not a valid dict type, please have a check!"
- )
-
- if "ComponentParam" not in config_json:
- return param
- """
- if default_section not in config_json:
- return param
- """
- param = self.recursive_parse_param_from_config(
- param,
- config_json.get("ComponentParam"),
- param_parse_depth=0,
- valid_check=valid_check,
- name=f"{module}#{cpn}",
- )
- return param
- def recursive_parse_param_from_config(
- self, param, config_json, param_parse_depth, valid_check, name
- ):
- if param_parse_depth > consts.PARAM_MAXDEPTH:
- raise ValueError("Param define nesting too deep!!!, can not parse it")
- inst_variables = param.__dict__
- for variable in inst_variables:
- attr = getattr(param, variable)
- if type(attr).__name__ in self.builtin_types or attr is None:
- if variable in config_json:
- option = config_json[variable]
- setattr(param, variable, option)
- elif variable in config_json:
- sub_params = self.recursive_parse_param_from_config(
- attr,
- config_json.get(variable),
- param_parse_depth + 1,
- valid_check,
- name,
- )
- setattr(param, variable, sub_params)
- if valid_check:
- redundant = []
- for var in config_json:
- if var not in inst_variables:
- redundant.append(var)
- if redundant and name is not None:
- raise ValueError(f"cpn `{name}` has redundant parameters {redundant}")
- return param
|