Skip to content

Commit

Permalink
support passing additional args and kwargs in ConfigRegistry.build
Browse files Browse the repository at this point in the history
Signed-off-by: Zhiyuan Chen <[email protected]>
  • Loading branch information
ZhiyuanChen committed Mar 30, 2024
1 parent b16ad1d commit 75080b9
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ Note that [`Config`][chanfig.Config] also has `default_factory=Config()` by defa

[`Registry`][chanfig.Registry] extends the [`NestedDict`][chanfig.NestedDict] and supports [`register`][chanfig.Registry.register], [`lookup`][chanfig.Registry.lookup], and [`build`][chanfig.Registry.build] to help you register constructors and build objects from a [`Config`][chanfig.Config].

[`ConfigRegistry`][chanfig.ConfigRegistry] is a subclass of [`Registry`][chanfig.Registry] that is specifically designed for building objects from a [`Config`][chanfig.Config] or a [`dataclass`][dataclasses.dataclass].
Just specify the `key` when creating the registry and pass `config` to the `build` method, and you will get the object you want.

### Variable

Have one value for multiple names at multiple places? We got you covered.
Expand Down
3 changes: 3 additions & 0 deletions README.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ Python 的`dict`自 Python 3.7 之后就是有序的,但是并没有一个内

[`Registry`][chanfig.Registry]继承自[`NestedDict`][chanfig.NestedDict],并且提供[`register`][chanfig.Registry.register][`lookup`][chanfig.Registry.lookup][`build`][chanfig.Registry.build]来帮助你注册构造函数并从[`Config`][chanfig.Config]来创建对象。

[`ConfigRegistry`][chanfig.ConfigRegistry]是一个[`Registry`][chanfig.Registry]的子类,他专为从一个[`Config`][chanfig.Config]或者一个[`dataclass`][dataclasses.dataclass]来构建一个对象而设计。
只需在创建注册表时指定`key`,然后在调用`build`方法时传入`config`,你就会得到你想要的对象。

### Variable

有一个值在多个地方以多个名字出现?我们给你提供掩护。
Expand Down
41 changes: 33 additions & 8 deletions chanfig/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,12 @@ def init(cls: Callable, *args: Any, **kwargs: Any) -> Any: # pylint: disable=W0
... def __init__(self, a, b):
... self.a = a
... self.b = b
>>> kwargs = {"a": 1, "b": 2}
>>> kwargs = {"a": 0, "b": 1}
>>> module = Registry.init(Module, **kwargs)
>>> type(module)
<class 'chanfig.registry.Module'>
>>> module.a
1
>>> module.b
2
>>> module.a, module.b
(0, 1)
"""

return cls(*args, **kwargs)
Expand Down Expand Up @@ -275,7 +273,7 @@ class ConfigRegistry(Registry):
"""
`ConfigRegistry` for components that can be initialised with a `config`.
`ConfigRegistry` is purcutularly useful when you want to construct a component from a configuration, such as a
`ConfigRegistry` is particularly useful when you want to construct a component from a configuration, such as a
Hugginface Transformers model.
See Also:
Expand Down Expand Up @@ -337,7 +335,34 @@ class ConfigRegistry(Registry):
(0, 1)
"""

def build(self, config) -> Any: # type: ignore[override]
@staticmethod
def init(cls: Callable, config, *args: Any, **kwargs: Any) -> Any: # pylint: disable=W0211
r"""
Constructor of component.
Args:
cls: The component to construct.
*args: The arguments to pass to the component.
**kwargs: The keyword arguments to pass to the component.
Returns:
(Any):
Examples:
>>> class Module:
... def __init__(self, config, a=None, b=None):
... self.config = config
... self.a = config.a if a is None else a
... self.b = config.b if b is None else b
>>> config = NestedDict({"a": 0, "b": 1})
>>> module = ConfigRegistry.init(Module, config, b=2)
>>> module.a, module.b
(0, 2)
"""

return cls(config, *args, **kwargs)

def build(self, config, *args, **kwargs) -> Any: # type: ignore[override]
r"""
Build a component.
Expand Down Expand Up @@ -389,7 +414,7 @@ def build(self, config) -> Any: # type: ignore[override]
config_, key = getattr(config_, key), rest
name = getattr(config_, key)

return self.init(self.lookup(name), config) # type: ignore[arg-type]
return self.init(self.lookup(name), config, *args, **kwargs) # type: ignore[arg-type]


GlobalRegistry = Registry()

0 comments on commit 75080b9

Please sign in to comment.