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

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

Download URL typedunits-0.0.1.dev20241018223705-cp312-cp312-win_amd64.whl
Size 705.9 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
0e1fd490a8aacfe0957774ddef2cb9fca5a9483807c3071b2d74083473b7edb7
BLAKE2b-256 checksum
How to use checksums
1c7ca6157d469c070dc51e2c58cae73f84a46f9d8eb000dff97ab9b0e1254fc3
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.dev20241018223705-cp312-cp312-win32.whl

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

Download URL typedunits-0.0.1.dev20241018223705-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
a3d1b1f9b5d8f7abb0dc363a0ebe0f94738646d6f5b7a56437e47e72b7687dae
BLAKE2b-256 checksum
How to use checksums
7e41eacf87588b529a7e2ffbea2568a420f328e1f563a3039c2b9e4238568d3f
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.dev20241018223705-cp312-cp312-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20241018223705-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
d9d84b3b29a0ec694078219c7bf44e7bd6f155820555d2f87e146212614bf6dc
BLAKE2b-256 checksum
How to use checksums
dc2dc469751999705e7055ad7c3f4caf512d7a8054186f2396fac059d94785a8
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.dev20241018223705-cp312-cp312-macosx_10_13_x86_64.whl

Download URL typedunits-0.0.1.dev20241018223705-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
e5e784d502d2a750df932532b7015d62bc876905f6c4f8286c24019f88c7fcf0
BLAKE2b-256 checksum
How to use checksums
c26ce70aaa7d059ff3694eede7765152b3fb03d8e646ba30a1b3f156974b913d
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.dev20241018223705-cp311-cp311-win_amd64.whl

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

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

Download URL typedunits-0.0.1.dev20241018223705-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
7daa688b478a80a7b2917d190511317617f8e8a8a94ad884f33a1b4bf57f43e8
BLAKE2b-256 checksum
How to use checksums
6be252927ed88cd548a28eaaef34fbbe9b15e5c32e7eb44e7a850d4d54e615c3
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.dev20241018223705-cp311-cp311-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20241018223705-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
e72fdffed56bd1330216271441c38632cf94926866ac832a764f0d10c27a407e
BLAKE2b-256 checksum
How to use checksums
f41c54bc0448a638a55dcb234b712c07c237b86a0dc95b6ee4f90a9e99153599
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.dev20241018223705-cp311-cp311-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20241018223705-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
4816a0709e879e349f94927b36508ddb2e07434ee9347cde0018f92f3668b2df
BLAKE2b-256 checksum
How to use checksums
2e19e130d29ebaec2657a39be41d51e228a79b08e6bce7c498146d5f8d204943
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.dev20241018223705-cp310-cp310-win_amd64.whl

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

Download URL typedunits-0.0.1.dev20241018223705-cp310-cp310-win32.whl
Size 667.5 kB
Tags CPython 3.10 Windows x86-32
SHA-256 checksum
How to use checksums
a60a29119443a5c4d96c954a6e630ee215f18ce5827d0dcfca0457cd0b60db78
BLAKE2b-256 checksum
How to use checksums
5f9a9622b01adf9b2e704930c5526e993da37f7f016ae46be9cc968488f6707f
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.dev20241018223705-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20241018223705-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
17251c981149285937db110ed043a51efc926c23261e56c4dea1401d04b14cdd
BLAKE2b-256 checksum
How to use checksums
fa31ce0fa5047e7396f166009d1a09977852aafe36a985630a59ae64d9369a8a
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.dev20241018223705-cp310-cp310-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20241018223705-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
3711c8c7c27ea0567d38b368f19aea26d9896449d35fe80b74d58014c79fccc5
BLAKE2b-256 checksum
How to use checksums
59fdae0a5b39edc35078593a3dafdd40c24ca694edae28dd0291f97371680132
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.dev20241018223705-cp310-cp310-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20241018223705-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
8c137ab0d8d5225ba167a0f0d61968d80b0224ab2cbd62bf443ce964e85e1e35
BLAKE2b-256 checksum
How to use checksums
3568ed0d59be738f271cd09d19f108242639d7ae90b41801dea277c2fe149562
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