Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add call count to function gui #86

Merged
merged 1 commit into from
Jan 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions magicgui/function_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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.

Expand Down Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions tests/test_magicgui.py
Original file line number Diff line number Diff line change
Expand Up @@ -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