Unit

PhysicalUnit class definition

Original author: Georg Brandl <georg@python.org>, https://bitbucket.org/birkenfeld/ipython-physics

exception PhysicalQuantities.unit.UnitError[source]
PhysicalQuantities.unit.add_composite_unit(name, factor, units, offset=0, verbosename='', prefixed=False, url='')[source]

Add new unit to the unit_table, defined relative to existing units.

Parameters

name: str

Name of the new unit (e.g., ‘km’, ‘degC’).

factor: float

Scaling factor relative to the base units derived from units.

units: str

String defining the base unit composition (e.g., ‘m’, ‘K’, ‘m/s’). This string is evaluated using the existing unit_table.

offset: float, optional

Additive offset factor (primarily for temperature scales). Default is 0.

verbosename: str, optional

A more descriptive name for the unit (e.g., ‘Kilometre’, ‘degree Celsius’). Default is ‘’.

prefixed: bool, optional

Indicates if this unit is a standard prefixed version (like ‘k’ilo, ‘m’illi) of the unit defined by units. Affects the baseunit attribute. Default is False.

url: str, optional

A URL linking to more information about the unit. Default is ‘’.

Returns

str

The name of the newly added unit.

Raises

KeyError

If a unit with name already exists or if the units string is invalid or cannot be evaluated.

ValueError

If factor or offset is not numeric.

PhysicalQuantities.unit.addunit(unit: PhysicalUnit)[source]

Add a new PhysicalUnit entry to the global unit_table.

Parameters

unit: PhysicalUnit

PhysicalUnit object to add.

Raises

KeyError

If a unit with the same name already exists in unit_table.

PhysicalQuantities.unit.convertvalue(value, src_unit, target_unit)[source]

Convert a numerical value between compatible units.

Handles both multiplicative factors and additive offsets (e.g., temperature scales).

Parameters

value: number or array-like

The numerical value(s) to convert.

src_unit: PhysicalUnit

The unit of the input value.

target_unit: PhysicalUnit

The unit to convert the value to.

Returns

number or array-like

The converted value(s) in target_unit.

Raises

UnitError

If src_unit and target_unit are incompatible (represent different physical dimensions) or if the conversion cannot be performed (e.g., involving incompatible offsets).

Examples

>>> from PhysicalQuantities import q
>>> convertvalue(1, q.mm.unit, q.km.unit)
1e-06
>>> convertvalue(0, q.degC.unit, q.K.unit) # 0 degC to K
273.15
>>> convertvalue(273.15, q.K.unit, q.degC.unit) # 273.15 K to degC
0.0
PhysicalQuantities.unit.findunit(unitname)[source]

Find and return a PhysicalUnit instance from its name string or object.

Uses caching for performance. Handles simple fraction notation like ‘1/s’.

Parameters

unitname: str or PhysicalUnit

The name of the unit (e.g., ‘m’, ‘km/h’, ‘N*m’) or a PhysicalUnit object.

Returns

PhysicalUnit

The corresponding PhysicalUnit instance.

Raises

UnitError

If the input unitname is an empty string, cannot be parsed, or does not correspond to a known unit in the unit_table.

Examples

>>> findunit('mm')
<PhysicalUnit mm>
>>> findunit('1/s')
<PhysicalUnit 1/s>
PhysicalQuantities.unit.isphysicalunit(x)[source]

Check if an object is an instance of the PhysicalUnit class.

Parameters

x: Any

Object to check.

Returns

bool

True if x is a PhysicalUnit instance, False otherwise.