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

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

Total release size: 23.1 MB

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

Download URL typedunits-0.0.1.dev20250305205907-cp313-cp313-win_amd64.whl
Size 768.2 kB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
89b0d925dd5471b59d045f5aaa1e2a68e0c5efbc1135ec2c504813bf72881d20
BLAKE2b-256 checksum
How to use checksums
b0c2b01efff107c63304d1d3fc3faa2dd071de6c2ce18d6761f14c1bb8b39c04
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.dev20250305205907-cp313-cp313-win32.whl

Download URL typedunits-0.0.1.dev20250305205907-cp313-cp313-win32.whl
Size 725.6 kB
Tags CPython 3.13 Windows x86-32
SHA-256 checksum
How to use checksums
e2b6005c0860a51ed676b2a90c0f8e23feacff43d977e2e39c36e476a7b4d912
BLAKE2b-256 checksum
How to use checksums
1675613ec032a4525a575df992b456bdaf7b5cc3d20d85d35245d06089c99d95
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.dev20250305205907-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250305205907-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
53025be671c9ea01b5797bb998643fa082380a3805e0ebf9720609d88a272850
BLAKE2b-256 checksum
How to use checksums
3a85170d50b00599b0c231126663ec0ccbadceefbb582c2d6f004508bb6f5d84
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.dev20250305205907-cp313-cp313-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250305205907-cp313-cp313-macosx_11_0_arm64.whl
Size 795.6 kB
Tags CPython 3.13 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
135b1c9930ec9471b748b8136c7d97c44cfcf3641fdabba4aca94993f1ccd460
BLAKE2b-256 checksum
How to use checksums
7c2779693ee89d67391f2f01eb9435f7cf8bf3fbb88bf5f7a4225ece4252efdd
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.dev20250305205907-cp313-cp313-macosx_10_13_x86_64.whl

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

Download URL typedunits-0.0.1.dev20250305205907-cp312-cp312-win_amd64.whl
Size 769.6 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
93392e0fef4e59157ef36981c1172d16ba2be243e5b28fbab03b93b66e9475ec
BLAKE2b-256 checksum
How to use checksums
1ad25db84b126b3d32dd826c86e1220db6a614b6e78b64db5c6661a33a7e3c78
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.dev20250305205907-cp312-cp312-win32.whl

Download URL typedunits-0.0.1.dev20250305205907-cp312-cp312-win32.whl
Size 726.7 kB
Tags CPython 3.12 Windows x86-32
SHA-256 checksum
How to use checksums
e4b0eba2a95c2ddccee03cd52149af7c9af43eb2e96691d8564f021d6c575c48
BLAKE2b-256 checksum
How to use checksums
14181a8b7df93b477742b58acd9fd1d55b7709f1a9711051b7fa9084ad881db0
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.dev20250305205907-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250305205907-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
e0bf999453a77b1bb3b46ec9dc229930b56c086c0031dc2b8a99ddc8f575cd90
BLAKE2b-256 checksum
How to use checksums
14941426fcac5a36a51b0816d2cb8bf672a3aecabe9f919c1481779fc3112083
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.dev20250305205907-cp312-cp312-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250305205907-cp312-cp312-macosx_11_0_arm64.whl
Size 804.6 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
4be541ad03a04edd025f7cdac735ee1dec33ef5db94bc5e5daeedeb67ff2c358
BLAKE2b-256 checksum
How to use checksums
7618e26f98aba54ad56ce3e0ede44a11cb19e958e644d7947c0f7cf55e36ce18
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.dev20250305205907-cp312-cp312-macosx_10_13_x86_64.whl

Download URL typedunits-0.0.1.dev20250305205907-cp312-cp312-macosx_10_13_x86_64.whl
Size 842.4 kB
Tags CPython 3.12 macOS 10.13+ x86-64
SHA-256 checksum
How to use checksums
50442e38e9a72181f04f5a8578f970ef1ef7cda1e92ce56e57407c126f9a93dd
BLAKE2b-256 checksum
How to use checksums
857a5dd556eedd011c07cd244e61f8862d95bfa28b1df1f656f4b0120f8832bf
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.dev20250305205907-cp311-cp311-win_amd64.whl

Download URL typedunits-0.0.1.dev20250305205907-cp311-cp311-win_amd64.whl
Size 797.0 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
4470a25e2146cf8ac8c200128dbd520141640fcfdc3e432850b8dc781d36749c
BLAKE2b-256 checksum
How to use checksums
8e502e241c8cebc1154703723acacf190ebf22404beeb7e00435d44e44c9bce4
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.dev20250305205907-cp311-cp311-win32.whl

Download URL typedunits-0.0.1.dev20250305205907-cp311-cp311-win32.whl
Size 761.6 kB
Tags CPython 3.11 Windows x86-32
SHA-256 checksum
How to use checksums
97b2cd4ea0e5a6f7236f064c838c64ad02c728cd7cbe0053e0a43f4f4af25dba
BLAKE2b-256 checksum
How to use checksums
d2f6188fd7474971075b351660ec80e9b316d2a49d4a72e9d41a0b31be1e0808
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.dev20250305205907-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250305205907-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
08336ff3154f273719e09bfc4f3d05339188f463c91b74be4c999e2a9126fa83
BLAKE2b-256 checksum
How to use checksums
4d28f27902342102add953769682ed2a2cca349fdf6e90e7cd637e14fcdbbc27
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.dev20250305205907-cp311-cp311-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250305205907-cp311-cp311-macosx_11_0_arm64.whl
Size 821.5 kB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
6426b59bb213923092a0a2bbd0b0e863a61b757c193aaec88f2d033215541e92
BLAKE2b-256 checksum
How to use checksums
7498b1f886828c422fb85429e0ed9ddde5f59eeaaa9f0cff2f81e2d3e843db8b
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.dev20250305205907-cp311-cp311-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20250305205907-cp311-cp311-macosx_10_9_x86_64.whl
Size 880.3 kB
Tags CPython 3.11 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
4edcb3964d3bf09bc23dd11952f5101249af9ad84aca604748b0d9b547207cad
BLAKE2b-256 checksum
How to use checksums
0b6113cf3f453abbe3f7611b51dc1f20947b08dea0a1d7a0a6124f2498a8cfd7
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.dev20250305205907-cp310-cp310-win_amd64.whl

Download URL typedunits-0.0.1.dev20250305205907-cp310-cp310-win_amd64.whl
Size 793.4 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
901e6ca076dfe6b9b72ca3e8aa9e11e841ebaeef66e999a764d7c1143e45b3b5
BLAKE2b-256 checksum
How to use checksums
5c85ff8c026c587306ddeaa0f74ac3184d8a9734409920f3a5cf16fff6fa2d9d
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.dev20250305205907-cp310-cp310-win32.whl

Download URL typedunits-0.0.1.dev20250305205907-cp310-cp310-win32.whl
Size 730.3 kB
Tags CPython 3.10 Windows x86-32
SHA-256 checksum
How to use checksums
b6c60f3c15d28dbe14ea471c0869add419a32f8f5a9275e7a564cffa1bfe64f5
BLAKE2b-256 checksum
How to use checksums
4439a0e4cb8d72c56349d97dd29e888f24c5682a94548157d42e46866287416f
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.dev20250305205907-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250305205907-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.5 MB
Tags CPython 3.10 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
9eb0f0be4d22909a70205127c6cd0644f70655dd5c7b4e6e94aa6a7a070cba87
BLAKE2b-256 checksum
How to use checksums
9e64028c954319d7826bb9802428800c0ebd7923f6caa66c4261ae93d998ab08
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.dev20250305205907-cp310-cp310-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250305205907-cp310-cp310-macosx_11_0_arm64.whl
Size 788.6 kB
Tags CPython 3.10 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
7a16cd42656c95d745ec93e7b469c3419fbad24288755ddffc1428b01ef5be9e
BLAKE2b-256 checksum
How to use checksums
fddfda94155a257d65abb5ee7ee9635f2df6a7af3adc083507679da40e744b32
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.dev20250305205907-cp310-cp310-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20250305205907-cp310-cp310-macosx_10_9_x86_64.whl
Size 845.5 kB
Tags CPython 3.10 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
1badd2fb28bdf3467e98048d218eaef0638fb1ef11e30fc0cbc033ac53b53f93
BLAKE2b-256 checksum
How to use checksums
bce3c25b25dc7067dc984c473c7aa17ec5a11b8280be8f8eba38ae6704f8932d
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