From f237f9688e06ae46fe98d195b134d01f5b38b2e0 Mon Sep 17 00:00:00 2001 From: janezd Date: Tue, 5 May 2020 18:01:12 +0200 Subject: [PATCH] SelectRows: Allow partial context matches --- Orange/widgets/data/owselectrows.py | 27 +++++++++++++++---- .../widgets/data/tests/test_owselectrows.py | 14 ++++++++++ 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/Orange/widgets/data/owselectrows.py b/Orange/widgets/data/owselectrows.py index b8806f3698a..5d213b28cc4 100644 --- a/Orange/widgets/data/owselectrows.py +++ b/Orange/widgets/data/owselectrows.py @@ -80,13 +80,30 @@ def match(self, context, domain, attrs, metas): conditions = context.values["conditions"] all_vars = attrs.copy() all_vars.update(metas) - # Use this after 2022/2/2: - # if all(all_vars.get(name) == tpe for name, tpe, *_ in conditions): - if all(all_vars.get(name) == tpe if len(rest) == 2 else name in all_vars - for name, tpe, *rest in conditions): - return 0.5 + matched = [all_vars.get(name) == tpe + # After 2022/2/2 remove this line: + if len(rest) == 2 else name in all_vars + for name, tpe, *rest in conditions] + if any(matched): + return 0.5 * sum(matched) / len(matched) return self.NO_MATCH + def filter_value(self, setting, data, domain, attrs, metas): + if setting.name != "conditions": + super().filter_value(setting, data, domain, attrs, metas) + return + + all_vars = attrs.copy() + all_vars.update(metas) + conditions = data["conditions"] + # Use this after 2022/2/2: if any(all_vars.get(name) == tpe: + # conditions[:] = [(name, tpe, *rest) for name, tpe, *rest in conditions + # if all_vars.get(name) == tpe] + conditions[:] = [ + (name, tpe, *rest) for name, tpe, *rest in conditions + if (all_vars.get(name) == tpe if len(rest) == 2 + else name in all_vars)] + class FilterDiscreteType(enum.Enum): Equal = "Equal" diff --git a/Orange/widgets/data/tests/test_owselectrows.py b/Orange/widgets/data/tests/test_owselectrows.py index b3a04178a97..bdbee4bd9fa 100644 --- a/Orange/widgets/data/tests/test_owselectrows.py +++ b/Orange/widgets/data/tests/test_owselectrows.py @@ -191,6 +191,20 @@ def test_partial_matches(self): self.assertEqual(condition[1], 2) self.assertTrue(condition[2][0].startswith("5.2")) + @override_locale(QLocale.C) + def test_partial_matches_with_missing_vars(self): + iris = Table("iris") + domain = iris.domain + self.widget = self.widget_with_context( + domain, [[domain[0].name, 2, ("5.2",)], + [domain[2].name, 2, ("4.2",)]]) + iris2 = iris.transform(Domain(domain.attributes[2:], None)) + self.send_signal(self.widget.Inputs.data, iris2) + condition = self.widget.conditions[0] + self.assertEqual(condition[0], domain[2].name) + self.assertEqual(condition[1], 2) + self.assertTrue(condition[2][0].startswith("4.2")) + def test_load_settings(self): iris = Table("iris")[:5] self.send_signal(self.widget.Inputs.data, iris)