Unit

Labs

class Unit

Description

A concrete amount of quantity to measure the values of quantities of the same kind.

This provides distinction between, e.g. km and m.

Format specification is:

  • :r - print unit reference, e.g. non-prefixed unit

  • :b - print unit in base units

  • :n - print unit name, including any prefix

By default, prints the unit symbol with optional prefix, or its expression if the unit is unnamed.

Inheritance:

Children
Members defined in Unit (22 members)

factor

R

Factor to scale the quantity by relative to type.

is_prefixable

R

Whether this unit can be prefixed.

is_prefixed

R

Whether this unit is prefixed.

kind

R

Quantity metadata of this unit.

name

R

Name given to this unit.

prefix

R

Prefix associated with this unit.

reference_unit

R

Unit this is a (named) reference of.

symbol

R

Symbol given to this unit.

zero

R

Value of the type when this is equal to 0.0.

__hash__

__repr__

__str__

as_prefixable

Convert this unit to prefixable.

base

Expression of this units as its base units.

convertible_from

Whether other unit is convertible to this, i.e. whether their types are equal.

from_absolute_base

Convert absolute base value to this unit’s absolute numerical representation.

from_relative_base

Convert base value relative to some origin to this unit’s numerical representation relative to the same origin.

named

Find or create a new base unit for kind.

one

Get a dimensionless unit one

prefixed

Find or create a new prefixed unit.

to_absolute_base

Convert absolute value to absolute base units numerically.

to_relative_base

Convert value relative to some origin to base units relative to the same origin numerically.

Attributes

property factor: float

Factor to scale the quantity by relative to type.

For base units, this will be equal to 1.0.

property is_prefixable: bool

Whether this unit can be prefixed.

Only named units without a prefix can be prefixed.

property is_prefixed: bool

Whether this unit is prefixed.

Prefixed units cannot again be prefixed.

property kind: syside.experimental.units.Kind

Quantity metadata of this unit.

property name: str | None

Name given to this unit.

Mainly for human readability.

property prefix: syside.experimental.units.Prefix | None

Prefix associated with this unit.

property reference_unit: syside.experimental.units.Unit | None

Unit this is a (named) reference of.

property symbol: str | None

Symbol given to this unit.

This is what is used in quantity formatting. Note that this does not include prefix.

property zero: float

Value of the type when this is equal to 0.0.

Methods

__hash__() → int
__repr__() → str
__str__() → str
as_prefixable() → syside.experimental.units.PrefixableUnit

Convert this unit to prefixable.

Raises ValueError if this unit cannot be prefixed.

base() → list[tuple[syside.experimental.units.Unit, fractions.Fraction]]

Expression of this units as its base units.

convertible_from(arg: syside.experimental.units.Unit, /) → bool

Whether other unit is convertible to this, i.e. whether their types are equal.

from_absolute_base(arg: float, /) → float

Convert absolute base value to this unit’s absolute numerical representation.

from_relative_base(arg: float, /) → float

Convert base value relative to some origin to this unit’s numerical representation relative to the same origin.

static named(name: str, symbol: str, kind: syside.experimental.units.Kind, space_before_symbol: bool = True) → syside.experimental.units.PrefixableUnit

Find or create a new base unit for kind.

static named(name: str, symbol: str, relative: tuple[str, float], kind: syside.experimental.units.Kind, space_before_symbol: bool = True) → syside.experimental.units.PrefixableUnit

Find or create a new base unit for kind with a custom magnitude and type symbol.

This is useful in cases where some root unit is not used as a base unit in the actual system of units, e.g. gram and kilogram - relative is (kg, 1e-3). This allows using print derived units in type representation without actually cyclically depending on them. We only require additional symbol to be used in place of actual symbol for consistent readability in formatted output, i.e. we do not want to print g when printing a unit type with mass dimensions. Additionally, this allows printing any quantity in its base units without having to resolve a new base unit by just normalizing value to base units.

static named(name: str, symbol: str, magnitude: float, reference: syside.experimental.units.Unit, space_before_symbol: bool = True) → syside.experimental.units.PrefixableUnit

Find or create a new unit scaled to reference.

static named(name: str, symbol: str, magnitude: float, expression: Sequence[tuple[syside.experimental.units.Unit, fractions.Fraction | int]], kind: syside.experimental.units.Kind) → syside.experimental.units.PrefixableUnit

Find or create a derived unit with a new expression for a given kind.

Raises ValueError if expression is incompatible with kind.

static named(name: str, symbol: str, magnitude: float, expression: Sequence[tuple[syside.experimental.units.Unit, fractions.Fraction | int]]) → syside.experimental.units.PrefixableUnit

Find or create a new derived unit with a given expression.

static one() → syside.experimental.units.Unit

Get a dimensionless unit one

static prefixed(arg0: syside.experimental.units.Prefix, arg1: syside.experimental.units.PrefixableUnit, /) → syside.experimental.units.Unit

Find or create a new prefixed unit.

to_absolute_base(arg: float, /) → float

Convert absolute value to absolute base units numerically.

to_relative_base(arg: float, /) → float

Convert value relative to some origin to base units relative to the same origin numerically.