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

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

Download URL typedunits-0.0.1.dev20241105180513-cp312-cp312-win_amd64.whl
Size 707.9 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
e4361d4e1880203b2b3932eeee6054262196b716d07bea6c31863d4396ead645
BLAKE2b-256 checksum
How to use checksums
1bf5ef122d39648cce97a01b711ca7db97bca3d1c802a848f23df2e5a15d81fc
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.dev20241105180513-cp312-cp312-win32.whl

Download URL typedunits-0.0.1.dev20241105180513-cp312-cp312-win32.whl
Size 667.8 kB
Tags CPython 3.12 Windows x86-32
SHA-256 checksum
How to use checksums
270aa2b821ee585bc18bfc7134c4fd65e57e656906e486b73ad25f3e4c27675f
BLAKE2b-256 checksum
How to use checksums
75a14a613d73453d32b6d8ba1f8b91db74efb4b26943a748a0a7aa69700e9038
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.dev20241105180513-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20241105180513-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
17e143e5782397bb92c7a6340b1b104d7c70ca32dbc9edea7197f8a99fc9f5ca
BLAKE2b-256 checksum
How to use checksums
3d5bcb0fa6878de54742a019f615d8d6e69cc17d96ca70dde5a39bacc88b6c43
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.dev20241105180513-cp312-cp312-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20241105180513-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
fdee961fffb330ebdbbbf413a67fffb463705f27a92b12f2ab17e743e5988e0b
BLAKE2b-256 checksum
How to use checksums
020d34ef591e7fcf0634290e6c11ceadafc3788e6302464673c3ebda29b2f20f
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.dev20241105180513-cp312-cp312-macosx_10_13_x86_64.whl

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

Download URL typedunits-0.0.1.dev20241105180513-cp311-cp311-win_amd64.whl
Size 735.3 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
908ea55779340aef1c0421c1aeeb1eaa109ba24e7ab60e9f4c12c9980f3d305d
BLAKE2b-256 checksum
How to use checksums
69058461e672c38391a3d6cc8bc1bedd7f6620996d21ee198b918b82a04b7a33
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.dev20241105180513-cp311-cp311-win32.whl

Download URL typedunits-0.0.1.dev20241105180513-cp311-cp311-win32.whl
Size 700.5 kB
Tags CPython 3.11 Windows x86-32
SHA-256 checksum
How to use checksums
4182d8067c58d5c6ff6bd41bae2b9bf6a5ce9d4c47d3aa07e87482a71f6cb226
BLAKE2b-256 checksum
How to use checksums
076e939ab7dc69f1aab4c1c1f21a2f78f5c1f93dba9c8b5ab41cf8e357c5c265
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.dev20241105180513-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20241105180513-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
daa75eb2219f251219b0c593cfc7d8a15949e39f87d31819673804785a87d7d2
BLAKE2b-256 checksum
How to use checksums
d22ac2f3b15fb9fc425e37d62bafd37906919a4fd3b635b4effd6d56d72c97ab
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.dev20241105180513-cp311-cp311-macosx_11_0_arm64.whl

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

Download URL typedunits-0.0.1.dev20241105180513-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
e84e394dabfbf4ad813e522e8c4ee345821a8fd79683c25eb29d1b3eed199e15
BLAKE2b-256 checksum
How to use checksums
11e0cbb7cd31f7f42a275bd3ded4dfae8df7e4b727fe86d6432bb170968f25af
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.dev20241105180513-cp310-cp310-win_amd64.whl

Download URL typedunits-0.0.1.dev20241105180513-cp310-cp310-win_amd64.whl
Size 731.2 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
b1ba64aec85237d9a51a85ca6a9fce0d99cef66a72ee144c0caaa52b54e75ea5
BLAKE2b-256 checksum
How to use checksums
390e2e2c60f60874172cc0158a8a59443baea96abbcd4ebfff2a598d83b8aebc
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.dev20241105180513-cp310-cp310-win32.whl

Download URL typedunits-0.0.1.dev20241105180513-cp310-cp310-win32.whl
Size 669.1 kB
Tags CPython 3.10 Windows x86-32
SHA-256 checksum
How to use checksums
aebd88598978cef59fd8c4c137b876c032250af751c9e972dbd8d647ed387321
BLAKE2b-256 checksum
How to use checksums
51bdb9153ce7f108f6e88d2f0cb4d3f0b56d3b16ecca7b2bd6b35eb7f77076c9
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.dev20241105180513-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20241105180513-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
a8f6a51dbd0f169a43706e0b9c9a4130e6241e4ed656bcc48e2adb26a9827be6
BLAKE2b-256 checksum
How to use checksums
362f0cd89b2d51e5fe9471221a59e8f3ce2d8fe6b2effe8ec80ede3aef87ab45
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.dev20241105180513-cp310-cp310-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20241105180513-cp310-cp310-macosx_11_0_arm64.whl
Size 719.0 kB
Tags CPython 3.10 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
75393ccc179d842da1ce7186274bda54abbc9cd3d861ff7c6540445310e8e593
BLAKE2b-256 checksum
How to use checksums
26b673c9febcac9fecbfaf27438b5863daba2bbe11f987752bfdc11a5700627e
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.dev20241105180513-cp310-cp310-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20241105180513-cp310-cp310-macosx_10_9_x86_64.whl
Size 777.3 kB
Tags CPython 3.10 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
263a753bdcdb30fb4d22b2fb98379779da6d1cbf43666469c945a7a0f4b44c9d
BLAKE2b-256 checksum
How to use checksums
50ef306ea3cefa4b62294bd2de29e74ce8f3309fb05440f704af8f782cd1f871
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