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

Uploaded CPython 3.13Windows x86-64

typedunits-0.0.1.dev20250305230914-cp313-cp313-win32.whl (734.6 kB view details)

Uploaded CPython 3.13Windows x86

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

Uploaded CPython 3.13macOS 11.0+ ARM64

typedunits-0.0.1.dev20250305230914-cp313-cp313-macosx_10_13_x86_64.whl (844.6 kB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

typedunits-0.0.1.dev20250305230914-cp312-cp312-win_amd64.whl (778.5 kB view details)

Uploaded CPython 3.12Windows x86-64

typedunits-0.0.1.dev20250305230914-cp312-cp312-win32.whl (735.8 kB view details)

Uploaded CPython 3.12Windows x86

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

Uploaded CPython 3.12macOS 11.0+ ARM64

typedunits-0.0.1.dev20250305230914-cp312-cp312-macosx_10_13_x86_64.whl (853.2 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

typedunits-0.0.1.dev20250305230914-cp311-cp311-win_amd64.whl (806.6 kB view details)

Uploaded CPython 3.11Windows x86-64

typedunits-0.0.1.dev20250305230914-cp311-cp311-win32.whl (770.7 kB view details)

Uploaded CPython 3.11Windows x86

typedunits-0.0.1.dev20250305230914-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.8 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20250305230914-cp311-cp311-macosx_11_0_arm64.whl (827.4 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

typedunits-0.0.1.dev20250305230914-cp311-cp311-macosx_10_9_x86_64.whl (889.5 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

typedunits-0.0.1.dev20250305230914-cp310-cp310-win_amd64.whl (803.2 kB view details)

Uploaded CPython 3.10Windows x86-64

typedunits-0.0.1.dev20250305230914-cp310-cp310-win32.whl (738.5 kB view details)

Uploaded CPython 3.10Windows x86

typedunits-0.0.1.dev20250305230914-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.5 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20250305230914-cp310-cp310-macosx_11_0_arm64.whl (794.4 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

typedunits-0.0.1.dev20250305230914-cp310-cp310-macosx_10_9_x86_64.whl (853.9 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305230914-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 9da1c029afe85252a4d3108be6f5a5b6f60e43b90057d27a635ac79a3736e7e2
MD5 9c71376811b368e75996ec27299e8368
BLAKE2b-256 ece0a5cdab101c920b7b90941af19796b164df46285e902d7ae91d5124bd8776

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305230914-cp313-cp313-win32.whl
Algorithm Hash digest
SHA256 de7b11f02e91512868602735c162e7bab9b1fbc55ff83869c85b3798cc650e6e
MD5 0afad0ec8e86c43739c4e63cc338789d
BLAKE2b-256 435adb74eca10b2e2b5e334c09f5ff9be8eafaf437fb61f3ccde565c3a8eeeaf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305230914-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 78bb6e647339431cef6ad59ce3da1b15795b5b2be36cd27c53c9c9975ad64e7a
MD5 2092dd55c05ea7291e990d94e5c8fe8a
BLAKE2b-256 0ad9619c7ea27e9073a6ad6777b61f68d46036af3053d1d9d4c9010b91ad59b4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305230914-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 63773f89628a27f3081433f8d401bb6f416561b1589343b404214700bd773fe2
MD5 9e75bbe1ce3d3283b4cbc51886c21642
BLAKE2b-256 1b9d3cb24d62e8734168b622f78ccbd42bcf0e668dff85db16e10efbb84c947e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305230914-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 e01068bafabaf0210b5822d136f820f7b4e1cdf143e9ef428b42ffdfc07edfa0
MD5 fe7bfaddbc9fe97cc62e011cade5c397
BLAKE2b-256 ff7ca40aac983e5170041421aab3b4bb935f542aa83d9e119aca0fbf170715df

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305230914-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 5a15109a5137441ea0c9b4f8d233b4f1dffe946f55d661aa8015f24b2e48bbc5
MD5 029a90a4f3eb45332b39d0aecce03d9b
BLAKE2b-256 2db40cbe272538d5b06f5006743adc746b46165219bcf73664456ce313b0817c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305230914-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 a24df92c25f99a20848dc93fb5b199a4f141ea5ddce645bde4b8e84bfacfa0ec
MD5 a51e0ee09ceff0f411d77bdaa1d66e6f
BLAKE2b-256 08e61ee4b0c7527936f4d82f71f9b927c88e0a6f5b18636b7afdca6145483100

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305230914-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8d840f6327acc7cc36b16423d65df3c7fe87aea39829156243036f89c8d87f93
MD5 646ca96e1cdc064bf111543f41dfdba9
BLAKE2b-256 51a2e13513e8ca824872523d7ed0ab9b5a20039f6a39f35aa8f00e104d16ad57

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305230914-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 cc71862537cf409ca9d157480fc9d1cd91000ccbd368f0b9a65cf448995a7dfb
MD5 08a33ac255ebfc4da0f6bcaa3c5f8602
BLAKE2b-256 e9194924c4b0deff1f20d69afef53f476bd64a08cbf29eca604cc30c13dd23b0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305230914-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 b190aa3867dcafc5b7e71e1dc88a81e82bb0c809ff8c210b2808269a82f9752f
MD5 f972586600e02f299cb43d1ee5fc671b
BLAKE2b-256 7be2c082c53dc6d0751abe3a8d3a268148d8775e3990aac48b8db50b4d93f20a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305230914-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 99fe54925bdab9c536f379e93ce3e69e62dce4da2dcfc06532c6a3d71c4f3cdd
MD5 bbc773b1a50312c2ed42733e560abd86
BLAKE2b-256 1e914230ad679eaa24b057fcc1d141357d4a504b7a12e4c4baa276d203b3396c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305230914-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 31bbc3fca40eb210071ebc215b79522eee3e3b4d34b664517b2c66144d1262fe
MD5 ea0956593ab7d25bd1d7a243094b851e
BLAKE2b-256 04645709cee6dd9812b04760f0f2827c81a4164a75edc058ce99717168bb5b22

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305230914-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 27c24d8f316936d008264cebd3c2e0b97c4291ff9588c760c0ad0a50674f3cf0
MD5 65c57bf6904d53a45fef6de34650ebf6
BLAKE2b-256 367a3b4d45de8024edb45c8c7b946fcc50994cce78c455daa7221888b368da2f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305230914-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2c260cc40c9b910479e73a872daad1344e5b59703b5ca257f193867197271db6
MD5 f9857c36e74c43c12ba2d3d2765d5185
BLAKE2b-256 8a04fce43f99e3fa277f7fe7a2d11cdeeab0590dcc284c60b8284906e8a8d23d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305230914-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 7949d3606c6dae0295c8691fe952101ee51aefbde6c8dcbf26ee097bb8c378be
MD5 b7e21c2a9380bbfa45bc71390fc3720a
BLAKE2b-256 81dd9318597394a9614a0601bbc0ffa4c674a9493a12710de662218016955130

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305230914-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 a013230287b86381bd5b8777e3a857f97c25a2e4fb2f20413e2b276470cac3a3
MD5 70dea33dc85b93f172e7c002e49e1398
BLAKE2b-256 41ea9a41497dbcc58644ddf68e60e144bb62b54a050eae44c3d0b3d82a30155c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305230914-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 690d3573b652c895c80e4e45f7ab5b9d07dcb284f0b20de95d3e3ebf84f8439d
MD5 dd1f23ed687551f44ee953e1f34f772d
BLAKE2b-256 46c40a2ad17a59964d65e3210b8c1b48984479b886c489ad8d60164aa7d20e53

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305230914-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5bdb7a4af9bbb6456eccb89acb7b70d457ad3084ec6ad7c09482d8e8327aa674
MD5 110855d387dea3139d18e3b2d14aca6d
BLAKE2b-256 e053cf39a6d53b7c40b7c24a9cb13a615775d7dde72c81d79bf2f4395da9f015

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305230914-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c64aa8bc8f3eadefa4d5199e001c814f3e277c2f07506d0a545bab727ea72305
MD5 c278a06df8e80e86b8a7a8a7e20255be
BLAKE2b-256 e0f71cb30e3c28248f3ab3d71713e1ade4e037ac359ce93a70baab3b8bcc3cf7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305230914-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 97e84b77dea5e8626dc2fb549ee037c6bf286fa08a9c0fd72d4e95324bb4ea49
MD5 9612c104893acbd7cfcaafab1bd0932b
BLAKE2b-256 4c41e8a996c5c3717b3c622432b744fa6116cfa35fc7e739a0115bb49450dcbc

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