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

Uploaded CPython 3.13Windows x86-64

typedunits-0.0.1.dev20250221034458-cp313-cp313-win32.whl (705.3 kB view details)

Uploaded CPython 3.13Windows x86

typedunits-0.0.1.dev20250221034458-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.5 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20250221034458-cp313-cp313-macosx_11_0_arm64.whl (773.7 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

typedunits-0.0.1.dev20250221034458-cp313-cp313-macosx_10_13_x86_64.whl (815.0 kB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

typedunits-0.0.1.dev20250221034458-cp312-cp312-win_amd64.whl (748.4 kB view details)

Uploaded CPython 3.12Windows x86-64

typedunits-0.0.1.dev20250221034458-cp312-cp312-win32.whl (706.5 kB view details)

Uploaded CPython 3.12Windows x86

typedunits-0.0.1.dev20250221034458-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.5 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20250221034458-cp312-cp312-macosx_11_0_arm64.whl (780.7 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

typedunits-0.0.1.dev20250221034458-cp312-cp312-macosx_10_13_x86_64.whl (820.2 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

typedunits-0.0.1.dev20250221034458-cp311-cp311-win_amd64.whl (775.8 kB view details)

Uploaded CPython 3.11Windows x86-64

typedunits-0.0.1.dev20250221034458-cp311-cp311-win32.whl (741.6 kB view details)

Uploaded CPython 3.11Windows x86

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

Uploaded CPython 3.11macOS 11.0+ ARM64

typedunits-0.0.1.dev20250221034458-cp311-cp311-macosx_10_9_x86_64.whl (859.2 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

typedunits-0.0.1.dev20250221034458-cp310-cp310-win_amd64.whl (772.2 kB view details)

Uploaded CPython 3.10Windows x86-64

typedunits-0.0.1.dev20250221034458-cp310-cp310-win32.whl (709.7 kB view details)

Uploaded CPython 3.10Windows x86

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

Uploaded CPython 3.10macOS 11.0+ ARM64

typedunits-0.0.1.dev20250221034458-cp310-cp310-macosx_10_9_x86_64.whl (824.9 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250221034458-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 7bd33f071d4d2afa242828181f9c140bed909865d782805d1356c92a975ae4c7
MD5 be60962ee46bb6ba2e43bd2e93a29955
BLAKE2b-256 848b12377194ccb4f7014977e87b79f072c2a3817aed4cda8b292b4b160f4bb8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250221034458-cp313-cp313-win32.whl
Algorithm Hash digest
SHA256 d8ba1f6fd9489f8af25d64e49a77d03e3ec26fc149e32ca6bb1658ee9c0e656e
MD5 cdeebaa4fa71c4fe6c8f8a0f907860a1
BLAKE2b-256 a0ddedea95c9c1bfbb25f19263c89c2bcf70dd5bdaf16de3f104cc06fc27c501

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250221034458-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7de17c89e9ae042c7bb65d670e21d1dabb3a377b5763e40c0b75aa3ecadaf715
MD5 29734fd4ebfcccb6cd6c681c96318054
BLAKE2b-256 552326db4719f8b12497d895a7f9d797b882766579171cee2be79d51a7ce9d04

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250221034458-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a2895984c246616a56f1695965f25ff7ab5ae5f44a93f3a60d8a5462f7761c73
MD5 1df368dc511e282110c80bb0dad07a13
BLAKE2b-256 2434c5a3b1a489ebce37b32793b80574148f6cecca83241628cdecadd568fc42

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250221034458-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 f5467d5677175c2c2246cf58bcf8fa027ddf01694fe21b62e8d10b372824ac4d
MD5 78974320be32c5d343b6475d5bfe6337
BLAKE2b-256 7c6cc4420e079073053e2ed87ecf03d150cc827672750bea8b80ac441ca39cf7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250221034458-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 9108171e3c61bbbe72ed94e9a000dbff108cc789a7a94c745496330841712206
MD5 6a94b68a9ea68f715e2758a530aaebc7
BLAKE2b-256 6c80f514fa0dfac6a2e6ea2128f92dc62f0934420428697b2639527772bfc482

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250221034458-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 e6e6ee4c8dca39073535d4e19756eb6d08101a46e2018e957b131c0a0c6ef024
MD5 b332a1de2bf8bf51268e3608445a52ee
BLAKE2b-256 cbc4e1bbcca40973796a635ab915922b3c3372c6d4a808ab25bedebbf006f3c1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250221034458-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 bfdca817261d189dbb601c96ded5a0a16bbc44ed2ed94ea33ab02be3d08f1b25
MD5 317f5f5eb546e90acb3660d09e914dbd
BLAKE2b-256 868ac900cfbc480d944bd96ddb60e826a5ef12a8d9f547e97ba3c286dcc95805

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250221034458-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 91cb685d975d77e20aae4d893bca9d5acc67c56dcb1695fb49df93631f71a389
MD5 64dd8deea5c1ff2e6ad8aa3c5bfdbfb1
BLAKE2b-256 ad4c8091d4a0c2ed0a6ed48b90c91fbfd5dd911594de9e0ff6ddb92435ece4ef

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250221034458-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 48eed84d1f5cef2c08e80e1d51bf5807fa3c76277fd0bf373ce72e8ae1adca91
MD5 ca66122b16d584be78f78eb152caeed5
BLAKE2b-256 eac0e0a270f896431ca9ed331cca122671cdba1a3f1d176d979dec5beae58625

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250221034458-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 1cf9958f9e5d935aba25c10809d36084afff358acd818b94af7f0aff62a81bec
MD5 a783cd8f371a762b66f8b471f69de71a
BLAKE2b-256 6c28c503bcccc8a68ee49e5ffea6ece7de0ce5127b8fac1e661f8b49a88f3536

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250221034458-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 9494e1dd178f520ac3cf00ccf8abae72c0515c19d5030c5da08ec0143e272f5b
MD5 eb7e525083e8f60123a9620a3647d35b
BLAKE2b-256 d4d673bb75ca079e94f426ea519a59e62ec456a7b32f4147c93e0ae90caaf3e2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250221034458-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 046f97f331b7c89d8562670143dd24995a41e0491010a048eef20be4cd0e074a
MD5 d12a4f908c71687ac060f8d739ea9881
BLAKE2b-256 076e9d811cb703d7089c21ae3bac6f6407d8a7619b4b1d1f80f2dae1537a6ab2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250221034458-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a3a6d4a43fda24791042688c434ddb72ae01c41b8c8b5b6c93287b29447ba6e0
MD5 2e4444c35d82a3afe0703e142f96cd3b
BLAKE2b-256 0017ce3f0a104df687ab92255b40b7720a9e819a14e0ac0f768b047e3e30c94e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250221034458-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 eaed1b1c0ffd0c7951bfb41c98213e2db65959947370874067911be2702bdf60
MD5 4319bb6ef5f33be4e3ec1c0509dc8f45
BLAKE2b-256 c7e4578868be0fb8255716f9cd396e30fbd1a99cf8a4fb55b8dc644bf8ebfc0e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250221034458-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 06c9677691c30a22727fc2243edf768cdc80a14dc2943f6ac8edca47baadd876
MD5 457fb2300f020b4fc64869d7cc088e39
BLAKE2b-256 92c4f88b777307f352382106279c96282613355c9153ac81e3592e4e800988d9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250221034458-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 0f5540d940125f1b3a12165c1996a0a5893342f07e1ffb5cede22cfb5ed39fff
MD5 4f00733aecc836308c7a1a4c7f69dd38
BLAKE2b-256 b2d38ee448a46dcc5321ea452a648f06777858419eab891866a04eea5afc56fc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250221034458-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ab9c0be628dc0991ca5b246965beccb68c51e10fef1ae726eaf87dc0641e5cad
MD5 bd016d08cd7f615187c9b5386bf53961
BLAKE2b-256 dc03cb653b0d3c53fbced5ad515035da847f40771f68547631fed2c19de2e67d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250221034458-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1f6ffa88e149a01f756d788d6fde531da4ba6e4d4543e0affa321b4ef2e68c60
MD5 2399408cf21fbe5677f5a84f6836d209
BLAKE2b-256 a03232f6088d0889fa302bdfc038d91466551ea4e3a7dac386f5c7c2752bb1ad

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250221034458-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 d1922d5e9e57c0741b5077aeeead41cd6204dc9d79a1cb408200956f7b32be2c
MD5 fcf04c6a824644f89c2cdd1290444530
BLAKE2b-256 502d426e2682f5637b33b857053659c6aebecf65f032fa397ca50fb6c4cafc78

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