Skip to main content
Pre-release

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

TUnits - Fast Python Units

Continuous Integration

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.dev20250125002134

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.dev20250125002134
File
typedunits-0.0.1.dev20250125002134-cp313-cp313-win_amd64.whl CPython 3.13 CPython 3.13 Windows x86-64 Details
typedunits-0.0.1.dev20250125002134-cp313-cp313-win32.whl CPython 3.13 CPython 3.13 Windows x86-32 Details
typedunits-0.0.1.dev20250125002134-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.13 CPython 3.13 Linux glibc 2.17+ x86-64 Details
typedunits-0.0.1.dev20250125002134-cp313-cp313-macosx_11_0_arm64.whl CPython 3.13 CPython 3.13 macOS 11.0+ ARM64 Details
typedunits-0.0.1.dev20250125002134-cp313-cp313-macosx_10_13_x86_64.whl CPython 3.13 CPython 3.13 macOS 10.13+ x86-64 Details
typedunits-0.0.1.dev20250125002134-cp312-cp312-win_amd64.whl CPython 3.12 CPython 3.12 Windows x86-64 Details
typedunits-0.0.1.dev20250125002134-cp312-cp312-win32.whl CPython 3.12 CPython 3.12 Windows x86-32 Details
typedunits-0.0.1.dev20250125002134-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.dev20250125002134-cp312-cp312-macosx_11_0_arm64.whl CPython 3.12 CPython 3.12 macOS 11.0+ ARM64 Details
typedunits-0.0.1.dev20250125002134-cp312-cp312-macosx_10_13_x86_64.whl CPython 3.12 CPython 3.12 macOS 10.13+ x86-64 Details
typedunits-0.0.1.dev20250125002134-cp311-cp311-win_amd64.whl CPython 3.11 CPython 3.11 Windows x86-64 Details
typedunits-0.0.1.dev20250125002134-cp311-cp311-win32.whl CPython 3.11 CPython 3.11 Windows x86-32 Details
typedunits-0.0.1.dev20250125002134-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.dev20250125002134-cp311-cp311-macosx_11_0_arm64.whl CPython 3.11 CPython 3.11 macOS 11.0+ ARM64 Details
typedunits-0.0.1.dev20250125002134-cp311-cp311-macosx_10_9_x86_64.whl CPython 3.11 CPython 3.11 macOS 10.9+ x86-64 Details
typedunits-0.0.1.dev20250125002134-cp310-cp310-win_amd64.whl CPython 3.10 CPython 3.10 Windows x86-64 Details
typedunits-0.0.1.dev20250125002134-cp310-cp310-win32.whl CPython 3.10 CPython 3.10 Windows x86-32 Details
typedunits-0.0.1.dev20250125002134-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.dev20250125002134-cp310-cp310-macosx_11_0_arm64.whl CPython 3.10 CPython 3.10 macOS 11.0+ ARM64 Details
typedunits-0.0.1.dev20250125002134-cp310-cp310-macosx_10_9_x86_64.whl CPython 3.10 CPython 3.10 macOS 10.9+ x86-64 Details

Total release size: 21.7 MB

Release files / typedunits-0.0.1.dev20250125002134-cp313-cp313-win_amd64.whl

Download URL typedunits-0.0.1.dev20250125002134-cp313-cp313-win_amd64.whl
Size 721.6 kB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
d4faae554ae001b263420eace093dd383d9a9bd096494bf8d287a05bda1612ca
BLAKE2b-256 checksum
How to use checksums
ed3f30e40d95b1fdd107c5d2ccd3e73d72649bd9fc9bbc8450bd93745279f2ee
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

Release files / typedunits-0.0.1.dev20250125002134-cp313-cp313-win32.whl

Download URL typedunits-0.0.1.dev20250125002134-cp313-cp313-win32.whl
Size 681.7 kB
Tags CPython 3.13 Windows x86-32
SHA-256 checksum
How to use checksums
4e60392d5291db9c46652c3fd91861f14f2f660528261d737a741007323e41d4
BLAKE2b-256 checksum
How to use checksums
91ce141114f787cece933b7113cbe6fe22eed36232b396a374c10009a9af7e59
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

Release files / typedunits-0.0.1.dev20250125002134-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250125002134-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.4 MB
Tags CPython 3.13 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
937298ffae85db0de2efe8eb567d69cf1d37d371b6f8212854d7fc53a14b32f8
BLAKE2b-256 checksum
How to use checksums
c5a21a34308905e38865a4f7bea1df2fdf58135040fc2a6cbeb4c0e9f9931ccb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

Release files / typedunits-0.0.1.dev20250125002134-cp313-cp313-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250125002134-cp313-cp313-macosx_11_0_arm64.whl
Size 748.3 kB
Tags CPython 3.13 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
ac8798304a6c3ae081fcce3b5541c5915e3b08c1ade417d16c8f86f0acb6dcb4
BLAKE2b-256 checksum
How to use checksums
927bf195da8c65917fd6782dd39c2cf0a72867ec8307f25b8fbbd119d37ba7f0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

Release files / typedunits-0.0.1.dev20250125002134-cp313-cp313-macosx_10_13_x86_64.whl

Download URL typedunits-0.0.1.dev20250125002134-cp313-cp313-macosx_10_13_x86_64.whl
Size 785.4 kB
Tags CPython 3.13 macOS 10.13+ x86-64
SHA-256 checksum
How to use checksums
0535726eb7eb0aee090452ed2f88b303dba2b419b5469e4778b5e731cd5def6f
BLAKE2b-256 checksum
How to use checksums
4d8f2550c7512f259ff62974a7cc48d6018ca1fa4624166c94ab100195671077
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

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

Download URL typedunits-0.0.1.dev20250125002134-cp312-cp312-win_amd64.whl
Size 723.2 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
7a2602e6729f6ac6fdee554a7dea4d42354b7d49b3fae6a0a329ba85b8bb9ba1
BLAKE2b-256 checksum
How to use checksums
24ea77af9f57f8da8e067080db69e8542ffc1ebaab358d10e3f3b00416236d10
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

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

Download URL typedunits-0.0.1.dev20250125002134-cp312-cp312-win32.whl
Size 682.7 kB
Tags CPython 3.12 Windows x86-32
SHA-256 checksum
How to use checksums
857d4e71888321649512b1065d857c5fa9b546927833654cd335b3a2c9713ee8
BLAKE2b-256 checksum
How to use checksums
06d6060d40286a24f72c5e6a253aabe09966cc89258890810be2cc23776452b3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

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

Download URL typedunits-0.0.1.dev20250125002134-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
2d5953b4f57eea1205b2a41edd95b4079758b07e4c3f2067a2c62547731cfb30
BLAKE2b-256 checksum
How to use checksums
8c9aa8ea5806e85439e3d1ddbe0a350a5766e3dac96a3b664e57b1344cf5a551
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

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

Download URL typedunits-0.0.1.dev20250125002134-cp312-cp312-macosx_11_0_arm64.whl
Size 756.2 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
a77c51fac52c5a5cf92a50eba40bec113c3030eca88b23c4659d6a2abb6b4223
BLAKE2b-256 checksum
How to use checksums
9f07887956b7c36df1f1b4f21fb1f1abc02d7b25ae2f6f09f9b94603ab2ca15e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

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

Download URL typedunits-0.0.1.dev20250125002134-cp312-cp312-macosx_10_13_x86_64.whl
Size 793.2 kB
Tags CPython 3.12 macOS 10.13+ x86-64
SHA-256 checksum
How to use checksums
2e19b470f332523f6c107308eb9f969555a410c681c074ca163f504f65320788
BLAKE2b-256 checksum
How to use checksums
f7912afcebca2ce042f32774bad9a7d03739a761fe2ce7bb6a3f820df3474887
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

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

Download URL typedunits-0.0.1.dev20250125002134-cp311-cp311-win_amd64.whl
Size 750.3 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
248d5d742afe5df230b468002dce5e34582a70c4c81511a132f5733b28d81dc4
BLAKE2b-256 checksum
How to use checksums
7ab1cd2e31a5d5dad9e3a9e832498db3e04a56fe7378089dc50fa982ae639176
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

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

Download URL typedunits-0.0.1.dev20250125002134-cp311-cp311-win32.whl
Size 715.9 kB
Tags CPython 3.11 Windows x86-32
SHA-256 checksum
How to use checksums
607c9c9d9d7ff3f1f4857a058fd632abcd0b4ca0423efc38a38069f5bb7a1fe1
BLAKE2b-256 checksum
How to use checksums
15f3c5d6f784fd51e522df0c111c307a84dbc7ba00ce7009c02030863f1605e1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

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

Download URL typedunits-0.0.1.dev20250125002134-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.6 MB
Tags CPython 3.11 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
3c16a15b6e115a3adfbddedf792be42131cb5cc1f9c7b666ff20712704e7626b
BLAKE2b-256 checksum
How to use checksums
0e9d0a2f1a22a75f7341534be4aa0b3d8bcf4f96c354843bf9cfb8034bb95de8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

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

Download URL typedunits-0.0.1.dev20250125002134-cp311-cp311-macosx_11_0_arm64.whl
Size 767.4 kB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
85274e4ed6df943fcd2782478448cea5b6d68420f4c412b6d91494e349188934
BLAKE2b-256 checksum
How to use checksums
f650622104c8db2f6f0407c24c8562d7278f8d369673ff0d02dfe12d41a35ac4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

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

Download URL typedunits-0.0.1.dev20250125002134-cp311-cp311-macosx_10_9_x86_64.whl
Size 826.4 kB
Tags CPython 3.11 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
122f82ec7a5ba002fac6441e04db35a7134551712c29954039549a634cd91e8a
BLAKE2b-256 checksum
How to use checksums
8dde1820c3e9a79a574849eb3f0c9d79fd12027ffc5e6477412ec47b31294065
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

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

Download URL typedunits-0.0.1.dev20250125002134-cp310-cp310-win_amd64.whl
Size 746.6 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
f02f55bab041c6dd196c90ccb294ad2dbd9d9ca6c0fa6f2c80f7fe0b0798f42b
BLAKE2b-256 checksum
How to use checksums
06fec569ccc544ce572b33e304bb75ad0ce4734506aed542378136fc5d196808
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

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

Download URL typedunits-0.0.1.dev20250125002134-cp310-cp310-win32.whl
Size 684.2 kB
Tags CPython 3.10 Windows x86-32
SHA-256 checksum
How to use checksums
6ceeb1635ce4192c6709670061e722c06379b9bbe0b6af64541e7111b57f4318
BLAKE2b-256 checksum
How to use checksums
a9e8bc9fae39a452ac2dd26144005bf53b86cc4fc1eb0a22dfe73a44f2f48160
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

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

Download URL typedunits-0.0.1.dev20250125002134-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
ef928c0293782526e8a941b5cd1fac4b3a4d3da2f873ee4561b258a383cfd328
BLAKE2b-256 checksum
How to use checksums
35ed2ae992f20763ca644d834485c7f9e62f16aab8dff5e447509f884a8421ef
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

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

Download URL typedunits-0.0.1.dev20250125002134-cp310-cp310-macosx_11_0_arm64.whl
Size 735.4 kB
Tags CPython 3.10 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
56f5541f3992d7e510853a402b8953fac10f13f68e0ef95718ea21a6073deb85
BLAKE2b-256 checksum
How to use checksums
4413b91362efd24bfdd4097aebff98f0990901e1e06e6376ad15e2b0e5560db3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

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

Download URL typedunits-0.0.1.dev20250125002134-cp310-cp310-macosx_10_9_x86_64.whl
Size 792.3 kB
Tags CPython 3.10 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
1bede08b6fe62270b0e2e30f5acbd60867042e86d6b5c144f41316c19ae0face
BLAKE2b-256 checksum
How to use checksums
51817593022105637e193af5a3248a522ed3f912d39ecd0c2840d30da09dc416
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 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