add InvalidIsotopeError and DimensionError

This commit is contained in:
Pim Nelissen
2026-02-25 14:17:14 +01:00
parent bab41c128b
commit e926338b69
3 changed files with 16 additions and 5 deletions

View File

@ -14,12 +14,20 @@ class OutOfBoundsError(Exception):
"""Raised when an object is attempted to be placed out of bounds.""" """Raised when an object is attempted to be placed out of bounds."""
class MissingNestedKeyError(Exception): class MissingConfigKeyError(KeyError):
"""Raised when a nested key is missing in the config.""" """Raised when a (nested) config key is missing in the config."""
def __init__(self, key, subkey=None): def __init__(self, key, subkey=None):
if subkey: if subkey:
self.message = f"Missing key in {key}: {subkey}" self.message = f"Missing key in {key}: {', '.join(list(subkey))}"
else: else:
self.message = f"Missing key: {key}" self.message = f"Missing key: {key}"
super().__init__(self.message) super().__init__(self.message)
class DimensionError(ValueError):
"""Raised if dimensions or coordinates do not match the system."""
class InvalidIsotopeError(ValueError):
"""Raised if attempting to load an isotope that is not valid."""

View File

@ -1,5 +1,6 @@
from typing import Dict, Type from typing import Dict, Type
from pg_rad.exceptions.exceptions import InvalidIsotopeError
from pg_rad.physics.attenuation import get_mass_attenuation_coeff from pg_rad.physics.attenuation import get_mass_attenuation_coeff
@ -46,5 +47,5 @@ preset_isotopes: Dict[str, Type[Isotope]] = {
def get_isotope(isotope_str: str) -> Isotope: def get_isotope(isotope_str: str) -> Isotope:
"""Lazy factory function to create isotope objects.""" """Lazy factory function to create isotope objects."""
if isotope_str not in preset_isotopes: if isotope_str not in preset_isotopes:
raise ValueError(f"Unknown isotope: {isotope_str}") raise InvalidIsotopeError(f"Unknown isotope: {isotope_str}")
return preset_isotopes[isotope_str]() return preset_isotopes[isotope_str]()

View File

@ -2,6 +2,8 @@ from typing import Self
import numpy as np import numpy as np
from pg_rad.exceptions.exceptions import DimensionError
class BaseObject: class BaseObject:
def __init__( def __init__(
@ -21,7 +23,7 @@ class BaseObject:
""" """
if len(pos) != 3: if len(pos) != 3:
raise ValueError("Position must be tuple of length 3 (x,y,z).") raise DimensionError("Position must be tuple of length 3 (x,y,z).")
self.pos = pos self.pos = pos
self.name = name self.name = name
self.color = color self.color = color