robotpy_toolkit_7407.unum package

Subpackages

Module contents

Main Unum module.

# TODO: consider alternatives to unum and see how they compare.

exception robotpy_toolkit_7407.unum.ConversionError(u)

Bases: UnumError

Failed to convert a unit to the desired type.

exception robotpy_toolkit_7407.unum.IncompatibleUnitsError(unit1, unit2)

Bases: TypeError

An operation on two Unums failed because the units were incompatible.

exception robotpy_toolkit_7407.unum.NameConflictError(unit_key)

Bases: UnumError

Tried to define a symbol that was already defined.

exception robotpy_toolkit_7407.unum.NonBasicUnitError(u)

Bases: UnumError

Expected a basic unit but got a non-basic unit.

exception robotpy_toolkit_7407.unum.ShouldBeUnitlessError(u)

Bases: TypeError

An operation on a Unum failed because it had units unexpectedly.

class robotpy_toolkit_7407.unum.Unum(unit, value=1, conv=None, name='')

Bases: object

Encapsulates a value attached to a unit.

Implements arithmetic operators, dynamic unit consistency checking, and string representation.

AUTO_NORM = True

If True, normalize unums for their string representation.

UNIT_DIV_SEP = '/'

Separator between numerator and denominator.

If set to None, negative exponents are used instead.

UNIT_FORMAT = '[%s]'

Format string for attached unit.

UNIT_HIDE_EMPTY = False

If True, unitless unums are displayed as raw numbers.

UNIT_INDENT = ' '

Separator between value and unit.

UNIT_SEP = '.'

e.g. “5 N.m”.

Type:

Separator between multiple units

UNIT_SORTING = True

If True, units are sorted alphabetically for display.

VALUE_FORMAT = '%s'

Format string for value.

asNumber(other=None)

Return the (normalized) raw value of self.

If other is supplied, first convert to other’s units before returning the raw value.

Raises NonBasicUnitError if other is supplied, but has a value other than 1. (e.g., kg.asNumber(2*g) is an error, but kg.asNumber(g) is ok.)

asUnit(other)

Return a Unum with this Unum’s value and the units of the given Unum.

Raises IncompatibleUnitsError if self can’t be converted to other. Raises NonBasicUnitError if other isn’t a basic unit.

checkNoUnit()

Raise ShouldBeUnitlessError if self has a unit.

static coerceToUnum(value)

Return a unitless Unum if value is a number.

If value is a Unum already, it is returned unmodified.

converted()

returns self converted following _unitTable raises UnumError exception if the self’s unit is not unique

or if no conversion exists for self

copy(normalized=False)

Return a copy of this Unum, normalizing the copy if specified.

classmethod getUnitTable()

Return a copy of the unit table.

matchUnits(other)

Return (self, other) where both Unums have the same units.

Raises IncompatibleUnitsError if there is no way to do this. If there are multiple ways to do this, the units of self, then other are preferred, and then by maximum level.

maxLevel()

returns the maximum level of self’s units

normalize(forDisplay=False)

Normalize our units IN PLACE and return self.

Substitutions may be applied to reduce the number of different units, while making the fewest substitutions.

If forDisplay is True, then prefer a single unit to no unit. # TODO: example of forDisplay. # TODO: simplify normalize so it fits in 80 columns…

replaced(u, conv_unum)

Return a Unum with the string u replaced by the Unum conv_unum.

If u is absent from self, a copy of self is returned.

classmethod reset(unitTable=None)

Clear the unit table, replacing it with the new one if provided.

This is generally only useful when playing around with defining new units in the interpreter.

strUnit()

Return a string representation of our unit.

classmethod unit(symbol, conv=0, name='')

Return a new unit represented by the string symbol.

If conv is 0, the new unit is a base unit. If conv is a Unum, the new unit is a derived unit equal to conv.

>>> KB = Unum.defineUnit("kB", 0, "kilobyte")
>>> MB = Unum.defineUnit("MB", 1000*KB, "megabyte")
exception robotpy_toolkit_7407.unum.UnumError

Bases: Exception

A Unum error occurred that was unrelated to dimensional errors.