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

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

Total release size: 15.9 MB

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

Download URL typedunits-0.0.1.dev20241111230424-cp312-cp312-win_amd64.whl
Size 707.8 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
1b9d57c088b9d49766206d0987dd23da3156841871da551f43923c60ac3b9c38
BLAKE2b-256 checksum
How to use checksums
b683ddf179cc945ce0cdb44a7c5fe3f9885fb21e67aad43d51e019ae3a24bd99
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.20

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

Download URL typedunits-0.0.1.dev20241111230424-cp312-cp312-win32.whl
Size 667.8 kB
Tags CPython 3.12 Windows x86-32
SHA-256 checksum
How to use checksums
521118f393611bcecdb74681957b999d86dad422b0bb5542bde2e5962df3283f
BLAKE2b-256 checksum
How to use checksums
5260cd5fd6dae6c58f825f474edfdd6e9fc1fdb443f454f2f27b13802188f518
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.20

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

Download URL typedunits-0.0.1.dev20241111230424-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
586ee545f9ba12977f641f704eff47d5bcb059dee888d8cd503523be5187980a
BLAKE2b-256 checksum
How to use checksums
ce59c64ca0857d6fd78a3e0e4e3ec2f28ae3b510ee73a5464bfaca9102cf3a20
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.20

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

Download URL typedunits-0.0.1.dev20241111230424-cp312-cp312-macosx_11_0_arm64.whl
Size 740.5 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
aea62a77aaca2ec35e937c0989423c51b003ead8a7e60cdcbd9f6332f679564f
BLAKE2b-256 checksum
How to use checksums
b9c63e911fae77cc8d63183fd5a0d5bbc633bb5e9f2dc9c6aab85bdd21e1faa6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.20

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

Download URL typedunits-0.0.1.dev20241111230424-cp312-cp312-macosx_10_13_x86_64.whl
Size 773.5 kB
Tags CPython 3.12 macOS 10.13+ x86-64
SHA-256 checksum
How to use checksums
bef56cf479b4b12f0d0e7991378a930ac6b455830f98512b8d58458a73cf2722
BLAKE2b-256 checksum
How to use checksums
3740af014c49a83e21d60d6badd92664123ff335f930642d995f0f43e47949ad
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.20

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

Download URL typedunits-0.0.1.dev20241111230424-cp311-cp311-win_amd64.whl
Size 735.2 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
7de9722fb4059b8299de1b887aee74ad50bf782abe5655003c19831bbcf545e9
BLAKE2b-256 checksum
How to use checksums
f7ba95972fad83177e9ba88e3b31053039d8b77852bfdce3cee9ec04f397a8b7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.20

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

Download URL typedunits-0.0.1.dev20241111230424-cp311-cp311-win32.whl
Size 700.3 kB
Tags CPython 3.11 Windows x86-32
SHA-256 checksum
How to use checksums
50e8b41cb28f667e0dd09380ce6ecbe03f19491b122ea47f4baad903c3ee9883
BLAKE2b-256 checksum
How to use checksums
b3d81c88055371f9716a76b4e503ac250f4d27a4dd23ca7742c09a5bccf67613
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.20

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

Download URL typedunits-0.0.1.dev20241111230424-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
4ab91fb54af807629d1e9a67a55e31be0095040f8df192217cbe56ada7e718c5
BLAKE2b-256 checksum
How to use checksums
7b651cd27498a97c02bcd898270e1a873674afe38d37205a890a5b226a9fb5a1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.20

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

Download URL typedunits-0.0.1.dev20241111230424-cp311-cp311-macosx_11_0_arm64.whl
Size 751.3 kB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
2280ca3c986bbd9fffcd5647fec1ddb0386b52e4d0cb5fa38d4ddf574f09ab4f
BLAKE2b-256 checksum
How to use checksums
a6a19720ca88ad98a70657a51931f169090441e2139a5b30a90b172bc247b6c6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.20

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

Download URL typedunits-0.0.1.dev20241111230424-cp311-cp311-macosx_10_9_x86_64.whl
Size 809.5 kB
Tags CPython 3.11 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
05077d95c76f05a903d6636c5279f0561bfe4bcdd86e1eb2579d18e492aab2c0
BLAKE2b-256 checksum
How to use checksums
67e21c4d8f4db262bdf010d3ff367a1b715485c91963bb59415b6e6e12d5aa2d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.20

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

Download URL typedunits-0.0.1.dev20241111230424-cp310-cp310-win_amd64.whl
Size 731.1 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
eae75225cdc082de8ef6d2cd62a4fc5531afec62434cf80399f0d3c11cc5fe60
BLAKE2b-256 checksum
How to use checksums
bfa6c50fd4dee34cef92952242859b3ae3dfda1bea2549613cb818605f1b8eae
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.20

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

Download URL typedunits-0.0.1.dev20241111230424-cp310-cp310-win32.whl
Size 669.2 kB
Tags CPython 3.10 Windows x86-32
SHA-256 checksum
How to use checksums
d1d32008e9d4a8b0c67aafe6c5c0db4585985eb88406973ab52700a18d861b39
BLAKE2b-256 checksum
How to use checksums
0c3359c23da660af549e7af09c6daf8a89a1518e4ee035a9a951a8ce87015fa6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.20

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

Download URL typedunits-0.0.1.dev20241111230424-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
41c794cd4c04d075b86edf4903206ce397996169d8812b466ce3b30af6cb3486
BLAKE2b-256 checksum
How to use checksums
b821133d3a4c26036b9a2a90e68f5155d9ef186db41b3d549b31b8ac35d1d344
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.20

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

Download URL typedunits-0.0.1.dev20241111230424-cp310-cp310-macosx_11_0_arm64.whl
Size 718.9 kB
Tags CPython 3.10 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
b496d8cd11cd4b4ab7f01e01c7d4336cadcc245f8be64b452646ca8a18509205
BLAKE2b-256 checksum
How to use checksums
02f2405ef170f9a7ce52d2a788582f4babd80cb2db11b5dd97f74ca219146f4e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.20

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

Download URL typedunits-0.0.1.dev20241111230424-cp310-cp310-macosx_10_9_x86_64.whl
Size 777.1 kB
Tags CPython 3.10 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
2c47c8fd8a1b46068928b8af4196706205dcd74b1332ff994a840f251b575083
BLAKE2b-256 checksum
How to use checksums
34201970e6f6e669527adf83ecae772b9f002eb11791725164da8a1ef89efa40
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.20

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