Skip to content

Commit 5665869

Browse files
committed
FIX: allow fractional digits in comparator tolerance (closes #260)
1 parent 7a85833 commit 5665869

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

doc/source/changes/version_0_34_2.rst.inc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@ Fixes
99

1010
* when code in the interactive console creates *and shows* a plot window, avoid showing it
1111
a second time (closes :editor_issue:`265`).
12+
13+
* depending on the system regional settings, comparator tolerance sometimes did not allow simple
14+
fractional numbers (e.g. 0.1). The only way to specify the tolerance was the scientific notation
15+
(closes :editor_issue:`260`).

larray_editor/comparator.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
import ast
2-
31
import numpy as np
42
import larray as la
53

64
from qtpy.QtCore import Qt
7-
from qtpy.QtGui import QDoubleValidator
85
from qtpy.QtWidgets import (QWidget, QVBoxLayout, QListWidget, QSplitter, QHBoxLayout,
96
QLabel, QCheckBox, QLineEdit, QComboBox, QMessageBox)
107

@@ -53,8 +50,10 @@ def __init__(self, parent=None, bg_gradient='red-white-blue', rtol=0, atol=0, na
5350
tolerance_layout.addWidget(tolerance_combobox)
5451
self.tolerance_combobox = tolerance_combobox
5552

53+
# We do not use a QDoubleValidator because, by default, it uses the
54+
# system locale (so we would need to parse the string using that
55+
# locale too) and does not provide any feedback to users on failure
5656
tolerance_line_edit = QLineEdit()
57-
tolerance_line_edit.setValidator(QDoubleValidator())
5857
tolerance_line_edit.setPlaceholderText("1e-8")
5958
tolerance_line_edit.setMaximumWidth(80)
6059
tolerance_line_edit.setToolTip("Press Enter to activate the new tolerance value")
@@ -118,8 +117,17 @@ def update_isequal(self):
118117

119118
try:
120119
tol_str = self.tolerance_line_edit.text()
121-
tol = ast.literal_eval(tol_str) if tol_str else 0
122-
atol, rtol = (tol, 0) if self.tolerance_combobox.currentText() == "absolute" else (0, tol)
120+
tol = float(tol_str) if tol_str else 0
121+
except ValueError as e:
122+
# this is necessary to avoid having the error message twice, because we
123+
# first show it here, which makes the tolerance_line_edit lose focus,
124+
# which triggers its editingFinished signal, which calls update_isequal,
125+
# which ends up here again if tol_str did not change in-between.
126+
self.tolerance_line_edit.setText('')
127+
tol = 0
128+
QMessageBox.critical(self, "Error", str(e))
129+
atol, rtol = (tol, 0) if self.tolerance_combobox.currentText() == "absolute" else (0, tol)
130+
try:
123131
self.isequal = self.array.eq(self.array0, rtol=rtol, atol=atol, nans_equal=self.nans_equal)
124132
except TypeError:
125133
self.isequal = self.array == self.array0

0 commit comments

Comments
 (0)