Objectory
Objectory is a lightweight Python library (version 0.3.1) designed for general-purpose object factories. It focuses on dynamic object factory implementations, allowing objects to be registered and instantiated from their configuration without altering the factory's core code. The library supports both abstract factory and registry patterns. As it is currently in a development stage (pre-1.0.0), the API may experience frequent changes.
Common errors
-
ModuleNotFoundError: No module named 'objectory'
cause The 'objectory' package is not installed in your current Python environment.fixRun `pip install objectory` to install the library. -
ValueError: Cannot create object for type 'NonExistentClass'
cause The string identifier passed to `factory` or `Registry.factory` does not correspond to an existing or registered class/function.fixVerify that 'NonExistentClass' is correctly spelled, accessible in the current scope, or properly registered with the `Registry` or `AbstractFactory` base class. -
TypeError: __init__() missing X required positional arguments
cause The arguments provided to `factory` (or `AbstractFactory.factory`, `Registry.factory`) do not match the `__init__` signature of the class being instantiated.fixEnsure that the `*args` and `**kwargs` passed to the factory method align with the constructor requirements of the target class.
Warnings
- breaking Objectory is in a pre-1.0.0 development stage, and its API is not guaranteed to be stable. Upgrading to new versions may introduce breaking changes, requiring code modifications.
- gotcha When using `factory` with string identifiers (e.g., 'module.ClassName'), ensure the specified module is importable and the class/function exists within it. Incorrect paths or missing imports will lead to runtime errors.
Install
-
pip install objectory
Imports
- factory
from objectory import factory
- AbstractFactory
from objectory import AbstractFactory
- Registry
from objectory import Registry
Quickstart
from objectory import factory, AbstractFactory, Registry
# 1. Basic factory usage with a built-in type
obj_list = factory("builtins.list")
print(f"Created list: {obj_list}")
obj_list_init = factory("builtins.list", [1, 2, 3])
print(f"Created list with data: {obj_list_init}")
# 2. AbstractFactory pattern
class BaseProduct(metaclass=AbstractFactory):
pass
class ConcreteProductA(BaseProduct):
def __init__(self, name="ProductA"):
self.name = name
class ConcreteProductB(BaseProduct):
def __init__(self, value=100):
self.value = value
product_a = BaseProduct.factory("ConcreteProductA", name="SpecialA")
print(f"Created product A: {product_a.name}")
product_b = BaseProduct.factory("ConcreteProductB")
print(f"Created product B value: {product_b.value}")
# 3. Registry pattern
my_registry = Registry()
@my_registry.register()
class RegisteredClass:
def __init__(self, message="Hello from registry!"):
self.message = message
registered_instance = my_registry.factory("RegisteredClass", message="Dynamic message!")
print(f"Created registered instance: {registered_instance.message}")