AUnit

class justunits.AUnit(raw_unit: justunits.BareUnit, prefix: Optional[justunits.Prefix] = None, power: Union[int, float] = 1)

A unit represents just a single unit like meter ‘m’ and second ‘s’. In this context a unit is defined a distingtiv symbol (mostly a single letter).

Parameters
  • raw_unit – The raw unit this unit is based on.

  • prefix – The SI-Prefix of this unit.

  • power – Power of this unit. Default is 1.

Examples

A unit is composed of an immutable RawUnit, SiPrefix and the power the RawUnit comes with.

>>> from justunits import SiPrefixes
>>> raw_apple = BareUnit("apple", "apple", "fruit")
>>> apple = AUnit(raw_apple)
>>> apple
AUnit(apple 'apple' fruit)
>>> print(apple)
apple
>>> kiloapple = AUnit(raw_apple, prefix=SiPrefixes.kilo)
>>> kiloapple
AUnit(kapple 'kiloapple' fruit, 1e+03apple)

AUnit are comparable and hashable to support usage as key within within mappings.

>>> another_apple = create_unit("apple", "apple", "fruit")
>>> apple == another_apple
True
>>> apple != kiloapple
True
>>> hash(apple) == hash(another_apple)
True
>>> hash(apple) == hash(kiloapple)
False
>>> map = {apple: "an apple", kiloapple: "many apples"}
>>> map[kiloapple]
'many apples'