particle_input

plasmapy.atomic.particle_input(wrapped_function: Callable = None, require: Union[str, typing.Set, typing.List, typing.Tuple] = set(), any_of: Union[str, typing.Set, typing.List, typing.Tuple] = set(), exclude: Union[str, typing.Set, typing.List, typing.Tuple] = set(), none_shall_pass: bool = False) → Any

Convert arguments to methods and functions to Particle objects.

Take positional and keyword arguments that are annotated with Particle, and pass through the Particle object corresponding to those arguments to the decorated function or method.

Optionally, raise an exception if the particle does not satisfy the specified categorical criteria.

Parameters:
  • wrapped_function (callable) – The function or method to be decorated.
  • require (str, set, list, or tuple, optional) – Categories that a particle must be in. If a particle is not in all of these categories, then an AtomicError will be raised.
  • any_of (str, set, list, or tuple, optional) – Categories that a particle may be in. If a particle is not in any of these categories, then an AtomicError will be raised.
  • exclude (str, set, list, or tuple, optional) – Categories that a particle cannot be in. If a particle is in any of these categories, then an AtomicError will be raised.
  • none_shall_pass (bool, optional) – If set to True, then the decorated argument may be set to None without raising an exception. In such cases, this decorator will pass None through to the decorated function or method. If set to False and the annotated argument is given a value of None, then this decorator will raise a TypeError.

Notes

If the annotated argument is named element, isotope, or ion, then the decorator will raise an InvalidElementError, InvalidIsotopeError, or InvalidIonError if the particle does not correspond to an element, isotope, or ion, respectively.

If exactly one argument is annotated with Particle, then the keywords Z and mass_numb may be used to specify the integer charge and/or mass number of an ion or isotope. However, the decorated function must allow Z and/or mass_numb as keywords in order to enable this functionality.

Raises:
  • TypeError – If the annotated argument is not a str, int, or Particle; or if Z or mass_numb is not an int.
  • plasmapy/utils/InvalidParticleError – If the annotated argument does not correspond to a valid particle.
  • plasmapy/utils/InvalidElementError – If an annotated argument is named element, and the input does not correspond to an element, isotope, or ion.
  • plasmapy/utils/InvalidIsotopeError – If an annotated argument is named isotope, and the input does not correspond to an isotope or an ion of an isotope.
  • plasmapy/utils/InvalidIonError – If an annotated argument is named ion, and the input does not correspond to an ion.
  • plasmapy/utils/ChargeError – If 'charged' is in the require argument and the particle is not explicitly charged, or if any_of = {'charged', 'uncharged'} and the particle does not have charge information associated with it.
  • plasmapy/utils/AtomicError – If an annotated argument does not meet the criteria set by the categories in the require, any_of, and exclude keywords; if more than one argument is annotated and Z or mass_numb are used as arguments; or if none of the arguments have been annotated with Particle.

Examples

The following simple decorated function returns the Particle object created from the function’s sole argument:

from plasmapy.atomic import particle_input, Particle
@particle_input
def decorated_function(particle: Particle):
    return particle

This decorator may be used for methods in instances of classes, as in the following example:

from plasmapy.atomic import particle_input, Particle
class SampleClass:
    @particle_input
    def decorated_method(self, particle: Particle):
        return particle
sample_instance = SampleClass()
sample_instance.decorated_method('Fe')

Some functions may intended to be used with only certain categories of particles. The require, any_of, and exclude keyword arguments enable this functionality.

from plasmapy.atomic import particle_input, Particle
@particle_input(
    require={'matter'},
    any_of={'charged', 'uncharged},
    exclude={'neutrino', 'antineutrino'},
)
def selective_function(particle: Particle):
    return particle