param_extract.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Copyright 2019 The FATE Authors. All Rights Reserved.
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. #
  18. # =============================================================================
  19. # Param Exact Class
  20. # =============================================================================
  21. import builtins
  22. from federatedml.util import consts
  23. class ParamExtract(object):
  24. def __init__(self):
  25. self.builtin_types = dir(builtins)
  26. def parse_param_from_config(
  27. self, param, config_json, valid_check=False, module=None, cpn=None
  28. ):
  29. if config_json is None or type(config_json).__name__ != "dict":
  30. raise Exception(
  31. "config file is not a valid dict type, please have a check!"
  32. )
  33. # default_section = type(param).__name__
  34. if "ComponentParam" not in config_json:
  35. return param
  36. """
  37. if default_section not in config_json:
  38. return param
  39. """
  40. param = self.recursive_parse_param_from_config(
  41. param,
  42. config_json.get("ComponentParam"),
  43. param_parse_depth=0,
  44. valid_check=valid_check,
  45. name=f"{module}#{cpn}",
  46. )
  47. return param
  48. def recursive_parse_param_from_config(
  49. self, param, config_json, param_parse_depth, valid_check, name
  50. ):
  51. if param_parse_depth > consts.PARAM_MAXDEPTH:
  52. raise ValueError("Param define nesting too deep!!!, can not parse it")
  53. inst_variables = param.__dict__
  54. for variable in inst_variables:
  55. attr = getattr(param, variable)
  56. if type(attr).__name__ in self.builtin_types or attr is None:
  57. if variable in config_json:
  58. option = config_json[variable]
  59. setattr(param, variable, option)
  60. elif variable in config_json:
  61. sub_params = self.recursive_parse_param_from_config(
  62. attr,
  63. config_json.get(variable),
  64. param_parse_depth + 1,
  65. valid_check,
  66. name,
  67. )
  68. setattr(param, variable, sub_params)
  69. if valid_check:
  70. redundant = []
  71. for var in config_json:
  72. if var not in inst_variables:
  73. redundant.append(var)
  74. if redundant and name is not None:
  75. raise ValueError(f"cpn `{name}` has redundant parameters {redundant}")
  76. return param