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

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

Total release size: 22.4 MB

Release files / typedunits-0.0.1.dev20250214195617-cp313-cp313-win_amd64.whl

Download URL typedunits-0.0.1.dev20250214195617-cp313-cp313-win_amd64.whl
Size 744.0 kB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
d45100cb35d414b7cc141093c171fe693334b3e2814f45b8fc3e8bf838ea1c25
BLAKE2b-256 checksum
How to use checksums
7fb82a6b3570af7d6094fd6353a940311bb9e9d24f651ea4b469509de543d26c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

Release files / typedunits-0.0.1.dev20250214195617-cp313-cp313-win32.whl

Download URL typedunits-0.0.1.dev20250214195617-cp313-cp313-win32.whl
Size 702.2 kB
Tags CPython 3.13 Windows x86-32
SHA-256 checksum
How to use checksums
bbc09e8d6b903423817e41e24a1fffd3d3380d923d23ee67001ba93f25273f60
BLAKE2b-256 checksum
How to use checksums
557a93cbed1b9f19ee56d83c8cf9a9798c5cdb433ec4c06d5435ad2b18af4418
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

Release files / typedunits-0.0.1.dev20250214195617-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250214195617-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.5 MB
Tags CPython 3.13 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
5d95426e8d0aa47d887b7219d5c96180d33fbd49573cece467f03fcf50d79fde
BLAKE2b-256 checksum
How to use checksums
6670146c07851bd8dd999634651adeebdf2c928034a856de351dca852cce0ed3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

Release files / typedunits-0.0.1.dev20250214195617-cp313-cp313-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250214195617-cp313-cp313-macosx_11_0_arm64.whl
Size 771.1 kB
Tags CPython 3.13 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
507b2544c37baf45da0c86ffb1afc26b0015e21765f810475dfbfcc94d0867e6
BLAKE2b-256 checksum
How to use checksums
077d207c138aa5088a95d6a4851dc91eac960f845a490624599903d4d1acad9b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

Release files / typedunits-0.0.1.dev20250214195617-cp313-cp313-macosx_10_13_x86_64.whl

Download URL typedunits-0.0.1.dev20250214195617-cp313-cp313-macosx_10_13_x86_64.whl
Size 811.0 kB
Tags CPython 3.13 macOS 10.13+ x86-64
SHA-256 checksum
How to use checksums
fc75468f62d4e48c14708ecb9f3e49a691b8c1434fd770790329a59d1c188b2f
BLAKE2b-256 checksum
How to use checksums
90f1aadf3df3954999be194b6ecb45f009f62c7b47605659b72be5a6c997df86
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

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

Download URL typedunits-0.0.1.dev20250214195617-cp312-cp312-win_amd64.whl
Size 745.0 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
26d811c823ed2e59f5b0d49f3720c8f27041498d5c105732c762024cb02639f2
BLAKE2b-256 checksum
How to use checksums
d03cd8a0efb2a695f0209b5fbfe57b2c8c120e219ba79e3a7ae6c10a2529c62e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

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

Download URL typedunits-0.0.1.dev20250214195617-cp312-cp312-win32.whl
Size 703.4 kB
Tags CPython 3.12 Windows x86-32
SHA-256 checksum
How to use checksums
551f5f1c83360b1f3d9d425ffb45e519223b5f08c02eb1f90e8b2021012112f4
BLAKE2b-256 checksum
How to use checksums
6662dcca96ea2f98b9f0a8029b5325d1554fbbf74841df5b96f1ff284e1d207c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

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

Download URL typedunits-0.0.1.dev20250214195617-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.5 MB
Tags CPython 3.12 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
839b34c14aa4d8c101b07be52ad8f59f83df62e464c31dacffb9fa23872f5b4e
BLAKE2b-256 checksum
How to use checksums
3a8a3e3b1da10211bf9f678e8fddada943ef52cb45dd113e2a5cb1d56710816b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

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

Download URL typedunits-0.0.1.dev20250214195617-cp312-cp312-macosx_11_0_arm64.whl
Size 778.4 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
ce3ed429f55f8d5a0cf12964833363b28a78f10cd9519ba83f4b41b57b0464cd
BLAKE2b-256 checksum
How to use checksums
2992c9343e94ade9dafe5b2a19ac76af025f62602ad050c39bd3f64e207552cf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

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

Download URL typedunits-0.0.1.dev20250214195617-cp312-cp312-macosx_10_13_x86_64.whl
Size 817.0 kB
Tags CPython 3.12 macOS 10.13+ x86-64
SHA-256 checksum
How to use checksums
7ea3e62e2772c1026a6dd466bd1f22610167839c16400a62ddc9f2b1b371d67e
BLAKE2b-256 checksum
How to use checksums
fa278178a36662147bf2de4562eafd424ff15e17ac499cae4963eb2accf270c5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

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

Download URL typedunits-0.0.1.dev20250214195617-cp311-cp311-win_amd64.whl
Size 772.1 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
bb9bf356e9c2954fbcfecd19ced9db2446cf67c7281838ef526085b66a18a183
BLAKE2b-256 checksum
How to use checksums
37f3229a99d31d9e77572a6546f51343229152b78698d6d5a48545002ba77a85
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

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

Download URL typedunits-0.0.1.dev20250214195617-cp311-cp311-win32.whl
Size 738.1 kB
Tags CPython 3.11 Windows x86-32
SHA-256 checksum
How to use checksums
6be93265972f51e5cf5dafffa373070f3f19ae3400a3f6c2aee524a6a4ceadfb
BLAKE2b-256 checksum
How to use checksums
aa23aa47715ecbf30840596316261eaea1a0a8989eb40e77f615771499165418
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

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

Download URL typedunits-0.0.1.dev20250214195617-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.7 MB
Tags CPython 3.11 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
20cd236b03e975ef4a51bd1711c8c5c7c99b4c0c8b0151d71f77cbb8fb65e0a9
BLAKE2b-256 checksum
How to use checksums
61b41a17f1de5499a7ffb81777b449daf9fd48597b442899850995be1bfcb0c3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

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

Download URL typedunits-0.0.1.dev20250214195617-cp311-cp311-macosx_11_0_arm64.whl
Size 796.7 kB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
a72af4ee1af5bd5fd9b313e13441929c1ce29670d38aca8e0c5d706890920439
BLAKE2b-256 checksum
How to use checksums
3efe9e451672099a56d806f0f6119cad00fa0021d8fe316ad0def33d1a34e066
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

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

Download URL typedunits-0.0.1.dev20250214195617-cp311-cp311-macosx_10_9_x86_64.whl
Size 856.2 kB
Tags CPython 3.11 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
867b3770f77f214ecf2859f2c1a48c973e9a57557e4908f711a419f586b2fa79
BLAKE2b-256 checksum
How to use checksums
2b1ee20b762d3b5d3ce81bf84cb79cf45e57c56d2e95e14f50c4a002424cafa0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

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

Download URL typedunits-0.0.1.dev20250214195617-cp310-cp310-win_amd64.whl
Size 768.8 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
318a8c4a9dc51173f83fecd46610377aae10049c581fc01d2801ddfc320cff85
BLAKE2b-256 checksum
How to use checksums
d24f5accb3d5af352fd04e3c2aecfa6ccb00c32ab80f0736acdebb42474150d1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

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

Download URL typedunits-0.0.1.dev20250214195617-cp310-cp310-win32.whl
Size 707.2 kB
Tags CPython 3.10 Windows x86-32
SHA-256 checksum
How to use checksums
64bcc5795c39525ba28f4ff2ccbc986985af0769077043843decf51bcc3b1b65
BLAKE2b-256 checksum
How to use checksums
30e16b29abc4ac6bf212cb602e44c831b9f1dd8bdb0134dab4ee968ef81ba175
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

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

Download URL typedunits-0.0.1.dev20250214195617-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.4 MB
Tags CPython 3.10 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
6f3ecaf6a3e1e3b7f8f3ebf595e705c69d77acc456978f7f165c9a13c7e3881b
BLAKE2b-256 checksum
How to use checksums
8c88a0dbb852cfa027aa76616fd47ecc8d8b55862a7ef38a0af45ac8564b0da7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

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

Download URL typedunits-0.0.1.dev20250214195617-cp310-cp310-macosx_11_0_arm64.whl
Size 758.8 kB
Tags CPython 3.10 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
ecfc631ce68881663670e9a32b56ca0188567053983657dab4739e2922eb571d
BLAKE2b-256 checksum
How to use checksums
3c7ae72ddc64f32867d0f57d2c26f9e850313ce8f34bed428863192eda8a15f9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

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

Download URL typedunits-0.0.1.dev20250214195617-cp310-cp310-macosx_10_9_x86_64.whl
Size 821.4 kB
Tags CPython 3.10 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
d73245170f4228a0e4a6346355769b66efa536d0cefce935b1c528f268b3c297
BLAKE2b-256 checksum
How to use checksums
333eae7098c7b6930c484a11ef44195b0a05931e6d7c4899c5b6c1af622eddb7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 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