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

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

Total release size: 16.0 MB

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

Download URL typedunits-0.0.1.dev20241114014153-cp312-cp312-win_amd64.whl
Size 712.1 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
1268a7049d61675f0fd533badb3f59e85cdc3d60221831c1f5f96b692e4ec821
BLAKE2b-256 checksum
How to use checksums
cdd336b6d5e2efa9dd4f5b73920f1922066bd5cb5ada9c0c75f12a99104b9504
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.dev20241114014153-cp312-cp312-win32.whl

Download URL typedunits-0.0.1.dev20241114014153-cp312-cp312-win32.whl
Size 672.2 kB
Tags CPython 3.12 Windows x86-32
SHA-256 checksum
How to use checksums
b225dd2d61be85fa9517579e896d66f75d6d4c6c356251725615e4de825d1ab3
BLAKE2b-256 checksum
How to use checksums
0842657989ec6a107038f7b9f74a883e63a5c758f05aa30c7a8fff13616f3e40
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.dev20241114014153-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20241114014153-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
3a23bed6dfb2c6b79bdbb433a65828f55d9a8a8be3067474596200e75c4ad722
BLAKE2b-256 checksum
How to use checksums
4fa0fd92381160d7890dca3c4bcb337ec99387d021fef38a3aeadf6309172f42
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.dev20241114014153-cp312-cp312-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20241114014153-cp312-cp312-macosx_11_0_arm64.whl
Size 744.6 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
b44e10477b6bb2e0dce618f70d3ea975f15a7f4e5513224636ed3943141309f3
BLAKE2b-256 checksum
How to use checksums
d40b016561d88eef49e27cd89875c307ed9f1e8b5a6ec001b65e2ad4d28cccb4
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.dev20241114014153-cp312-cp312-macosx_10_13_x86_64.whl

Download URL typedunits-0.0.1.dev20241114014153-cp312-cp312-macosx_10_13_x86_64.whl
Size 777.5 kB
Tags CPython 3.12 macOS 10.13+ x86-64
SHA-256 checksum
How to use checksums
d466d3221672030f2bca4876f64c3f600404e1433f94539a7ac6cbb4946ab58e
BLAKE2b-256 checksum
How to use checksums
d1189a711419d07c5063de0c7656776d355342210d0cbe29ad7d73b97a2cfae5
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.dev20241114014153-cp311-cp311-win_amd64.whl

Download URL typedunits-0.0.1.dev20241114014153-cp311-cp311-win_amd64.whl
Size 739.5 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
1f99da94e1834b7f6b26fa9e8ac3f1022ecef5fa8e92dd4364adfb203de9778b
BLAKE2b-256 checksum
How to use checksums
4c181fabdc7b319cabfe4e5f91888f190f1261baa3a0934f1f5ec5a44358099e
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.dev20241114014153-cp311-cp311-win32.whl

Download URL typedunits-0.0.1.dev20241114014153-cp311-cp311-win32.whl
Size 705.0 kB
Tags CPython 3.11 Windows x86-32
SHA-256 checksum
How to use checksums
8499c5757fd810560cb32a5fcd9ef59f19ce2cadeb71ac9b626f06d337b9b74c
BLAKE2b-256 checksum
How to use checksums
2d23669cc2f9dca263670742fc72a234b491c31fd8a5d0062fea007b51ec665c
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.dev20241114014153-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20241114014153-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
d4908d0fb697ef23ced1b5d8420ee1750452715481d2c0a400257a913d603d86
BLAKE2b-256 checksum
How to use checksums
7412a324c840d85c0c40006c5f01be3b3bd2661fcf3f3e012dd5b549f81cdd49
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.dev20241114014153-cp311-cp311-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20241114014153-cp311-cp311-macosx_11_0_arm64.whl
Size 755.8 kB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
03749e98611f3a3b8f98540deb1455f52267cd56e22e7e41c576be835060ce99
BLAKE2b-256 checksum
How to use checksums
b0d0b8073f1a240a0be3eb8a87e96291e4ee18b17ba51a5dd4b35158bbcb3ef9
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.dev20241114014153-cp311-cp311-macosx_10_9_x86_64.whl

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

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

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

Download URL typedunits-0.0.1.dev20241114014153-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
9b270508fab6419973af5a4558c32b25e3b73190ec6c7ded7decbb799c47a70b
BLAKE2b-256 checksum
How to use checksums
0eede0de9bda197bca84ec570c0135da8f2453722d9b3f8ca15d7237e6ef602d
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.dev20241114014153-cp310-cp310-macosx_11_0_arm64.whl

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

Download URL typedunits-0.0.1.dev20241114014153-cp310-cp310-macosx_10_9_x86_64.whl
Size 781.4 kB
Tags CPython 3.10 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
27f61a6d2cd46f099170eb15d088df518fa2cbeca95c4a0b33cb82c5dd55d28d
BLAKE2b-256 checksum
How to use checksums
c30c8ea6772df79e0b9ed3380a5c7656eadf7735eebeb51add2ca294b56a857f
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