Skip to main content

No project description provided

Project description

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


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

typedunits-0.0.1.dev20241105180513-cp312-cp312-win_amd64.whl (707.9 kB view details)

Uploaded CPython 3.12 Windows x86-64

typedunits-0.0.1.dev20241105180513-cp312-cp312-win32.whl (667.8 kB view details)

Uploaded CPython 3.12 Windows x86

typedunits-0.0.1.dev20241105180513-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.4 MB view details)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20241105180513-cp312-cp312-macosx_11_0_arm64.whl (740.5 kB view details)

Uploaded CPython 3.12 macOS 11.0+ ARM64

typedunits-0.0.1.dev20241105180513-cp312-cp312-macosx_10_13_x86_64.whl (773.6 kB view details)

Uploaded CPython 3.12 macOS 10.13+ x86-64

typedunits-0.0.1.dev20241105180513-cp311-cp311-win_amd64.whl (735.3 kB view details)

Uploaded CPython 3.11 Windows x86-64

typedunits-0.0.1.dev20241105180513-cp311-cp311-win32.whl (700.5 kB view details)

Uploaded CPython 3.11 Windows x86

typedunits-0.0.1.dev20241105180513-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.5 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20241105180513-cp311-cp311-macosx_11_0_arm64.whl (751.4 kB view details)

Uploaded CPython 3.11 macOS 11.0+ ARM64

typedunits-0.0.1.dev20241105180513-cp311-cp311-macosx_10_9_x86_64.whl (809.5 kB view details)

Uploaded CPython 3.11 macOS 10.9+ x86-64

typedunits-0.0.1.dev20241105180513-cp310-cp310-win_amd64.whl (731.2 kB view details)

Uploaded CPython 3.10 Windows x86-64

typedunits-0.0.1.dev20241105180513-cp310-cp310-win32.whl (669.1 kB view details)

Uploaded CPython 3.10 Windows x86

typedunits-0.0.1.dev20241105180513-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.3 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20241105180513-cp310-cp310-macosx_11_0_arm64.whl (719.0 kB view details)

Uploaded CPython 3.10 macOS 11.0+ ARM64

typedunits-0.0.1.dev20241105180513-cp310-cp310-macosx_10_9_x86_64.whl (777.3 kB view details)

Uploaded CPython 3.10 macOS 10.9+ x86-64

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241105180513-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 e4361d4e1880203b2b3932eeee6054262196b716d07bea6c31863d4396ead645
MD5 38e182a110b05c274374b682bf0de5da
BLAKE2b-256 1bf5ef122d39648cce97a01b711ca7db97bca3d1c802a848f23df2e5a15d81fc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241105180513-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 270aa2b821ee585bc18bfc7134c4fd65e57e656906e486b73ad25f3e4c27675f
MD5 5f61c9d3db51d73421a4d5b9390458d4
BLAKE2b-256 75a14a613d73453d32b6d8ba1f8b91db74efb4b26943a748a0a7aa69700e9038

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241105180513-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 17e143e5782397bb92c7a6340b1b104d7c70ca32dbc9edea7197f8a99fc9f5ca
MD5 16415708df76c189cbd9de9644109ece
BLAKE2b-256 3d5bcb0fa6878de54742a019f615d8d6e69cc17d96ca70dde5a39bacc88b6c43

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241105180513-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 fdee961fffb330ebdbbbf413a67fffb463705f27a92b12f2ab17e743e5988e0b
MD5 0c97e0d4cdd945e77fbbaea74c12e42d
BLAKE2b-256 020d34ef591e7fcf0634290e6c11ceadafc3788e6302464673c3ebda29b2f20f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241105180513-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 efbb4e534011aea308aca8e3a7f9cfc8b41cc7cd7d531fe1ad208858d0b56578
MD5 4c093ec7a7ed334950b285e2ff80a9fd
BLAKE2b-256 ef461d3221a401711fabbccce342053e55a34a14c7ff8a76e2b68ce0cd8a428c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241105180513-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 908ea55779340aef1c0421c1aeeb1eaa109ba24e7ab60e9f4c12c9980f3d305d
MD5 500e63e3825669d8f6f08de0f1c3dd06
BLAKE2b-256 69058461e672c38391a3d6cc8bc1bedd7f6620996d21ee198b918b82a04b7a33

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241105180513-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 4182d8067c58d5c6ff6bd41bae2b9bf6a5ce9d4c47d3aa07e87482a71f6cb226
MD5 c297aa1d0f4d288f9677b41eb4e014b5
BLAKE2b-256 076e939ab7dc69f1aab4c1c1f21a2f78f5c1f93dba9c8b5ab41cf8e357c5c265

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241105180513-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 daa75eb2219f251219b0c593cfc7d8a15949e39f87d31819673804785a87d7d2
MD5 3265493b597418a9cc3cbb44333ef167
BLAKE2b-256 d22ac2f3b15fb9fc425e37d62bafd37906919a4fd3b635b4effd6d56d72c97ab

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241105180513-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 dd065182c157d870a14af5bc399d8b891ab1f6fe0c60df311c7959fad97a11b5
MD5 0e872cffad310a42275dfc6e1dea7f5c
BLAKE2b-256 b77ab866167c21b2828f4a4e84c2af1692a1441cdb708378fed848ff3651f435

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241105180513-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 e84e394dabfbf4ad813e522e8c4ee345821a8fd79683c25eb29d1b3eed199e15
MD5 7b84c8cf04421e733a16816257711b05
BLAKE2b-256 11e0cbb7cd31f7f42a275bd3ded4dfae8df7e4b727fe86d6432bb170968f25af

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241105180513-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 b1ba64aec85237d9a51a85ca6a9fce0d99cef66a72ee144c0caaa52b54e75ea5
MD5 eb7e6f96609bfdc93dfe20085b68d1ae
BLAKE2b-256 390e2e2c60f60874172cc0158a8a59443baea96abbcd4ebfff2a598d83b8aebc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241105180513-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 aebd88598978cef59fd8c4c137b876c032250af751c9e972dbd8d647ed387321
MD5 012abf83dec61d18b74e2514c4e28575
BLAKE2b-256 51bdb9153ce7f108f6e88d2f0cb4d3f0b56d3b16ecca7b2bd6b35eb7f77076c9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241105180513-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a8f6a51dbd0f169a43706e0b9c9a4130e6241e4ed656bcc48e2adb26a9827be6
MD5 bc66f5b33018d5960ce7c69ae1320f56
BLAKE2b-256 362f0cd89b2d51e5fe9471221a59e8f3ce2d8fe6b2effe8ec80ede3aef87ab45

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241105180513-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 75393ccc179d842da1ce7186274bda54abbc9cd3d861ff7c6540445310e8e593
MD5 08d3dc1b510cc5010fb808ff558a4260
BLAKE2b-256 26b673c9febcac9fecbfaf27438b5863daba2bbe11f987752bfdc11a5700627e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241105180513-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 263a753bdcdb30fb4d22b2fb98379779da6d1cbf43666469c945a7a0f4b44c9d
MD5 c348723993a357c5d3b89fad06168c44
BLAKE2b-256 50ef306ea3cefa4b62294bd2de29e74ce8f3309fb05440f704af8f782cd1f871

See more details on using hashes here.

Supported by

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