From 211ab1a1bb3a331da4b6b38589103bfb1edab2c0 Mon Sep 17 00:00:00 2001 From: Talley Lambert Date: Tue, 5 Jan 2021 13:25:50 -0500 Subject: [PATCH] add call count to function gui --- magicgui/function_gui.py | 11 +++++++++++ tests/test_magicgui.py | 15 +++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/magicgui/function_gui.py b/magicgui/function_gui.py index b811af9c6..5dd741b66 100644 --- a/magicgui/function_gui.py +++ b/magicgui/function_gui.py @@ -96,6 +96,7 @@ def __init__( self._result_name = "" self._bound: Dict[str, Any] = {} self.bind(bind) + self._call_count: int = 0 self._call_button: Optional[PushButton] = None if call_button: @@ -118,6 +119,15 @@ def __init__( if show: self.show() + @property + def call_count(self) -> int: + """Return the number of times the function has been called.""" + return self._call_count + + def reset_call_count(self) -> None: + """Reset the call count to 0.""" + self._call_count = 0 + def bind(self, kwargs: dict): """Bind key/value pairs to the function signature. @@ -202,6 +212,7 @@ def __call__(self, *args: Any, **kwargs: Any): bound.apply_defaults() value = self._function(*bound.args, **bound.kwargs) + self._call_count += 1 if self._result_widget is not None: with self._result_widget.changed.blocker(): self._result_widget.value = value diff --git a/tests/test_magicgui.py b/tests/test_magicgui.py index a22103e6d..727c40f4b 100644 --- a/tests/test_magicgui.py +++ b/tests/test_magicgui.py @@ -463,3 +463,18 @@ def method(self, sigma: float = 1): assert b.method(sigma=4) == ("b", 4) assert a.method() == ("a", 2) assert b.method() == ("b", 5) + + +def test_call_count(): + """Test that a function gui remembers how many times it's been called.""" + + @magicgui + def func(): + pass + + assert func.call_count == 0 + func() + func() + assert func.call_count == 2 + func.reset_call_count() + assert func.call_count == 0