Skip to main content

A fast units and dimensions library with support for static dimensionality checking and protobuffer serialization.

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

Uploaded CPython 3.13Windows x86-64

typedunits-0.0.1.dev20250912005737-cp313-cp313-win32.whl (683.6 kB view details)

Uploaded CPython 3.13Windows x86

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

Uploaded CPython 3.13macOS 11.0+ ARM64

typedunits-0.0.1.dev20250912005737-cp313-cp313-macosx_10_13_x86_64.whl (798.2 kB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

typedunits-0.0.1.dev20250912005737-cp312-cp312-win_amd64.whl (734.4 kB view details)

Uploaded CPython 3.12Windows x86-64

typedunits-0.0.1.dev20250912005737-cp312-cp312-win32.whl (684.6 kB view details)

Uploaded CPython 3.12Windows x86

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

Uploaded CPython 3.12macOS 11.0+ ARM64

typedunits-0.0.1.dev20250912005737-cp312-cp312-macosx_10_13_x86_64.whl (804.0 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

typedunits-0.0.1.dev20250912005737-cp311-cp311-win_amd64.whl (814.7 kB view details)

Uploaded CPython 3.11Windows x86-64

typedunits-0.0.1.dev20250912005737-cp311-cp311-win32.whl (749.0 kB view details)

Uploaded CPython 3.11Windows x86

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

Uploaded CPython 3.11macOS 11.0+ ARM64

typedunits-0.0.1.dev20250912005737-cp311-cp311-macosx_10_9_x86_64.whl (849.0 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

typedunits-0.0.1.dev20250912005737-cp310-cp310-win_amd64.whl (814.7 kB view details)

Uploaded CPython 3.10Windows x86-64

typedunits-0.0.1.dev20250912005737-cp310-cp310-win32.whl (719.6 kB view details)

Uploaded CPython 3.10Windows x86

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

Uploaded CPython 3.10macOS 11.0+ ARM64

typedunits-0.0.1.dev20250912005737-cp310-cp310-macosx_10_9_x86_64.whl (811.3 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250912005737-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 c8e547250a10350db668483d3ff8a537286cb771026d7152ce7ca367b0ae9aca
MD5 229e2d5a0f0de9223ae2b541460f17e7
BLAKE2b-256 f625264286078448b2a50ffc4cd375ac9009b540fccf0b3f4c0fca570e13dd66

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250912005737-cp313-cp313-win32.whl
Algorithm Hash digest
SHA256 b49de34c206ca174fc5e227e52e69ea14b88fad0709c6f9105a92a5df0bf744a
MD5 75b6652ea5491a2871c88d7315915d99
BLAKE2b-256 ebdebbbee93c7e338892fa57ea6a2e732bf0b58d909f92914b8faaa4b47bb5f7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250912005737-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 95a074cffbb608c87dd1786ae1af4d7a8c88d1b93291f3a8a998389352f53a18
MD5 70073d52062666e15a7d3cdf18e2245e
BLAKE2b-256 c5472a0c046f19a351953a26350aa0402b304703d8dd7f8089ab51f51827a88d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250912005737-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 cc69cc4a2d8171c7a715778b8b0b12016c8d66695a7ee4d0dd8eb03e6dc96587
MD5 56b531bfd92844f69579b6154f15f6e1
BLAKE2b-256 e9d080108f57395d8279a9f7489e11387b1d11ef16b2850eb81be724be1a9b00

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250912005737-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 15a095782947e6eece5e2b6cbf562aceb8ca29fa3cb55224b48c8fb8890329d0
MD5 b7c725bda89befffdd467a9a64ce9557
BLAKE2b-256 de01d52b3d524968aca45dbe957bb28858a0ddf86b29f42b64019b8fd0759c73

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250912005737-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 7854d1efe2fe053e0af399ed7395f1acdf27d8d189aadb7bbcafe887faef2264
MD5 6b8839807caaaa4c13491ba336a3e23b
BLAKE2b-256 221b4f7043730aa889e0b3c256b50a7c6480c9c68777f5e0d5eb04f841eeb975

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250912005737-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 6388d6140ffd254fff1a9c5a52ccc2f9bba2e88e8367fe8f5aed414fdfa935d1
MD5 b844a6736fe2992ff6ddcdc2c985a66c
BLAKE2b-256 128cb32dc650865733a818e66df4efcc5a8f460a4aece51a7d4579a3873d51ee

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250912005737-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a827701d23b0c65f9a473f30203abca2a35a115b72f6afee71208d8c4e016c3c
MD5 727fef0e30c3c6942456780aeaac95b7
BLAKE2b-256 e8fea6fd2abce56440b1aa4715c888cc74ef1f12e092cd419fd76f2109b09974

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250912005737-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d49c82651b02f9e2dafe2f060f8bbbef289f68e5d731bbad1b52980226cfac0e
MD5 10e8d8e070b39fbef7eeac4498479ee7
BLAKE2b-256 31b72c1c1b50631a9984b2f3f1abf52f675cea5f07ff30471eb5589db9047288

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250912005737-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 f33930ebaf7a0c792bc3113b12f36faab9eb00f6115ae2c32db02494038cfb1a
MD5 e89035b7eae275171c06dd8f8f5e1b6a
BLAKE2b-256 981038b4d440b088d7f54d1399aefbd7f77cecd2559ee2502f0c1a08d0badc48

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250912005737-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 e34ef38f22671061184f792036993f44f485f1e4b9ee873ad4c303ade9b63a83
MD5 bc585868273e21a22302da2b6071fca5
BLAKE2b-256 64226ee43e5dbc45dc8997d17121e86f541bf59216cbf5b37d682479b10fcdf9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250912005737-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 155bdb1b049735a88f640e765ad54ee1389a51274f748c14e1224f5a9e61483f
MD5 c57696704fce6c958cf76c7b2d163750
BLAKE2b-256 8f9c125c486982cfe8e2cfd81bce88bcc1a75307780798a695329a376a9b9c12

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250912005737-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 15f556552e6dd00697d2e78e76de98977172ce3844b524d3df53ccfd6c20c210
MD5 1748b9ce376c95f05eb107e2299fd13e
BLAKE2b-256 0e05eb0f69ed6ca34ff412b0a0999101e29628472ce8fdca3a86ce9b7ef104a4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250912005737-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b7158460f8b6279850d809ea68e73c4672d809230abc6ce9ae6f977144b4111c
MD5 a7b2b79ccbed8aef6e916da6e94d9f05
BLAKE2b-256 9e0c22dbcaa429120b28aae1b95d6fb2fdd1ef7215c6fe4deafcb56b923d0590

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250912005737-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 c2218ab6ed65813e2a91b2a755e6f7b721610f8a44694bd1c85ccaedeb7ce1b2
MD5 b56e7e4366ff0d2f2a9ed5a4501bce47
BLAKE2b-256 5b5ee70500b1d09a43f4bec4792c54a74b21f49293862efd63c29ccdcd5525af

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250912005737-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 a640a54dc6184fcced8a12cb65b0dc7c5bf123665e733130afa5cebe401abd26
MD5 da3cff84fd2e97a190dc39e30fbbf7b0
BLAKE2b-256 a505d62193538464862c6953a120d927f198a0e90e0d5c3221114ffc85b7e866

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250912005737-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 1fc8509b4ee38fc0f50c3bdc6198ea643eb146caaae68b15ee9b290ca670a766
MD5 65dc4fc343a18332886e4c8a3f2e3d9b
BLAKE2b-256 4d4a9cb9d35803e78063bac0fcfa96d4d7f2d071e26ab5f9b91657d058b2ca85

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250912005737-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 899c38a1fe345ecbf42cd211e652eb47042f0f6a4fd30eee62e3a543b251377c
MD5 c6f6f7bfe61af075a66b786e344fb357
BLAKE2b-256 072bb18bd1339f14a4209225940ef31a36f065952d5fa24752dd8099011cf144

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250912005737-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d12e4a528308e75ebcc072066a492e69891933b953718b730542ca9c0594b5d8
MD5 70fbee571474acddeb87e6a0579f841f
BLAKE2b-256 8a5d7fe4ccdd48be82d3a68eede111d2756931c870d996c507ffd241981a821d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250912005737-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 90815f5d91301d2edab53d82d23c2f9c3e8f96e6a7081241f490da30be6b414c
MD5 29fa8dfb4b47597aea5eb66644c226cc
BLAKE2b-256 d604b6aff9c0cf90664327dac75730d366b2688230214512bd27a59ee2e1bd4e

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