Skip to content
This repository was archived by the owner on Nov 6, 2025. It is now read-only.
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
23 changes: 15 additions & 8 deletions src/pymodaq_gui/parameter/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,22 +147,24 @@ def getValues(param:Parameter,) -> OrderedDict:
return param.getValues()


def compareParameters(param1:Parameter, param2:Parameter, opts: list = None)-> bool:
def compareParameters(param1:Parameter, param2:Parameter, with_self: bool = True)-> bool:
"""Compare the structure and the opts of two parameters with their children,
return True if structure and all opts are identical
return True if structure and all opts are identical.
If with_self is False, only the children opts are compared.
Parameters
----------
param1: Parameter
param2: Parameter
with_self: bool

Returns
-------
Bool
"""
if opts is None:
opts = []
return getOpts(param1) == getOpts(param2)

is_same = getOpts(param1) == getOpts(param2)
if with_self:
is_same = is_same and (param1.opts == param2.opts)
return is_same

def compareStructureParameter(param1:Parameter, param2: Parameter,)-> bool:
"""Compare the structure of two parameters with their children, return True if structure is identical
Expand All @@ -178,18 +180,23 @@ def compareStructureParameter(param1:Parameter, param2: Parameter,)-> bool:
return getStruct(param1) == getStruct(param2)


def compareValuesParameter(param1:Parameter, param2: Parameter,)-> bool:
def compareValuesParameter(param1:Parameter, param2: Parameter, with_self: bool = True)-> bool:
"""Compare the structure and the values of two parameters with their children, return True if structures and values are identical
Parameters
If with_self is False, only the children opts are compared.
----------
param1: Parameter
param2: Parameter
with_self: bool

Returns
-------
Bool
"""
return getValues(param1) == getValues(param2)
is_same = getValues(param1) == getValues(param2)
if with_self:
is_same = is_same and (param1.value == param2.value)
return is_same


def iter_children(param, childlist: list = [], filter_type=(), filter_name=(), select_filter=False)-> list:
Expand Down
6 changes: 3 additions & 3 deletions tests/managers/parameter_manager_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def test_load(qtbot, tmp_path):
ptree.save_settings_slot(file_path)

parameter_copy = Parameter.create(name='settings', type='group', children=ParameterEx.params)
assert compareValuesParameter(ptree.settings, parameter_copy)
assert compareValuesParameter(ptree.settings, parameter_copy, with_self=False)

parameters = iter_children_params(ptree.settings, childlist=[])
parameters_copy = iter_children_params(parameter_copy, childlist=[])
Expand All @@ -99,7 +99,7 @@ def test_load(qtbot, tmp_path):
elif 'tabular_table' == parameter.opts['type']:
parameter.setValue(TableModel([[0.5, 0.2, 0.6]], ['value20', 'val2', '555']))

assert not compareValuesParameter(ptree.settings, parameter_copy)
assert not compareValuesParameter(ptree.settings, parameter_copy, with_self=False)
assert compareStructureParameter(ptree.settings, parameter_copy)

ptree.update_settings_slot(file_path)
Expand All @@ -109,7 +109,7 @@ def test_load(qtbot, tmp_path):
if parameter.value() != pcopy.value():
print(parameter)

assert compareValuesParameter(ptree.settings, parameter_copy)
assert compareValuesParameter(ptree.settings, parameter_copy, with_self=False)
assert compareStructureParameter(ptree.settings, parameter_copy)

ptree.settings_tree.close()
Expand Down
17 changes: 14 additions & 3 deletions tests/parameter_test/param_utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,13 @@
[{'title': 'Standard int:', 'name': 'aint', 'type': 'int', 'value': 20,}]},
]},
]

P1 = Parameter(name='settings1', type='group', children=params1)
P2 = Parameter(name='settings2', type='group', children=params2)
P3 = Parameter(name='settings3', type='group', children=params3)
P4 = Parameter(name='settings4', type='group', children=params4)
P1_bool = Parameter(name='settings1', type='bool', children=params1)
P1_noedit = Parameter(name='settings1', type='group', children=params1, editable=False)

def test_iter_children_params():
settings = Parameter.create(name='settings', type='group', children=params)
Expand Down Expand Up @@ -144,7 +147,13 @@ def test_compareParameters():
assert [putils.compareParameters(param1=P1,param2=P1) == True,
putils.compareParameters(param1=P1,param2=P2) == False,
putils.compareParameters(param1=P1,param2=P3) == False,
putils.compareParameters(param1=P1,param2=P4) == False]
putils.compareParameters(param1=P1,param2=P4) == False,
putils.compareParameters(param1=P1,param2=P1_bool) == False,
putils.compareParameters(param1=P1,param2=P1_bool, with_self=False) == True,
putils.compareParameters(param1=P1,param2=P1_noedit) == False,
putils.compareParameters(param1=P1,param2=P1_noedit, with_self=False) == True]


def test_compareStructureParameter():
assert [putils.compareStructureParameter(param1=P1,param2=P1) == True,
putils.compareStructureParameter(param1=P1,param2=P2) == True,
Expand All @@ -155,7 +164,9 @@ def test_compareValuesParameter():
assert [putils.compareValuesParameter(param1=P1,param2=P1) == True,
putils.compareValuesParameter(param1=P1,param2=P2) == True,
putils.compareValuesParameter(param1=P1,param2=P3) == False,
putils.compareValuesParameter(param1=P1,param2=P4) == False]
putils.compareValuesParameter(param1=P1,param2=P4) == False,
putils.compareValuesParameter(param1=P1,param2=P1_bool) == False,
putils.compareValuesParameter(param1=P1,param2=P1_bool, with_self=False) == True]


class TestScroll:
Expand Down Expand Up @@ -240,6 +251,6 @@ def test_ParameterWithPath_serialize():
param_back: putils.ParameterWithPath = putils.ser_factory.get_apply_deserializer(
putils.ser_factory.get_apply_serializer(p1_with_path))
assert param_back.path == p1_with_path.path
assert putils.compareParameters(param_back.parameter, p1_with_path.parameter)
assert putils.compareParameters(param_back.parameter, p1_with_path.parameter, with_self=False)
assert type(p1_with_path.parameter) == type(param_back.parameter)

Loading