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

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

Total release size: 16.2 MB

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

Download URL typedunits-0.0.1.dev20241120184900-cp312-cp312-win_amd64.whl
Size 722.1 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
d439fb5ef63a1af2e091836e4088486ed174d2d695d7169fb98b0b56b3bf8225
BLAKE2b-256 checksum
How to use checksums
6ee50f33f5e9a97d1627dedf3da9c229dfc966d8fa6828091f1cbc587423ca3e
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.dev20241120184900-cp312-cp312-win32.whl

Download URL typedunits-0.0.1.dev20241120184900-cp312-cp312-win32.whl
Size 682.0 kB
Tags CPython 3.12 Windows x86-32
SHA-256 checksum
How to use checksums
508cfa14a2c08c4a93f3db9f98c2bed7c7d5ea067cfcd6c1d4e9ce2a9a0d86e2
BLAKE2b-256 checksum
How to use checksums
9a9eb25fb56ed20aa11e32fce87ab1c1a7d9d6f38425a7aa4970edf9995d72a8
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.dev20241120184900-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20241120184900-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
4c26236af7c93873ffe26acee6b2f9a9b36947bf3d43d13e6856ccced120210f
BLAKE2b-256 checksum
How to use checksums
27ceb4d7b14489c51dd8e14919a154a83080dc708bbe8d158f0526f953710dc5
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.dev20241120184900-cp312-cp312-macosx_11_0_arm64.whl

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

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

Download URL typedunits-0.0.1.dev20241120184900-cp311-cp311-win_amd64.whl
Size 749.4 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
2eba004ee802adb26a5985121e7a4a826c118a4a2a4351873a800a4cf775afcf
BLAKE2b-256 checksum
How to use checksums
49ea3e1ed10cdca2363c5b921f0ec482fd2ed2f71f7b6bcd35471b5c01554657
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.dev20241120184900-cp311-cp311-win32.whl

Download URL typedunits-0.0.1.dev20241120184900-cp311-cp311-win32.whl
Size 714.8 kB
Tags CPython 3.11 Windows x86-32
SHA-256 checksum
How to use checksums
460f2737afc075753062418e926f56cf265167d7dcef2228fa1151a2e0fb9735
BLAKE2b-256 checksum
How to use checksums
8d2afb070f9c0771234a3a9856aa729cd7241196185e40bd1ac3cecebdac6566
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.dev20241120184900-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20241120184900-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
2d46c925f12239263710b86171b5a0fa276bd3c3bf228174ae04006d5994c62f
BLAKE2b-256 checksum
How to use checksums
ed12f6d8deef06d8cc1871dd508b53fde9f17f81be26c11575a6a0a8d5aee573
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.dev20241120184900-cp311-cp311-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20241120184900-cp311-cp311-macosx_11_0_arm64.whl
Size 767.1 kB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
7767df0b38fa568e7a929018146c89e0b8aadeeb929168134b8f3e0927b594a5
BLAKE2b-256 checksum
How to use checksums
777f4f661d9760132ce775cbbe90d2951da9dca73cb79d26c07d818cd6c7a1c3
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.dev20241120184900-cp311-cp311-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20241120184900-cp311-cp311-macosx_10_9_x86_64.whl
Size 825.5 kB
Tags CPython 3.11 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
2bb31c687926496ffb6f6702fcf24719ec4157eba4d7cfc033ede2ad360ae1e2
BLAKE2b-256 checksum
How to use checksums
e137d6b19492a72fe62078b7bf526da9aab0632ff8da5ead6e19cd6629ca07a4
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.dev20241120184900-cp310-cp310-win_amd64.whl

Download URL typedunits-0.0.1.dev20241120184900-cp310-cp310-win_amd64.whl
Size 745.7 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
5b8ace8a43793dc307448f5e08b0e0c83fa804496cf06be1c2a0bea55afc696c
BLAKE2b-256 checksum
How to use checksums
674db384438328c4664cb9f127b179bdcfa6f75607bed39053b9ee08e8034023
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.dev20241120184900-cp310-cp310-win32.whl

Download URL typedunits-0.0.1.dev20241120184900-cp310-cp310-win32.whl
Size 683.6 kB
Tags CPython 3.10 Windows x86-32
SHA-256 checksum
How to use checksums
07c353d9fe92429f29b098d0941963fbe1d46f155ac53dc1cf886de25c8172a3
BLAKE2b-256 checksum
How to use checksums
e556d0270921fbd43a83db5f1f894a862a800520a266c785ef75aaf0968fc534
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.dev20241120184900-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20241120184900-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
67bde050a3c40b954ec017361085e8f720db0833deee166cf79d10836b22edef
BLAKE2b-256 checksum
How to use checksums
9bc1def9e00f8f8908781e7cb1f92626cf86eefd85797dfdd9dcc5d8d65a598a
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.dev20241120184900-cp310-cp310-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20241120184900-cp310-cp310-macosx_11_0_arm64.whl
Size 734.4 kB
Tags CPython 3.10 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
b3d5927ffaa2b22d2c853a0e9dd2562ab0e202d51fe41a5daef5f44edad5eecd
BLAKE2b-256 checksum
How to use checksums
46a9f48c1bda5c48e2942334dce475b17014beb58877c4cab28a02e691da1929
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.dev20241120184900-cp310-cp310-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20241120184900-cp310-cp310-macosx_10_9_x86_64.whl
Size 791.9 kB
Tags CPython 3.10 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
d943ee24bb84a085cd0f2cc2ff8e51b80cc0f75166c56fd766531fba187ad21f
BLAKE2b-256 checksum
How to use checksums
f85bcaf1871575c42166e25fe1b56db9258b0a4f7a151cee00e572a17f5c3b11
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