flambe.compile.registrable

Module Contents

flambe.compile.registrable.logger[source]
flambe.compile.registrable.yaml[source]
flambe.compile.registrable._reg_prefix :Optional[str][source]
flambe.compile.registrable.R[source]
flambe.compile.registrable.A[source]
flambe.compile.registrable.RT[source]
exception flambe.compile.registrable.RegistrationError[source]

Bases: Exception

Error thrown when acessing yaml tag on a non-registered class

Thrown when trying to access the default yaml tag for a class typically occurs when called on an abstract class

flambe.compile.registrable.make_from_yaml_with_metadata(from_yaml_fn: Callable[..., Any], tag: str, factory_name: Optional[str] = None) → Callable[..., Any][source]
flambe.compile.registrable.make_to_yaml_with_metadata(to_yaml_fn: Callable[..., Any]) → Callable[..., Any][source]
class flambe.compile.registrable.registration_context(namespace: str)[source]
__enter__(self)[source]
__exit__(self, *args: Any)[source]
__call__(self, func: Callable[..., Any])[source]
class flambe.compile.registrable.Registrable[source]

Bases: abc.ABC

Subclasses automatically registered as yaml tags

Automatically registers subclasses with the yaml loader by adding a constructor and representer which can be overridden

_yaml_tags :Dict[Any, List[str]][source]
_yaml_tag_namespace :Dict[Type, str][source]
_yaml_registered_factories :Set[str][source]
classmethod __init_subclass__(cls: Type[R], should_register: Optional[bool] = True, tag_override: Optional[str] = None, tag_namespace: Optional[str] = None, **kwargs: Mapping[str, Any])[source]
static register_tag(class_: RT, tag: str, tag_namespace: Optional[str] = None)[source]
static get_default_tag(class_: RT, factory_name: Optional[str] = None)[source]

Retrieve default yaml tag for class cls

Retrieve the default tag (aka the last one, which will be the only one, or the alias if it exists) for use in yaml representation

classmethod to_yaml(cls, representer: Any, node: Any, tag: str)[source]

Use representer to create yaml representation of node

See Component class, and experiment/options for examples

classmethod from_yaml(cls, constructor: Any, node: Any, factory_name: str)[source]

Use constructor to create an instance of cls

See Component class, and experiment/options for examples

flambe.compile.registrable.alias(tag: str, tag_namespace: Optional[str] = None) → Callable[[RT], RT][source]

Decorate a Registrable subclass with a new tag

Can be added multiple times to give a class multiple aliases, however the top most alias tag will be the default tag which means it will be used when representing the class in YAML

flambe.compile.registrable.register(cls: Type[A], tag: str) → Type[A][source]

Safely register a new tag for a class

Similar to alias, but it’s intended to be used on classes that are not already subclasses of Registrable, and it is NOT a decorator

class flambe.compile.registrable.registrable_factory(fn: Any)[source]

Decorate Registrable factory method for use in the config

This Descriptor class will set properties that allow the factory method to be specified directly in the config as a suffix to the tag; for example:

class MyModel(Component):

    @registrable_factory
    def from_file(cls, path):
        # load instance from path
        ...
        return instance

defines the factory, which can then be used in yaml:

model: !MyModel.from_file
    path: some/path/to/file.pt
__set_name__(self, owner: type, name: str)[source]
class flambe.compile.registrable.MappedRegistrable[source]

Bases: flambe.compile.registrable.Registrable

classmethod to_yaml(cls, representer: Any, node: Any, tag: str)[source]

Use representer to create yaml representation of node

classmethod from_yaml(cls, constructor: Any, node: Any, factory_name: str)[source]

Use constructor to create an instance of cls