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

Uploaded CPython 3.13Windows x86-64

typedunits-0.0.1.dev20250415001833-cp313-cp313-win32.whl (733.0 kB view details)

Uploaded CPython 3.13Windows x86

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

Uploaded CPython 3.13macOS 11.0+ ARM64

typedunits-0.0.1.dev20250415001833-cp313-cp313-macosx_10_13_x86_64.whl (843.4 kB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

typedunits-0.0.1.dev20250415001833-cp312-cp312-win_amd64.whl (776.4 kB view details)

Uploaded CPython 3.12Windows x86-64

typedunits-0.0.1.dev20250415001833-cp312-cp312-win32.whl (734.3 kB view details)

Uploaded CPython 3.12Windows x86

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

Uploaded CPython 3.12macOS 11.0+ ARM64

typedunits-0.0.1.dev20250415001833-cp312-cp312-macosx_10_13_x86_64.whl (851.1 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

typedunits-0.0.1.dev20250415001833-cp311-cp311-win_amd64.whl (804.1 kB view details)

Uploaded CPython 3.11Windows x86-64

typedunits-0.0.1.dev20250415001833-cp311-cp311-win32.whl (769.4 kB view details)

Uploaded CPython 3.11Windows x86

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

Uploaded CPython 3.11macOS 11.0+ ARM64

typedunits-0.0.1.dev20250415001833-cp311-cp311-macosx_10_9_x86_64.whl (887.8 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

typedunits-0.0.1.dev20250415001833-cp310-cp310-win_amd64.whl (800.9 kB view details)

Uploaded CPython 3.10Windows x86-64

typedunits-0.0.1.dev20250415001833-cp310-cp310-win32.whl (736.4 kB view details)

Uploaded CPython 3.10Windows x86

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

Uploaded CPython 3.10macOS 11.0+ ARM64

typedunits-0.0.1.dev20250415001833-cp310-cp310-macosx_10_9_x86_64.whl (852.1 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250415001833-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 b320be2a24aefd29979c60e88ec262fd894b6cd073885cac7aae093f7a347a0c
MD5 0be9f0217e1b07a11c1312153590418d
BLAKE2b-256 5ade28af40647f4ed4eef9fcb19d318611083f458e111d857201da0251a485f4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250415001833-cp313-cp313-win32.whl
Algorithm Hash digest
SHA256 f3128c727353034b42599045fb241eb33fa492d555f220635a38c8b9d54100ca
MD5 fa606cd182d9590bab64b0970ce242d4
BLAKE2b-256 4ee669c3f6889b97fee366d45d57d2ee30e99e21e9b5f82510d9f267f5744ace

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250415001833-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 2e83cc264bd9844d72abb476de448073144ba268112d7ebb753beb793506eec2
MD5 25a1f3c69f79373d7c408b2d55331f0f
BLAKE2b-256 072ecb7619d6d6435f035249409c6fb6b8d38659b2bd4c26894418b286286c8d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250415001833-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b7ad1250f430d8ebada9587b848b84723fe93beff6010b83ee499b4077cc9fd5
MD5 fde88a5742b7384c22b3c876f69fbbfb
BLAKE2b-256 d45e7525533ff7e6b102ea27202f094edb9109db2ca2bced8fc1333d5fbd8ce5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250415001833-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 eabcf65acc65c0f02662b2edcf02c0ba29e23141d162a721e96e16566598d7dc
MD5 79618e5ff1a22ec1aeb9aab50ab875e0
BLAKE2b-256 cdee6bac77fc59bd7e37f87bea55cf062a8fb9c480a84835f5e26746a7b7757d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250415001833-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 50216dd5e9cf804e8888661a957f126f46994e21d125af5f6d39278c84abcc1b
MD5 c954a2763ae50f46ede91e7a8c94852d
BLAKE2b-256 02351ddb00b120024b6c5c00fb627c8a6ea9863fd819ae8cbd2f99c9610c9500

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250415001833-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 eaa89d56edb11d3408a3ccb32315f15cf083a006ecc5d0041750e20096c8bfd1
MD5 76fc74b3b728e51c1b253a32b9a3276e
BLAKE2b-256 f34177af265c9e058e320a56f10f177e6c083cf0096a827fa19c7d930edae1fc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250415001833-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 311107dcc5b3d4c770ce17b9376584b5863fc4ac3956f97319e48a4cb39883c1
MD5 4895a7ea43bbc60faa959124030ad069
BLAKE2b-256 a32fd52384f755bdf0d91cd0044f8de3b2d0e3fe2852116b1603afd6b8a36382

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250415001833-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e2110a9149930b12861cf80428fc35e5b58ef511195b51ed2d082f9e15cc2ed7
MD5 c55a69fa62a585adcda3f1b91a6fb13f
BLAKE2b-256 dc7366740398f1a9bda31598c7beb231b2135cbc42a204688239a52bc9aa1dd8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250415001833-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 ffc9c118cbe82ccab3c44970b4e6e579021e20473f5335e6333239902be31cae
MD5 af076e1134a18e9a27f9118ffdd6353d
BLAKE2b-256 3ea67d8e93264739abdd845386d3ab4695091555808b40b58ee548fae96a405c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250415001833-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 6499430e221cdaa5ba0ece560a48f8ff302f43f2837683ebaede845c6db64dfe
MD5 cca830d52cc1bb45801bab8b64587d32
BLAKE2b-256 5b3e916d3cf6f132100ca0e645e69ed5e167f7c8787d08ded535512c96993ff7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250415001833-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 ba507758bd73670aa5c3b8bdc571cdb06364e3c330c018fdc15188b9551ee323
MD5 6c5630a1d01812c246cc833d22af77cf
BLAKE2b-256 b86bb2b3c69080b2dc3d950fac63f763d0cf14903c8eaf939143ab4f82a55f4d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250415001833-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8ad6936e907163f59a8497ca488436c4bd268a4c16133d2570e18a5a44348eca
MD5 cfb68cb2d0af5e833f8f0e8077ae34b0
BLAKE2b-256 ab3de86ed32cbce5280f33783bc2ed9c4ac473aec68de8f3af56bb42abe150f2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250415001833-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 47c82b8d93dc7e8d197fae858486ba135c89c8565a9d6fd6d2f20fc079d1014b
MD5 f97b35114d75a81835577a8d2b8410cf
BLAKE2b-256 2284438e2a9dfca430a820a618eeec83e32a66cb8e3e0ccfcd069193e87d6302

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250415001833-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 ee95c084b4907a89d4b1727933d3a071dbe6bbda32f8d251245fbd938fced746
MD5 a1689891479573429f20b2febf3193be
BLAKE2b-256 1dbae4d21ac853059a4b60578eeada49d6184153c6bea2f271882c8ef755e443

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250415001833-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 18722b7086d6f97876aa5c222bc12df9d3ca96eb4de37e9829be261df10cba37
MD5 dca191675113bd552922e1aa27d0c9df
BLAKE2b-256 7d6b01b7d731f0df82eb396b477b0aa889be8161c3c910d54b309aa573e2e4bd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250415001833-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 9f145d0ba664b009b90ffb0aa38f311c583f851a01f4a56520bd758efc6e497b
MD5 2f568e258ef01563b56e86a40f7a8562
BLAKE2b-256 1d72f77745d477b51578fd181c4bc994bdfbd97de4a548d629bfc37e849da3e6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250415001833-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6c9c9feae2bfc4e094946f0e128433d05804993949fb31695f93309c5d4a3e56
MD5 c12f47918a513faae461e01c6c1bcb0c
BLAKE2b-256 1a40495321420ccaae39cc2ea7b4ce93cfa4f306bcaf108a52c5438d73958ab5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250415001833-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 06f30e2b89382e011d64d066e50c52ca775d08ee5a998bee42a3561b88683261
MD5 20de1c83d6e141c92787327af28ff7de
BLAKE2b-256 4923860057a2e5c967c4cb4d7b2ad7c96fb04bab6c2dd393453cdfe5823b9782

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250415001833-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 8b11cce2a2b00dccdb0bcbc2c3a39cdbfa210b888619a35fab74f07b9a9f25fb
MD5 69394e6076d64c9ad96954c1ee0b9e73
BLAKE2b-256 1cb3428964abe35c21fedaf981571fa3a32671242ce30f12bd10d471b1a6a22d

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