def __init__( self, locals_: Dict, exclude=('kwargs', 'self', '__class__', 'locals_')) -> None: """This base class helps sub-classes to capture their arguments passed to ``__init__``, and also their types so that they can be deserialized from a config in dict form. Args: locals_: Obtained by :meth:`locals`. exclude: Arguments to be excluded. Examples: >>> class MyClass(ConfigTracker): >>> def __init__(self, i_need_this='yes') -> None: >>> super().__init__(locals()) >>> obj = MyClass() >>> print(obj.config) {'i_need_this': 'yes', 'classpath': 'test_config_tracker.MyClass'} """ if 'kwargs' in locals_: locals_.update(locals_['kwargs']) self.config = SerializableDict( (k, v.config if hasattr(v, 'config') else v) for k, v in locals_.items() if k not in exclude) self.config['classpath'] = classpath_of(self)
def config(self) -> dict: """ The config of this object, which are public properties. If any properties needs to be excluded from this config, simply declare it with prefix ``_``. """ return dict([('classpath', classpath_of(self))] + [(k, v.config if hasattr(v, 'config') else v) for k, v in self.__dict__.items() if not k.startswith('_')])
def __init__(self, transform: Transform) -> None: super().__init__() self.meta = { 'class_path': classpath_of(self), 'hanlp_version': hanlp.version.__version__, } self.model: Optional[tf.keras.Model] = None self.config = SerializableDict() self.transform = transform # share config with transform for convenience, so we don't need to pass args around if self.transform.config: for k, v in self.transform.config.items(): self.config[k] = v self.transform.config = self.config
def __init__(self, component: Component, input_key: str = None, output_key: str = None, **kwargs) -> None: super().__init__() if not hasattr(self, 'config'): self.config = {'classpath': classpath_of(self)} self.output_key = output_key self.input_key = input_key self.component = component self.kwargs = kwargs self.config.update({ 'component': component.config, 'input_key': self.input_key, 'output_key': self.output_key, 'kwargs': self.kwargs })
def _savable_config(self): def convert(k, v): if not isinstance(v, SerializableDict) and hasattr(v, 'config'): v = v.config elif isinstance(v, (set, tuple)): v = list(v) if isinstance(v, dict): v = dict(convert(_k, _v) for _k, _v in v.items()) return k, v config = SerializableDict( convert(k, v) for k, v in sorted(self.config.items())) config.update({ # 'create_time': now_datetime(), 'classpath': classpath_of(self), 'hanlp_version': hanlp.__version__, }) return config
def config(self): return dict([('classpath', classpath_of(self))] + [(k, v) for k, v in self.__dict__.items() if not k.startswith('_')])
def __init__(self, *pipes: Pipe) -> None: super().__init__() if not hasattr(self, 'config'): self.config = {'classpath': classpath_of(self)} if pipes: self.extend(pipes)
def meta(self): return { 'classpath': classpath_of(self), 'hanlp_version': hanlp.version.__version__, 'pipes': [pipe.config for pipe in self] }
def to_dict(self) -> dict: d = super().to_dict() d['type'] = classpath_of(self) return d
def config(self): return { 'classpath': classpath_of(self), 'dictionary': dict(self.items()) }
def __init__(self, function: Callable) -> None: super().__init__() self.config = {} self.function = function self.config['function'] = classpath_of(function) self.config['classpath'] = classpath_of(self)