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

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

Total release size: 22.7 MB

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

Download URL typedunits-0.0.1.dev20250221185308-cp313-cp313-win_amd64.whl
Size 751.6 kB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
bcc69b9e40f537a01a56d026eb2c7f76faaa5e48dd7d2d03dadea08b18be63cf
BLAKE2b-256 checksum
How to use checksums
83121b63bd86ee26c86b2bc89c2c1971b944a0e25579b62f260b35ddb7cc9d8b
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.dev20250221185308-cp313-cp313-win32.whl

Download URL typedunits-0.0.1.dev20250221185308-cp313-cp313-win32.whl
Size 709.5 kB
Tags CPython 3.13 Windows x86-32
SHA-256 checksum
How to use checksums
cd2179727e6980a6aca55371b9e94674bb79f47656cb5835aaa756819dd6ed23
BLAKE2b-256 checksum
How to use checksums
0413a89c8812b05bcbff8c74b3de4b80b8023e9cc2b2faf25cbcc37806a7835b
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.dev20250221185308-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250221185308-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.6 MB
Tags CPython 3.13 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
0316e22b31e45bca6d2ec5d162a316aaa2a05589abb7020009859a0f4973fedb
BLAKE2b-256 checksum
How to use checksums
f00d7701c38ab30350bc8bb15494d2d4e1c1811de7c48dee06362db0d63bf8ee
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.dev20250221185308-cp313-cp313-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250221185308-cp313-cp313-macosx_11_0_arm64.whl
Size 779.7 kB
Tags CPython 3.13 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
0bba3a201198018a93819609fa5556d59907bb27773878be866743c0412f106d
BLAKE2b-256 checksum
How to use checksums
dba7ead24f4ceafe9249bd39b81034dffe1f4db0e8a77a3121bc537c28b78dd0
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.dev20250221185308-cp313-cp313-macosx_10_13_x86_64.whl

Download URL typedunits-0.0.1.dev20250221185308-cp313-cp313-macosx_10_13_x86_64.whl
Size 819.8 kB
Tags CPython 3.13 macOS 10.13+ x86-64
SHA-256 checksum
How to use checksums
eea73dfebce782ee45c1c28a1d0fd3333eee7c74d899fe4e195eeba4b3e6fddb
BLAKE2b-256 checksum
How to use checksums
a1ce1aabed15945fe88fc0d6cc9d1d6588e83eb7d51c68c1415594b6230bca1a
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.dev20250221185308-cp312-cp312-win_amd64.whl

Download URL typedunits-0.0.1.dev20250221185308-cp312-cp312-win_amd64.whl
Size 753.0 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
10105c04ed6c4cbe5d29784c541aebc02203784d485676c1ce9c4f8b7ba644f5
BLAKE2b-256 checksum
How to use checksums
21b2d4c26eb8dc98f5f4b6fd5af78c814ed1b88b551833817278488954bc95a7
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.dev20250221185308-cp312-cp312-win32.whl

Download URL typedunits-0.0.1.dev20250221185308-cp312-cp312-win32.whl
Size 710.6 kB
Tags CPython 3.12 Windows x86-32
SHA-256 checksum
How to use checksums
50406bb24169eb812ef3f55f277efaa7bcdbb3cee11d6530b957164ac163399b
BLAKE2b-256 checksum
How to use checksums
c012e645a6acff7b53a0e2b1ec27679abc4cc2c10c59f4535ee49fef2de36067
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.dev20250221185308-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250221185308-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.6 MB
Tags CPython 3.12 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
b458fcf647c8ba142d3a1ed14d85324894a809baf9976c66240ee6ba4b2b408d
BLAKE2b-256 checksum
How to use checksums
7d55471dd2d53fa01b2d7272a01c32d033bcf719d6db8e86c094daef01e87f79
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.dev20250221185308-cp312-cp312-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250221185308-cp312-cp312-macosx_11_0_arm64.whl
Size 787.3 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
c453a6ee5008f2e63a833581b2f47a92229ecb4aeff67047f1627f89b7589901
BLAKE2b-256 checksum
How to use checksums
5f47d8b8726dd1312bfd1ab47e323238d2e1c847df46f02878b136a9dd821e66
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.dev20250221185308-cp312-cp312-macosx_10_13_x86_64.whl

Download URL typedunits-0.0.1.dev20250221185308-cp312-cp312-macosx_10_13_x86_64.whl
Size 825.2 kB
Tags CPython 3.12 macOS 10.13+ x86-64
SHA-256 checksum
How to use checksums
5e39541fa774c8a060b444d739d907a6a5ad52a76a493a7bb8b6c33b2a496208
BLAKE2b-256 checksum
How to use checksums
617010f399204c27f540a79545b735f6a256fbaf7028a0e440456c9854c48d75
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.dev20250221185308-cp311-cp311-win_amd64.whl

Download URL typedunits-0.0.1.dev20250221185308-cp311-cp311-win_amd64.whl
Size 780.6 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
8d09f1b5d96d0527383b06eaa593ea203f25135976d3ab0e69e2d36621024fb7
BLAKE2b-256 checksum
How to use checksums
4e1b186669468097b2b184100abe9d43407cdb215dad975281c47fb0d6854db6
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.dev20250221185308-cp311-cp311-win32.whl

Download URL typedunits-0.0.1.dev20250221185308-cp311-cp311-win32.whl
Size 744.7 kB
Tags CPython 3.11 Windows x86-32
SHA-256 checksum
How to use checksums
600c7e6f5aaa40d4142695ea596e33d30c92e1b552ef430a81915533606a6a8d
BLAKE2b-256 checksum
How to use checksums
ea660f22b23072c2bbd55e97b7f1aa5861f4cffd8dd3bb2da219613dece1f383
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.dev20250221185308-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250221185308-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.7 MB
Tags CPython 3.11 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
47e3a919a9c5f9b4d14038d060a401e2fa259163cae3cb54f218d24cda6fd7a1
BLAKE2b-256 checksum
How to use checksums
d6396f63359a1df704899fae0547337642edcfb299f484d9ce8ff906041f7a00
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.dev20250221185308-cp311-cp311-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250221185308-cp311-cp311-macosx_11_0_arm64.whl
Size 804.7 kB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
c7937d3d64a40f8728f75fb27670681f89f686a0785660d8086749f0964255e4
BLAKE2b-256 checksum
How to use checksums
d6ff54e9d368a3aab446e7c12e0fb9143056ff1b06e70a7b7adc70010386cfcd
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.dev20250221185308-cp311-cp311-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20250221185308-cp311-cp311-macosx_10_9_x86_64.whl
Size 863.6 kB
Tags CPython 3.11 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
a968ee85dd94e6368aa13884d1be3a3938d818e9495c53a9d9a93e8fd6ea3976
BLAKE2b-256 checksum
How to use checksums
f143c7ac52b2afa6655719bdfde38212177ccbe21a31d90bf7774b9b438d7fb4
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.dev20250221185308-cp310-cp310-win_amd64.whl

Download URL typedunits-0.0.1.dev20250221185308-cp310-cp310-win_amd64.whl
Size 776.7 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
80145a8cd7086577ffaebbb08eaeac221cfdf02121cff86951a65c066978820b
BLAKE2b-256 checksum
How to use checksums
9c777950d253c59ac8578e13c0271a383bb9ddf594211173f4fb53afe52d3659
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.dev20250221185308-cp310-cp310-win32.whl

Download URL typedunits-0.0.1.dev20250221185308-cp310-cp310-win32.whl
Size 714.0 kB
Tags CPython 3.10 Windows x86-32
SHA-256 checksum
How to use checksums
c10c40767ed48062a863a30509de26a0480ca8f7bcf73b2b8b481da8cc0b1e85
BLAKE2b-256 checksum
How to use checksums
f02ed10d36539a75e6221c7ecda5a2f3aee4e3a185a2010725148c826624d1c3
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.dev20250221185308-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250221185308-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.4 MB
Tags CPython 3.10 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
0cef68ec8213f6bb47da901a2904f642179f7eac4be4edbc1d39e39fb957b592
BLAKE2b-256 checksum
How to use checksums
f72def218ee509996e614580bbf6a1937fa4872a658e74d8d811ed3502494744
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.dev20250221185308-cp310-cp310-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250221185308-cp310-cp310-macosx_11_0_arm64.whl
Size 772.0 kB
Tags CPython 3.10 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
0792c4e5a9ba6f106a797a803b8b649cb371b0ba84fe3507b07de699bd7b377c
BLAKE2b-256 checksum
How to use checksums
ae8c20bc276362d35d7b4f355b157b3667d0750e132e3a1f5e11c6da4592731b
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.dev20250221185308-cp310-cp310-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20250221185308-cp310-cp310-macosx_10_9_x86_64.whl
Size 829.5 kB
Tags CPython 3.10 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
d78a28d40611da1206bd07e57fcfc79557e1f195f9d8930814a7922ef9a87b5e
BLAKE2b-256 checksum
How to use checksums
c513e7b6b646c47ed56f372c51fa9df773beb15c83ae2f349efe18e342a686d6
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