magic_utils.registries package
Submodules
magic_utils.registries.aliasRegistry module
- class magic_utils.registries.aliasRegistry.AliasRegistry(registry_name: str = 'BaseRegistry', *args, **kwargs)[source]
Bases:
Registry- add_alias(key: Hashable, alias: Hashable) None[source]
Add an alias to an existing canonical key.
- Parameters:
key – A canonical key or alias.
alias – The new alias.
- Raises:
DuplicateKeyError – If alias already exists.
- property aliases: dict[Hashable, Hashable]
Return mapping of alias -> canonical.
- property canonicals: dict[Hashable, set[Hashable]]
Return mapping of canonical -> aliases.
- get(key: Hashable) Any[source]
Retrieve a value by key or alias.
- Parameters:
key – Canonical key or alias.
- register(canonical: Hashable, value: Any, aliases: list[Hashable] | None = None) None[source]
Register a canonical key with optional aliases.
Ensures that the canonical and all aliases are globally unique across both canonical keys and aliases.
- Parameters:
canonical – The primary key.
value – Value to store.
aliases – Optional list of aliases.
- Raises:
DuplicateKeyError – On duplicate canonical or alias.
- register_class(key: Hashable | None = None, aliases: list[Hashable] | None = None) object[source]
Decorator to register a class with optional aliases.
- Parameters:
key – Canonical key.
aliases – Optional aliases.
- register_function(key: Hashable | None = None, aliases: list[Hashable] | None = None) Any[source]
Decorator to register a function with optional aliases.
- Parameters:
key – Canonical key.
aliases – Optional aliases.
- remove(key: Hashable | None = None, value: Any = None, rmv_all: bool = False) None[source]
Remove an entry and all its aliases.
- Parameters:
key – Key or alias.
value – Value.
rmv_all – Passed to base class.
- remove_alias(key: Hashable, alias: Hashable) None[source]
Remove an alias from a canonical key.
- Parameters:
key – A canonical key or alias.
alias – The alias to remove.
- Raises:
MissingKeyError – If key or alias not found.
- resolve_key_to_canonical(key: Hashable) Hashable[source]
Resolve a key or alias to its canonical key.
magic_utils.registries.registry module
- class magic_utils.registries.registry.Registry(registry_name: str = 'BaseRegistry', *args, **kwargs)[source]
Bases:
object- get(key: Hashable) Any | None[source]
Retrieve an item from the registry.
- Parameters:
key – The key to retrieve.
- Returns:
The value associated with the key.
- Raises:
NotRegisteredError – If the key is not registered and exceptions are enabled.
- register(key: Hashable, value: Any) None[source]
Register one key with a specified value. Skips registration if a key is already registered completely.
- Parameters:
key – A hashable key to register.
value – The value to associate with the given key.
- Raises:
DuplicateError – If a key is already registered and exceptions are enabled.
- register_class(key: Hashable | None = None)[source]
A decorator to register a class to the registry.
- Parameters:
key – The key to register the class with.
- Returns:
The decorated class.
- register_function(key: Hashable | None = None) Any[source]
A decorator to register a function itself to the registry. This registration happens only once when the function is first defined.
- Parameters:
key – A single key to register the function with.
- Returns:
The decorated function.
- property registry: Dict[Hashable, Any]
Access the underlying registry.
- Returns:
The dictionary containing all registered items.
- Return type:
Dict[Hashable, Any]
- remove(key: Hashable | None = None, value: Any = None, rmv_all: bool = False) None[source]
Remove an item from the registry by key or value.
- Parameters:
key – The key to remove. If None, removal is based on the value.
value – The value to remove. If None, removal is based on the key.
rmv_all – Whether to remove all matches. If True, removes all matches. Only needed if removed by value.
Else raises error if multiple matches are found. :raises ValueError: If both key and value are passed or None. :raises NotRegisteredError: If the key or value is not found and exceptions are enabled.