Repository: PlasmaPy/PlasmaPy
Pull request #281 (2018-02-27 19:25:21)

Particle class code review fixes
A few minor improvements to `particle_class.py` as discussed today during the code review session.

StanczakDominik:  Reverting change to astropy_helpers, I did not intend this to get committed :D
pep8speaks: Hello @StanczakDominik! Thanks for updating your pull request.



 - In the file [`plasmapy/atomic/tests/test_particle_class.py`](https://github.com/PlasmaPy/PlasmaPy/blob/fb797ebe40c32269ef2353197d9ed8fa7bd720df/plasmapy/atomic/tests/test_particle_class.py), following are the PEP8 issues :

> [Line 520:1](https://github.com/PlasmaPy/PlasmaPy/blob/fb797ebe40c32269ef2353197d9ed8fa7bd720df/plasmapy/atomic/tests/test_particle_class.py#L520): [E302](https://duckduckgo.com/?q=pep8%20E302) expected 2 blank lines, found 1



Patch:
diff --git a/plasmapy/atomic/particle_class.py b/plasmapy/atomic/particle_class.py
index 38775757..5b3bab61 100644
--- a/plasmapy/atomic/particle_class.py
+++ b/plasmapy/atomic/particle_class.py
@@ -347,8 +347,6 @@ def __init__(self,
                 else:
                     attributes['half-life'] = \
                         _Isotopes[isotope].get('half-life', None)
-            elif element and not isotope:
-                attributes['half-life'] = None
 
             if ion == 'He-4 2+':
                 attributes['spin'] = 0
@@ -631,6 +629,15 @@ def mass_number(self) -> int:
             raise InvalidIsotopeError(self._isotope_errmsg)
         return self._attributes['mass number']
 
+    @property
+    def neutron_number(self) -> int:
+        """
+        Return the number of neutrons of the isotope corresponding
+        to this particle, or raises an `~plasmapy.utils.InvalidIsotopeError` if
+        the particle does not correspond to an isotope.
+        """
+        return self.mass_number - self.atomic_number
+
     @property
     def isotopic_abundance(self) -> u.Quantity:
         """
@@ -735,7 +742,7 @@ def spin(self) -> Union[int, float]:
     @property
     def periodic_table(self):
         """
-        Return a ~collections.namedtuple` to access category, period,
+        Return a `~collections.namedtuple` to access category, period,
         group, and block information about an element.
 
         If the particle is not an element, isotope, or ion, then this
@@ -868,3 +875,9 @@ def become_set(arg: Union[str, Set, Tuple, List]) -> Set[str]:
             return False
 
         return require <= self._categories
+
+    def is_electron(self) -> bool:
+        """
+        Returns True if the particle is an electron.
+        """
+        return self.particle == "e-"


Test patch:
diff --git a/plasmapy/atomic/tests/test_particle_class.py b/plasmapy/atomic/tests/test_particle_class.py
index f6033c72..ae4bfc2c 100644
--- a/plasmapy/atomic/tests/test_particle_class.py
+++ b/plasmapy/atomic/tests/test_particle_class.py
@@ -171,6 +171,7 @@
       'mass_number': 2,
       'baryon_number': 2,
       'lepton_number': 0,
+      'neutron_number': 1,
       'is_category(require=("ion", "isotope"))': True,
       'periodic_table.group': 1,
       'periodic_table.block': 's',
@@ -188,6 +189,7 @@
       'mass_number': 3,
       'baryon_number': 3,
       'lepton_number': 0,
+      'neutron_number': 2,
       'is_category("ion", "isotope")': True,
       'is_category(require="uncharged")': False,
       'periodic_table.group': 1,
@@ -233,6 +235,7 @@
       'integer_charge': ChargeError,
       'atomic_number': 3,
       'mass_number': 7,
+      'neutron_number': 4,
       'baryon_number': 7,
       'half_life': np.inf * u.s,
       'nuclide_mass': 1.1647614796180463e-26 * u.kg,
@@ -247,6 +250,7 @@
       'integer_charge': 22,
       'atomic_number': 112,
       'mass_number': 276,
+      'neutron_number': 164,
       'baryon_number': 276,
       'lepton_number': 0}),
 
@@ -512,3 +516,9 @@ def test_particle_half_life_string():
 
     with pytest.warns(MissingAtomicDataWarning):
         assert isinstance(Particle(isotope).half_life, str)
+
+
+@pytest.mark.parametrize("p, is_one", [(Particle('e-'), True),
+                                       (Particle('p+'), False)])
+def test_particle_is_electron(p, is_one):
+    assert p.is_electron() == is_one