Skip to content

Commit

Permalink
fix: remove Particle.name_root
Browse files Browse the repository at this point in the history
Fix-up to #72

This property is dubious (should be a method at most) and clogs the API
  • Loading branch information
redeboer committed Jun 18, 2021
1 parent c4cd302 commit 78aaabf
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 46 deletions.
17 changes: 9 additions & 8 deletions src/qrules/particle.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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]


Expand Down
78 changes: 40 additions & 38 deletions tests/unit/test_particle.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
Particle,
ParticleCollection,
Spin,
_get_name_root,
create_antiparticle,
create_particle,
)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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",
}

0 comments on commit 78aaabf

Please sign in to comment.