1
0
forked from IPKM/nmreval
closes #179

Co-authored-by: Dominik Demuth <dominik.demuth@physik.tu-darmstadt.de>
Reviewed-on: IPKM/nmreval#187
This commit is contained in:
Dominik Demuth 2023-12-17 16:09:59 +00:00
parent ce9bd5d2fd
commit 789801228a
2 changed files with 32 additions and 10 deletions

View File

@ -3,7 +3,7 @@ from __future__ import annotations
import re import re
from nmreval.io.asciireader import AsciiReader from nmreval.io.asciireader import AsciiReader
from nmreval.utils import NUMBER_RE from nmreval.utils import NUMBER_RE, numbers_from_string
from ..Qt import QtGui, QtCore, QtWidgets from ..Qt import QtGui, QtCore, QtWidgets
from .._py.asciidialog import Ui_ascii_reader from .._py.asciidialog import Ui_ascii_reader
@ -243,19 +243,31 @@ class QAsciiReader(QtWidgets.QDialog, Ui_ascii_reader):
if self.reader is None: if self.reader is None:
return return
try: success = True
pattern = re.compile(pattern)
self.regex_input.setStyleSheet('color: rgb(0, 0, 0)')
self._matches = [m for m in pattern.finditer(str(self.reader.fname.stem))]
except re.error:
self._matches = [] self._matches = []
if self._matches: if pattern:
try:
re_pattern = re.compile(pattern)
except re.error:
success = False
else:
self._matches = [m for m in re_pattern.finditer(str(self.reader.fname.stem))]
else:
success = False
# matches exist and have numbers in them
if self._matches and all([len(numbers_from_string(m.group())) for m in self._matches]):
self.re_match_index.blockSignals(True) self.re_match_index.blockSignals(True)
self.re_match_index.setMaximum(len(self._matches)) self.re_match_index.setMaximum(len(self._matches))
self.re_match_index.blockSignals(False) self.re_match_index.blockSignals(False)
else: else:
self.regex_input.setStyleSheet('color: rgb(255, 0, 0)') success = False
if success:
self.regex_input.setStyleSheet('color: rgb(0, 0, 0)')
else:
self.regex_input.setStyleSheet('background-color: rgba(255, 0, 0, 50)')
self.show_match(self.re_match_index.value()) self.show_match(self.re_match_index.value())
@ -273,7 +285,9 @@ class QAsciiReader(QtWidgets.QDialog, Ui_ascii_reader):
val = 0 val = 0
if self.re_button.isChecked() and self._matches: if self.re_button.isChecked() and self._matches:
m = self._matches[self.re_match_index.value()-1] m = self._matches[self.re_match_index.value()-1]
val = float(NUMBER_RE.search(m.group()).group().replace('p', '.')) val = numbers_from_string(m.group())
# numbers_from returns list of floats we use first match if available
val = val[0] if val else 0.0
elif self.custom_button.isChecked(): elif self.custom_button.isChecked():
val = float(self.custom_input.text()) val = float(self.custom_input.text())

View File

@ -5,3 +5,11 @@ from .constants import *
NUMBER_RE = re.compile(r'[-+]?\d*[+p.]?\d+([eE][-+]?\d+)?', re.MULTILINE) NUMBER_RE = re.compile(r'[-+]?\d*[+p.]?\d+([eE][-+]?\d+)?', re.MULTILINE)
UNSIGNED_NUMBER_RE = re.compile(r'[-+]?\d*[+p.]?\d+([eE][-+]?\d+)?', re.MULTILINE) UNSIGNED_NUMBER_RE = re.compile(r'[-+]?\d*[+p.]?\d+([eE][-+]?\d+)?', re.MULTILINE)
def numbers_from_string(any_string: str) -> list[float]:
print(any_string)
matches = []
for m in NUMBER_RE.finditer(any_string):
matches.append(float(m.group().replace('p', '.')))
return matches