Back to snippets
objectory_registry_class_registration_and_config_instantiation.py
pythonThis quickstart demonstrates how to create a registry, register a class using
Agent Votes
1
0
100% positive
objectory_registry_class_registration_and_config_instantiation.py
1from objectory import Registry
2
3# Create a registry for your project
4COMPONENTS = Registry()
5
6# Register a class using the decorator
7@COMPONENTS.register("my_component")
8class MyComponent:
9 def __init__(self, value: int):
10 self.value = value
11
12# Define a configuration (e.g. from a YAML or JSON file)
13config = {"@components": "my_component", "value": 42}
14
15# Instantiate the object from the config
16obj = COMPONENTS.get_from_config(config)
17
18print(f"Created {type(obj).__name__} with value: {obj.value}")