carputils
Doxygen code documentation for the python framework controlling openCARP
Public Member Functions | Properties | List of all members
carputils.carpio.quantity.PhysicalQuantity Class Reference

Provides a class to convert physical quantities from a string to numerical values. More...

Public Member Functions

def __init__ (self, unitdict, iunit, ounit)
 
def from_string (self, valuestr, ounit=None)
 Convert a str to numerical value. More...
 
def __call__ (self, valuestr)
 

Properties

 input_unit = property
 Get the default input unit. More...
 
 output_unit = property
 Get the output unit. More...
 
 units = property
 Get all the supported units. More...
 

Detailed Description

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:

# supported units and their scale factors to a reference unit, kPa in this case
unitdict = {'Pa': 0.001, 'kPa': 1.0, 'mPa': 1000.0, 'mmHg': 0.133322387415}
# defualt input unit
iunit = 'kPa'
# output unit
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()
# `args.pressure` is of type `float` and already converted to the desired output unit

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
unitdictdict A dictionary containing the physical unit and the scale factor to a reference unit
iunitstr The default input unit if not unit is given
ounitstr The output unit

Constructor & Destructor Documentation

◆ __init__()

def carputils.carpio.quantity.PhysicalQuantity.__init__ (   self,
  unitdict,
  iunit,
  ounit 
)

Member Function Documentation

◆ __call__()

def carputils.carpio.quantity.PhysicalQuantity.__call__ (   self,
  valuestr 
)

◆ from_string()

def carputils.carpio.quantity.PhysicalQuantity.from_string (   self,
  valuestr,
  ounit = None 
)

Convert a str to numerical value.

Parameters
valuestrstr A string representing the physical quantity
ounitstr, optional The desired output unit, if None the default output unit is used

Property Documentation

◆ input_unit

carputils.carpio.quantity.PhysicalQuantity.input_unit = property
static

Get the default input unit.

◆ output_unit

carputils.carpio.quantity.PhysicalQuantity.output_unit = property
static

Get the output unit.

◆ units

carputils.carpio.quantity.PhysicalQuantity.units = property
static

Get all the supported units.


The documentation for this class was generated from the following file: