From 789801228a4dfab770518eb675659a771feda453 Mon Sep 17 00:00:00 2001 From: Dominik Demuth Date: Sun, 17 Dec 2023 16:09:59 +0000 Subject: [PATCH] 179 (#187) closes #179 Co-authored-by: Dominik Demuth Reviewed-on: https://gitea.pkm.physik.tu-darmstadt.de/IPKM/nmreval/pulls/187 --- src/gui_qt/io/asciireader.py | 34 ++++++++++++++++++++++++---------- src/nmreval/utils/__init__.py | 8 ++++++++ 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/src/gui_qt/io/asciireader.py b/src/gui_qt/io/asciireader.py index 8cbbd37..a4c9524 100644 --- a/src/gui_qt/io/asciireader.py +++ b/src/gui_qt/io/asciireader.py @@ -3,7 +3,7 @@ from __future__ import annotations import re 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 .._py.asciidialog import Ui_ascii_reader @@ -243,19 +243,31 @@ class QAsciiReader(QtWidgets.QDialog, Ui_ascii_reader): if self.reader is None: return - try: - 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 = [] + success = True + 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.setMaximum(len(self._matches)) self.re_match_index.blockSignals(False) 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()) @@ -273,7 +285,9 @@ class QAsciiReader(QtWidgets.QDialog, Ui_ascii_reader): val = 0 if self.re_button.isChecked() and self._matches: 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(): val = float(self.custom_input.text()) diff --git a/src/nmreval/utils/__init__.py b/src/nmreval/utils/__init__.py index 24e8009..64ec28c 100755 --- a/src/nmreval/utils/__init__.py +++ b/src/nmreval/utils/__init__.py @@ -5,3 +5,11 @@ from .constants import * 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