param_extract.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. PARAM_MAXDEPTH = 5
  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 > 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:
  75. raise ValueError(f"cpn `{name}` has redundant parameters {redundant}")
  76. return param
  77. @staticmethod
  78. def change_param_to_dict(obj):
  79. ret_dict = {}
  80. variable_dict = obj.__dict__
  81. for variable in variable_dict:
  82. attr = getattr(obj, variable)
  83. if attr and type(attr).__name__ not in dir(builtins):
  84. ret_dict[variable] = ParamExtract.change_param_to_dict(attr)
  85. else:
  86. ret_dict[variable] = attr
  87. return ret_dict
  88. @staticmethod
  89. def get_not_builtin_types(obj):
  90. ret_dict = {}
  91. variable_dict = obj.__dict__
  92. for variable in variable_dict:
  93. attr = getattr(obj, variable)
  94. if attr and type(attr).__name__ not in dir(builtins):
  95. ret_dict[variable] = ParamExtract.get_not_builtin_types(attr)
  96. return ret_dict