Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

TUnits - Fast Python Units

GCB CI status:
GCB Build Status

Implements unit of measurement arithmetic, where a number is associated with a product of powers of base units and values with compatible units can be added.

The library is written in Cython for speed. The library provides the ability to statically check dimensionality type (see below) and a limited protobuffer serialization support for select units (see below). Contributions to extend support for more units are welcome.

A precompiled wheel can be installed using pip install typedunits [--pre].

Example

>> import tunits
>> from tunits.units import meter, km, N, MHz

>> print(3*MHz)
Frequency(3, 'MHz')

>> print(5*meter + km)
Length(1005.0, 'm')

>> print(N/meter)
N/m

>> print((N/meter).in_base_units())
kg/s^2

>> print(2*km / tunits.Value(3, 's'))
0.666666666667 km/s

Static Type Check

TypedUnits provides the ability to statically check the dimensionality of variables and parameters. For example mypy would complain about incompatible types for the following code.

from tunits import Frequency, LengthArray
from tunits.units import meter, km, MHz

def frequency_fn(f: Frequency) -> Frequency:
    return 3*f

x = 2 * meter
x_arr = LengthArray([1, 2], km)
y = 3 * km
f = 7 * MHz

# okay
print(frequency_fn(f))
print(x + y)
print(x_arr - y)

# not okay
print(frequency_fn(x))
print(f + x)
print(x - f)
frequency_fn(x_arr)
$ mypy my_code.py
my_code.py:18: error: Argument 1 to "frequency_fn" has incompatible type "Length"; expected "Frequency"  [arg-type]
my_code.py:19: error: No overload variant of "__add__" of "Value" matches argument type "Length"  [operator]
my_code.py:19: note: Possible overload variants:
my_code.py:19: note:     def __add__(self, int | float | complex | number[Any], /) -> Frequency
my_code.py:19: note:     def __add__(self, ValueArray | list[Any] | tuple[Any] | ndarray[Any, dtype[Any]], /) -> ValueArray
my_code.py:19: note:     def __add__(self, Frequency, /) -> Frequency
my_code.py:20: error: No overload variant of "__sub__" of "Value" matches argument type "Frequency"  [operator]
my_code.py:20: note: Possible overload variants:
my_code.py:20: note:     def __sub__(self, int | float | complex | number[Any], /) -> Length
my_code.py:20: note:     def __sub__(self, list[Any] | tuple[Any] | ndarray[Any, dtype[Any]], /) -> ValueArray
my_code.py:20: note:     def __sub__(self, Length, /) -> Length
my_code.py:21: error: Argument 1 to "frequency_fn" has incompatible type "LengthArray"; expected "Frequency"  [arg-type]
Found 4 errors in 1 file (checked 1 source file)

Serialization support

TypedUnits provides protobuffer serialization support for selected units. Contributions are welcome to increase serialization coverage.

>> from tunits import Frequency
>> from tunits.units import MHz
>>
>> v = 3*MHz
>> msg = v.to_proto()
>> print(msg)
units {
  unit: HERTZ
  scale: MEGA
}
real_value: 3

>> Frequency.from_proto(msg)
Frequency(3.0, 'MHz')

Installation

  1. To install a precompiled wheel (add --pre for prelease version)

    pip install typedunits # [--pre] 
    
  2. To locally build the latest version from the main branch

    pip install git+https://github.com/quantumlib/TypedUnits
    
  3. For a local editable copy

    git clone https://github.com/quantumlib/TypedUnits
    cd TypedUnits
    pip install -e .
    

Development and Testing

  1. Clone the repository.

    git clone https://github.com/quantumlib/TypedUnits
    
    cd TypedUnits
    

    All future steps assume you are in the repository's directory.

  2. Install dev environment dependencies.

    pip install -r dev_tools/dev.env.txt
    
  3. Install TUnits

    pip install -e .
    
  4. Test.

    pytest
    

Formatting

dev_tools/format.sh  # to format
dev_tools/format.sh --check  # to verify format

Note: This is not an officially supported Google product

Metadata

Release files for typedunits 0.0.1.dev20241206010532

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distributions (wheels)

Table of built distributions (wheels) for typedunits 0.0.1.dev20241206010532
File
typedunits-0.0.1.dev20241206010532-cp312-cp312-win_amd64.whl CPython 3.12 CPython 3.12 Windows x86-64 Details
typedunits-0.0.1.dev20241206010532-cp312-cp312-win32.whl CPython 3.12 CPython 3.12 Windows x86-32 Details
typedunits-0.0.1.dev20241206010532-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.12 CPython 3.12 Linux glibc 2.17+ x86-64 Details
typedunits-0.0.1.dev20241206010532-cp312-cp312-macosx_11_0_arm64.whl CPython 3.12 CPython 3.12 macOS 11.0+ ARM64 Details
typedunits-0.0.1.dev20241206010532-cp312-cp312-macosx_10_13_x86_64.whl CPython 3.12 CPython 3.12 macOS 10.13+ x86-64 Details
typedunits-0.0.1.dev20241206010532-cp311-cp311-win_amd64.whl CPython 3.11 CPython 3.11 Windows x86-64 Details
typedunits-0.0.1.dev20241206010532-cp311-cp311-win32.whl CPython 3.11 CPython 3.11 Windows x86-32 Details
typedunits-0.0.1.dev20241206010532-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.11 CPython 3.11 Linux glibc 2.17+ x86-64 Details
typedunits-0.0.1.dev20241206010532-cp311-cp311-macosx_11_0_arm64.whl CPython 3.11 CPython 3.11 macOS 11.0+ ARM64 Details
typedunits-0.0.1.dev20241206010532-cp311-cp311-macosx_10_9_x86_64.whl CPython 3.11 CPython 3.11 macOS 10.9+ x86-64 Details
typedunits-0.0.1.dev20241206010532-cp310-cp310-win_amd64.whl CPython 3.10 CPython 3.10 Windows x86-64 Details
typedunits-0.0.1.dev20241206010532-cp310-cp310-win32.whl CPython 3.10 CPython 3.10 Windows x86-32 Details
typedunits-0.0.1.dev20241206010532-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.10 CPython 3.10 Linux glibc 2.17+ x86-64 Details
typedunits-0.0.1.dev20241206010532-cp310-cp310-macosx_11_0_arm64.whl CPython 3.10 CPython 3.10 macOS 11.0+ ARM64 Details
typedunits-0.0.1.dev20241206010532-cp310-cp310-macosx_10_9_x86_64.whl CPython 3.10 CPython 3.10 macOS 10.9+ x86-64 Details

Total release size: 16.2 MB

Release files / typedunits-0.0.1.dev20241206010532-cp312-cp312-win_amd64.whl

Download URL typedunits-0.0.1.dev20241206010532-cp312-cp312-win_amd64.whl
Size 722.2 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
6f188bf2889a0d8a29640ab1bbdb7989aedc532c99b185690eb5ea49708cef73
BLAKE2b-256 checksum
How to use checksums
fc8c470434cd6be72f9924726607b347e6a98d5b2a9659a890c1230ee658b2aa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.9.21

Release files / typedunits-0.0.1.dev20241206010532-cp312-cp312-win32.whl

Download URL typedunits-0.0.1.dev20241206010532-cp312-cp312-win32.whl
Size 682.0 kB
Tags CPython 3.12 Windows x86-32
SHA-256 checksum
How to use checksums
45ae7fee3cdfe84bcdb3a7482baea107f83f5c2d8b296e6e70b6dd4c2ce86394
BLAKE2b-256 checksum
How to use checksums
52a54a1596fdf4582d773fd25a02c50d65a8ae6ea82556bd3d8245316130b0d3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.9.21

Release files / typedunits-0.0.1.dev20241206010532-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20241206010532-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.4 MB
Tags CPython 3.12 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
4a34375669d190a87d2d94b18a5afca406e7147f8b8869b1c1fbb779f2b0982d
BLAKE2b-256 checksum
How to use checksums
3e0aff194dade674adce2a5a7c87a65b0325d3df348f2429ea123f33cf46313c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.9.21

Release files / typedunits-0.0.1.dev20241206010532-cp312-cp312-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20241206010532-cp312-cp312-macosx_11_0_arm64.whl
Size 755.5 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
4915336509131ed2e8a051b61a0e26d6f8d0d5e997780ed81c1a4b6384a55806
BLAKE2b-256 checksum
How to use checksums
20c2f381a3bc883e5ad2d87b4b8de668fb713d0b00e3468a8a40ad104a9800ea
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.9.21

Release files / typedunits-0.0.1.dev20241206010532-cp312-cp312-macosx_10_13_x86_64.whl

Download URL typedunits-0.0.1.dev20241206010532-cp312-cp312-macosx_10_13_x86_64.whl
Size 792.0 kB
Tags CPython 3.12 macOS 10.13+ x86-64
SHA-256 checksum
How to use checksums
4532d42f4a94d6b7fc30da18b50a9e9ea5f5900f1ac8f9ba56531f57199b086d
BLAKE2b-256 checksum
How to use checksums
107963949218f0084c63fe95f502ab7a873525904c45f34c7cd82cae3df58942
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.9.21

Release files / typedunits-0.0.1.dev20241206010532-cp311-cp311-win_amd64.whl

Download URL typedunits-0.0.1.dev20241206010532-cp311-cp311-win_amd64.whl
Size 749.4 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
0996c44a23af961f4a8a1fb064f223f20945f043187420020c93097d230d22c7
BLAKE2b-256 checksum
How to use checksums
d4f9d3b1130e3d187f77c3269a3e44075829af1af7cbed6efdf4d27fe22e0f26
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.9.21

Release files / typedunits-0.0.1.dev20241206010532-cp311-cp311-win32.whl

Download URL typedunits-0.0.1.dev20241206010532-cp311-cp311-win32.whl
Size 714.8 kB
Tags CPython 3.11 Windows x86-32
SHA-256 checksum
How to use checksums
e0f24435df42e22aac7090e829a5a62b5788c4f4fc29bb89a66e8b988b078238
BLAKE2b-256 checksum
How to use checksums
41bcdb29d3b73259ab09b85ab053d2463249c3c53b5805ce098a69b8aaaaca9a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.9.21

Release files / typedunits-0.0.1.dev20241206010532-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20241206010532-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.5 MB
Tags CPython 3.11 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
8434103d5fb071c42743e7cfcac548e45ce0e7324d7a0cd910462ec9087a5846
BLAKE2b-256 checksum
How to use checksums
b6439d83c41d2c7c4141db421a0247c5034402d6aeb23dd6c7f3cc07cb64f1a1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.9.21

Release files / typedunits-0.0.1.dev20241206010532-cp311-cp311-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20241206010532-cp311-cp311-macosx_11_0_arm64.whl
Size 767.1 kB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
a1d10867eb3817956353481d4a0811fa3d098fd192755ad6194d00d31176231e
BLAKE2b-256 checksum
How to use checksums
6f3ca64cb770c8ac55f6306b7759f4422ead070a7314329bb217f4f9253e0dc2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.9.21

Release files / typedunits-0.0.1.dev20241206010532-cp311-cp311-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20241206010532-cp311-cp311-macosx_10_9_x86_64.whl
Size 825.5 kB
Tags CPython 3.11 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
fd928f45d456fc16a2999c336766a9cd5ffb14a0d88470ca0ae46b4222f35bfc
BLAKE2b-256 checksum
How to use checksums
49dc1c068bc31e3be35b6a9269fcbf8ddbb5cb0557fb6faf45e3afd7492b75f7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.9.21

Release files / typedunits-0.0.1.dev20241206010532-cp310-cp310-win_amd64.whl

Download URL typedunits-0.0.1.dev20241206010532-cp310-cp310-win_amd64.whl
Size 745.7 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
c673f388306994be1a6cede09aab8a9fa934bba24b001a0e758f58e16bd81a0f
BLAKE2b-256 checksum
How to use checksums
054b0fd0edd84f9f7e7eb01cdd72516ac3f7365ff665f7c0df78b8d429dc42a8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.9.21

Release files / typedunits-0.0.1.dev20241206010532-cp310-cp310-win32.whl

Download URL typedunits-0.0.1.dev20241206010532-cp310-cp310-win32.whl
Size 683.6 kB
Tags CPython 3.10 Windows x86-32
SHA-256 checksum
How to use checksums
eb01c5e96b3b4d660a55deffcd87ad8b4d8592e7b254c5081d2b9b9791f126ed
BLAKE2b-256 checksum
How to use checksums
54a78cfec8d7d44b3e8cc73850ae28e3e5f65c9520454d0eb1d37442f1af7951
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.9.21

Release files / typedunits-0.0.1.dev20241206010532-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20241206010532-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.3 MB
Tags CPython 3.10 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
06a53dc741d49b3ad68d586a5c0bfbffff873631b6d7e99d078cb76602ee25d4
BLAKE2b-256 checksum
How to use checksums
ca3a3ebab6e12b8de8ddc8e214286b7884518ef80ae4f817b3800eacc214d6da
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.9.21

Release files / typedunits-0.0.1.dev20241206010532-cp310-cp310-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20241206010532-cp310-cp310-macosx_11_0_arm64.whl
Size 734.4 kB
Tags CPython 3.10 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
7443e4c6964c3d2ddc0a3b17a7a584633dca7d421c8e7dca3ead804c50b42d25
BLAKE2b-256 checksum
How to use checksums
abbdf5ad5a4608df081f8464935aab9dde36dbe62f6b788ba76e76c0a43dc16c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.9.21

Release files / typedunits-0.0.1.dev20241206010532-cp310-cp310-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20241206010532-cp310-cp310-macosx_10_9_x86_64.whl
Size 791.9 kB
Tags CPython 3.10 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
71b01147cb385fd7b5a27d330817c78db159be3484c1db821c86721e8738894a
BLAKE2b-256 checksum
How to use checksums
20a3b72dfd3cf4d8d787339170ed835d8535aedac0f8abb5b59d6047996dbdd9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.9.21

Release history Release notifications | RSS feed

0.0.2

30 release files

0.0.1

24 release files

This release
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page