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

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

Download URL typedunits-0.0.1.dev20241018223718-cp312-cp312-win_amd64.whl
Size 705.9 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
80e7d344e6da9307f62c937aed80055eb9a290628d7a5704ba49f7d3fda10b85
BLAKE2b-256 checksum
How to use checksums
c30dc5c6f839bf0cd647ea89e6a983a51a0e8bab22da8a628a54f9189bc60259
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.dev20241018223718-cp312-cp312-win32.whl

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

Download URL typedunits-0.0.1.dev20241018223718-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
53636b444334ed401fd65a844b3721202a3e1993ef06f29b1334f1461d8f8bec
BLAKE2b-256 checksum
How to use checksums
e8f4dc71ad7b1cb77e1899c5d1802b2db0a7fa290482f1bd68faafac2e9730b5
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.dev20241018223718-cp312-cp312-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20241018223718-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
fc3a930fc62c871a7eb1d03a409ba814fd14ed69be1055d3081beab9190f4461
BLAKE2b-256 checksum
How to use checksums
9507a186ef064e860e9dd91302733500ca29570d7c34b1b1a71c56e164fa0049
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.dev20241018223718-cp312-cp312-macosx_10_13_x86_64.whl

Download URL typedunits-0.0.1.dev20241018223718-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
f8ac0c180099d5a34cc046f34e3f39aed3f8f5f074f660808341721d35933d9d
BLAKE2b-256 checksum
How to use checksums
6952496d9f634116b88bd293bbf756d2ac86d368a1f34c49ffc6929346647dcd
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.dev20241018223718-cp311-cp311-win_amd64.whl

Download URL typedunits-0.0.1.dev20241018223718-cp311-cp311-win_amd64.whl
Size 733.3 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
3af5184095c31d1baae10f71f9668b5bf4b263730c6afb1dad5d47ca2cabd62a
BLAKE2b-256 checksum
How to use checksums
1befe6b72a6aeda51c6f1202c0fe2dde8673271b037e4c9531ac35c4df5ee2fa
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.dev20241018223718-cp311-cp311-win32.whl

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

Download URL typedunits-0.0.1.dev20241018223718-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
2932f61e2c887b64baa7bbc63f7deb23421411bff76a8f2770728a65f6f78002
BLAKE2b-256 checksum
How to use checksums
3b0b865cf051d4e28ce06339f1fb43965f4331e0f8d3d510e65cb12c1236d0c8
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.dev20241018223718-cp311-cp311-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20241018223718-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
116d4008f6c7da29b3549ccdd91c3094b4facdc308d0bea8cb819223abd5b8b1
BLAKE2b-256 checksum
How to use checksums
edaa3b7c7c2b5a9cd26dccb32231d36464852d8e00e954685dac341c09ea9402
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.dev20241018223718-cp311-cp311-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20241018223718-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
431af2dab62a41cfaaf9c9777f8b95478a447d2303318ecdf4dab709dfe96137
BLAKE2b-256 checksum
How to use checksums
ba29f224278bf746301130539131ee81d18534af0574fa248a196d26dc7c872f
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.dev20241018223718-cp310-cp310-win_amd64.whl

Download URL typedunits-0.0.1.dev20241018223718-cp310-cp310-win_amd64.whl
Size 729.2 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
006f0cd1d8731c4faa20fe821cbce637146fa16769583ec888043aab8ffee32c
BLAKE2b-256 checksum
How to use checksums
19fc255c9678c94ad9ba9749ec2ce86724a712f9e93422e602508da48cf132f9
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.dev20241018223718-cp310-cp310-win32.whl

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

Download URL typedunits-0.0.1.dev20241018223718-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
f8a66e6d6ce9bc7fe0d1a53bd95938a903824f14d88d95272d3319872eb90d6d
BLAKE2b-256 checksum
How to use checksums
770000f270ca7ebecc0b8be5ab1e8c70049faad0782da5c6e0879ea70492817e
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.dev20241018223718-cp310-cp310-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20241018223718-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
021ffab55a0d6dce40aefc00259d0aae82eb39d6fdc7c86d8a6f397b5f7aaa50
BLAKE2b-256 checksum
How to use checksums
0c6aab0304efeb9e5414d7bba6fc0dcf4bb054ebc177c0d708e6ec8d904eb6e8
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.dev20241018223718-cp310-cp310-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20241018223718-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
21d49dc0e7046440d388859fa93e639ac385e9cc41a6ad4a0cee5ab70c522fdb
BLAKE2b-256 checksum
How to use checksums
5bb3aa711b727a4637cbf04dfb89511cc8d57950fc4c27c44b169bf8d9d58528
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