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 6, 2019
1 parent 5e4bc54 commit f7090db
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 15 deletions.
24 changes: 17 additions & 7 deletions Orange/widgets/data/owselectrows.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,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 1. 1. 2021:
# 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):
value[i] = (attr, op,
Expand All @@ -71,7 +76,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 1. 1. 2021:
# 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 @@ -705,11 +713,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 1. 1. 2021
#
# @classmethod
# def migrate_context(cls, context, version):
# if not version:
# # Just remove; can't migrate because variables types are unknown
# context.values["conditions"] = []


class CheckBoxPopup(QWidget):
Expand Down
43 changes: 35 additions & 8 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,40 @@ 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):
iris = Table("iris")
# Uncomment this on 1. 1. 2021
#
# 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, [])

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_use_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"))

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,
uncomment OWSelectRows.migrate, and remove this test.
Basically, undo this commit.
""")

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

0 comments on commit f7090db

Please sign in to comment.