Warning

You are reading the documentation related to the development version. Go here if you are looking for the documentation of the stable release.

spectrochempy.Unit

class Unit(*args, **kwargs)[source]

Attributes Summary

default_format

Default formatting string.

dimensionality

returns: dict -- Dimensionality of the PlainUnit, e.g.

dimensionless

Return True if the PlainUnit is dimensionless; False otherwise.

Methods Summary

from_(value[, strict, name])

Converts a numerical value or quantity to this unit

is_compatible_with(self, other, *contexts, ...)

check if the other object is compatible

m_from(value[, strict, name])

Converts a numerical value or quantity to this unit, then returns the magnitude of the converted value

Attributes Documentation

default_format = ''

Default formatting string.

dimensionality

returns: dict – Dimensionality of the PlainUnit, e.g. {length: 1, time: -1}

dimensionless

Return True if the PlainUnit is dimensionless; False otherwise.

Methods Documentation

from_(value, strict=True, name='value')[source]

Converts a numerical value or quantity to this unit

Parameters
  • value – a Quantity (or numerical value if strict=False) to convert

  • strict – boolean to indicate that only quantities are accepted (Default value = True)

  • name – descriptive name to use if an exception occurs (Default value = “value”)

Returns

type – The converted value as this unit

is_compatible_with(self, other: Any, *contexts: Union[str, Context], **ctx_kwargs: Any ) -> bool: """check if the other object is compatible Parameters ---------- other The object to check. Treated as dimensionless if not a Quantity, PlainUnit or str. *contexts : str or pint.Context Contexts to use in the transformation. **ctx_kwargs : Values for the Context/s Returns ------- bool """ from .quantity import PlainQuantity if contexts or self._REGISTRY._active_ctx: try: (1 * self).to(other, *contexts, **ctx_kwargs) return True except DimensionalityError: return False if isinstance(other, (PlainQuantity, PlainUnit))[source]

check if the other object is compatible

Parameters
  • other – The object to check. Treated as dimensionless if not a Quantity, PlainUnit or str.

  • *contexts (str or pint.Context) – Contexts to use in the transformation.

  • **ctx_kwargs – Values for the Context/s

Returns

bool

m_from(value, strict=True, name='value')[source]

Converts a numerical value or quantity to this unit, then returns the magnitude of the converted value

Parameters
  • value – a Quantity (or numerical value if strict=False) to convert

  • strict – boolean to indicate that only quantities are accepted (Default value = True)

  • name – descriptive name to use if an exception occurs (Default value = “value”)

Returns

type – The magnitude of the converted value

Examples using spectrochempy.Unit