LCOV - code coverage report
Current view: top level - src/pairinteraction_gui - utils.py (source / functions) Hit Total Coverage
Test: coverage.info Lines: 22 32 68.8 %
Date: 2026-08-17 11:46:44 Functions: 4 5 80.0 %

          Line data    Source code
       1             : # SPDX-FileCopyrightText: 2025 PairInteraction Developers
       2             : # SPDX-License-Identifier: LGPL-3.0-or-later
       3             : 
       4           1 : import re
       5           1 : from typing import Literal
       6             : 
       7           1 : AVAILABLE_SPECIES = [
       8             :     "Rb",
       9             :     "Li",
      10             :     "Na",
      11             :     "K",
      12             :     "Cs",
      13             :     "Sr88_sqdt",
      14             :     "Sr87_mqdt",
      15             :     "Sr88_mqdt",
      16             :     "Yb171_mqdt",
      17             :     "Yb173_mqdt",
      18             :     "Yb174_mqdt",
      19             : ]
      20           1 : SpeciesTypes = Literal["sqdt_monovalent", "sqdt_divalent", "mqdt_halfint", "mqdt_int"]
      21             : 
      22             : 
      23           1 : class DatabaseMissingError(Exception):
      24           1 :     def __init__(self, err: RuntimeError) -> None:
      25           0 :         super().__init__(str(err))
      26             : 
      27             : 
      28           1 : class NoStateFoundError(Exception):
      29           1 :     def __init__(self, err: ValueError) -> None:
      30           1 :         super().__init__(str(err))
      31             : 
      32             : 
      33           1 : def get_custom_error(err: Exception) -> Exception:
      34             :     """Get a custom error message based on the type of error."""
      35           1 :     if isinstance(err, RuntimeError) and "No tables found for" in str(err):
      36           0 :         return DatabaseMissingError(err)
      37           1 :     if isinstance(err, ValueError) and ("No state found" in str(err) or "quantum number m must be" in str(err)):
      38           1 :         return NoStateFoundError(err)
      39           0 :     return err
      40             : 
      41             : 
      42           1 : def get_species_type(species: str) -> SpeciesTypes:
      43             :     """Return the species type based on the species name of the ... atom."""
      44           1 :     if "mqdt" in species:
      45           0 :         match = re.search(r"\d+", species)
      46           0 :         if match:
      47           0 :             if int(match.group()) % 2 == 0:
      48           0 :                 return "mqdt_int"
      49           0 :             return "mqdt_halfint"
      50           0 :         raise ValueError(f"Invalid species name: {species}")
      51           1 :     if species.endswith("_sqdt"):  # two valence electrons, i.e. integer spin s (singlet or triplet)
      52           0 :         return "sqdt_divalent"
      53           1 :     return "sqdt_monovalent"
      54             : 
      55             : 
      56           1 : def label_to_object_name(label: str) -> str:
      57             :     """Convert a display label to an human readable object name (and QSettings key)."""
      58           1 :     label = label.lower().strip()
      59           1 :     label = re.sub(r"\s+", "_", label)
      60           1 :     label = re.sub(r"[\\/]+", "_", label)
      61           1 :     return label.replace("δ", "delta")

Generated by: LCOV version 1.16