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.dev20250125002134-cp313-cp313-win_amd64.whl (721.6 kB view details)

Uploaded CPython 3.13Windows x86-64

typedunits-0.0.1.dev20250125002134-cp313-cp313-win32.whl (681.7 kB view details)

Uploaded CPython 3.13Windows x86

typedunits-0.0.1.dev20250125002134-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.4 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20250125002134-cp313-cp313-macosx_11_0_arm64.whl (748.3 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

typedunits-0.0.1.dev20250125002134-cp313-cp313-macosx_10_13_x86_64.whl (785.4 kB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

typedunits-0.0.1.dev20250125002134-cp312-cp312-win_amd64.whl (723.2 kB view details)

Uploaded CPython 3.12Windows x86-64

typedunits-0.0.1.dev20250125002134-cp312-cp312-win32.whl (682.7 kB view details)

Uploaded CPython 3.12Windows x86

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

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20250125002134-cp312-cp312-macosx_11_0_arm64.whl (756.2 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

typedunits-0.0.1.dev20250125002134-cp312-cp312-macosx_10_13_x86_64.whl (793.2 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

typedunits-0.0.1.dev20250125002134-cp311-cp311-win_amd64.whl (750.3 kB view details)

Uploaded CPython 3.11Windows x86-64

typedunits-0.0.1.dev20250125002134-cp311-cp311-win32.whl (715.9 kB view details)

Uploaded CPython 3.11Windows x86

typedunits-0.0.1.dev20250125002134-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.6 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20250125002134-cp311-cp311-macosx_11_0_arm64.whl (767.4 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

typedunits-0.0.1.dev20250125002134-cp311-cp311-macosx_10_9_x86_64.whl (826.4 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

typedunits-0.0.1.dev20250125002134-cp310-cp310-win_amd64.whl (746.6 kB view details)

Uploaded CPython 3.10Windows x86-64

typedunits-0.0.1.dev20250125002134-cp310-cp310-win32.whl (684.2 kB view details)

Uploaded CPython 3.10Windows x86

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

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20250125002134-cp310-cp310-macosx_11_0_arm64.whl (735.4 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

typedunits-0.0.1.dev20250125002134-cp310-cp310-macosx_10_9_x86_64.whl (792.3 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250125002134-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 d4faae554ae001b263420eace093dd383d9a9bd096494bf8d287a05bda1612ca
MD5 ddbc507f23da2b42f13c70c3784458cd
BLAKE2b-256 ed3f30e40d95b1fdd107c5d2ccd3e73d72649bd9fc9bbc8450bd93745279f2ee

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250125002134-cp313-cp313-win32.whl
Algorithm Hash digest
SHA256 4e60392d5291db9c46652c3fd91861f14f2f660528261d737a741007323e41d4
MD5 7a59cb5b3530f19b80b8d9179f79c5df
BLAKE2b-256 91ce141114f787cece933b7113cbe6fe22eed36232b396a374c10009a9af7e59

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250125002134-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 937298ffae85db0de2efe8eb567d69cf1d37d371b6f8212854d7fc53a14b32f8
MD5 4ba1c0c4c079f5a578d281a3f2ff5e94
BLAKE2b-256 c5a21a34308905e38865a4f7bea1df2fdf58135040fc2a6cbeb4c0e9f9931ccb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250125002134-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ac8798304a6c3ae081fcce3b5541c5915e3b08c1ade417d16c8f86f0acb6dcb4
MD5 91e181ef543720eaec0cdd8bfb69bac0
BLAKE2b-256 927bf195da8c65917fd6782dd39c2cf0a72867ec8307f25b8fbbd119d37ba7f0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250125002134-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 0535726eb7eb0aee090452ed2f88b303dba2b419b5469e4778b5e731cd5def6f
MD5 52e5700ddca1417d8ddc3ead37c6f9cb
BLAKE2b-256 4d8f2550c7512f259ff62974a7cc48d6018ca1fa4624166c94ab100195671077

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250125002134-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 7a2602e6729f6ac6fdee554a7dea4d42354b7d49b3fae6a0a329ba85b8bb9ba1
MD5 1701829d68e6352df7b03ead19694db6
BLAKE2b-256 24ea77af9f57f8da8e067080db69e8542ffc1ebaab358d10e3f3b00416236d10

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250125002134-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 857d4e71888321649512b1065d857c5fa9b546927833654cd335b3a2c9713ee8
MD5 875a126e8073b76966c6d877b48f8fe9
BLAKE2b-256 06d6060d40286a24f72c5e6a253aabe09966cc89258890810be2cc23776452b3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250125002134-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 2d5953b4f57eea1205b2a41edd95b4079758b07e4c3f2067a2c62547731cfb30
MD5 7f2ca19fee6f5700a4ff2d810a3f783f
BLAKE2b-256 8c9aa8ea5806e85439e3d1ddbe0a350a5766e3dac96a3b664e57b1344cf5a551

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250125002134-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a77c51fac52c5a5cf92a50eba40bec113c3030eca88b23c4659d6a2abb6b4223
MD5 206311da09180665f034892245fc392f
BLAKE2b-256 9f07887956b7c36df1f1b4f21fb1f1abc02d7b25ae2f6f09f9b94603ab2ca15e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250125002134-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 2e19b470f332523f6c107308eb9f969555a410c681c074ca163f504f65320788
MD5 255d9f2034a91016bbca23a72c755d94
BLAKE2b-256 f7912afcebca2ce042f32774bad9a7d03739a761fe2ce7bb6a3f820df3474887

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250125002134-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 248d5d742afe5df230b468002dce5e34582a70c4c81511a132f5733b28d81dc4
MD5 1b4520e19609bdfe9166774c899ad2b3
BLAKE2b-256 7ab1cd2e31a5d5dad9e3a9e832498db3e04a56fe7378089dc50fa982ae639176

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250125002134-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 607c9c9d9d7ff3f1f4857a058fd632abcd0b4ca0423efc38a38069f5bb7a1fe1
MD5 8a0dfeb4c71150d5492bbc625b6674d9
BLAKE2b-256 15f3c5d6f784fd51e522df0c111c307a84dbc7ba00ce7009c02030863f1605e1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250125002134-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 3c16a15b6e115a3adfbddedf792be42131cb5cc1f9c7b666ff20712704e7626b
MD5 ecc45ffa9b8b599e1271254014c6128e
BLAKE2b-256 0e9d0a2f1a22a75f7341534be4aa0b3d8bcf4f96c354843bf9cfb8034bb95de8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250125002134-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 85274e4ed6df943fcd2782478448cea5b6d68420f4c412b6d91494e349188934
MD5 3591d81856a76bbe570e69033f814cd5
BLAKE2b-256 f650622104c8db2f6f0407c24c8562d7278f8d369673ff0d02dfe12d41a35ac4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250125002134-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 122f82ec7a5ba002fac6441e04db35a7134551712c29954039549a634cd91e8a
MD5 b582f5493ab49d5a6d09f6b6953f7374
BLAKE2b-256 8dde1820c3e9a79a574849eb3f0c9d79fd12027ffc5e6477412ec47b31294065

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250125002134-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 f02f55bab041c6dd196c90ccb294ad2dbd9d9ca6c0fa6f2c80f7fe0b0798f42b
MD5 77402bc725ab2b3ad9b90d6d1fdff3db
BLAKE2b-256 06fec569ccc544ce572b33e304bb75ad0ce4734506aed542378136fc5d196808

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250125002134-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 6ceeb1635ce4192c6709670061e722c06379b9bbe0b6af64541e7111b57f4318
MD5 e8bf704c868a8685774b7b8c1158a7a9
BLAKE2b-256 a9e8bc9fae39a452ac2dd26144005bf53b86cc4fc1eb0a22dfe73a44f2f48160

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250125002134-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ef928c0293782526e8a941b5cd1fac4b3a4d3da2f873ee4561b258a383cfd328
MD5 6d3d92f7975708e3dc3d61d767bf5564
BLAKE2b-256 35ed2ae992f20763ca644d834485c7f9e62f16aab8dff5e447509f884a8421ef

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250125002134-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 56f5541f3992d7e510853a402b8953fac10f13f68e0ef95718ea21a6073deb85
MD5 07b7c73476c7c0d5a46492fb91a0abf5
BLAKE2b-256 4413b91362efd24bfdd4097aebff98f0990901e1e06e6376ad15e2b0e5560db3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250125002134-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 1bede08b6fe62270b0e2e30f5acbd60867042e86d6b5c144f41316c19ae0face
MD5 4b8a048c2c2f91ebc983e7a7d6b839f9
BLAKE2b-256 51817593022105637e193af5a3248a522ed3f912d39ecd0c2840d30da09dc416

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