Skip to content

Commit

Permalink
Remove misleading/awkward/unexpected Particle.__int__ method (#265)
Browse files Browse the repository at this point in the history
* Remove awkward Particle.__init__

* Demo notebook updated accordingly

* Adapt Particle class accordingly

* Adapt tests accordingly

* Fix notebook demo
  • Loading branch information
eduardo-rodrigues authored Sep 10, 2020
1 parent ce2d8c1 commit ec7c1b5
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 22 deletions.
11 changes: 1 addition & 10 deletions notebooks/ParticleDemo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@
"metadata": {},
"outputs": [],
"source": [
"[int(p) for p in all_particles]"
"[int(p.pdgid) for p in all_particles]"
]
},
{
Expand Down Expand Up @@ -620,15 +620,6 @@
"p.pdgid"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"PDGID(p)"
]
},
{
"cell_type": "markdown",
"metadata": {
Expand Down
13 changes: 4 additions & 9 deletions src/particle/particle/particle.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,11 +590,11 @@ def __le__(self, other):
# Sort by absolute particle numbers
# The positive one should come first
if type(self) == type(other):
return abs(int(self) - 0.25) < abs(int(other) - 0.25)
return abs(int(self.pdgid) - 0.25) < abs(int(other.pdgid) - 0.25)

# Comparison with anything else should produce normal comparisons.
else:
return int(self) < other
return int(self.pdgid) < other

def __eq__(self, other):
# type: (Any) -> bool
Expand All @@ -608,11 +608,6 @@ def __hash__(self):
# type: () -> int
return hash(self.pdgid)

# Integer == PDGID
def __int__(self):
# type: () -> int
return int(self.pdgid)

# Shared with PDGID

@property
Expand Down Expand Up @@ -1043,9 +1038,9 @@ def findall(

# particle=True is particle, False is antiparticle, and None is both
if particle is not None:
if particle and int(item) < 0:
if particle and int(item.pdgid) < 0:
continue
elif (not particle) and int(item) > 0:
elif (not particle) and int(item.pdgid) > 0:
continue

# If a filter function is passed, evaluate and skip if False
Expand Down
5 changes: 2 additions & 3 deletions tests/particle/test_particle.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ def test_pdg():
def test_pdg_convert():
p = Particle.from_pdgid(211)
assert isinstance(p.pdgid, PDGID)
assert int(p) == 211
assert PDGID(p) == 211
assert p.pdgid == 211
assert int(p.pdgid) == 211


def test_sorting():
Expand Down Expand Up @@ -576,7 +576,6 @@ def test_to_list():
def test_ampgen_style_names(name, pid):
particle = Particle.from_string(name)

assert int(particle) == pid
assert particle.pdgid == pid
assert particle == pid

Expand Down

0 comments on commit ec7c1b5

Please sign in to comment.