Skip to content

Commit

Permalink
Select Rows: Add code for support of old settings and ensure its remo…
Browse files Browse the repository at this point in the history
…val in the future
  • Loading branch information
janezd committed Dec 13, 2019
1 parent 1c69e6f commit 7323aac
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 14 deletions.
24 changes: 17 additions & 7 deletions Orange/widgets/data/owselectrows.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,12 @@ def encode_setting(self, context, setting, value):
def decode_setting(self, setting, value, domain=None):
value = super().decode_setting(setting, value, domain)
if setting.name == 'conditions':
for i, (attr, _, op, values) in enumerate(value):
# Use this after 2021/1/1:
# for i, (attr, _, op, values) in enumerate(value):
for i, condition in enumerate(value):
attr = condition[0]
op, values = condition[-2:]

var = attr in domain and domain[attr]
if var and var.is_continuous and not isinstance(var, TimeVariable):
values = [QLocale().toString(float(i), 'f') for i in values]
Expand All @@ -73,7 +78,10 @@ def match(self, context, domain, attrs, metas):
conditions = context.values["conditions"]
all_vars = attrs
all_vars.update(metas)
if all(all_vars.get(name) == tpe for name, tpe, *_ in conditions):
# Use this after 2021/1/1:
# 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
return self.NO_MATCH

Expand Down Expand Up @@ -707,11 +715,13 @@ def send_report(self):
("Non-matching data",
nonmatch_inst > 0 and "{} instances".format(nonmatch_inst))))

@classmethod
def migrate_context(cls, context, version):
if not version:
# Just remove; can't migrate because variables types are unknown
context.values["conditions"] = []
# Uncomment this on 2021/1/1
#
# @classmethod
# def migrate_context(cls, context, version):
# if not version or version < 2:
# # Just remove; can't migrate because variables types are unknown
# context.values["conditions"] = []


class CheckBoxPopup(QWidget):
Expand Down
44 changes: 37 additions & 7 deletions Orange/widgets/data/tests/test_owselectrows.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Test methods with long descriptive names can omit docstrings
# pylint: disable=missing-docstring
import time

from AnyQt.QtCore import QLocale, Qt
from AnyQt.QtTest import QTest
from AnyQt.QtWidgets import QLineEdit, QComboBox
Expand Down Expand Up @@ -259,15 +261,43 @@ def test_change_var_type(self):
new_iris = iris.transform(new_domain)
self.send_signal(self.widget.Inputs.data, new_iris)

def test_migration_to_version_1(self):
# Uncomment this on 2021/1/1
#
# def test_migration_to_version_1(self):
# iris = Table("iris")
#
# ch = SelectRowsContextHandler()
# context = ch.new_context(iris.domain, *ch.encode_domain(iris.domain))
# context.values = dict(conditions=[["petal length", 2, (5.2,)]])
# settings = dict(context_settings=[context])
# widget = self.create_widget(OWSelectRows, settings)
# self.assertEqual(widget.conditions, [])

@override_locale(QLocale.C)
def test_support_old_settings(self):
iris = Table("iris")
self.widget = self.widget_with_context(
iris.domain, [["sepal length", 2, ("5.2",)]])
self.send_signal(self.widget.Inputs.data, iris)
condition = self.widget.conditions[0]
self.assertEqual(condition[0], "sepal length")
self.assertEqual(condition[1], 2)
self.assertTrue(condition[2][0].startswith("5.2"))

ch = SelectRowsContextHandler()
context = ch.new_context(iris.domain, *ch.encode_domain(iris.domain))
context.values = dict(conditions=[["petal length", 2, (5.2,)]])
settings = dict(context_settings=[context])
widget = self.create_widget(OWSelectRows, settings)
self.assertEqual(widget.conditions, [])
def test_end_support_for_version_0(self):
if time.gmtime().tm_year == 2021:
self.fail("""
Happy new year 2021!
Now remove support for version==None settings in
SelectRowsContextHandler.decode_setting and SelectRowsContextHandler.match,
and uncomment OWSelectRows.migrate.
In tests, uncomment test_migration_to_version_1,
and remove test_support_old_settings and this test.
Basically, revert this commit.
""")

def widget_with_context(self, domain, conditions):
ch = SelectRowsContextHandler()
Expand Down

0 comments on commit 7323aac

Please sign in to comment.