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

Uploaded CPython 3.12 Windows x86-64

typedunits-0.0.1.dev20241114014153-cp312-cp312-win32.whl (672.2 kB view details)

Uploaded CPython 3.12 Windows x86

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

Uploaded CPython 3.12 macOS 11.0+ ARM64

typedunits-0.0.1.dev20241114014153-cp312-cp312-macosx_10_13_x86_64.whl (777.5 kB view details)

Uploaded CPython 3.12 macOS 10.13+ x86-64

typedunits-0.0.1.dev20241114014153-cp311-cp311-win_amd64.whl (739.5 kB view details)

Uploaded CPython 3.11 Windows x86-64

typedunits-0.0.1.dev20241114014153-cp311-cp311-win32.whl (705.0 kB view details)

Uploaded CPython 3.11 Windows x86

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

Uploaded CPython 3.11 macOS 11.0+ ARM64

typedunits-0.0.1.dev20241114014153-cp311-cp311-macosx_10_9_x86_64.whl (813.8 kB view details)

Uploaded CPython 3.11 macOS 10.9+ x86-64

typedunits-0.0.1.dev20241114014153-cp310-cp310-win_amd64.whl (735.4 kB view details)

Uploaded CPython 3.10 Windows x86-64

typedunits-0.0.1.dev20241114014153-cp310-cp310-win32.whl (673.2 kB view details)

Uploaded CPython 3.10 Windows x86

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

Uploaded CPython 3.10 macOS 11.0+ ARM64

typedunits-0.0.1.dev20241114014153-cp310-cp310-macosx_10_9_x86_64.whl (781.4 kB view details)

Uploaded CPython 3.10 macOS 10.9+ x86-64

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241114014153-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 1268a7049d61675f0fd533badb3f59e85cdc3d60221831c1f5f96b692e4ec821
MD5 8480c70ae0ad11f1f0b9920f49b3daa3
BLAKE2b-256 cdd336b6d5e2efa9dd4f5b73920f1922066bd5cb5ada9c0c75f12a99104b9504

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241114014153-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 b225dd2d61be85fa9517579e896d66f75d6d4c6c356251725615e4de825d1ab3
MD5 de9b26a2bcc6f5c98acb4efd9912b06f
BLAKE2b-256 0842657989ec6a107038f7b9f74a883e63a5c758f05aa30c7a8fff13616f3e40

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241114014153-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 3a23bed6dfb2c6b79bdbb433a65828f55d9a8a8be3067474596200e75c4ad722
MD5 15d7b8843d79a3932a68a963aa11a7f3
BLAKE2b-256 4fa0fd92381160d7890dca3c4bcb337ec99387d021fef38a3aeadf6309172f42

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241114014153-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b44e10477b6bb2e0dce618f70d3ea975f15a7f4e5513224636ed3943141309f3
MD5 30048ede982056e4d82d686dd6d2e7e3
BLAKE2b-256 d40b016561d88eef49e27cd89875c307ed9f1e8b5a6ec001b65e2ad4d28cccb4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241114014153-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 d466d3221672030f2bca4876f64c3f600404e1433f94539a7ac6cbb4946ab58e
MD5 1f361f1cf949f78becebbd640afca8a4
BLAKE2b-256 d1189a711419d07c5063de0c7656776d355342210d0cbe29ad7d73b97a2cfae5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241114014153-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 1f99da94e1834b7f6b26fa9e8ac3f1022ecef5fa8e92dd4364adfb203de9778b
MD5 a5242b089d71bc1517f10d3ef1efc83a
BLAKE2b-256 4c181fabdc7b319cabfe4e5f91888f190f1261baa3a0934f1f5ec5a44358099e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241114014153-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 8499c5757fd810560cb32a5fcd9ef59f19ce2cadeb71ac9b626f06d337b9b74c
MD5 a6710bf563be6a11418744cad5c21be4
BLAKE2b-256 2d23669cc2f9dca263670742fc72a234b491c31fd8a5d0062fea007b51ec665c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241114014153-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d4908d0fb697ef23ced1b5d8420ee1750452715481d2c0a400257a913d603d86
MD5 79cfe70fbe4d8b2690d5bb10a5bd2bf2
BLAKE2b-256 7412a324c840d85c0c40006c5f01be3b3bd2661fcf3f3e012dd5b549f81cdd49

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241114014153-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 03749e98611f3a3b8f98540deb1455f52267cd56e22e7e41c576be835060ce99
MD5 5f71770652be06fec8597f62dccdab27
BLAKE2b-256 b0d0b8073f1a240a0be3eb8a87e96291e4ee18b17ba51a5dd4b35158bbcb3ef9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241114014153-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 e47bd039579518cb1693fc3a75f710eefcb0d497c2fa00ae217f396b10f19907
MD5 b7ba20db402608d0b9c2c4c06f39bc35
BLAKE2b-256 2813a09f565ae209feea33efc8148cefe4a7474e4a5f483f2ebf63e09ff5f346

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241114014153-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 c054651922b12ae36ba50b3b6515b905b4d946ce812f2010bb2e9a734b34db97
MD5 aedf176d684fe56ee4dfd297c4f2c8fe
BLAKE2b-256 a6cd842f72b5bf582da7ede0a4b64ef6721850e9f719c24c98117045eb65b061

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241114014153-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 7f9297d5640dbb96382f153fba0a39675823cf9f5a31bf44e4ab4e48736367c2
MD5 2f2c1f2691d8fcc9448b4b036e55b693
BLAKE2b-256 de0ffec187720fb44448d5f1a49862f42d13693e74c111a0858c46c2838824d8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241114014153-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9b270508fab6419973af5a4558c32b25e3b73190ec6c7ded7decbb799c47a70b
MD5 e6b9d5da770ce0393dd30b15a01273b9
BLAKE2b-256 0eede0de9bda197bca84ec570c0135da8f2453722d9b3f8ca15d7237e6ef602d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241114014153-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 af35625ad2a21a37de5fff2af845de15d95248a78a0765a3667ef79e19ca709a
MD5 b72f5572f3756e39241874ebbf16a4f8
BLAKE2b-256 e3be28bda69a0e55e0df421e197dd22bd53fe9ee6b65fed6cbd2a4cf95bb79a6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241114014153-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 27f61a6d2cd46f099170eb15d088df518fa2cbeca95c4a0b33cb82c5dd55d28d
MD5 f7c81331efd72c69c9a3f407c37e2992
BLAKE2b-256 c30c8ea6772df79e0b9ed3380a5c7656eadf7735eebeb51add2ca294b56a857f

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