unitsnet-py
The unitsnet-py package provides an efficient way to store unit variables and perform easy conversions to different units when it required.
It offers support for more than 100 unit types across various unit categories, including pretty-printing, comparison, and arithmetic methods.
The API is designed to be user-friendly and straightforward to use.
The library is built on top of the Units.NET project and leverages their definitions sources to generate the Python unit classes.
The unitsnet-py package does not require any external dependencies or packages to function.
Package is available on PyPI at https://pypi.org/project/unitsnet-py/
Units.NET on other platforms
Install via PyPi
pip install unitsnet-py
Example Usage
from unitsnet_py import Angle, AngleUnits, Length, LengthUnits
angle = Angle.from_degrees(180)
angle = Angle(180, AngleUnits.Degree)
print(angle.radians)
print(angle.microradians)
print(angle.gradians)
print(angle.microdegrees)
print(angle.convert(AngleUnits.Radian))
print(angle.convert(AngleUnits.Microradian))
print(angle.convert(AngleUnits.Gradian))
print(angle.convert(AngleUnits.Microdegree))
print(angle.to_string())
print(angle.to_string(AngleUnits.Degree))
print(angle.to_string(AngleUnits.Radian))
print(angle.to_string(AngleUnits.Radian, 2))
Additional methods
Check, compare, calculate etc. with unitsnet:
length1 = Length.from_meters(10)
length2 = Length.from_decimeters(100)
length3 = Length.from_meters(3)
print(length1 == length2)
print(length1 == length3)
print(length3 > length1)
print(length3 < length1)
print(length2 >= length1)
results1 = length1 + length3
results2 = length1 - length3
results3 = length1 * length3
results4 = length1 / length3
results5 = length1 % length3
results6 = length1 ** length3
print(results1.to_string(LengthUnits.Meter))
print(results2.to_string(LengthUnits.Meter))
print(results3.to_string(LengthUnits.Meter))
print(results4.to_string(LengthUnits.Meter))
print(results5.to_string(LengthUnits.Meter))
print(results6.to_string(LengthUnits.Meter))
import numpy as np
np_array = np.array([[2, 4, 6], [7, 8, 9]])
np_array_length = Length.from_kilometers(np_array)
print(np_array_length.meters)
np_array_double_length = np_array_length + np_array_length
print(np_array_double_length.kilometers)
print(np_array_double_length.meters)
DTO - Data Transfer Object
As UnitsNet provides a convenient way to work within a running service, there are occasions where the data needs to be exposed outside of the service, typically through an API containing the unit value or consumed from an API.
To support this with a clear API schema and make it easy to convert to and from this schema to the specific format, it's recommended to use DTOs and the UnitsNet flavor converters.
from unitsnet_py import Length, LengthDto, LengthUnits
length = Length.from_meters(100.01)
length_dto_json = length.to_dto_json()
length_dto_represents_in_km_json = length.to_dto_json(LengthUnits.Kilometer)
length_from_meters_dto = Length.from_dto_json(length_dto_json)
length_from_km_dto = Length.from_dto_json(length_dto_represents_in_km_json)
length_dto: LengthDto = length.to_dto()
length_json = length_dto.to_json()
length_dto: LengthDto = LengthDto.from_json(length_json)
length = Length.from_dto(length_dto)
Check out the OpenAPI unitsnet-openapi-spec example schema.
Also, refer to the detailed discussions on GitHub: haimkastner/unitsnet-js#31 & angularsen/UnitsNet#1378.
Supported units
UnitsNet supported Units
Units.NET on other platforms
Get the same strongly typed units on other platforms, based on the same unit definitions.