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

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

Download URL typedunits-0.0.1.dev20241112205618-cp312-cp312-win_amd64.whl
Size 712.3 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
4c163f5b57315c5f5f3f4f5b80b55c8f9db594d9a46d66bf97b4ad491128a66e
BLAKE2b-256 checksum
How to use checksums
7fe0b9954c0b648ca1de1c848eade877559156fe9e72d70ec914ce132efcc173
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.dev20241112205618-cp312-cp312-win32.whl

Download URL typedunits-0.0.1.dev20241112205618-cp312-cp312-win32.whl
Size 672.3 kB
Tags CPython 3.12 Windows x86-32
SHA-256 checksum
How to use checksums
4cc90163a937004a388b44b586696600828f912d29251217fb5ac5c6689edc58
BLAKE2b-256 checksum
How to use checksums
675ca6fd3633a5b1064d227e3d9e37937947048ba2d83542147aa9318e86d51c
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.dev20241112205618-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20241112205618-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
8da3a27b3b55d707b7f6347f379162af928b6888a35b6ef260332ab7a5962b70
BLAKE2b-256 checksum
How to use checksums
ffc7cf28aefa2174e24995f035e057211fe850f0720a401c180cbc3236ba532d
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.dev20241112205618-cp312-cp312-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20241112205618-cp312-cp312-macosx_11_0_arm64.whl
Size 744.9 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
78dfa108d1cd3408fc49f00032c9dce92c2b1df5348dd4224166ef568ee69ee4
BLAKE2b-256 checksum
How to use checksums
24c367e06b9e8574065a48f7eef6d400879c15095a12393cbbc753205c526d93
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.dev20241112205618-cp312-cp312-macosx_10_13_x86_64.whl

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

Download URL typedunits-0.0.1.dev20241112205618-cp311-cp311-win_amd64.whl
Size 739.7 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
5a500bde6f7da537ecb65030462882045d18605596a9e608726ec83fb9242c2b
BLAKE2b-256 checksum
How to use checksums
b25af64df98047d891d23fbd06359fbf2a2130c987f0e4c96a97b7442f198295
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.dev20241112205618-cp311-cp311-win32.whl

Download URL typedunits-0.0.1.dev20241112205618-cp311-cp311-win32.whl
Size 704.9 kB
Tags CPython 3.11 Windows x86-32
SHA-256 checksum
How to use checksums
751b0bfe1ec121495192de47fa5d9982ff6a879091b06923c097d443d2d3ad55
BLAKE2b-256 checksum
How to use checksums
76e673ac5da1afdabcfa0f8afa362046ace4cb4638024581b60e360419f0b20a
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.dev20241112205618-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20241112205618-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
c2a34e59027d68e8dcbdeb80f5156f4f887d698d4da19fb6a49ea47974c5852b
BLAKE2b-256 checksum
How to use checksums
bec7b2247a6a3d669a70784ab8b421787ccca12fddfdabe4f134f913a987f161
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.dev20241112205618-cp311-cp311-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20241112205618-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
2047b11abc04093214a17b3a284e35dce76b39c03932d0b095cbe2cfef8b582c
BLAKE2b-256 checksum
How to use checksums
9e3f8a5a633aad5d5a035a8df8c1f53561926cd1488e795184848dbce01cc744
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.dev20241112205618-cp311-cp311-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20241112205618-cp311-cp311-macosx_10_9_x86_64.whl
Size 814.0 kB
Tags CPython 3.11 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
65662c78e78f6467f5235f12e35b330ff597828114d552eb8e6174c0092caaab
BLAKE2b-256 checksum
How to use checksums
5fa91dc96e3054cb2a5c65f86694d8ef69753c523ece6bd924490362d0bd82df
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.dev20241112205618-cp310-cp310-win_amd64.whl

Download URL typedunits-0.0.1.dev20241112205618-cp310-cp310-win_amd64.whl
Size 735.6 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
e3885fd3263b2036817b33df06973d39c579a3dec1421d26d0c9b5bc510c014d
BLAKE2b-256 checksum
How to use checksums
1f773b7fc5f3c2267bf2c89a28a861d546551bb61af7d00d6ec684c0901fee62
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.dev20241112205618-cp310-cp310-win32.whl

Download URL typedunits-0.0.1.dev20241112205618-cp310-cp310-win32.whl
Size 673.7 kB
Tags CPython 3.10 Windows x86-32
SHA-256 checksum
How to use checksums
128699558a6faa985730b4c6b5d5fe1b7b5ef3fe5dd5831bd7a996c0c68954d1
BLAKE2b-256 checksum
How to use checksums
d6fda6a75a439ed6ee9ecf4d69403adeeee4a5dd51947a10e5c6afb755b93afc
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.dev20241112205618-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20241112205618-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
89d858954003ac548a18d6123aa016a00b90226301d938c11471355b26e2902c
BLAKE2b-256 checksum
How to use checksums
4be1b032a2489885a47abff2777d38c7cacecbaf8784704b8eabfe3a11f94662
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.dev20241112205618-cp310-cp310-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20241112205618-cp310-cp310-macosx_11_0_arm64.whl
Size 723.3 kB
Tags CPython 3.10 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
486c59c53a0948cff9661ec82b37d3a598a9b1bec51cb7d1b1aa7a213076bf65
BLAKE2b-256 checksum
How to use checksums
25e30383ca08066a44fd9c4c6a4a8f983c3f122b2ba1edab2843c21be24e1ed2
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.dev20241112205618-cp310-cp310-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20241112205618-cp310-cp310-macosx_10_9_x86_64.whl
Size 781.6 kB
Tags CPython 3.10 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
76515c61571d388674abe3bf60f605b4a9db4d3ed5546b03deeb1ebfb570e411
BLAKE2b-256 checksum
How to use checksums
2aa7b972e83eb7aab2f7891dd841bfcc9426d491cda59feb1f3b42e361684d5a
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