Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

TUnits - Fast Python Units

Continuous Integration

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

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

Download URL typedunits-0.0.1.dev20241206012251-cp312-cp312-win_amd64.whl
Size 722.0 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
1a2dbdbc1c4ea598ae3267f894de4fff7d95a8cb955274ac720f0c52c83def18
BLAKE2b-256 checksum
How to use checksums
3b318c97f8a30488f2a911425c751ad5ff52d734f1162c7c7e3d6b52c4789896
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.9.21

Release files / typedunits-0.0.1.dev20241206012251-cp312-cp312-win32.whl

Download URL typedunits-0.0.1.dev20241206012251-cp312-cp312-win32.whl
Size 681.8 kB
Tags CPython 3.12 Windows x86-32
SHA-256 checksum
How to use checksums
ab9bf41ed8321efcbf09e2184261fc7567ccc4d179d0a022a1f3610f70bc29f0
BLAKE2b-256 checksum
How to use checksums
f2b8bad3e6214a3ff9850f7e14ebbb316d76824d9163ab2ef3366502c2e3b1ea
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.9.21

Release files / typedunits-0.0.1.dev20241206012251-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20241206012251-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
a12a3700805caacee1b21956936efab05a9c50085f3276d0508407d082ceeb12
BLAKE2b-256 checksum
How to use checksums
eedddbe2ec39002e9a256a46ba67f11cfb35f36b7170edf73d183b32d04ba44b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.9.21

Release files / typedunits-0.0.1.dev20241206012251-cp312-cp312-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20241206012251-cp312-cp312-macosx_11_0_arm64.whl
Size 755.4 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
52066f0de200e980e4744589e0320d45b762af76bdd0923349b2f227f0512b0c
BLAKE2b-256 checksum
How to use checksums
300dffc1593cf472b4191763b8a1cf8c091afce3d98613c39510bf3697492742
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.9.21

Release files / typedunits-0.0.1.dev20241206012251-cp312-cp312-macosx_10_13_x86_64.whl

Download URL typedunits-0.0.1.dev20241206012251-cp312-cp312-macosx_10_13_x86_64.whl
Size 791.9 kB
Tags CPython 3.12 macOS 10.13+ x86-64
SHA-256 checksum
How to use checksums
92e20f3da49c64ec6f55def4299bcc28edf8282787bb2625b6dbec15768eff1c
BLAKE2b-256 checksum
How to use checksums
aed3aacaa4611c359af533df84ccf29001d74b93f2188b430cadc7dbf06e308e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.9.21

Release files / typedunits-0.0.1.dev20241206012251-cp311-cp311-win_amd64.whl

Download URL typedunits-0.0.1.dev20241206012251-cp311-cp311-win_amd64.whl
Size 749.2 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
dfd0ebbb1a7aa0bb3218d5310a25ea9d918850cd78c6e07b5079f8a169c95ecc
BLAKE2b-256 checksum
How to use checksums
1e7a798db17c5b56d03e4f2d734dbe7d913a166e6890793f63d22bc384b5f5ca
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.9.21

Release files / typedunits-0.0.1.dev20241206012251-cp311-cp311-win32.whl

Download URL typedunits-0.0.1.dev20241206012251-cp311-cp311-win32.whl
Size 714.7 kB
Tags CPython 3.11 Windows x86-32
SHA-256 checksum
How to use checksums
e5132b869f52dace77c4e712ddc8ea681de30a05e20b2bf1266d2f53cc42b253
BLAKE2b-256 checksum
How to use checksums
f4712e3d1e304d32e2f59a219a72ff8e69e77521a18c889e75925e2210502a39
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.9.21

Release files / typedunits-0.0.1.dev20241206012251-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20241206012251-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
c71f61cee54b3ac60162e05cfd9fa4bd5e659d4c1f65214f1002da8040ef40f2
BLAKE2b-256 checksum
How to use checksums
bf5eac61576f75efb3e31a9034cbfc4f1b3b0c7e399550e057fe44ff5c9b14f9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.9.21

Release files / typedunits-0.0.1.dev20241206012251-cp311-cp311-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20241206012251-cp311-cp311-macosx_11_0_arm64.whl
Size 767.0 kB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
1c8e15132940ba6d5dfb6042bb4cc91802580991f1fe0839ba6c158a24fadb6d
BLAKE2b-256 checksum
How to use checksums
294a46ac7169ae61b8d41451aa585a889c56a318decf3cf326323e6256d307a3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.9.21

Release files / typedunits-0.0.1.dev20241206012251-cp311-cp311-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20241206012251-cp311-cp311-macosx_10_9_x86_64.whl
Size 825.4 kB
Tags CPython 3.11 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
8678dfa42d44ee3b5828014aad4358aa7ad92ea080f88ee99c21bb927efa5031
BLAKE2b-256 checksum
How to use checksums
5c197e6dad934d75b3567e63b85928a23c3f8e451d4f1588f1ecfb5a55ffb7a3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.9.21

Release files / typedunits-0.0.1.dev20241206012251-cp310-cp310-win_amd64.whl

Download URL typedunits-0.0.1.dev20241206012251-cp310-cp310-win_amd64.whl
Size 745.5 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
a6836bc1945950a29c80555a2f6a6db39f9591b40f3f94857209f6e5cbce1654
BLAKE2b-256 checksum
How to use checksums
2c3240cb2ae5bf7ccedc75371d30aa9d19621644deedcb6cd11d39859a1474b5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.9.21

Release files / typedunits-0.0.1.dev20241206012251-cp310-cp310-win32.whl

Download URL typedunits-0.0.1.dev20241206012251-cp310-cp310-win32.whl
Size 683.5 kB
Tags CPython 3.10 Windows x86-32
SHA-256 checksum
How to use checksums
f3b966ebab57e83b67743d0b25bb9b1df36c80b137f7b5696769ea0e1318acaf
BLAKE2b-256 checksum
How to use checksums
e5644484356ce3be9b0dd9915ca9593e61dd0a2da4b70288f290fcd03b994a2c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.9.21

Release files / typedunits-0.0.1.dev20241206012251-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20241206012251-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
7c40891a9e020c86ab867b5ca4ad2c8791fbb3a70bc038deb5ab38e03f47c2d0
BLAKE2b-256 checksum
How to use checksums
c6675ca6189f0e78e377bf775c1bbe8377cbfa1607308aa76ec409d64ff7dbb4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.9.21

Release files / typedunits-0.0.1.dev20241206012251-cp310-cp310-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20241206012251-cp310-cp310-macosx_11_0_arm64.whl
Size 734.3 kB
Tags CPython 3.10 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
55b17cb2bd1b95e267cfdf94ba8f482f5d5f121486e5ac0ade1af0fd67f8a521
BLAKE2b-256 checksum
How to use checksums
4f8153affa908f416674935ffa51c588b04084820b4f0542bce1045d8fc66668
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.9.21

Release files / typedunits-0.0.1.dev20241206012251-cp310-cp310-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20241206012251-cp310-cp310-macosx_10_9_x86_64.whl
Size 791.8 kB
Tags CPython 3.10 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
9fae77f37334e75b81bd0bca1df926e8aadbe78bd5ea7cf65fefd0d66398964d
BLAKE2b-256 checksum
How to use checksums
d56943aefd489e30453e9bcf3d3ee6079394717d60b8556af7a8f692f6f54751
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.9.21

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