LCOV - code coverage report
Current view: top level - src/pairinteraction_gui - utils.py (source / functions) Hit Total Coverage
Test: coverage.info Lines: 23 34 67.6 %
Date: 2026-04-17 09:29:39 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_singlet",
      14             :     "Sr88_triplet",
      15             :     "Sr87_mqdt",
      16             :     "Sr88_mqdt",
      17             :     "Yb171_mqdt",
      18             :     "Yb173_mqdt",
      19             :     "Yb174_mqdt",
      20             : ]
      21           1 : SpeciesTypes = Literal["sqdt_duplet", "sqdt_singlet", "sqdt_triplet", "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 "singlet" in species:
      53           0 :         return "sqdt_singlet"
      54           1 :     if "triplet" in species:
      55           0 :         return "sqdt_triplet"
      56           1 :     return "sqdt_duplet"
      57             : 
      58             : 
      59           1 : def label_to_object_name(label: str) -> str:
      60             :     """Convert a display label to an human readable object name (and QSettings key)."""
      61           1 :     label = label.lower().strip()
      62           1 :     label = re.sub(r"\s+", "_", label)
      63           1 :     label = re.sub(r"[\\/]+", "_", label)
      64           1 :     return label.replace("δ", "delta")

Generated by: LCOV version 1.16