Skip to content

Commit

Permalink
Create Class: Better error description for invalid regular expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
janezd committed Jan 20, 2025
1 parent be9a03b commit 39e0dd4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
22 changes: 11 additions & 11 deletions Orange/widgets/data/owcreateclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ class Warning(widget.OWWidget.Warning):
class Error(widget.OWWidget.Error):
class_name_duplicated = Msg("Class name duplicated.")
class_name_empty = Msg("Class name should not be empty.")
invalid_regular_expression = Msg("Invalid regular expression.")
invalid_regular_expression = Msg("Invalid regular expression: {}")

def __init__(self):
super().__init__()
Expand Down Expand Up @@ -456,15 +456,15 @@ def class_labels(self):
return [label_edit.text() or f"C{next(class_count)}"
for label_edit, _ in self.line_edits]

def check_patterns(self):
def invalid_patterns(self):
if not self.regular_expressions:
return True
for pattern in self.active_rules:
return None
for _, pattern in self.active_rules:
try:
re.compile(pattern[1])
re.compile(pattern)
except re.error:
return False
return True
return pattern
return None

def update_counts(self):
"""Recompute and update the counts of matches."""
Expand Down Expand Up @@ -569,8 +569,8 @@ def _set_placeholders():
lab_edit.setPlaceholderText(label)

_clear_labels()
if not self.check_patterns():
self.Error.invalid_regular_expression()
if (invalid := self.invalid_patterns()) is not None:
self.Error.invalid_regular_expression(invalid)
return
self.Error.invalid_regular_expression.clear()

Expand All @@ -588,8 +588,8 @@ def _set_placeholders():
def apply(self):
"""Output the transformed data."""
self.Error.clear()
if not self.check_patterns():
self.Error.invalid_regular_expression()
if (invalid := self.invalid_patterns()) is not None:
self.Error.invalid_regular_expression(invalid)
self.Outputs.data.send(None)
return

Expand Down
9 changes: 5 additions & 4 deletions Orange/widgets/data/tests/test_owcreateclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,10 +387,10 @@ def test_check_patterns(self):
widget.line_edits[0][1].setText("[a")

widget.regular_expressions = False
self.assertTrue(widget.check_patterns())
self.assertIsNone(widget.invalid_patterns())

widget.regular_expressions = True
self.assertFalse(widget.check_patterns())
self.assertEqual(widget.invalid_patterns(), "[a")

def test_check_re_counts(self):
widget = self.widget
Expand All @@ -407,13 +407,14 @@ def test_check_re_counts(self):
widget.apply()
self.assertIsNotNone(self.get_output(self.widget.Outputs.data))

widget.line_edits[0][1].setText("a[.*a")
widget.line_edits[1][1].setText("b[")
self._check_counts([["", ""], ["", ""], ["", ""]])
self.assertTrue(widget.Error.invalid_regular_expression.is_shown())
self.assertIn("b[", str(widget.Error.invalid_regular_expression))
widget.apply()
self.assertIsNone(self.get_output(self.widget.Outputs.data))

widget.line_edits[0][1].setText("a.*a")
widget.line_edits[1][1].setText("b")
self._check_counts([["45", ""], ["30", "+ 4"], ["26", ""]])
self.assertFalse(widget.Error.invalid_regular_expression.is_shown())
widget.apply()
Expand Down
2 changes: 1 addition & 1 deletion i18n/si/msgs.jaml
Original file line number Diff line number Diff line change
Expand Up @@ -4741,7 +4741,7 @@ widgets/data/owcreateclass.py:
class `Error`:
Class name duplicated.: Spremenljivka s tem imenom že obstaja.
Class name should not be empty.: Ime razreda ne more biti prazno.
Invalid regular expression.: Napačen regularni izraz.
'Invalid regular expression: {}': 'Napačen regularni izraz: {}'
def `__init__`:
class_name: false
New Class Name: Ime novega razreda
Expand Down

0 comments on commit 39e0dd4

Please sign in to comment.