|
1 |
| -import ast |
2 |
| - |
3 | 1 | import numpy as np
|
4 | 2 | import larray as la
|
5 | 3 |
|
6 | 4 | from qtpy.QtCore import Qt
|
7 |
| -from qtpy.QtGui import QDoubleValidator |
8 | 5 | from qtpy.QtWidgets import (QWidget, QVBoxLayout, QListWidget, QSplitter, QHBoxLayout,
|
9 | 6 | QLabel, QCheckBox, QLineEdit, QComboBox, QMessageBox)
|
10 | 7 |
|
@@ -53,8 +50,10 @@ def __init__(self, parent=None, bg_gradient='red-white-blue', rtol=0, atol=0, na
|
53 | 50 | tolerance_layout.addWidget(tolerance_combobox)
|
54 | 51 | self.tolerance_combobox = tolerance_combobox
|
55 | 52 |
|
| 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 |
56 | 56 | tolerance_line_edit = QLineEdit()
|
57 |
| - tolerance_line_edit.setValidator(QDoubleValidator()) |
58 | 57 | tolerance_line_edit.setPlaceholderText("1e-8")
|
59 | 58 | tolerance_line_edit.setMaximumWidth(80)
|
60 | 59 | tolerance_line_edit.setToolTip("Press Enter to activate the new tolerance value")
|
@@ -118,8 +117,17 @@ def update_isequal(self):
|
118 | 117 |
|
119 | 118 | try:
|
120 | 119 | 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: |
123 | 131 | self.isequal = self.array.eq(self.array0, rtol=rtol, atol=atol, nans_equal=self.nans_equal)
|
124 | 132 | except TypeError:
|
125 | 133 | self.isequal = self.array == self.array0
|
|
0 commit comments