Provides a class to convert physical quantities from a string to numerical values.
More...
Provides a class to convert physical quantities from a string to numerical values.
The PhysicalQuantity class can be used in combination with the ArgumentParser class whenever physical quantities are required as arguments. First you have to define your physical quantity:
unitdict = {'Pa': 0.001, 'kPa': 1.0, 'mPa': 1000.0, 'mmHg': 0.133322387415}
iunit = 'kPa'
ounit = 'mmHg'
PressureQuantity = PhysicalQuantity(unitdict, iunit, ounit)
Next you can define your argument:
parser.add_argument('--pressure', type=PressureQuantity, default=10.0, ....)
args = parser.parse_args()
Now the user can state a pressure with different units:
./script.py --pressure 10.0Pa
./script.py --pressure 10.0kPa
./script.py --pressure 10.0mPa
./script.py --pressure 10.0mmHg
./script.py --pressure 10.0 # No unit, default input unit is used
Defines a physical quantity
- Parameters
-
unitdict | dict A dictionary containing the physical unit and the scale factor to a reference unit |
iunit | str The default input unit if not unit is given |
ounit | str The output unit |