Skip to main content

A fast units and dimensions library with support for static dimensionality checking and protobuffer serialization.

Project description

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

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

typedunits-0.0.2.dev20251020203009-cp313-cp313-win_amd64.whl (739.2 kB view details)

Uploaded CPython 3.13Windows x86-64

typedunits-0.0.2.dev20251020203009-cp313-cp313-win32.whl (685.0 kB view details)

Uploaded CPython 3.13Windows x86

typedunits-0.0.2.dev20251020203009-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (2.7 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64manylinux: glibc 2.28+ x86-64

typedunits-0.0.2.dev20251020203009-cp313-cp313-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (2.6 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ ARM64manylinux: glibc 2.28+ ARM64

typedunits-0.0.2.dev20251020203009-cp313-cp313-macosx_11_0_arm64.whl (757.0 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

typedunits-0.0.2.dev20251020203009-cp312-cp312-win_amd64.whl (735.8 kB view details)

Uploaded CPython 3.12Windows x86-64

typedunits-0.0.2.dev20251020203009-cp312-cp312-win32.whl (685.9 kB view details)

Uploaded CPython 3.12Windows x86

typedunits-0.0.2.dev20251020203009-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (2.8 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64manylinux: glibc 2.28+ x86-64

typedunits-0.0.2.dev20251020203009-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (2.6 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ ARM64manylinux: glibc 2.28+ ARM64

typedunits-0.0.2.dev20251020203009-cp312-cp312-macosx_11_0_arm64.whl (756.9 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

typedunits-0.0.2.dev20251020203009-cp311-cp311-win_amd64.whl (816.0 kB view details)

Uploaded CPython 3.11Windows x86-64

typedunits-0.0.2.dev20251020203009-cp311-cp311-win32.whl (750.4 kB view details)

Uploaded CPython 3.11Windows x86

typedunits-0.0.2.dev20251020203009-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (2.8 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64manylinux: glibc 2.28+ x86-64

typedunits-0.0.2.dev20251020203009-cp311-cp311-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (2.7 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ ARM64manylinux: glibc 2.28+ ARM64

typedunits-0.0.2.dev20251020203009-cp311-cp311-macosx_11_0_arm64.whl (784.1 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

typedunits-0.0.2.dev20251020203009-cp310-cp310-win_amd64.whl (815.9 kB view details)

Uploaded CPython 3.10Windows x86-64

typedunits-0.0.2.dev20251020203009-cp310-cp310-win32.whl (720.8 kB view details)

Uploaded CPython 3.10Windows x86

typedunits-0.0.2.dev20251020203009-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (2.7 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64manylinux: glibc 2.28+ x86-64

typedunits-0.0.2.dev20251020203009-cp310-cp310-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (2.6 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ ARM64manylinux: glibc 2.28+ ARM64

typedunits-0.0.2.dev20251020203009-cp310-cp310-macosx_11_0_arm64.whl (747.0 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

File details

Details for the file typedunits-0.0.2.dev20251020203009-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for typedunits-0.0.2.dev20251020203009-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 55220c94542d28e5d98010de411245cf93ae570e28812dbd4e36f20428aa448d
MD5 ed48ee0eaa9626422ab010264fe04d1b
BLAKE2b-256 aab22aa6d02c121feb3635b9a4f15fadfb9e8d62a1514d225774b19b576ba3bd

See more details on using hashes here.

File details

Details for the file typedunits-0.0.2.dev20251020203009-cp313-cp313-win32.whl.

File metadata

File hashes

Hashes for typedunits-0.0.2.dev20251020203009-cp313-cp313-win32.whl
Algorithm Hash digest
SHA256 603d40fad8c93c3be821ad04e095738661fd57574faa6564e119fea2572a4638
MD5 17b46a2c201f5f6e34457716fb13c577
BLAKE2b-256 a5fcb1415ecbda96242ea368d7a85af6e788625f10f5e66e133813ddb5aa2294

See more details on using hashes here.

File details

Details for the file typedunits-0.0.2.dev20251020203009-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for typedunits-0.0.2.dev20251020203009-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 c4ba79c2184c4d6830890e859579dd1ba755982c70a2f7b9e4db17f82ab9d550
MD5 98e6fdf0af74cd07745d4a11fb0db269
BLAKE2b-256 f84fd78e7f8cd705cea64e9a0d2f8afac74b4e368a7d11c56179fc8b19977f16

See more details on using hashes here.

File details

Details for the file typedunits-0.0.2.dev20251020203009-cp313-cp313-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for typedunits-0.0.2.dev20251020203009-cp313-cp313-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 fdd95a5b31fbf114abfe8492899e6ff01837b9c915407f16193404da049b58fd
MD5 8d791f6a94e2cb410faeaf510f8e6160
BLAKE2b-256 d765d8d33a50e9bf2da6e825620e6baac2f0af14fbf2b6d4f3a96d627e92b82f

See more details on using hashes here.

File details

Details for the file typedunits-0.0.2.dev20251020203009-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for typedunits-0.0.2.dev20251020203009-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2e97e806c84c83f965f99fc492889682e09155133d4d2eb1987962471beb1cde
MD5 f602fafb6a3c0911fe5c821d41835b89
BLAKE2b-256 18b4775a708efedb1975cb42cb62575203f229b9acb09c261aa91f7fd0dd42d3

See more details on using hashes here.

File details

Details for the file typedunits-0.0.2.dev20251020203009-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for typedunits-0.0.2.dev20251020203009-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 d5a04f72e6e64f3429302c0fc8ef59ccf4456caced4ec1bd1b4986f582c56c0b
MD5 2e4aa5bae3eca11bbebe17c01e493fef
BLAKE2b-256 1b4e44631875c8190730985bb4047ea6654e1ca5427ea4d19ed3d52bb0ac338c

See more details on using hashes here.

File details

Details for the file typedunits-0.0.2.dev20251020203009-cp312-cp312-win32.whl.

File metadata

File hashes

Hashes for typedunits-0.0.2.dev20251020203009-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 db2df23918a903fc7fe21b6d645d17d7044536ab7922a3f62db560ea9c8bb8cf
MD5 5bbb659003d2e53f338f41c4c3436641
BLAKE2b-256 fafda671502685740e247849976e8bb843724cda667fa9699343c538c90cf3c2

See more details on using hashes here.

File details

Details for the file typedunits-0.0.2.dev20251020203009-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for typedunits-0.0.2.dev20251020203009-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 758bd1d3f2a1eab14ab3f6bf5e687ab21da174000c2908b39e7bbd1d4a10244e
MD5 5baa22979ff0e48c91151464524f71d0
BLAKE2b-256 7aadfbdbd3bb8046a8c3371afeb2b31daeb6961ed69bd026b3adcefc2491caff

See more details on using hashes here.

File details

Details for the file typedunits-0.0.2.dev20251020203009-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for typedunits-0.0.2.dev20251020203009-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 f7d4027e0109c93b21bbacef9abcf48e5691ab995348fbb01eef0c288f21541b
MD5 276cd8673477b52044a06a2f01641962
BLAKE2b-256 ffe23a8d273d29c335098599e5c60e5f61117f988bc6d814d0b169d0d7fd87e4

See more details on using hashes here.

File details

Details for the file typedunits-0.0.2.dev20251020203009-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for typedunits-0.0.2.dev20251020203009-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 86e001d3cdbcacd1a4e9da320482f55bca44c40fe9504f5f148d4d07c2693dd1
MD5 2b464b5f2be1c0661a64d8e9b9688f4a
BLAKE2b-256 a1c4584f409cf1d778763305c88d4245938b67da97ffc7380f10f3b0fc950aaa

See more details on using hashes here.

File details

Details for the file typedunits-0.0.2.dev20251020203009-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for typedunits-0.0.2.dev20251020203009-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 39ddaa1f1699724d313c77d1405f3e621d5a0aa45e904b31cfd469a005612b9e
MD5 fa22d2ee1cb85debf59d399fc78216eb
BLAKE2b-256 cb33d4a7d67808db70c2192bb039d51ca5080e3fbe92ff62f4f27d9ef26d1598

See more details on using hashes here.

File details

Details for the file typedunits-0.0.2.dev20251020203009-cp311-cp311-win32.whl.

File metadata

File hashes

Hashes for typedunits-0.0.2.dev20251020203009-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 2df5b3d68c73e0d925fa015df17a0d07d59a72b4015f66d5aab1ad557a83ec68
MD5 d8c498891ae37dbfe300a0d5d8d65f37
BLAKE2b-256 fe7480993b4b0e51743b78fa050ec30a6e7fd88443156aaeb639d3d7eb2085e5

See more details on using hashes here.

File details

Details for the file typedunits-0.0.2.dev20251020203009-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for typedunits-0.0.2.dev20251020203009-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 4a1b393d5dda2cf437632572976d187fd672613b50165c6f611a1416e97a7440
MD5 b9af9b39b8ba86ea83430dfbdc6cefa5
BLAKE2b-256 b67d90e87053c3415e42e588f48a2abba80a615fcc0265f50cd1efffa8f44f6a

See more details on using hashes here.

File details

Details for the file typedunits-0.0.2.dev20251020203009-cp311-cp311-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for typedunits-0.0.2.dev20251020203009-cp311-cp311-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 7bb6be8514b26c29f12b82ec4d6c73c49c28089b180bd3a4151d4738517081ef
MD5 bc687148cc555a00407dfe8756116e8f
BLAKE2b-256 21783dbbc125642a46b385aa9c96fbefe8605f564594e7abe2eb6a6b2d687bb5

See more details on using hashes here.

File details

Details for the file typedunits-0.0.2.dev20251020203009-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for typedunits-0.0.2.dev20251020203009-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b593e1f85170b6e8a4eec6afe55a80b2a421580079dacf5e2f2d72db81459072
MD5 dd9ab863e25f991b10a7d7292f79b50d
BLAKE2b-256 a3b803b090205d91f738d1e3c3f9a31b73414da9cfbadb4de045a3984be60d44

See more details on using hashes here.

File details

Details for the file typedunits-0.0.2.dev20251020203009-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for typedunits-0.0.2.dev20251020203009-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 590ab0d2d59424901a48c062cd79b53608ddeaf0e4d065d28c647af9628325d8
MD5 95fbbcefaf9db2a44a08142796204a8e
BLAKE2b-256 3dd69aabc200aeb97842a7a9850fd07d835b4b5000b5ff9cbcd2db54abf8dc0f

See more details on using hashes here.

File details

Details for the file typedunits-0.0.2.dev20251020203009-cp310-cp310-win32.whl.

File metadata

File hashes

Hashes for typedunits-0.0.2.dev20251020203009-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 8acac3d821df719ddd49e05fdc4ea9188aeb0509e229a8f96071ffb57dee3fca
MD5 e1e40e4f565947411028603972c46d62
BLAKE2b-256 8a124de589b6ea2ddfa95f25dc91aa9caaad688414c592a2ec05a15014c3648d

See more details on using hashes here.

File details

Details for the file typedunits-0.0.2.dev20251020203009-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for typedunits-0.0.2.dev20251020203009-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 e0f19e97420c83c7bf74a979925e89a2f650a631a419483a249a3bd3c0d5bd2d
MD5 bb2d2da7b214e9c2c556510b30f89bff
BLAKE2b-256 715ed45af9cf6dcd5774ad1e611e21edc8f149f0277ba00a916c2d3fd486b87f

See more details on using hashes here.

File details

Details for the file typedunits-0.0.2.dev20251020203009-cp310-cp310-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for typedunits-0.0.2.dev20251020203009-cp310-cp310-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 e44db1a6a6947b93f7e34d41481638224c67311b9e040181f73f161870772991
MD5 cda887b5c7a387e95975310f6c68dbd7
BLAKE2b-256 6c76004cc5ad0b16ab901b2026978bdf85de0f626b84164ee54966b6e67bfaf0

See more details on using hashes here.

File details

Details for the file typedunits-0.0.2.dev20251020203009-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for typedunits-0.0.2.dev20251020203009-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 4026c6fcbded191cc0d806973f425be13d7cad563fc2b3baa0a34d1edddcb342
MD5 e13c6f7e1d17a775cd443f8eaa1c51ed
BLAKE2b-256 df0c8156c16fb0da316be47b133cbead6ab2224da6754ca3eeaca1f6eff60781

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page