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

            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              :     "Sr88_ion",
      20              : ]
      21            1 : SpeciesTypes = Literal["sqdt_monovalent", "sqdt_divalent", "mqdt_halfint", "mqdt_int"]
      22              : 
      23              : 
      24            1 : class DatabaseMissingError(Exception):
      25            1 :     def __init__(self, err: RuntimeError) -> None:
      26            0 :         super().__init__(str(err))
      27              : 
      28              : 
      29            1 : class NoStateFoundError(Exception):
      30            1 :     def __init__(self, err: ValueError) -> None:
      31            1 :         super().__init__(str(err))
      32              : 
      33              : 
      34            1 : def get_custom_error(err: Exception) -> Exception:
      35              :     """Get a custom error message based on the type of error."""
      36            1 :     if isinstance(err, RuntimeError) and "No tables found for" in str(err):
      37            0 :         return DatabaseMissingError(err)
      38            1 :     if isinstance(err, ValueError) and ("No state found" in str(err) or "quantum number m must be" in str(err)):
      39            1 :         return NoStateFoundError(err)
      40            0 :     return err
      41              : 
      42              : 
      43            1 : def get_species_type(species: str) -> SpeciesTypes:
      44              :     """Return the species type based on the species name of the ... atom."""
      45            1 :     if "mqdt" in species:
      46            0 :         match = re.search(r"\d+", species)
      47            0 :         if match:
      48            0 :             if int(match.group()) % 2 == 0:
      49            0 :                 return "mqdt_int"
      50            0 :             return "mqdt_halfint"
      51            0 :         raise ValueError(f"Invalid species name: {species}")
      52            1 :     if species.endswith("_sqdt"):  # two valence electrons, i.e. integer spin s (singlet or triplet)
      53            0 :         return "sqdt_divalent"
      54            1 :     return "sqdt_monovalent"
      55              : 
      56              : 
      57            1 : def label_to_object_name(label: str) -> str:
      58              :     """Convert a display label to an human readable object name (and QSettings key)."""
      59            1 :     label = label.lower().strip()
      60            1 :     label = re.sub(r"\s+", "_", label)
      61            1 :     label = re.sub(r"[\\/]+", "_", label)
      62            1 :     return label.replace("δ", "delta")
        

Generated by: LCOV version 2.0-1