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

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

Download URL typedunits-0.0.1.dev20241018230039-cp312-cp312-win_amd64.whl
Size 705.9 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
483d43375900b3e55132a50e9b3233d9a301324c73c3819af1c99418ccf6eed4
BLAKE2b-256 checksum
How to use checksums
9ae2bc54410025b43cff4a223446a67f018d03c727c4de28342910010616c0cc
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.dev20241018230039-cp312-cp312-win32.whl

Download URL typedunits-0.0.1.dev20241018230039-cp312-cp312-win32.whl
Size 665.8 kB
Tags CPython 3.12 Windows x86-32
SHA-256 checksum
How to use checksums
7ceab6634aa483e4eb4895e6fa3efcc427e08cd5f4978cc393744905641076a1
BLAKE2b-256 checksum
How to use checksums
18614e98cbf5fa96317ae294ec85bbc4f39a6d1bbd32995044867ef74de7f866
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.dev20241018230039-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20241018230039-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
c096e8a1520e079a4133c2b3fce215f3a87b1b99e11ce72dfe5b3e66b5fda994
BLAKE2b-256 checksum
How to use checksums
7688c2f576b8482472ca2954e4154dbd0f7596980893033e89228679f53a3884
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.dev20241018230039-cp312-cp312-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20241018230039-cp312-cp312-macosx_11_0_arm64.whl
Size 739.5 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
b92652572439ca16cce103caf90dce245ca37d08e240b6c1b97405f788eb9a29
BLAKE2b-256 checksum
How to use checksums
9255e4cff33a9528ea6ed0835db0e334c627cf2282333a905031bd54f5b25a93
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.dev20241018230039-cp312-cp312-macosx_10_13_x86_64.whl

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

Download URL typedunits-0.0.1.dev20241018230039-cp311-cp311-win_amd64.whl
Size 733.3 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
96e315bba7cf53f6dce7f28c3de7daad6bad69137ca601b42617c697ad2a8e24
BLAKE2b-256 checksum
How to use checksums
ca6d7e7e8092160bd609d7a0b34d6f596baeef4475083aa7aa2276e89873158e
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.dev20241018230039-cp311-cp311-win32.whl

Download URL typedunits-0.0.1.dev20241018230039-cp311-cp311-win32.whl
Size 698.9 kB
Tags CPython 3.11 Windows x86-32
SHA-256 checksum
How to use checksums
c638569c83735400c8b9972e6770d54a5f757ccf69fa410501d778ec4391b79b
BLAKE2b-256 checksum
How to use checksums
22c489ab885d2b7801cd47ae535290b899b91cd76586c4433f41a189c7e29953
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.dev20241018230039-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20241018230039-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
eaca7bb6a1b7795a59cc241175f714202f1edae2480157165692a8530efaba99
BLAKE2b-256 checksum
How to use checksums
92808699aa020c0c946679fe9382f9c2b9c528ffdd6eb84791dcdbaae3f82569
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.dev20241018230039-cp311-cp311-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20241018230039-cp311-cp311-macosx_11_0_arm64.whl
Size 749.3 kB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
f852b71fbc6c43197528a459864e9bd164f3dc2678e937e09543192c013f792e
BLAKE2b-256 checksum
How to use checksums
54959e7a1c1409ccb0b7b707c4ad3181c3349575b752b73b9cfa6dcf79f70688
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.dev20241018230039-cp311-cp311-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20241018230039-cp311-cp311-macosx_10_9_x86_64.whl
Size 807.5 kB
Tags CPython 3.11 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
7836b2bddb5ad628ca71aecd77b317ace5bc1c9eab7df3f01cf4167a41fad5c3
BLAKE2b-256 checksum
How to use checksums
45825121c05079d92288ff3a5fb77f8af533d23a048ea6cc77a236318cd413f7
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.dev20241018230039-cp310-cp310-win_amd64.whl

Download URL typedunits-0.0.1.dev20241018230039-cp310-cp310-win_amd64.whl
Size 729.2 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
53ada8f188e7d1d8eca56cdae24c80e8e568e40d0d61bfc9074861d54c72ea89
BLAKE2b-256 checksum
How to use checksums
d173a3e2c14598ca84413b7c89f6df7b95dd89a9a97c137f180dbba957a0b034
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.dev20241018230039-cp310-cp310-win32.whl

Download URL typedunits-0.0.1.dev20241018230039-cp310-cp310-win32.whl
Size 667.5 kB
Tags CPython 3.10 Windows x86-32
SHA-256 checksum
How to use checksums
3abc3953f0e9a743833802713796092357b5fa48fc160248a11009ab0480383f
BLAKE2b-256 checksum
How to use checksums
54d65a92a3511df78243128514460a06c6bab9a260acac632f24bb4e8b4af0e0
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.dev20241018230039-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20241018230039-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
be705706d7bf2f0e237c71311bcc904245110fcd989290c7c52a13c54b48b3d0
BLAKE2b-256 checksum
How to use checksums
6e123a6230d4e1247e0358b5c2b7d2ec32a3c4c121c7e95f830407c3309d0296
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.dev20241018230039-cp310-cp310-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20241018230039-cp310-cp310-macosx_11_0_arm64.whl
Size 717.0 kB
Tags CPython 3.10 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
fcd52952bfbee1d74c47d119ba3c4b195eeae281d28f8d8db0a7768c79944058
BLAKE2b-256 checksum
How to use checksums
16a13726dee346210a4b9a7233343a18a2ea6791d61500d81d378aa4b9d0b2e2
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.dev20241018230039-cp310-cp310-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20241018230039-cp310-cp310-macosx_10_9_x86_64.whl
Size 775.1 kB
Tags CPython 3.10 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
f922d2191093711fdf260cef61dd01d08e132dfd4b4be61879a6cecd221290ae
BLAKE2b-256 checksum
How to use checksums
5bca2347a46c238a6de63ceaf16993597f05cb37a1fd13cb6c61b6d5d5400461
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