File: class_method.py

package info (click to toggle)
magicgui 0.10.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 21,880 kB
  • sloc: python: 11,573; makefile: 11; sh: 9
file content (35 lines) | stat: -rw-r--r-- 979 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
"""# Deocrate class methods with magicgui

Demonstrates decorating a class method with magicgui.

Once the class is instantiated, `instance.method_name` will return a FunctionGui
in which the instance will always be provided as the first argument (i.e. "self") when
the FunctionGui or method is called.
"""

from magicgui import event_loop, magicgui
from magicgui.widgets import Container


class MyObject:
    """Example object class."""

    def __init__(self, name):
        self.name = name
        self.counter = 0.0

    @magicgui(auto_call=True)
    def method(self, sigma: float = 0):
        """Example class method."""
        print(f"instance: {self.name}, counter: {self.counter}, sigma: {sigma}")
        self.counter = self.counter + sigma
        return self.name


with event_loop():
    a = MyObject("a")
    b = MyObject("b")
    container = Container(widgets=[a.method, b.method])
    container.show()
    assert a.method() == "a"
    assert b.method() == "b"