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

Uploaded CPython 3.13Windows x86-64

typedunits-0.0.1.dev20250307001849-cp313-cp313-win32.whl (735.2 kB view details)

Uploaded CPython 3.13Windows x86

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

Uploaded CPython 3.13macOS 11.0+ ARM64

typedunits-0.0.1.dev20250307001849-cp313-cp313-macosx_10_13_x86_64.whl (845.1 kB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

typedunits-0.0.1.dev20250307001849-cp312-cp312-win_amd64.whl (779.0 kB view details)

Uploaded CPython 3.12Windows x86-64

typedunits-0.0.1.dev20250307001849-cp312-cp312-win32.whl (736.4 kB view details)

Uploaded CPython 3.12Windows x86

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

Uploaded CPython 3.12macOS 11.0+ ARM64

typedunits-0.0.1.dev20250307001849-cp312-cp312-macosx_10_13_x86_64.whl (853.9 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

typedunits-0.0.1.dev20250307001849-cp311-cp311-win_amd64.whl (807.2 kB view details)

Uploaded CPython 3.11Windows x86-64

typedunits-0.0.1.dev20250307001849-cp311-cp311-win32.whl (771.5 kB view details)

Uploaded CPython 3.11Windows x86

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

Uploaded CPython 3.11macOS 11.0+ ARM64

typedunits-0.0.1.dev20250307001849-cp311-cp311-macosx_10_9_x86_64.whl (890.1 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

typedunits-0.0.1.dev20250307001849-cp310-cp310-win_amd64.whl (803.8 kB view details)

Uploaded CPython 3.10Windows x86-64

typedunits-0.0.1.dev20250307001849-cp310-cp310-win32.whl (739.1 kB view details)

Uploaded CPython 3.10Windows x86

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

Uploaded CPython 3.10macOS 11.0+ ARM64

typedunits-0.0.1.dev20250307001849-cp310-cp310-macosx_10_9_x86_64.whl (854.6 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250307001849-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 3f7d6943f36df126cf7268c3d360f5033618c996c65b17e84d7a887e575a874b
MD5 250f8f0eef7b040d43e3349622365073
BLAKE2b-256 806ca58cbb88ff24012c1c85196cbd7c3de83719db0422260823f8885342e954

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250307001849-cp313-cp313-win32.whl
Algorithm Hash digest
SHA256 14dd51bed532299fb755d3598a72a98f24ce4d25ec299cd739270d4cf07112ea
MD5 aa9bd0417b08eb1c5fd6733a67bd9398
BLAKE2b-256 3de88529e2973d74c85fa6b1799495eb4b04d76a81ffd57d143ea597d4a0d88c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250307001849-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b92a99eee8bc254eba1a2d11eb8b1f5f709bd8b1b250fc5e7ae13cff41506b76
MD5 f211c228002f0090c19d80f92f63f39c
BLAKE2b-256 0eafa7fb0079fdfc894ce09aceb3b16da0995cf6c2e03e94ad529cf0dbed7250

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250307001849-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 5ae0a1426b253b93c7653a179ba52844fd7e2ab4282c9f4fdadd26a4d353ef2b
MD5 5b457c628dd70c7bb72a9c05a2f28ed9
BLAKE2b-256 8c2d744548035236dd1e302b3cd07de5127a35f4933253eb1f72449974210207

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250307001849-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 a9e336178954bd3db8cfbf09e4743a2081b985741f59736213dca2fd05143817
MD5 3fb022c56d23309cc36688f86d11e811
BLAKE2b-256 63343d0154219b83fe126758216f29860234ffe5cbaca50bc117c829a071200b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250307001849-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 c688f3586652ab18f3b3c3265351a3c5183a74d4278672ec47c789e37aec2d49
MD5 020404b1a49a4032728dc45cdaa53a0a
BLAKE2b-256 a16252404468b65d3ca18d9347f1692732c54121cb60c738a92927d1471ebd5d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250307001849-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 834cd439186cca95a82d568a0360270e45e4540eca97048d4de67072b70f6a6a
MD5 5d04a17fc9c627979eafa43868a42f13
BLAKE2b-256 ef75699907cccd4c0ebb1b5b5d841032018f1ba277bbd9f0bb03dac2572fbfd2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250307001849-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 3b5ec632d9340965364cdf1df144ddc148ca37ff1c70a287c17673d9a2ddabbd
MD5 79baf0a041f60fa5e88a455fbabddb84
BLAKE2b-256 13721ebe094edfe8d88fc6a91416014f26d666386b88b4302fb95710d75858a7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250307001849-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2751a9d02e9b56b3550e64eea80ad6018d2947d11a8fa728ddf65f4681708148
MD5 e2efebb3002eeef6f6973b1b1da623ec
BLAKE2b-256 3caa318513835c93d33bcaaca42c9e2fe2000a089de7519edefbbbbc714ed9ab

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250307001849-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 fc87161a7d99c3a4e65f4c053ce80ed0041c13425ce14c9208bc7d34aa23d53e
MD5 a8354dcd324e6bc0ba7dbf459b76dd54
BLAKE2b-256 0a4c25820c9bb6169303e169fded5a33e6a8344768885ccd1c28aa50736eb34c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250307001849-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 ea6fde997e919bf45c27603f1f3cf0d60985b15c32fc0015e9b83971b2159617
MD5 7dedeb1d1f4435f05c940d9b165a985a
BLAKE2b-256 be23b8b6de5479ac84c6e16e103649609fd24a1c074aaabeaa6684bf227de2ae

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250307001849-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 426d3b93c54aa966aacc7afe51ae77f05f688ef4e8ebed2be347be931182a3df
MD5 7878e87728a61e09f1bb035e958876d3
BLAKE2b-256 1b6498d3c15b96751da5c85d778ff76187fa75dc23559857866eb497e9ec7dba

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250307001849-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 19912b51d0d71ef3e8a14b950bf5d2824e2222404a70d0035c026835aaf6cb38
MD5 4b1e716ccd9e515929a9181325f721f6
BLAKE2b-256 3afece54b1d558080aa61254ad38a64140e5c267b19cc00f7abebe13b4f9c104

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250307001849-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c68622d4f8abaf9cd25812110946c94ae6a53d9fd546f927ee468a1e4b205803
MD5 d36dff14bbbd903be3370e5231e03591
BLAKE2b-256 53cca8c5de4c8d97822239fc8a995d3cee51d0b624c221eef78cff6db3d34842

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250307001849-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 c6b1addac9e50cf76011122bcb747558a629ad806e778a867c2a785a9d56fb2a
MD5 dd50ef01df4fd581233f8d36b3cdba14
BLAKE2b-256 2377286bef92321829cae3c50a597af72b19b6ac6b292b6dd152eca128053470

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250307001849-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 973d563a727e48df81c998f369b76d1ece8ee63c935ccc5295b57ee928dfd331
MD5 48c1f3ab592e81de8df54a6cb5875b9c
BLAKE2b-256 e8115013fbe85926d69e0d619f78bf23e2c0066a63d3c1407c2185ef9adc8ec0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250307001849-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 ec2fa6b49695f018544f61a50a5326bc66d215ff8f037fb1f1d4d12ca8a8db36
MD5 29a034941da80c923464241a3e8fd8e3
BLAKE2b-256 6706abdedfb1c9b970a035c2d3a8b394a9a2a7a5e42b3c19d959cb008a055da5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250307001849-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 0d9777fa4928244a2c94218891ad55b9126ea57b1be0ee83b06db8a0c083e020
MD5 f040cbbeda75708be2eb1685df0508a4
BLAKE2b-256 e244e66b66217383fbd235bedb8c236b936cdfeefa1c7df382f160756c6b6a30

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250307001849-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 534a69d346c27a04436333899b93e95bc29b9d9c307c65c92f60fa7cd49705b8
MD5 c0d1c96541933a098cb8ca6a6e6a7b82
BLAKE2b-256 171d5316f60bbbf3e5d4d0c34406633f8783160623ed60b47590a7840fceda65

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250307001849-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 53269f95897c8c6faa65fa68db58e2ff61c688ecbd8cf5271cae65b7c9a3e59b
MD5 caab091fe655b8abe859d6e2bded12b8
BLAKE2b-256 424d233ad806fbed0797915e5684b673d5bbabb343e1f33ea42711451734d0b4

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