Skip to main content

No project description provided

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.1.dev20250728235222-cp313-cp313-win_amd64.whl (738.2 kB view details)

Uploaded CPython 3.13Windows x86-64

typedunits-0.0.1.dev20250728235222-cp313-cp313-win32.whl (683.9 kB view details)

Uploaded CPython 3.13Windows x86

typedunits-0.0.1.dev20250728235222-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.6 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20250728235222-cp313-cp313-macosx_11_0_arm64.whl (755.6 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

typedunits-0.0.1.dev20250728235222-cp313-cp313-macosx_10_13_x86_64.whl (798.4 kB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

typedunits-0.0.1.dev20250728235222-cp312-cp312-win_amd64.whl (734.7 kB view details)

Uploaded CPython 3.12Windows x86-64

typedunits-0.0.1.dev20250728235222-cp312-cp312-win32.whl (684.8 kB view details)

Uploaded CPython 3.12Windows x86

typedunits-0.0.1.dev20250728235222-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.6 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20250728235222-cp312-cp312-macosx_11_0_arm64.whl (755.6 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

typedunits-0.0.1.dev20250728235222-cp312-cp312-macosx_10_13_x86_64.whl (804.2 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

typedunits-0.0.1.dev20250728235222-cp311-cp311-win_amd64.whl (814.9 kB view details)

Uploaded CPython 3.11Windows x86-64

typedunits-0.0.1.dev20250728235222-cp311-cp311-win32.whl (749.2 kB view details)

Uploaded CPython 3.11Windows x86

typedunits-0.0.1.dev20250728235222-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.7 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20250728235222-cp311-cp311-macosx_11_0_arm64.whl (783.0 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

typedunits-0.0.1.dev20250728235222-cp311-cp311-macosx_10_9_x86_64.whl (849.7 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

typedunits-0.0.1.dev20250728235222-cp310-cp310-win_amd64.whl (814.8 kB view details)

Uploaded CPython 3.10Windows x86-64

typedunits-0.0.1.dev20250728235222-cp310-cp310-win32.whl (719.8 kB view details)

Uploaded CPython 3.10Windows x86

typedunits-0.0.1.dev20250728235222-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.5 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20250728235222-cp310-cp310-macosx_11_0_arm64.whl (745.9 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

typedunits-0.0.1.dev20250728235222-cp310-cp310-macosx_10_9_x86_64.whl (811.7 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

File details

Details for the file typedunits-0.0.1.dev20250728235222-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250728235222-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 7adeef03e9f7593fe5d7d6b4341a1e98c845c03e4b16e7fcbdae4ff0b9e7b328
MD5 66293bbb0eb09d430cf317ccb7d50732
BLAKE2b-256 942d69dd4b2f9f86a61dd2e2602d4df39bf3e29887803675e0508763b7c289c8

See more details on using hashes here.

File details

Details for the file typedunits-0.0.1.dev20250728235222-cp313-cp313-win32.whl.

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250728235222-cp313-cp313-win32.whl
Algorithm Hash digest
SHA256 1b0d0c6a31547971897a20865bc90ac0d89dd13cf784139d98664688b61ac7eb
MD5 4d005f401669a2c8c33aaef5206320f9
BLAKE2b-256 8cc2bd211e95a7aaa1daf5b6a53a47e3e494105b6bd0522da90e86a634d2bb18

See more details on using hashes here.

File details

Details for the file typedunits-0.0.1.dev20250728235222-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250728235222-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 11a8953582d278c734bc031e5b065a471930c8faec12b548fbd7046bd41f31ab
MD5 3120e0e74a03d87cf4f39818590f4cc7
BLAKE2b-256 eebb172fa76a5812093526410ebe6d0abb696a63255bd13934c4102b9f5f3262

See more details on using hashes here.

File details

Details for the file typedunits-0.0.1.dev20250728235222-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250728235222-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8595b6e69c3e7dd14eac91c9de015050ef78d2c1742cf316cac4c361c1d7dae6
MD5 5c92277ae7003363c6620eb913010c15
BLAKE2b-256 5328095e9ce87b6dcb3755bed1845bbeb8749ea5ad24ebd57a83bfa35a51c804

See more details on using hashes here.

File details

Details for the file typedunits-0.0.1.dev20250728235222-cp313-cp313-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250728235222-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 f85d3b0a84b61aa8b0cd3e58028186347388aaa4ab233a0c13c1faa994fa8286
MD5 008f885f88d4cc29f3965b3ff2c11e81
BLAKE2b-256 faf5b2aa28230aa942a111d48da3c7dbfba6fe65f99a2145da08351c78bcd7c3

See more details on using hashes here.

File details

Details for the file typedunits-0.0.1.dev20250728235222-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250728235222-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 60c4775aa7aa9f3408f5e74b4b45ac10b4db21136002bf544b201b1bf62d8644
MD5 654e40520d1149faa9dcfbf67c763585
BLAKE2b-256 c223d91e51e982239fc273932ba25c1f76444e03a7c4f2ba3fcc2d66e79a92cd

See more details on using hashes here.

File details

Details for the file typedunits-0.0.1.dev20250728235222-cp312-cp312-win32.whl.

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250728235222-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 864b24ce2138b3eb6e416cc2ab7dc7652edfffcb9e03c6e4d63bed7cb122ad20
MD5 f2695c0a250ddae61b49c07a30a3b8c3
BLAKE2b-256 d0986f8f915f21684a9e794df91f6d504d73b2f2d916a9497c9c8995d6e7422b

See more details on using hashes here.

File details

Details for the file typedunits-0.0.1.dev20250728235222-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250728235222-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 731260b4583a1a5b42c54d759febc966b73f58d7763c35c8e3437de56b545549
MD5 16557d4023bdf6c4bb6276c7877f5457
BLAKE2b-256 574781b9c6efec017efaea7384f1a361a00dcbaf1310c1d304ca413d0fd05071

See more details on using hashes here.

File details

Details for the file typedunits-0.0.1.dev20250728235222-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250728235222-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 57dd53d0d5e06615e4bcd9514a6909f7773b7d6eeda2374542da47665323d014
MD5 f145ab4a248ed667ef644f282aea8344
BLAKE2b-256 d2e9f3775cdc27512fe0203734c18a200d308c2ded3d900944430a868eeff410

See more details on using hashes here.

File details

Details for the file typedunits-0.0.1.dev20250728235222-cp312-cp312-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250728235222-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 784aad71e623fde0f2483a67c18b0b297db67a4f830c35874069faebdf138834
MD5 be1055e6880cb02e3497c80ca64214ea
BLAKE2b-256 d2f2dd569be48975341b1115b7bb35f4351ad330fb3bd4c3a99895fb2663816b

See more details on using hashes here.

File details

Details for the file typedunits-0.0.1.dev20250728235222-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250728235222-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 0fee1d6510358486faf464a06c50ed035347dd00c93fbf6e7ecf073d3363ae63
MD5 186ff3a3ffee154e78ff6c969620e70c
BLAKE2b-256 84bb1f7b4413b5934a656b56273de3278d9d82f396ba314e4df3a536c3eb6491

See more details on using hashes here.

File details

Details for the file typedunits-0.0.1.dev20250728235222-cp311-cp311-win32.whl.

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250728235222-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 068fdb1dae2ac9fbb3144a94901285cad66427b34d51e10836a12b406a321329
MD5 62ae06655761756a20de97fe9877a4ad
BLAKE2b-256 23651ce137a766adf820449774bef815c750173e735939c0e4b4353f04400ded

See more details on using hashes here.

File details

Details for the file typedunits-0.0.1.dev20250728235222-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250728235222-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 34606e4e4e8c8a1c83f62a234caf5b11be014f2736406ae2423cf65000ec6d67
MD5 af4b698fa098b43308cede2a92d5c7a6
BLAKE2b-256 9e147905742d12a64e0b5cee828ae16dcd21b8348d2daada773401a7b9e5e083

See more details on using hashes here.

File details

Details for the file typedunits-0.0.1.dev20250728235222-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250728235222-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c10f056d95c57c1925f095920fe5b302925c6f14da3e67df740a0e44d8635a8d
MD5 89f9067283da0228fa0e61ab4d43a1f0
BLAKE2b-256 bc6b80acce6da8bb98babc4e1c2331a36bc9b16cdc45991e0ad6751300e4e949

See more details on using hashes here.

File details

Details for the file typedunits-0.0.1.dev20250728235222-cp311-cp311-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250728235222-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 5da69de5ccc6d292062f2cf2f82e2d71d86e34a34a2ea071598149a5e8a2f027
MD5 7bf2e1d6b67012a878d8753159c777c3
BLAKE2b-256 058a49e42402ca9d63881a6ada163e7f0232d78c96812926e4e6e69ae821c85b

See more details on using hashes here.

File details

Details for the file typedunits-0.0.1.dev20250728235222-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250728235222-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 4387c41d181711e063cc9f1694c88955b36fab5da1a79cc6f4c37556a319134b
MD5 ce13d14a78b04b0e2bfcf0a75042a274
BLAKE2b-256 dd15ded2bd1384cfdda5171a36b2ff6f0dd2f49ed34645f3d82269e7c13e1660

See more details on using hashes here.

File details

Details for the file typedunits-0.0.1.dev20250728235222-cp310-cp310-win32.whl.

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250728235222-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 f5fe2b3ffa44c5e1652631e97c5dd5b3811e9b4fc0d6553a6617ae12e5a58612
MD5 0599899f346f1d545009fb0c1646ff52
BLAKE2b-256 d029d310d2f7edbfab7e4ae5c286a6153ae64b57595e2d8bfa7acca0421a3af5

See more details on using hashes here.

File details

Details for the file typedunits-0.0.1.dev20250728235222-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250728235222-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1834f318fd51af6b381cdb7fbb865853045be1c4608aabc8758f004bec81e641
MD5 d07beae702742fbcc91a56529a5e301b
BLAKE2b-256 5232fa6b86d10e847b437ac52622b42b4a9abe9805367c3ef53aae3a301b4bea

See more details on using hashes here.

File details

Details for the file typedunits-0.0.1.dev20250728235222-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250728235222-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 4abd53ed1231554d7638eb030878812c16bb9dc5bca27bf6284ea96308d5f78b
MD5 d331ff6a0af0f30a459d081db2168df9
BLAKE2b-256 6463dfcef120aba9f7f2ada857ec86e50107551482494b3748ebef10660e5325

See more details on using hashes here.

File details

Details for the file typedunits-0.0.1.dev20250728235222-cp310-cp310-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250728235222-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 1c2ebe26a67500a4d6b35be01b70d6be9cbfccb65dad70a374eedc3e8ae55a41
MD5 49fac1db2fce2c3200403c8d9d200646
BLAKE2b-256 1690fbf7827ea249c6f21d9f36c6c725a29af437ca0cc683923febe84ab008d4

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