Skip to content

Method macro

MethodMacro

Bases: BaseMacro

Decorator that marks methods which should be exposed as macros.

Source code in quam/core/macro/method_macro.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class MethodMacro(BaseMacro):
    """Decorator that marks methods which should be exposed as macros."""

    def __init__(self, func: T) -> None:
        functools.wraps(func)(self)
        self.func = func
        self.instance = None

    def __get__(self, instance, owner):
        # Store the instance to which this method is bound
        self.instance = instance
        return self

    def apply(self, *args, **kwargs) -> Any:
        """Implements BaseMacro.apply by calling the wrapped function"""
        if self.instance is not None:
            # Call the function with the instance as the first argument
            return self.func(self.instance, *args, **kwargs)
        return self.func(*args, **kwargs)

    def __call__(self, *args, **kwargs):
        if args and args[0] is self.instance:
            args = args[1:]
        return self.apply(*args, **kwargs)

    @staticmethod
    def is_macro_method(obj: Any) -> bool:
        return isinstance(obj, MethodMacro)

apply(*args, **kwargs)

Implements BaseMacro.apply by calling the wrapped function

Source code in quam/core/macro/method_macro.py
26
27
28
29
30
31
def apply(self, *args, **kwargs) -> Any:
    """Implements BaseMacro.apply by calling the wrapped function"""
    if self.instance is not None:
        # Call the function with the instance as the first argument
        return self.func(self.instance, *args, **kwargs)
    return self.func(*args, **kwargs)