utilities.pintutils package

Submodules

utilities.pintutils.jsonpintencoder module

class utilities.pintutils.jsonpintencoder.JsonPintEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)

Bases: json.encoder.JSONEncoder

Class extending json.JSONEncoder to handle pint Quantity and Unit objects.

Code taken directly from stackoverflow

default(obj)

If a pint.Quantity or pint.Unit object is passed in obj, a string representation of the object will be returned. If the object is not one of these types, the base implementation is called (to raise a TypeError).

Parameters

obj (pint.Quantity or pint.Unit) – a pint object

Returns

a string representation of the pint object

Return type

str

utilities.pintutils.unitmanager module

utilities.pintutils.unitmanager.unitmanager(label: str, unit: Optional[pint.unit.Unit] = None, value: Optional[numbers.Number] = None) Dict[str, str]

Formats a quantity label suitable for use on the x-axis of a chart.

Units are output in their shortform (nm for nanometer). If unit is not provided, the quantity will be treated as dimensionless.

Example for a wavelength of 280 nm:

>>> from utilities.pintutils import unitmanager
>>> ureg = unitmanager.pint.UnitRegistry()
>>> output = unitmanager.unitmanager('wavelength', ureg.nanometer, 280)
>>> output
{'name': 'wavelength', 'unit': 'nm', 'label': 'wavelength (nm)', 'value': 280, 'quantity': '280 nm'}
>>> print(output['label'])
wavelength (nm)
>>> print(f'peak position = {output["quantity"]}')
peak position = 280 nm

Example for unitless absorbance:

>>> from utilities.pintutils import unitmanager
>>> ureg = unitmanager.pint.UnitRegistry()
>>> output = unitmanager.unitmanager('absorbance')
>>> output
{'name': 'absorbance', 'unit': '', 'label': 'absorbance'}
>>> print(output['label'])
absorbance
Parameters
  • label (str) – Name of the unit, for example ‘wavelength’

  • unit (pint.Unit, optional) – Name of the unit using the pint method of describing units.

  • value (Number, optional) – Value of the unit, for example 280 (for wavelength = 280 nm)

Returns

Dictionary containing the name, unit, axis label (and quantity if provided)

Return type

Dict[str, str]

Module contents