mudata.register_mudata_namespace#
- mudata.register_mudata_namespace(name)#
Decorator for registering custom functionality with a
MuDataobject.This decorator allows you to extend MuData objects with custom methods and properties organized under a namespace. The namespace becomes accessible as an attribute on MuData instances, providing a clean way to you to add domain-specific functionality without modifying the MuData class itself, or extending the class with additional methods as you see fit in your workflow.
- Parameters:
name (
str) – Name under which the accessor should be registered. This will be the attribute name used to access your namespace’s functionality on MuData objects (e.g.,instance.name). Cannot conflict with existing MuData attributes. The list of reserved attributes includes everything outputted bydir(MuData).- Return type:
Callable[[type[TypeVar(NameSpT, bound=ExtensionNamespace)]],type[TypeVar(NameSpT, bound=ExtensionNamespace)]]- Returns:
A decorator that registers the decorated class as a custom namespace.
Notes
Implementation requirements:
The decorated class must have an
__init__method that accepts exactly one parameter (besidesself) namedmdataand annotated with typeMuData.The namespace will be initialized with the MuData object on first access and then cached on the instance.
If the namespace name conflicts with an existing namespace, a warning is issued.
If the namespace name conflicts with a built-in MuData attribute, an AttributeError is raised.
Examples
>>> @register_mudata_namespace("do_something") ... class DoSomething: ... def __init__(self, mdata: MuData): ... self._obj = mdata ... ... def has_foo(self) -> bool: ... return hasattr(self._obj, "foo") >>> >>> # Create a MuData object >>> obj = MuData() >>> >>> # use the registered namespace >>> obj.do_something.has_foo() False