Source code for httk.atomistic.models.protopattern.label_string

"""Backend wrapping a raw canonical protopattern label string."""

from typing import Any, Self

from httk.atomistic.models.protopattern.backend import ProtopatternBackend
from httk.atomistic.models.protopattern.notation import try_parse_protopattern


[docs] class ProtopatternLabelString(ProtopatternBackend): r"""Wrap a canonical protopattern label held as a plain string. The string must be a canonical unsuffixed protopattern label (no ``:`` species suffix); anything else is declined. Parsing is eager, so the wrapped value is validated at adoption. :param obj: The canonical protopattern label text. :param \*\*hints: Backend-selection hints. """
[docs] kind = "protopattern"
_raw: str @classmethod def _backend_adopt(cls, obj: Any, **hints: Any) -> Self | None: r"""Adopt a canonical protopattern label string. :param obj: The source object to adopt. :param \**hints: Backend-selection hints. :return: An initialized backend, or ``None`` when this backend declines ``obj``. """ if hints and hints.get("kind", "protopattern") != "protopattern": return None if not isinstance(obj, str): return None if try_parse_protopattern(obj) is None: return None return cls(obj, **hints) def __init__(self, obj: str, **hints: Any) -> None: self._raw = obj parsed = try_parse_protopattern(obj) assert parsed is not None # _backend_adopt guarantees a canonical label self._value = parsed @property
[docs] def spacegroup(self): """Return the standard-setting space group of the parsed label.""" return self._value.spacegroup
@property
[docs] def occupations(self): """Return the class-partitioned occupations of the parsed label.""" return self._value.occupations
[docs] def unwrap(self) -> str: """Return the original label text.""" return self._raw