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.dev20241111230424-cp312-cp312-win_amd64.whl (707.8 kB view details)

Uploaded CPython 3.12 Windows x86-64

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

Uploaded CPython 3.12 Windows x86

typedunits-0.0.1.dev20241111230424-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.dev20241111230424-cp312-cp312-macosx_11_0_arm64.whl (740.5 kB view details)

Uploaded CPython 3.12 macOS 11.0+ ARM64

typedunits-0.0.1.dev20241111230424-cp312-cp312-macosx_10_13_x86_64.whl (773.5 kB view details)

Uploaded CPython 3.12 macOS 10.13+ x86-64

typedunits-0.0.1.dev20241111230424-cp311-cp311-win_amd64.whl (735.2 kB view details)

Uploaded CPython 3.11 Windows x86-64

typedunits-0.0.1.dev20241111230424-cp311-cp311-win32.whl (700.3 kB view details)

Uploaded CPython 3.11 Windows x86

typedunits-0.0.1.dev20241111230424-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.dev20241111230424-cp311-cp311-macosx_11_0_arm64.whl (751.3 kB view details)

Uploaded CPython 3.11 macOS 11.0+ ARM64

typedunits-0.0.1.dev20241111230424-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.dev20241111230424-cp310-cp310-win_amd64.whl (731.1 kB view details)

Uploaded CPython 3.10 Windows x86-64

typedunits-0.0.1.dev20241111230424-cp310-cp310-win32.whl (669.2 kB view details)

Uploaded CPython 3.10 Windows x86

typedunits-0.0.1.dev20241111230424-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.dev20241111230424-cp310-cp310-macosx_11_0_arm64.whl (718.9 kB view details)

Uploaded CPython 3.10 macOS 11.0+ ARM64

typedunits-0.0.1.dev20241111230424-cp310-cp310-macosx_10_9_x86_64.whl (777.1 kB view details)

Uploaded CPython 3.10 macOS 10.9+ x86-64

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241111230424-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 1b9d57c088b9d49766206d0987dd23da3156841871da551f43923c60ac3b9c38
MD5 a6174a5adcc6d1281beb43ab96286308
BLAKE2b-256 b683ddf179cc945ce0cdb44a7c5fe3f9885fb21e67aad43d51e019ae3a24bd99

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241111230424-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 521118f393611bcecdb74681957b999d86dad422b0bb5542bde2e5962df3283f
MD5 5813c38e746f5673d7b0fc670c917371
BLAKE2b-256 5260cd5fd6dae6c58f825f474edfdd6e9fc1fdb443f454f2f27b13802188f518

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241111230424-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 586ee545f9ba12977f641f704eff47d5bcb059dee888d8cd503523be5187980a
MD5 3cbc40647338da3e16632ddd1732f9be
BLAKE2b-256 ce59c64ca0857d6fd78a3e0e4e3ec2f28ae3b510ee73a5464bfaca9102cf3a20

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241111230424-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 aea62a77aaca2ec35e937c0989423c51b003ead8a7e60cdcbd9f6332f679564f
MD5 8db17859c9df04e4f0db432e4af61662
BLAKE2b-256 b9c63e911fae77cc8d63183fd5a0d5bbc633bb5e9f2dc9c6aab85bdd21e1faa6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241111230424-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 bef56cf479b4b12f0d0e7991378a930ac6b455830f98512b8d58458a73cf2722
MD5 48c65b2a4ed68b0f04ce70e0fb7208f7
BLAKE2b-256 3740af014c49a83e21d60d6badd92664123ff335f930642d995f0f43e47949ad

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241111230424-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 7de9722fb4059b8299de1b887aee74ad50bf782abe5655003c19831bbcf545e9
MD5 2acf2545def62058d45fa8c8ed40e967
BLAKE2b-256 f7ba95972fad83177e9ba88e3b31053039d8b77852bfdce3cee9ec04f397a8b7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241111230424-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 50e8b41cb28f667e0dd09380ce6ecbe03f19491b122ea47f4baad903c3ee9883
MD5 f889380a8e0fae0478442dbf1e52636b
BLAKE2b-256 b3d81c88055371f9716a76b4e503ac250f4d27a4dd23ca7742c09a5bccf67613

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241111230424-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 4ab91fb54af807629d1e9a67a55e31be0095040f8df192217cbe56ada7e718c5
MD5 4c779689bf8b19bfdaa2616a4ddbaadf
BLAKE2b-256 7b651cd27498a97c02bcd898270e1a873674afe38d37205a890a5b226a9fb5a1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241111230424-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2280ca3c986bbd9fffcd5647fec1ddb0386b52e4d0cb5fa38d4ddf574f09ab4f
MD5 55b020f811c8bbe29e45cb4d33c9a546
BLAKE2b-256 a6a19720ca88ad98a70657a51931f169090441e2139a5b30a90b172bc247b6c6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241111230424-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 05077d95c76f05a903d6636c5279f0561bfe4bcdd86e1eb2579d18e492aab2c0
MD5 d413cf2f9a31d084a901c9691d931cf1
BLAKE2b-256 67e21c4d8f4db262bdf010d3ff367a1b715485c91963bb59415b6e6e12d5aa2d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241111230424-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 eae75225cdc082de8ef6d2cd62a4fc5531afec62434cf80399f0d3c11cc5fe60
MD5 0c937f94815f5497b9d23c21a30ec707
BLAKE2b-256 bfa6c50fd4dee34cef92952242859b3ae3dfda1bea2549613cb818605f1b8eae

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241111230424-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 d1d32008e9d4a8b0c67aafe6c5c0db4585985eb88406973ab52700a18d861b39
MD5 bcd1a9feb88c622f71a4ef52ce886770
BLAKE2b-256 0c3359c23da660af549e7af09c6daf8a89a1518e4ee035a9a951a8ce87015fa6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241111230424-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 41c794cd4c04d075b86edf4903206ce397996169d8812b466ce3b30af6cb3486
MD5 b8644d8c7de41b580b4b72e93730ce9a
BLAKE2b-256 b821133d3a4c26036b9a2a90e68f5155d9ef186db41b3d549b31b8ac35d1d344

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241111230424-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b496d8cd11cd4b4ab7f01e01c7d4336cadcc245f8be64b452646ca8a18509205
MD5 f184d289c910d3c830950c647ed8e72d
BLAKE2b-256 02f2405ef170f9a7ce52d2a788582f4babd80cb2db11b5dd97f74ca219146f4e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241111230424-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 2c47c8fd8a1b46068928b8af4196706205dcd74b1332ff994a840f251b575083
MD5 a01f7941d01046af6344cf12e1115783
BLAKE2b-256 34201970e6f6e669527adf83ecae772b9f002eb11791725164da8a1ef89efa40

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