Python allennlp.common.Registrable() Examples
The following are 10
code examples of allennlp.common.Registrable().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
allennlp.common
, or try the search function
.
Example #1
Source File: from_params_test.py From allennlp with Apache License 2.0 | 5 votes |
def test_dict(self): from allennlp.common.registrable import Registrable class A(Registrable): pass @A.register("b") class B(A): def __init__(self, size: int) -> None: self.size = size class C(Registrable): pass @C.register("d") class D(C): def __init__(self, items: Dict[str, A]) -> None: self.items = items params = Params( { "type": "d", "items": {"first": {"type": "b", "size": 1}, "second": {"type": "b", "size": 2}}, } ) d = C.from_params(params) assert isinstance(d.items, dict) assert len(d.items) == 2 assert all(isinstance(key, str) for key in d.items.keys()) assert all(isinstance(value, B) for value in d.items.values()) assert d.items["first"].size == 1 assert d.items["second"].size == 2
Example #2
Source File: from_params_test.py From allennlp with Apache License 2.0 | 5 votes |
def test_list(self): from allennlp.common.registrable import Registrable class A(Registrable): pass @A.register("b") class B(A): def __init__(self, size: int) -> None: self.size = size class C(Registrable): pass @C.register("d") class D(C): def __init__(self, items: List[A]) -> None: self.items = items params = Params( {"type": "d", "items": [{"type": "b", "size": 1}, {"type": "b", "size": 2}]} ) d = C.from_params(params) assert isinstance(d.items, list) assert len(d.items) == 2 assert all(isinstance(item, B) for item in d.items) assert d.items[0].size == 1 assert d.items[1].size == 2
Example #3
Source File: from_params_test.py From allennlp with Apache License 2.0 | 5 votes |
def test_tuple(self): from allennlp.common.registrable import Registrable class A(Registrable): pass @A.register("b") class B(A): def __init__(self, size: int) -> None: self.size = size class C(Registrable): pass @C.register("d") class D(C): def __init__(self, name: str) -> None: self.name = name class E(Registrable): pass @E.register("f") class F(E): def __init__(self, items: Tuple[A, C]) -> None: self.items = items params = Params( {"type": "f", "items": [{"type": "b", "size": 1}, {"type": "d", "name": "item2"}]} ) f = E.from_params(params) assert isinstance(f.items, tuple) assert len(f.items) == 2 assert isinstance(f.items[0], B) assert isinstance(f.items[1], D) assert f.items[0].size == 1 assert f.items[1].name == "item2"
Example #4
Source File: from_params_test.py From allennlp with Apache License 2.0 | 5 votes |
def test_kwargs_with_multiple_inheritance(self): # Basic idea: have two identical classes, differing only in the order of their multiple # inheritance, and make sure that passing kwargs up to the super class works in both cases. class A(Registrable): def __init__(self, a: int): self.a = a from numbers import Number @A.register("b1") class B1(A, Number): def __init__(self, b: float, **kwargs): super().__init__(**kwargs) self.b = b @A.register("b2") class B2(Number, A): def __init__(self, b: float, **kwargs): super().__init__(**kwargs) self.b = b b = B1.from_params(params=Params({"a": 4, "b": 5})) assert b.b == 5 assert b.a == 4 b = B2.from_params(params=Params({"a": 4, "b": 5})) assert b.b == 5 assert b.a == 4
Example #5
Source File: from_params_test.py From allennlp with Apache License 2.0 | 5 votes |
def test_only_infer_superclass_params_if_unknown(self): from allennlp.common.registrable import Registrable class BaseClass(Registrable): def __init__(self): self.x = None self.a = None self.rest = None @BaseClass.register("a") class A(BaseClass): def __init__(self, a: int, x: int, **kwargs): super().__init__() self.x = x self.a = a self.rest = kwargs @BaseClass.register("b") class B(A): def __init__(self, a: str, x: int = 42, **kwargs): super().__init__(x=x, a=-1, raw_a=a, **kwargs) params = Params({"type": "b", "a": "123"}) # The param `x` should not be required as it has default value in `B` # The correct type of the param `a` should be inferred from `B` as well. instance = BaseClass.from_params(params) assert instance.x == 42 assert instance.a == -1 assert len(instance.rest) == 1 assert type(instance.rest["raw_a"]) == str assert instance.rest["raw_a"] == "123"
Example #6
Source File: from_params_test.py From allennlp with Apache License 2.0 | 5 votes |
def test_kwargs_are_passed_to_deeper_superclasses(self): from allennlp.common.registrable import Registrable class BaseClass(Registrable): def __init__(self): self.a = None self.b = None self.c = None @BaseClass.register("a") class A(BaseClass): def __init__(self, a: str): super().__init__() self.a = a @BaseClass.register("b") class B(A): def __init__(self, b: str, **kwargs): super().__init__(**kwargs) self.b = b @BaseClass.register("c") class C(B): def __init__(self, c, **kwargs): super().__init__(**kwargs) self.c = c params = Params({"type": "c", "a": "a_value", "b": "b_value", "c": "c_value"}) instance = BaseClass.from_params(params) assert instance.a == "a_value" assert instance.b == "b_value" assert instance.c == "c_value"
Example #7
Source File: from_params_test.py From allennlp with Apache License 2.0 | 5 votes |
def test_iterable(self): from allennlp.common.registrable import Registrable class A(Registrable): pass @A.register("b") class B(A): def __init__(self, size: int) -> None: self.size = size class C(Registrable): pass @C.register("d") class D(C): def __init__(self, items: Iterable[A]) -> None: self.items = items params = Params( {"type": "d", "items": [{"type": "b", "size": 1}, {"type": "b", "size": 2}]} ) d = C.from_params(params) assert isinstance(d.items, Iterable) items = list(d.items) assert len(items) == 2 assert all(isinstance(item, B) for item in items) assert items[0].size == 1 assert items[1].size == 2
Example #8
Source File: from_params_test.py From allennlp with Apache License 2.0 | 5 votes |
def test_mapping(self): from allennlp.common.registrable import Registrable class A(Registrable): pass @A.register("b") class B(A): def __init__(self, size: int) -> None: self.size = size class C(Registrable): pass @C.register("d") class D(C): def __init__(self, items: Mapping[str, A]) -> None: self.items = items params = Params( { "type": "d", "items": {"first": {"type": "b", "size": 1}, "second": {"type": "b", "size": 2}}, } ) d = C.from_params(params) assert isinstance(d.items, Mapping) assert len(d.items) == 2 assert all(isinstance(key, str) for key in d.items.keys()) assert all(isinstance(value, B) for value in d.items.values()) assert d.items["first"].size == 1 assert d.items["second"].size == 2
Example #9
Source File: from_params_test.py From allennlp with Apache License 2.0 | 4 votes |
def test_extras(self): from allennlp.common.registrable import Registrable class A(Registrable): pass @A.register("b") class B(A): def __init__(self, size: int, name: str) -> None: self.size = size self.name = name @A.register("c") class C(A): def __init__(self, size: int, name: str) -> None: self.size = size self.name = name # custom from params @classmethod def from_params(cls, params: Params, size: int, **extras) -> "C": # type: ignore name = params.pop("name") return cls(size=size, name=name) # Check that extras get passed, even though A doesn't need them. params = Params({"type": "b", "size": 10}) b = A.from_params(params, name="extra") assert b.name == "extra" assert b.size == 10 # Check that extra extras don't get passed. params = Params({"type": "b", "size": 10}) b = A.from_params(params, name="extra", unwanted=True) assert b.name == "extra" assert b.size == 10 # Now the same with a custom from_params. params = Params({"type": "c", "name": "extra_c"}) c = A.from_params(params, size=20) assert c.name == "extra_c" assert c.size == 20 # Check that extra extras don't get passed. params = Params({"type": "c", "name": "extra_c"}) c = A.from_params(params, size=20, unwanted=True) assert c.name == "extra_c" assert c.size == 20
Example #10
Source File: from_params_test.py From allennlp with Apache License 2.0 | 4 votes |
def test_set(self): from allennlp.common.registrable import Registrable class A(Registrable): def __init__(self, name: str) -> None: self.name = name def __eq__(self, other): return self.name == other.name def __hash__(self): return hash(self.name) @A.register("b") class B(A): pass class C(Registrable): pass @C.register("d") class D(C): def __init__(self, items: Set[A]) -> None: self.items = items params = Params( { "type": "d", "items": [ {"type": "b", "name": "item1"}, {"type": "b", "name": "item2"}, {"type": "b", "name": "item2"}, ], } ) d = C.from_params(params) assert isinstance(d.items, set) assert len(d.items) == 2 assert all(isinstance(item, B) for item in d.items) assert any(item.name == "item1" for item in d.items) assert any(item.name == "item2" for item in d.items)