Repository navigation
v0.5.0 — Exact scaled units and custom unit definitions
unitx 0.5.0 introduces exact scaled units and lets you define new units directly from existing units or quantities.
- Custom unit definitions: call
.define()onUnit,ScaledUnit, orQuantity, with a required name, optional aliases, and configurable SI prefix support. - Exact scaling:
ScaledUnitpreserves rational and symbolic scales through arithmetic, powers, conversion, copying, and pickling. - Conflict detection: validate symbols, names, aliases, and generated prefixed forms before updating the registry.
- Improved display and typing: show explicit scales in quantity output, reconstruct escaped symbols correctly, and infer
ScaledUnitfor mixed unit arithmetic. - Parser expressions: use
Unit.expression()to obtain the formal unit product in parser syntax.
from unitx.factor import PI
from unitx.quantity import Quantity
from unitx.unit import Unit
# Define a derived unit.
newton = Unit('kg*m/s2').define('N', name='newton')
# Define a unit from a quantity.
minute = (60 * Unit('s')).define(
'min', name='minute', prefixable=False
)
# Define a unit with an exact symbolic scale.
degree = ((PI / 180) * Unit()).define(
'deg', name='degree', prefixable=False
)
assert Quantity(2, minute).to('s').value == 120
assert Quantity(1, 'kN').to(newton).value == 1000
assert degree.factor == PI / 180Definitions are local to the current process and leave their source objects unchanged. Prefer Fraction or Decimal for exact numeric definitions; floats follow Factor’s rational approximation rules and may warn or round to zero and fail. Restoring custom units in another process requires their definitions to be registered there.