def _particle_call_string(arg, kwargs=None) -> str: """ Return a `str` that recreates the call to create a particular `~plasmapy.particles.Particle` instance from the input. """ if kwargs is None: kwargs = {} return call_string(Particle, arg, kwargs)
def _particle_call_string(arg: Union[str, Integral], kwargs: Optional[Dict] = None) -> str: """ Return a `str` that recreates the call to create a particular `~plasmapy.particles.Particle` instance from the input. """ if kwargs is None: kwargs = {} return call_string(Particle, arg, kwargs)
def test_Particle_class(arg, kwargs, expected_dict): """ Test that `~plasmapy.particles.Particle` objects for different subatomic particles, elements, isotopes, and ions return the expected properties. Provide a detailed error message that lists all of the inconsistencies with the expected results. """ call = call_string(Particle, arg, kwargs) errmsg = "" try: particle = Particle(arg, **kwargs) except Exception as exc: raise AtomicError(f"Problem creating {call}") from exc for key in expected_dict.keys(): expected = expected_dict[key] if inspect.isclass(expected) and issubclass(expected, Exception): # Exceptions are expected to be raised when accessing certain # attributes for some particles. For example, accessing a # neutrino's mass should raise a MissingAtomicDataError since # only upper limits of neutrino masses are presently available. # If expected_dict[key] is an exception, then check to make # sure that this exception is raised. try: with pytest.raises(expected): exec(f"particle.{key}") except pytest.fail.Exception: errmsg += f"\n{call}[{key}] does not raise {expected}." except Exception: errmsg += ( f"\n{call}[{key}] does not raise {expected} but " f"raises a different exception." ) else: try: result = eval(f"particle.{key}") assert result == expected or u.isclose(result, expected) except AssertionError: errmsg += ( f"\n{call}.{key} returns {result} instead " f"of the expected value of {expected}." ) except Exception: errmsg += f"\n{call}.{key} raises an unexpected exception." if len(errmsg) > 0: raise Exception(f"Problems with {call}:{errmsg}")
def __repr__(self): """ Returns ------- str Examples -------- >>> from astropy import units as u >>> PlasmaBlob(1e4*u.K, 1e20/u.m**3, particle='p') PlasmaBlob(T_e=10000.0*u.K, n_e=1e+20*u.m**-3, particle='p', Z=1) """ argument_dict = {'T_e': self.T_e, 'n_e': self.n_e, 'particle': self.particle, 'Z': self.Z} return call_string(PlasmaBlob, (), argument_dict)
def test_call_string(function, args, kwargs, expected): """Tests that call_string returns a string that is equivalent to the function call.""" assert expected == call_string(function, args, kwargs)