diff --git a/src/qrules/particle.py b/src/qrules/particle.py index 8ab68755..7a39d853 100644 --- a/src/qrules/particle.py +++ b/src/qrules/particle.py @@ -202,18 +202,11 @@ def __attrs_post_init__(self) -> None: ")" ) - @property - def name_root(self) -> str: - name_root = self.name - name_root = re.sub(r"\(.+\)", "", name_root) - name_root = re.sub(r"[\*\+\-~\d']", "", name_root) - return name_root - def __gt__(self, other: Any) -> bool: if isinstance(other, Particle): def sorting_key(particle: Particle) -> tuple: - name_root = particle.name_root + name_root = _get_name_root(particle.name) return ( name_root[0].lower(), name_root, @@ -256,6 +249,14 @@ def _repr_pretty_(self, p: PrettyPrinter, cycle: bool) -> None: p.text(")") +def _get_name_root(name: str) -> str: + """Strip a string (particularly the `.Particle.name`) of specifications.""" + name_root = name + name_root = re.sub(r"\(.+\)", "", name_root) + name_root = re.sub(r"[\*\+\-~\d']", "", name_root) + return name_root + + ParticleWithSpin = Tuple[Particle, float] diff --git a/tests/unit/test_particle.py b/tests/unit/test_particle.py index 66736ab9..9fca863e 100644 --- a/tests/unit/test_particle.py +++ b/tests/unit/test_particle.py @@ -11,6 +11,7 @@ Particle, ParticleCollection, Spin, + _get_name_root, create_antiparticle, create_particle, ) @@ -118,44 +119,6 @@ def test_gt(self, name1, name2, particle_database: ParticleCollection): pdg = particle_database assert pdg[name1] > pdg[name2] - def test_name_root(self, particle_database: ParticleCollection): - name_roots = {p.name_root for p in particle_database} - assert name_roots == { - "a", - "B", - "b", - "chi", - "D", - "Delta", - "e", - "eta", - "f", - "g", - "gamma", - "h", - "J/psi", - "K", - "Lambda", - "mu", - "N", - "n", - "nu", - "Omega", - "omega", - "p", - "phi", - "pi", - "psi", - "rho", - "Sigma", - "tau", - "Upsilon", - "W", - "Xi", - "Y", - "Z", - } - def test_neg(self, particle_database: ParticleCollection): pip = particle_database.find(211) pim = particle_database.find(-211) @@ -472,3 +435,42 @@ def test_create_particle( assert new_particle.width == 0.5 assert new_particle.baryon_number == template_particle.baryon_number assert new_particle.strangeness == template_particle.strangeness + + +def test_get_name_root(particle_database: ParticleCollection): + name_roots = {_get_name_root(p.name) for p in particle_database} + assert name_roots == { + "a", + "B", + "b", + "chi", + "D", + "Delta", + "e", + "eta", + "f", + "g", + "gamma", + "h", + "J/psi", + "K", + "Lambda", + "mu", + "N", + "n", + "nu", + "Omega", + "omega", + "p", + "phi", + "pi", + "psi", + "rho", + "Sigma", + "tau", + "Upsilon", + "W", + "Xi", + "Y", + "Z", + }