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

Uploaded CPython 3.13Windows x86-64

typedunits-0.0.1.dev20250221185308-cp313-cp313-win32.whl (709.5 kB view details)

Uploaded CPython 3.13Windows x86

typedunits-0.0.1.dev20250221185308-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.dev20250221185308-cp313-cp313-macosx_11_0_arm64.whl (779.7 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

typedunits-0.0.1.dev20250221185308-cp313-cp313-macosx_10_13_x86_64.whl (819.8 kB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

typedunits-0.0.1.dev20250221185308-cp312-cp312-win_amd64.whl (753.0 kB view details)

Uploaded CPython 3.12Windows x86-64

typedunits-0.0.1.dev20250221185308-cp312-cp312-win32.whl (710.6 kB view details)

Uploaded CPython 3.12Windows x86

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

Uploaded CPython 3.12macOS 11.0+ ARM64

typedunits-0.0.1.dev20250221185308-cp312-cp312-macosx_10_13_x86_64.whl (825.2 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

typedunits-0.0.1.dev20250221185308-cp311-cp311-win_amd64.whl (780.6 kB view details)

Uploaded CPython 3.11Windows x86-64

typedunits-0.0.1.dev20250221185308-cp311-cp311-win32.whl (744.7 kB view details)

Uploaded CPython 3.11Windows x86

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

Uploaded CPython 3.11macOS 11.0+ ARM64

typedunits-0.0.1.dev20250221185308-cp311-cp311-macosx_10_9_x86_64.whl (863.6 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

typedunits-0.0.1.dev20250221185308-cp310-cp310-win_amd64.whl (776.7 kB view details)

Uploaded CPython 3.10Windows x86-64

typedunits-0.0.1.dev20250221185308-cp310-cp310-win32.whl (714.0 kB view details)

Uploaded CPython 3.10Windows x86

typedunits-0.0.1.dev20250221185308-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.4 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20250221185308-cp310-cp310-macosx_11_0_arm64.whl (772.0 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

typedunits-0.0.1.dev20250221185308-cp310-cp310-macosx_10_9_x86_64.whl (829.5 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250221185308-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 bcc69b9e40f537a01a56d026eb2c7f76faaa5e48dd7d2d03dadea08b18be63cf
MD5 4a54c0e14266baa8c54fc09e45808854
BLAKE2b-256 83121b63bd86ee26c86b2bc89c2c1971b944a0e25579b62f260b35ddb7cc9d8b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250221185308-cp313-cp313-win32.whl
Algorithm Hash digest
SHA256 cd2179727e6980a6aca55371b9e94674bb79f47656cb5835aaa756819dd6ed23
MD5 f5b63d33a1ddde3cdba7949b614c5c11
BLAKE2b-256 0413a89c8812b05bcbff8c74b3de4b80b8023e9cc2b2faf25cbcc37806a7835b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250221185308-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 0316e22b31e45bca6d2ec5d162a316aaa2a05589abb7020009859a0f4973fedb
MD5 9ec8449a12dbb8baa2f62e738ed5d64b
BLAKE2b-256 f00d7701c38ab30350bc8bb15494d2d4e1c1811de7c48dee06362db0d63bf8ee

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250221185308-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0bba3a201198018a93819609fa5556d59907bb27773878be866743c0412f106d
MD5 4fd32a2b61cb4df2092e0efb4a669516
BLAKE2b-256 dba7ead24f4ceafe9249bd39b81034dffe1f4db0e8a77a3121bc537c28b78dd0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250221185308-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 eea73dfebce782ee45c1c28a1d0fd3333eee7c74d899fe4e195eeba4b3e6fddb
MD5 0015dc30c739906e4f22395c91338e0b
BLAKE2b-256 a1ce1aabed15945fe88fc0d6cc9d1d6588e83eb7d51c68c1415594b6230bca1a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250221185308-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 10105c04ed6c4cbe5d29784c541aebc02203784d485676c1ce9c4f8b7ba644f5
MD5 5bc2e2bce98ae067762c5327544bf7c2
BLAKE2b-256 21b2d4c26eb8dc98f5f4b6fd5af78c814ed1b88b551833817278488954bc95a7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250221185308-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 50406bb24169eb812ef3f55f277efaa7bcdbb3cee11d6530b957164ac163399b
MD5 54f6f7055ff6a1561e7e7a003a8e2cdd
BLAKE2b-256 c012e645a6acff7b53a0e2b1ec27679abc4cc2c10c59f4535ee49fef2de36067

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250221185308-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b458fcf647c8ba142d3a1ed14d85324894a809baf9976c66240ee6ba4b2b408d
MD5 7c2e00aa1810b15287f52189334144a0
BLAKE2b-256 7d55471dd2d53fa01b2d7272a01c32d033bcf719d6db8e86c094daef01e87f79

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250221185308-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c453a6ee5008f2e63a833581b2f47a92229ecb4aeff67047f1627f89b7589901
MD5 2a3f7940d65ba2d5301ead08f550ce7f
BLAKE2b-256 5f47d8b8726dd1312bfd1ab47e323238d2e1c847df46f02878b136a9dd821e66

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250221185308-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 5e39541fa774c8a060b444d739d907a6a5ad52a76a493a7bb8b6c33b2a496208
MD5 2e68657cc00d76dcd5c8ea51ef1c638f
BLAKE2b-256 617010f399204c27f540a79545b735f6a256fbaf7028a0e440456c9854c48d75

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250221185308-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 8d09f1b5d96d0527383b06eaa593ea203f25135976d3ab0e69e2d36621024fb7
MD5 7b98932216b022fba8c4201b44996a5b
BLAKE2b-256 4e1b186669468097b2b184100abe9d43407cdb215dad975281c47fb0d6854db6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250221185308-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 600c7e6f5aaa40d4142695ea596e33d30c92e1b552ef430a81915533606a6a8d
MD5 eb419ed4485c765f72b3350618e8d129
BLAKE2b-256 ea660f22b23072c2bbd55e97b7f1aa5861f4cffd8dd3bb2da219613dece1f383

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250221185308-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 47e3a919a9c5f9b4d14038d060a401e2fa259163cae3cb54f218d24cda6fd7a1
MD5 8ae43393e45d5963f404107475fcb480
BLAKE2b-256 d6396f63359a1df704899fae0547337642edcfb299f484d9ce8ff906041f7a00

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250221185308-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c7937d3d64a40f8728f75fb27670681f89f686a0785660d8086749f0964255e4
MD5 f7293822cc773ab9c0e49d76cfb3806c
BLAKE2b-256 d6ff54e9d368a3aab446e7c12e0fb9143056ff1b06e70a7b7adc70010386cfcd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250221185308-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 a968ee85dd94e6368aa13884d1be3a3938d818e9495c53a9d9a93e8fd6ea3976
MD5 f2a6e24821d605bc0e7e5056a8141d45
BLAKE2b-256 f143c7ac52b2afa6655719bdfde38212177ccbe21a31d90bf7774b9b438d7fb4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250221185308-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 80145a8cd7086577ffaebbb08eaeac221cfdf02121cff86951a65c066978820b
MD5 2d2bfe37c9987134b3f927050e620014
BLAKE2b-256 9c777950d253c59ac8578e13c0271a383bb9ddf594211173f4fb53afe52d3659

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250221185308-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 c10c40767ed48062a863a30509de26a0480ca8f7bcf73b2b8b481da8cc0b1e85
MD5 2012384493759a15e9129c3f12d9d719
BLAKE2b-256 f02ed10d36539a75e6221c7ecda5a2f3aee4e3a185a2010725148c826624d1c3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250221185308-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 0cef68ec8213f6bb47da901a2904f642179f7eac4be4edbc1d39e39fb957b592
MD5 8fba76eed7d8d9af12f76cff6fc184ed
BLAKE2b-256 f72def218ee509996e614580bbf6a1937fa4872a658e74d8d811ed3502494744

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250221185308-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0792c4e5a9ba6f106a797a803b8b649cb371b0ba84fe3507b07de699bd7b377c
MD5 1c92249d1d07196201fbf7689c2e4d76
BLAKE2b-256 ae8c20bc276362d35d7b4f355b157b3667d0750e132e3a1f5e11c6da4592731b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250221185308-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 d78a28d40611da1206bd07e57fcfc79557e1f195f9d8930814a7922ef9a87b5e
MD5 21e00573e500fe36bfefbc2157b10c70
BLAKE2b-256 c513e7b6b646c47ed56f372c51fa9df773beb15c83ae2f349efe18e342a686d6

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