Skip to content

Commit

Permalink
Sourcery refactored master branch (#288)
Browse files Browse the repository at this point in the history
* 'Refactored by Sourcery'

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update src/particle/pdgid/functions.py

* Update src/particle/pdgid/functions.py

* Apply suggestions from code review

* Apply suggestions from code review

Co-authored-by: Sourcery AI <>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Eduardo Rodrigues <[email protected]>
  • Loading branch information
3 people authored Mar 23, 2021
1 parent 08c00ba commit a703fce
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 124 deletions.
4 changes: 2 additions & 2 deletions src/particle/converters/bimap.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,6 @@ def DirectionalMaps(name_A, name_B, converters=(str, str), filename=None):
name_B = name_B.upper()

fieldnames = None
skipinitialspace = True

if filename is None:
file_object = data.open_text(data, "conversions.csv")
elif hasattr(filename, "read"):
Expand All @@ -194,6 +192,8 @@ def DirectionalMaps(name_A, name_B, converters=(str, str), filename=None):
file_object = open(str(filename))

with file_object as _f:
skipinitialspace = True

to_map = {
converters[1](v[name_B]): converters[0](v[name_A])
for v in csv.DictReader(
Expand Down
28 changes: 12 additions & 16 deletions src/particle/particle/particle.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ def table_loaded(cls):
"""
Check to see if the table is loaded.
"""
return not cls._table is None
return cls._table is not None

@classmethod
def all(cls):
Expand Down Expand Up @@ -419,8 +419,7 @@ def to_list(
tbl_all = tbl_all[:n_rows]

# Build all table rows
tbl = []
tbl.append(tbl_names)
tbl = [tbl_names]
for p in tbl_all:
tbl.append([getattr(p, attr) for attr in tbl_names])

Expand Down Expand Up @@ -854,13 +853,8 @@ def _width_or_lifetime(self):
self.width < 0.05
): # corresponds to a lifetime of approximately 1.3e-20 seconds
assert self.lifetime is not None
if self.width_lower == self.width_upper:
e = width_to_lifetime(self.width - self.width_lower) - self.lifetime
s = "Lifetime = {lifetime} ns".format(
lifetime=str_with_unc(self.lifetime, e, e)
)
else:
s = "Lifetime = {lifetime} ns".format(
if self.width_lower != self.width_upper:
return "Lifetime = {lifetime} ns".format(
lifetime=str_with_unc(
self.lifetime,
width_to_lifetime(self.width - self.width_lower)
Expand All @@ -869,7 +863,10 @@ def _width_or_lifetime(self):
- width_to_lifetime(self.width + self.width_upper),
)
)
return s
e = width_to_lifetime(self.width - self.width_lower) - self.lifetime
return "Lifetime = {lifetime} ns".format(
lifetime=str_with_unc(self.lifetime, e, e)
)
else:
return "Width = {width} MeV".format(
width=str_with_unc(self.width, self.width_upper, self.width_lower)
Expand Down Expand Up @@ -1110,7 +1107,7 @@ def findall(
except TypeError: # skip checks such as 'lambda p: p.width > 0',
continue # which fail when width=None
else:
if not (filter_fn in item.name):
if filter_fn not in item.name:
continue

# At this point, if you break, you will not add a match
Expand Down Expand Up @@ -1210,10 +1207,9 @@ def from_string_list(cls, name):
list_can = cls.findall(name=name, particle=particle)
if list_can:
return list_can
else:
list_can = cls.findall(pdg_name=short_name, particle=particle)
if list_can:
return list_can
list_can = cls.findall(pdg_name=short_name, particle=particle)
if list_can:
return list_can

mat_str = getname.match(short_name)

Expand Down
120 changes: 45 additions & 75 deletions src/particle/pdgid/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,7 @@ def is_lepton(pdgid):
"""Does this PDG ID correspond to a lepton?"""
if _extra_bits(pdgid) > 0:
return False
if 11 <= int(_fundamental_id(pdgid)) <= 18:
return True
return False
return 11 <= int(_fundamental_id(pdgid)) <= 18


def is_hadron(pdgid):
Expand All @@ -124,9 +122,7 @@ def is_hadron(pdgid):
return True
if is_pentaquark(pdgid):
return True
if is_Rhadron(pdgid):
return True
return False
return bool(is_Rhadron(pdgid))


def is_meson(pdgid):
Expand Down Expand Up @@ -154,13 +150,11 @@ def is_meson(pdgid):
and _digit(pdgid, Location.Nq1) == 0
):
# check for illegal antiparticles
if (
_digit(pdgid, Location.Nq3) == _digit(pdgid, Location.Nq2)
and int(pdgid) < 0
):
return False
else:
return True
return (
_digit(pdgid, Location.Nq3) != _digit(pdgid, Location.Nq2)
or int(pdgid) >= 0
)

return False


Expand All @@ -184,18 +178,12 @@ def is_baryon(pdgid):
if abspid(pdgid) in {2110, 2210}:
return True

if (
return (
_digit(pdgid, Location.Nj) > 0
and _digit(pdgid, Location.Nq3) > 0
and _digit(pdgid, Location.Nq2) > 0
and _digit(pdgid, Location.Nq1) > 0
):
return True

if is_Rhadron(pdgid) or is_pentaquark(pdgid):
return False

return False
)


def is_diquark(pdgid):
Expand All @@ -207,14 +195,12 @@ def is_diquark(pdgid):
return False
if 0 < int(_fundamental_id(pdgid)) <= 100:
return False
if (
return (
_digit(pdgid, Location.Nj) > 0
and _digit(pdgid, Location.Nq3) == 0
and _digit(pdgid, Location.Nq2) > 0
and _digit(pdgid, Location.Nq1) > 0
):
return True
return False
)


def is_nucleus(pdgid):
Expand Down Expand Up @@ -258,7 +244,7 @@ def is_pentaquark(pdgid):
return False
if _digit(pdgid, Location.N) != 9:
return False
if _digit(pdgid, Location.Nr) == 9 or _digit(pdgid, Location.Nr) == 0:
if _digit(pdgid, Location.Nr) in {9, 0}:
return False
if _digit(pdgid, Location.Nj) == 9 or _digit(pdgid, Location.Nl) == 0:
return False
Expand All @@ -274,9 +260,7 @@ def is_pentaquark(pdgid):
return False
if _digit(pdgid, Location.Nq1) > _digit(pdgid, Location.Nl):
return False
if _digit(pdgid, Location.Nl) > _digit(pdgid, Location.Nr):
return False
return True
return _digit(pdgid, Location.Nl) <= _digit(pdgid, Location.Nr)


def is_gauge_boson_or_higgs(pdgid):
Expand All @@ -288,7 +272,7 @@ def is_gauge_boson_or_higgs(pdgid):
The graviton and the boson content of a two-Higgs-doublet scenario
and of additional SU(2)xU(1) groups are found in the range 31-40.
"""
return True if 21 <= abspid(pdgid) <= 40 else False
return 21 <= abspid(pdgid) <= 40


def is_sm_gauge_boson_or_higgs(pdgid):
Expand All @@ -302,7 +286,7 @@ def is_sm_gauge_boson_or_higgs(pdgid):
if abspid(pdgid) == 24: # W is the only SM gauge boson not its antiparticle
return True

return True if 21 <= int(pdgid) <= 25 else False
return 21 <= int(pdgid) <= 25


def is_generator_specific(pdgid):
Expand Down Expand Up @@ -367,13 +351,11 @@ def is_Rhadron(pdgid):
if is_SUSY(pdgid):
return False
# All R-hadrons have at least 3 core digits
if (
_digit(pdgid, Location.Nq2) == 0
or _digit(pdgid, Location.Nq3) == 0
or _digit(pdgid, Location.Nj) == 0
):
return False
return True
return (
_digit(pdgid, Location.Nq2) != 0
and _digit(pdgid, Location.Nq3) != 0
and _digit(pdgid, Location.Nj) != 0
)


def is_Qball(pdgid):
Expand All @@ -391,9 +373,7 @@ def is_Qball(pdgid):
return False
if (abspid(pdgid) // 10) % 10000 == 0:
return False
if _digit(pdgid, Location.Nj) != 0:
return False
return True
return _digit(pdgid, Location.Nj) == 0


def is_dyon(pdgid):
Expand All @@ -419,9 +399,7 @@ def is_dyon(pdgid):
return False
if _digit(pdgid, Location.Nq3) == 0:
return False
if _digit(pdgid, Location.Nj) != 0:
return False
return True
return _digit(pdgid, Location.Nj) == 0


def is_SUSY(pdgid):
Expand All @@ -433,13 +411,11 @@ def is_SUSY(pdgid):
"""
if _extra_bits(pdgid) > 0:
return False
if _digit(pdgid, Location.N) != 1 and _digit(pdgid, Location.N) != 2:
if _digit(pdgid, Location.N) not in {1, 2}:
return False
if _digit(pdgid, Location.Nr) != 0:
return False
if _fundamental_id(pdgid) == 0:
return False
return True
return _fundamental_id(pdgid) != 0


def is_technicolor(pdgid):
Expand All @@ -451,7 +427,7 @@ def is_technicolor(pdgid):
"""
if _extra_bits(pdgid) > 0:
return False
return True if _digit(pdgid, Location.N) == 3 else False
return _digit(pdgid, Location.N) == 3


def is_composite_quark_or_lepton(pdgid):
Expand All @@ -465,9 +441,7 @@ def is_composite_quark_or_lepton(pdgid):
return False
if _fundamental_id(pdgid) == 0:
return False
if not (_digit(pdgid, Location.N) == 4 and _digit(pdgid, Location.Nr) == 0):
return False
return True
return _digit(pdgid, Location.N) == 4 and _digit(pdgid, Location.Nr) == 0


def has_down(pdgid):
Expand Down Expand Up @@ -520,15 +494,15 @@ def has_fundamental_anti(pdgid):

# Check generator-specific PDGIDs
if 81 <= fid <= 100:
return True if fid in {82, 84, 85, 86, 87} else False
return fid in {82, 84, 85, 86, 87}

# Check PDGIDs from 1 to 79
_cp_conjugates = {21, 22, 23, 25, 32, 33, 35, 36, 39, 40, 43}
_unassigned = (
[9, 10, 19, 20, 26] + list(range(26, 32)) + list(range(45, 80))
) # not in conversion.csv
if (1 <= fid <= 79) and fid not in _cp_conjugates:
return False if fid in _unassigned else True
return fid not in _unassigned

return False

Expand Down Expand Up @@ -686,12 +660,12 @@ def three_charge(pdgid):
charge = ch100[sid - 1]
if aid in {1000017, 1000018, 1000034, 1000052, 1000053, 1000054}:
charge = 0
if aid == 5100061 or aid == 5100062:
if aid in {5100061, 5100062}:
charge = 6
elif _digit(pdgid, Location.Nj) == 0: # KL, KS, or undefined
return 0
elif q1 == 0 or (is_Rhadron(pdgid) and q1 == 9): # mesons
if q2 == 3 or q2 == 5:
if q2 in {3, 5}:
charge = ch100[q3 - 1] - ch100[q2 - 1]
else:
charge = ch100[q2 - 1] - ch100[q3 - 1]
Expand Down Expand Up @@ -764,7 +738,7 @@ def S(pdgid):
nl = (abspid(pdgid) // 10000) % 10
js = abspid(pdgid) % 10

if not (js == 1 or js >= 3):
if js != 1 and js < 3:
return 0

if nl == 0:
Expand Down Expand Up @@ -816,44 +790,40 @@ def L(pdgid):
js = abspid(pdgid) % 10

if nl == 0:
if js == 1:
if js in {1, 3}:
return 0
if js == 3:
return 0
if js == 5:
elif js == 5:
return 1
if js == 7:
elif js == 7:
return 2
if js == 9:
elif js == 9:
return 3
elif nl == 1:
if js == 1:
return 1
if js == 3:
if js in {1, 3}:
return 1
if js == 5:
elif js == 5:
return 2
if js == 7:
elif js == 7:
return 3
if js == 9:
elif js == 9:
return 4
elif nl == 2:
if js == 3:
return 1
if js == 5:
elif js == 5:
return 2
if js == 7:
elif js == 7:
return 3
if js == 9:
elif js == 9:
return 4
elif nl == 3:
if js == 3:
return 2
if js == 5:
elif js == 5:
return 3
if js == 7:
elif js == 7:
return 4
if js == 9:
elif js == 9:
return 5

return 0
Expand Down
Loading

0 comments on commit a703fce

Please sign in to comment.