Repository: PlasmaPy/PlasmaPy
Pull request #107 (2017-09-12 00:57:20)

Added electric_charge function and test cases
As @namurphy requested I added a function that returns the actual charge as a `Quantity` along with some basic test cases.


Patch:
diff --git a/plasmapy/atomic/atomic.py b/plasmapy/atomic/atomic.py
index 3c8c99c6..d888daec 100644
--- a/plasmapy/atomic/atomic.py
+++ b/plasmapy/atomic/atomic.py
@@ -1546,3 +1546,52 @@ def __extract_charge_state(argument):
                           "unlikely to occur in nature.")
 
     return argument, charge_state
+
+
+def electric_charge(argument):
+    """Returns the electric charge (in coulombs) of an ion or other particle
+
+    Parameters
+    ----------
+    argument : string
+        String representing an element or isotope followed by charge state
+        information.
+
+    Returns
+    -------
+    charge: Quantity
+        The electric charge in coulombs.
+
+    Raises
+    ------
+    ValueError:
+        If the charge state or isotope information is invalid, or the charge
+        state exceeds the atomic number.
+
+    UserWarning:
+        If the input represents an ion with a charge state that is below -3.
+
+    Notes
+    -----
+    This function supports two formats for the charge state information.
+
+    The first format is a string that has information for the element
+    or isotope at the beginning, a space in between, and the charge
+    state information in the form of an integer followed by a plus or
+    minus sign, or a plus or minus sign followed by an integer.
+
+    The second format is a string containing element information at
+    the beginning, following by one or more plus or minus signs.
+
+    This function returns -1.6021766208e-19 C for electrons and
+    1.6021766208e-19 C for positrons.
+
+    Examples
+    --------
+    >>> q = electric_charge('p')
+    <Quantity 1.6021766208e-19 C>
+    """
+
+    charge = charge_state(argument) * const.e.to('C')
+
+    return charge


Test patch:
diff --git a/plasmapy/atomic/tests/test_atomic.py b/plasmapy/atomic/tests/test_atomic.py
index b37ef30b..0b63b086 100644
--- a/plasmapy/atomic/tests/test_atomic.py
+++ b/plasmapy/atomic/tests/test_atomic.py
@@ -7,7 +7,7 @@
                       energy_from_nuclear_reaction, is_isotope_stable,
                       half_life, known_isotopes, common_isotopes,
                       stable_isotopes, isotopic_abundance, charge_state,
-                      Elements, Isotopes)
+                      electric_charge, Elements, Isotopes)
 
 import pytest
 
@@ -707,9 +707,6 @@ def test_charge_state():
     with pytest.raises(ValueError):
         charge_state('Fe 29+')
 
-    with pytest.raises(ValueError):
-        charge_state('Fe 29+')
-
     with pytest.raises(UserWarning):
         charge_state('H---')
 
@@ -718,3 +715,25 @@ def test_charge_state():
 
     with pytest.raises(UserWarning):
         charge_state('Og 10-')
+
+
+def test_electric_charge():
+    assert electric_charge('p').value == 1.6021766208e-19
+    assert electric_charge('p').unit == 'C'
+    assert electric_charge('e').value == -1.6021766208e-19
+    assert electric_charge('alpha').value == 3.2043532416e-19
+
+    with pytest.raises(ValueError):
+        electric_charge('badinput')
+
+    with pytest.raises(ValueError):
+        electric_charge(' ')
+
+    with pytest.raises(ValueError):
+        electric_charge('Au 81+')
+
+    with pytest.raises(UserWarning):
+        electric_charge('Au 81-')
+
+    with pytest.raises(UserWarning):
+        electric_charge('H---')