Unit-Aware Measurements

3.2.2 · active · verified Sun Apr 12

The `measurement` library (also known as `python-measurement`) provides easy-to-use unit-aware measurement objects in Python. It allows for manipulation and conversion between various SI, US, and Imperial units for quantities like Distance, Weight, Volume, Temperature, Energy, Speed, and Time. The library is currently active, with its latest release (3.2.2) on January 10, 2023, and maintains a stable API.

Warnings

Install

Imports

Quickstart

Demonstrates creating measurement objects, performing basic arithmetic, and converting between different units.

from measurement.measures import Weight, Distance

# Create a Weight object
weight_1 = Weight(lb=125)
weight_2 = Weight(kg=40)

# Perform arithmetic operations
added_together = weight_1 + weight_2
print(f"Weight 1: {weight_1}")
print(f"Weight 2: {weight_2}")
print(f"Sum in pounds: {added_together.lb:.2f} lb")
print(f"Sum in kilograms: {added_together.kg:.2f} kg")

# Create a Distance object and convert units
distance_m = Distance(m=1000)
distance_km = distance_m.km
distance_miles = distance_m.miles

print(f"1000 meters is {distance_km:.2f} km")
print(f"1000 meters is {distance_miles:.2f} miles")

view raw JSON →