Skip to content

Commit

Permalink
fix eager for register function with params=None
Browse files Browse the repository at this point in the history
  • Loading branch information
ianhi committed Apr 2, 2021
1 parent bc70108 commit 5fa4613
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
6 changes: 4 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"python.pythonPath": "/home/ian/anaconda3/envs/mpl-inter/bin/python",
"python.formatting.provider": "black"
"python.formatting.provider": "black",
"python.languageServer": "Pylance",
"python.linting.enabled": false,
"python.analysis.completeFunctionParens": true
}
2 changes: 1 addition & 1 deletion mpl_interactions/_version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
version_info = (0, 17, 6)
version_info = (0, 17, 7)
__version__ = ".".join(map(str, version_info))
17 changes: 11 additions & 6 deletions mpl_interactions/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,16 +191,21 @@ def register_callback(self, callback, params=None, eager=False):
Parameters
----------
callback : callable
a function called
params : list of str, str, or None
the params to be passed to the callback
eager : bool
Whether to call the callback immediately upon registration
A function called. Should accept all of the parameters specified by *params*
as a kwargs.
params : str, list of str, or None
The params to be passed to the callback. If *None* then all params
currently registered with this controls object will be used.
eager : bool, default: False
If True, call the callback immediately upon registration
"""
if isinstance(params, str):
params = [params]
if eager:
callback(**{key: self.params[key] for key in params})
if params is None:
callback(**self.params)
else:
callback(**{key: self.params[key] for key in params})
self._register_function(callback, fig=None, params=params)

def _register_function(self, f, fig=None, params=None):
Expand Down
16 changes: 16 additions & 0 deletions mpl_interactions/tests/test_controls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from mpl_interactions.controller import Controls


def test_eager_register():
zoop = []

def cb(**kwargs):
zoop.append(1)

ctrls = Controls(beep=(0, 1), boop=(0, 1))
ctrls.register_callback(cb, None, eager=True)
assert len(zoop) == 1

ctrls = Controls(beep=(0, 1), boop=(0, 1))
ctrls.register_callback(cb, "beep", eager=True)
assert len(zoop) == 1

0 comments on commit 5fa4613

Please sign in to comment.