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

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

Total release size: 13.6 MB

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

Download URL typedunits-0.0.1.dev20240912144504-cp312-cp312-win_amd64.whl
Size 705.8 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
66acab0fa6dab8065e72405ede76875137e0255bcf6b87fff9f8643275f59972
BLAKE2b-256 checksum
How to use checksums
b3c063acfe57994c3127421ea3bf76a68d29a698e5dde134b5aca4933ac84603
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.dev20240912144504-cp312-cp312-win32.whl

Download URL typedunits-0.0.1.dev20240912144504-cp312-cp312-win32.whl
Size 665.7 kB
Tags CPython 3.12 Windows x86-32
SHA-256 checksum
How to use checksums
aadd3ed2a97c99f39a3bedd380863cae6ec83c312444607de13317f12243c0da
BLAKE2b-256 checksum
How to use checksums
62c995df128146c7365c100b8e60e9732ca68393249c9d7f8a831fb9ac6a89b9
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.dev20240912144504-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20240912144504-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
1c027cf1c655b4312cfdcfb08680ebb99da2f91df25e5355e95ec327eae768da
BLAKE2b-256 checksum
How to use checksums
1fe4db65036fb9b69804b45c511b8261365e9feb90192fa6f397870b6fe42d5f
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.dev20240912144504-cp312-cp312-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20240912144504-cp312-cp312-macosx_11_0_arm64.whl
Size 739.4 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
16741a163198a0b57954541b12d37143f5f0a1840620f84240862388bd80e835
BLAKE2b-256 checksum
How to use checksums
15ebce30a53ea4294b2c54d00e41fb966728d4221186fe85147d38d4a579d7cb
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.dev20240912144504-cp311-cp311-win_amd64.whl

Download URL typedunits-0.0.1.dev20240912144504-cp311-cp311-win_amd64.whl
Size 733.2 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
2db21a445d5cf3f709861f17d94860e4221c6b2cab8864cb28f3643120a6038c
BLAKE2b-256 checksum
How to use checksums
3efbc44a630e7f90adf472877cc5570b4cf9a6321e57c3bd24968dda6ea7f3f6
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.dev20240912144504-cp311-cp311-win32.whl

Download URL typedunits-0.0.1.dev20240912144504-cp311-cp311-win32.whl
Size 698.8 kB
Tags CPython 3.11 Windows x86-32
SHA-256 checksum
How to use checksums
80b006e8d957d5702613d0dc619cb52610586b986742bf29674be682dadd3251
BLAKE2b-256 checksum
How to use checksums
b6f61f71c94c17790a2b5c7839899f6bc66b49c3f305c36d8baea0460caeb50a
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.dev20240912144504-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20240912144504-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
e32e9f58ca76e9e263d57ad63b86f86602e029811eff76cc564d0a6fd71c551c
BLAKE2b-256 checksum
How to use checksums
aa3c8bf1e7a6966b33843e8841a42a512984cd5d3025798e3839416c2ddcde45
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.dev20240912144504-cp311-cp311-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20240912144504-cp311-cp311-macosx_11_0_arm64.whl
Size 749.2 kB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
8bffc03e99ad7576ba389f5983c250ec96b3a14be36eabd9ab13a97c6a3ffcbc
BLAKE2b-256 checksum
How to use checksums
daba5080870a8fa5f51990d49cd5ad244a8f91cd78d399a4b4a8d6e2f44dab96
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.dev20240912144504-cp310-cp310-win_amd64.whl

Download URL typedunits-0.0.1.dev20240912144504-cp310-cp310-win_amd64.whl
Size 729.1 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
70a74ec75a0bb753eae42dfe5f468839260530b6dc222da73f99006dab3f627a
BLAKE2b-256 checksum
How to use checksums
951799e98a13886efcd111eba9180ea4c6ea89d710e87b7b0b5633ece4030602
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.dev20240912144504-cp310-cp310-win32.whl

Download URL typedunits-0.0.1.dev20240912144504-cp310-cp310-win32.whl
Size 667.4 kB
Tags CPython 3.10 Windows x86-32
SHA-256 checksum
How to use checksums
0f01ee184911dafa7bb3141fc6c768d63362d713edc77508c542a3e3ba54cf2d
BLAKE2b-256 checksum
How to use checksums
4bfbdb7db48c37f02b02c87688b4d8dea8fd83fe4d0cc42b1d4cea820dccae88
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.dev20240912144504-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20240912144504-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
139a5ab07c8e1eee3714313389345ec374f215ab9a3499d1d50f782cf6d506ca
BLAKE2b-256 checksum
How to use checksums
c1d0fe505e538c0b5433fbe26847fb8ff0049b6905bc90a2ff9a243f409bc65a
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.dev20240912144504-cp310-cp310-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20240912144504-cp310-cp310-macosx_11_0_arm64.whl
Size 716.9 kB
Tags CPython 3.10 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
f75015c8196d5d2b0717a3a2a09730aec3f9537edc36d201c6ae9573314cc16a
BLAKE2b-256 checksum
How to use checksums
81c5d893ba12f6bc2ebb0ffce0573bbd276efd7e5f484071be44a3cbb624bf6f
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