mudata.register_mudata_namespace

mudata.register_mudata_namespace#

mudata.register_mudata_namespace(name)#

Decorator for registering custom functionality with a MuData object.

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 by dir(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:

  1. The decorated class must have an __init__ method that accepts exactly one parameter (besides self) named mdata and annotated with type MuData.

  2. The namespace will be initialized with the MuData object on first access and then cached on the instance.

  3. If the namespace name conflicts with an existing namespace, a warning is issued.

  4. 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