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 .
    

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

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

Download URL typedunits-0.0.1.dev20241015220558-cp312-cp312-win_amd64.whl
Size 705.8 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
5d67acc53359941e00461aa544339e4874fef01b5b500b9231c41b2791a1bd81
BLAKE2b-256 checksum
How to use checksums
1ba75257247a3b528df7d3b9ea07ddded73d004685ecad5afd8f0457e6a718be
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.dev20241015220558-cp312-cp312-win32.whl

Download URL typedunits-0.0.1.dev20241015220558-cp312-cp312-win32.whl
Size 665.7 kB
Tags CPython 3.12 Windows x86-32
SHA-256 checksum
How to use checksums
4edbb705a929615a1e6ec7717617c6640d8e4334bd0beea5896cf7e47a31c27a
BLAKE2b-256 checksum
How to use checksums
2d8373af4310503ca1989d9b4030939fe7702298c34b9f8939eb0d2880010f7c
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.dev20241015220558-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20241015220558-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
94c91c507a796c75703be2667b287c47c862c0b1f7707c449488f70dc8bcdf26
BLAKE2b-256 checksum
How to use checksums
3f8977cf72b40305cf886b68ac3d59ff39b1059a5af501105521192320486207
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.dev20241015220558-cp312-cp312-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20241015220558-cp312-cp312-macosx_11_0_arm64.whl
Size 739.4 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
69006bb6ef3efa6db17393ad32976821681a33980af6d014e8207a121f7d2c69
BLAKE2b-256 checksum
How to use checksums
55874fb4437a49345a1b583ea2064dba220b866cefe6239967882eab2934e396
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.dev20241015220558-cp312-cp312-macosx_10_13_x86_64.whl

Download URL typedunits-0.0.1.dev20241015220558-cp312-cp312-macosx_10_13_x86_64.whl
Size 771.0 kB
Tags CPython 3.12 macOS 10.13+ x86-64
SHA-256 checksum
How to use checksums
5e1494b6c4ab2b5d1ee9851981045777b6af81096da1ab39a4288c1af4923615
BLAKE2b-256 checksum
How to use checksums
6a558677aaf7dbb89cc75d5fdd2a4f505c1ebc18c9bb99a3eec9a4fca98914b6
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.dev20241015220558-cp311-cp311-win_amd64.whl

Download URL typedunits-0.0.1.dev20241015220558-cp311-cp311-win_amd64.whl
Size 733.2 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
3aa069409f943195ee046c14e72de53f467d583fd3427583cb6dedd5fe1af2b4
BLAKE2b-256 checksum
How to use checksums
79fb9f95f0af06b75e01ae34d38046a428d8dc8fcee74fc34dcca4c94aee2cef
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.dev20241015220558-cp311-cp311-win32.whl

Download URL typedunits-0.0.1.dev20241015220558-cp311-cp311-win32.whl
Size 698.8 kB
Tags CPython 3.11 Windows x86-32
SHA-256 checksum
How to use checksums
3a5ceac381f29c6ebfbb953712ae23e6a124e93b02782f5f4f832f8c99895d12
BLAKE2b-256 checksum
How to use checksums
6ea18b20c53c0eea827e88c835711b5e1f00b5ef97a69dd4e8194028557aeb47
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.dev20241015220558-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20241015220558-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
1475d6d3fd8c65170b1e93174d3e80bc3159818bf113eb415a54b010fcd5b7f3
BLAKE2b-256 checksum
How to use checksums
c35c02d60ba1cea1b1b2dd9f6ba02ef694ab3280d1bc7d644f2b3f580a4e6e77
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.dev20241015220558-cp311-cp311-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20241015220558-cp311-cp311-macosx_11_0_arm64.whl
Size 749.2 kB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
0a2e4def5f7951e269f6b90863184239adc432ab43503bc6b1da22a7c6def51c
BLAKE2b-256 checksum
How to use checksums
18fb51896e2d366d5185e20a41e4f889245384f65c4c30f02001266403fe7151
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.dev20241015220558-cp311-cp311-macosx_10_9_x86_64.whl

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

Download URL typedunits-0.0.1.dev20241015220558-cp310-cp310-win_amd64.whl
Size 729.1 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
e05ddf1ac665b48bbb0dd19e96bbfe65d934390c6fb0431e7dd520f73873c195
BLAKE2b-256 checksum
How to use checksums
f81b44c3e91cd22bee70d2952f6c395c804e793b0aa4c8353049d910281d6224
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.dev20241015220558-cp310-cp310-win32.whl

Download URL typedunits-0.0.1.dev20241015220558-cp310-cp310-win32.whl
Size 667.4 kB
Tags CPython 3.10 Windows x86-32
SHA-256 checksum
How to use checksums
2cbed10620335f3e5d5932e23849fb775d397123c1ab7c406b1a9f9406e09ad8
BLAKE2b-256 checksum
How to use checksums
ede0f598340b8453c5c63092b4671b143b3ea8b8f7e9b6267e224119d5a6e3ca
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.dev20241015220558-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20241015220558-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
f3feb09b48aefb99dbb6cdcd4947be0518c2569cc00c2b362cc2de3290709b2c
BLAKE2b-256 checksum
How to use checksums
b9362c7a6481f32c84c78ec2ab23a2abd7ab2e60407d1a096d8359dc077195bb
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.dev20241015220558-cp310-cp310-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20241015220558-cp310-cp310-macosx_11_0_arm64.whl
Size 716.9 kB
Tags CPython 3.10 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
3fd99f2481b4a3d29e5a67ef608f49a5f3a6c8c7fde74e7de53065299bc81a47
BLAKE2b-256 checksum
How to use checksums
59957b6f830b8a967bb68ea6d55268e070862d341dd5db8c6c020ad83eec89aa
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.dev20241015220558-cp310-cp310-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20241015220558-cp310-cp310-macosx_10_9_x86_64.whl
Size 775.0 kB
Tags CPython 3.10 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
5276e319c66d907194660f0e2ad637fce06e7b2539e148aa1d732293b7ac48b5
BLAKE2b-256 checksum
How to use checksums
baa53772125846a5f0dc7f09844ca507f1b1deb85a00447fd29469f6d69dc1ed
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