From 9ed51cd1e265b163c2487c6df3a54a019618490e Mon Sep 17 00:00:00 2001 From: Chris Mullins Date: Sun, 28 Feb 2021 03:00:19 +0000 Subject: [PATCH] Add keys() method to Parameter class This returns the names attribute, allowing **unpacking of Parameter subclasses. --- pyqtgraph/parametertree/Parameter.py | 3 +++ pyqtgraph/parametertree/tests/test_Parameter.py | 17 +++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/pyqtgraph/parametertree/Parameter.py b/pyqtgraph/parametertree/Parameter.py index 6a41e1bb..ba4ca6b7 100644 --- a/pyqtgraph/parametertree/Parameter.py +++ b/pyqtgraph/parametertree/Parameter.py @@ -677,6 +677,9 @@ class Parameter(QtCore.QObject): names = (names,) return self.param(*names).setValue(value) + def keys(self): + return self.names + def child(self, *names): """Return a child parameter. Accepts the name of the child or a tuple (path, to, child) diff --git a/pyqtgraph/parametertree/tests/test_Parameter.py b/pyqtgraph/parametertree/tests/test_Parameter.py index a3de5493..70feab5f 100644 --- a/pyqtgraph/parametertree/tests/test_Parameter.py +++ b/pyqtgraph/parametertree/tests/test_Parameter.py @@ -34,3 +34,20 @@ def test_parameter_hasdefault_none(passdefault): p.setDefault(None) assert not p.hasDefault() + +def test_unpack_parameter(): + # test that **unpacking correctly returns child name/value maps + params = [ + dict(name='a', type='int', value=1), + dict(name='b', type='str', value='2'), + dict(name='c', type='float', value=3.0), + ] + p = Parameter.create(name='params', type='group', children=params) + result = dict(**p) + + assert 'a' in result + assert result['a'] == 1 + assert 'b' in result + assert result['b'] == '2' + assert 'c' in result + assert result['c'] == 3.0