Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Feature Constructor: no fail when no values #2417

Merged
merged 2 commits into from
Jun 21, 2017
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
14 changes: 8 additions & 6 deletions Orange/widgets/data/owfeatureconstructor.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ def setEditorData(self, data, domain):
def editorData(self):
values = self.valuesedit.text()
values = re.split(r"(?<!\\),", values)
values = tuple(v.replace(r"\,", ",").strip() for v in values)
values = tuple(filter(None, [v.replace(r"\,", ",").strip() for v in values]))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use generators instead of list comprehension, that is

values = tuple(filter(None, (v.replace(r",", ",").strip() for v in values)))

return DiscreteDescriptor(
name=self.nameedit.text(),
values=values,
Expand Down Expand Up @@ -292,7 +292,7 @@ def data(self, index, role=Qt.DisplayRole):
return super().data(index, role)


class FeatureConstructorSettingsHandler(DomainContextHandler):
class FeatureConstructorHandler(DomainContextHandler):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I preferred the original name. In fact, I'd call it FeatureConstructorContextHandler and disable the pylint's warning. The current name suggest that it handles the feature constructor, not its contexts.

"""Context handler that filters descriptors"""

def is_valid_item(self, setting, descriptor, attrs, metas):
Expand Down Expand Up @@ -326,7 +326,7 @@ class OWFeatureConstructor(OWWidget):

want_main_area = False

settingsHandler = FeatureConstructorSettingsHandler()
settingsHandler = FeatureConstructorHandler()
descriptors = ContextSetting([])
currentIndex = ContextSetting(-1)

Expand Down Expand Up @@ -850,7 +850,7 @@ def make_arg(name):
if sys.version_info >= (3, 0):
return ast.arg(arg=name, annotation=None)
else:
return ast.Name(id=arg, ctx=ast.Param(), lineno=1, col_offset=0)
return ast.Name(id=name, ctx=ast.Param(), lineno=1, col_offset=0)

lambda_ = ast.Lambda(
args=ast.arguments(
Expand Down Expand Up @@ -902,7 +902,7 @@ def make_arg(name):
"weibullvariate": random.weibullvariate,
"triangular": random.triangular,
"uniform": random.uniform}
)
)


class FeatureFunc:
Expand Down Expand Up @@ -930,8 +930,10 @@ def unique(seq):
return unique_el


def main(argv=sys.argv):
def main(argv=None):
from AnyQt.QtWidgets import QApplication
if argv is None:
argv = sys.argv
app = QApplication(list(argv))
argv = app.arguments()
if len(argv) > 1:
Expand Down
34 changes: 26 additions & 8 deletions Orange/widgets/data/tests/test_owfeatureconstructor.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
from Orange.widgets.data.owfeatureconstructor import (DiscreteDescriptor,
ContinuousDescriptor,
StringDescriptor,
construct_variables, OWFeatureConstructor)
construct_variables, OWFeatureConstructor,
DiscreteFeatureEditor)

from Orange.widgets.data.owfeatureconstructor import (
freevars, make_lambda, validate_exp
)
from Orange.widgets.data.owfeatureconstructor import freevars, validate_exp

import dill as pickle # Import dill after Orange because patched

Expand Down Expand Up @@ -99,11 +98,11 @@ class PicklingTest(unittest.TestCase):
def test_lambdas_pickle(self):
NONLOCAL_CONST = 5

lambda_func = lambda x, LOCAL_CONST=7: \
x * LOCAL_CONST * NONLOCAL_CONST * self.CLASS_CONST * GLOBAL_CONST
lambda_func = lambda x, local_const=7: \
x * local_const * NONLOCAL_CONST * self.CLASS_CONST * GLOBAL_CONST

def nested_func(x, LOCAL_CONST=7):
return x * LOCAL_CONST * NONLOCAL_CONST * self.CLASS_CONST * GLOBAL_CONST
def nested_func(x, local_const=7):
return x * local_const * NONLOCAL_CONST * self.CLASS_CONST * GLOBAL_CONST

self.assertEqual(lambda_func(11),
pickle.loads(pickle.dumps(lambda_func))(11))
Expand Down Expand Up @@ -240,3 +239,22 @@ def test_error_invalid_expression(self):
)
self.widget.apply()
self.assertTrue(self.widget.Error.invalid_expressions.is_shown())

def test_discrete_no_values(self):
"""
Should not fail when there are no values set.
GH-2417
"""
data = Table("iris")
self.widget.setData(data)
discreteFeatureEditor = DiscreteFeatureEditor()

discreteFeatureEditor.valuesedit.setText("")
discreteFeatureEditor.nameedit.setText("D1")
discreteFeatureEditor.expressionedit.setText("iris")
self.widget.addFeature(
discreteFeatureEditor.editorData()
)
self.assertFalse(self.widget.Error.more_values_needed.is_shown())
self.widget.apply()
self.assertTrue(self.widget.Error.more_values_needed.is_shown())