Fallbacks
We can declare parameters that act in place of another in case that parameter is not specified.
For example: Say we want to specify a particle either by its rest mass or rest energy; both will do the job. By default we wish to use the energy within our program but the mass is okay too since we know the appropriate conversion rule.
from scipy.constants import speed_of_light as c
from scipy.constants import elementary_charge as e
class Particle(Configurable):
energy = PhysicalQuantity(unit='eV').fallback(
PhysicalQuantity('mass', unit='kg').transform(lambda x: x * c**2 / e)
)
Here we added another parameter mass
that acts as a possible substitute for the energy
parameter; we also need to add the appropriate transformation to the main parameter for any substitute, here E_0 = m_0 * c^2
(dividing by e
because the unit should be eV
).
So any of the following two configurations would do:
energy: 510.9989461 [keV]
mass: 9.10938356e-31 [kg]
Alternative Syntax
class Example(Configurable):
parameter = Integer() | Integer('other')
The primary parameter's name is automatically derived from the field name: parameter.name == 'parameter'
.
Edited by Dominik Vilsmeier