Skip to content

Factory


Air Force Research Laboratory (AFRL) Autonomous Capabilities Team (ACT3) Reinforcement Learning (RL) Core.

This is a US Government Work not subject to copyright protection in the US.

The use, dissemination or disclosure of data in this file is subject to limitation or restriction. See accompanying README and LICENSE for details.


Factory (BaseModel) pydantic-model ¤

Factory class to allow subclass creation with pydantic

TODO: Create a simple motivating example

Source code in corl/libraries/factory.py
class Factory(BaseModel):
    """Factory class to allow subclass creation with pydantic

    TODO: Create a simple motivating example
    """
    type: PyObject
    config: Dict[str, Any] = {}

    def build(self, **kwargs):
        """Build the object contained within this factory."""
        return self.type(**self.config, **kwargs)

    @classmethod
    def resolve_factory(cls, v):
        """Validator for converting a factory into the built object.

        Usage in a pydantic model:
        resolve_factory = validator('name', pre=True, allow_reuse=True)(Factory.resolve_factory)
        """
        try:
            v['type']
        except (TypeError, KeyError):
            # Not something that should be built with the factory
            return v
        else:
            factory = cls(**v)
            return factory.build()

build(self, **kwargs) ¤

Build the object contained within this factory.

Source code in corl/libraries/factory.py
def build(self, **kwargs):
    """Build the object contained within this factory."""
    return self.type(**self.config, **kwargs)

resolve_factory(v) classmethod ¤

Validator for converting a factory into the built object.

Usage in a pydantic model: resolve_factory = validator('name', pre=True, allow_reuse=True)(Factory.resolve_factory)

Source code in corl/libraries/factory.py
@classmethod
def resolve_factory(cls, v):
    """Validator for converting a factory into the built object.

    Usage in a pydantic model:
    resolve_factory = validator('name', pre=True, allow_reuse=True)(Factory.resolve_factory)
    """
    try:
        v['type']
    except (TypeError, KeyError):
        # Not something that should be built with the factory
        return v
    else:
        factory = cls(**v)
        return factory.build()