Source code for molfoundry.isotope

from .exceptions import LogicError


[docs] class Isotope: """Representation of the isotope of an atom. The value is either the "most abundant" sentinel ``-1`` or a concrete mass number ``>= 1``. """ def __init__(self, isotope: int = -1): if isotope != -1 and isotope < 1: raise LogicError( f"Invalid isotope {isotope}: must be -1 (most abundant) or >= 1." ) self._isotope = isotope def __int__(self) -> int: return self._isotope def __str__(self) -> str: return str(self._isotope) def __eq__(self, other: object) -> bool: if isinstance(other, Isotope): return self._isotope == other._isotope if isinstance(other, int): return self._isotope == other return NotImplemented def __hash__(self) -> int: # Defining ``__eq__`` otherwise makes the type unhashable; hash on the # underlying value so an Isotope and the equal plain int hash alike. return hash(self._isotope)