Objectory

0.3.1 · active · verified Thu Apr 16

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

Warnings

Install

Imports

Quickstart

Demonstrates the three main ways to use Objectory: direct factory instantiation, abstract factory inheritance, and dynamic class registration with a Registry.

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}")

view raw JSON →