serializer.py 文件源码

python
阅读 26 收藏 0 点赞 0 评论 0

项目:xnmt 作者: neulab 项目源码 文件源码
def set_serialize_params_recursive(self, obj):
    base_arg_names = map(lambda x: x[0], inspect.getmembers(yaml.YAMLObject))
    if not isinstance(obj, Serializable):
      raise RuntimeError("attempting deserialization of non-Serializable object %s of type %s" % (str(obj), type(obj)))
    init_args = self.get_init_args(obj)
    class_param_names = [x[0] for x in inspect.getmembers(obj.__class__)]
    init_args.remove("self")
    obj.serialize_params = {}
    items = inspect.getmembers(obj)
    for name, val in items:
      if name=="yaml_context":
        raise ValueError("'yaml_context' is a reserved specifier, please rename argument")
      if name in base_arg_names or name.startswith("_") or name in ["serialize_params", "init_params", "kwargs"] or name in class_param_names: continue
      if isinstance(val, Serializable):
        obj.serialize_params[name] = val
        self.set_serialize_params_recursive(val)
      elif type(val) in [type(None), bool, int, float, str, type(six.u("")), datetime.datetime, dict, set]:
        obj.serialize_params[name] = val
      elif type(val)==list:
        obj.serialize_params[name] = val
        for item in val:
          if isinstance(item, Serializable):
            self.set_serialize_params_recursive(item)
      else:
        continue
      if not name in init_args:
        raise ValueError("unknown init parameter for %s: %s" % (obj.yaml_tag, name))
    obj.init_params = dict(obj.serialize_params)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号