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

Uploaded CPython 3.13Windows x86-64

typedunits-0.0.1.dev20250212005636-cp313-cp313-win32.whl (699.1 kB view details)

Uploaded CPython 3.13Windows x86

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

Uploaded CPython 3.13macOS 11.0+ ARM64

typedunits-0.0.1.dev20250212005636-cp313-cp313-macosx_10_13_x86_64.whl (808.3 kB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

typedunits-0.0.1.dev20250212005636-cp312-cp312-win_amd64.whl (742.6 kB view details)

Uploaded CPython 3.12Windows x86-64

typedunits-0.0.1.dev20250212005636-cp312-cp312-win32.whl (700.1 kB view details)

Uploaded CPython 3.12Windows x86

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

Uploaded CPython 3.12macOS 11.0+ ARM64

typedunits-0.0.1.dev20250212005636-cp312-cp312-macosx_10_13_x86_64.whl (814.4 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

typedunits-0.0.1.dev20250212005636-cp311-cp311-win_amd64.whl (769.6 kB view details)

Uploaded CPython 3.11Windows x86-64

typedunits-0.0.1.dev20250212005636-cp311-cp311-win32.whl (735.1 kB view details)

Uploaded CPython 3.11Windows x86

typedunits-0.0.1.dev20250212005636-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.6 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20250212005636-cp311-cp311-macosx_11_0_arm64.whl (790.3 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

typedunits-0.0.1.dev20250212005636-cp311-cp311-macosx_10_9_x86_64.whl (851.3 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

typedunits-0.0.1.dev20250212005636-cp310-cp310-win_amd64.whl (766.2 kB view details)

Uploaded CPython 3.10Windows x86-64

typedunits-0.0.1.dev20250212005636-cp310-cp310-win32.whl (703.5 kB view details)

Uploaded CPython 3.10Windows x86

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

Uploaded CPython 3.10macOS 11.0+ ARM64

typedunits-0.0.1.dev20250212005636-cp310-cp310-macosx_10_9_x86_64.whl (815.9 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250212005636-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 e6507767e8d60a00a7c943cfdc8c629be0c185e981edcec4746ac5aea5a73ed1
MD5 6224b44a3545681769461bb728e15dc5
BLAKE2b-256 429c5ccb998eb050fea45a79d2e7e01b851ac7ce8de938852061bf606f0d770d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250212005636-cp313-cp313-win32.whl
Algorithm Hash digest
SHA256 e9357aa8488a28e2cbe53f68ac6aacc989ec6927cb1eb75d7e760b9f8bf5f620
MD5 a015d3fb471a661f3a6c9f5d06f8677c
BLAKE2b-256 05f019692e5e80829ed2265800a07a942cf8ab4ca31a282feda2e82409579888

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250212005636-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7244f69fa6650dcc5d66f99490b823c4584d5b7be91a5e86fcad2081b11981ed
MD5 011edbac6e103482433e474a7d90b4c6
BLAKE2b-256 d3320bfc6dc48479de7c887a8c8becb1f6af80a75b5769803851c83155d493e8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250212005636-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 6c857c6695854fcd05e60462a74676c65a1aa6b3a39cb6ecdc03e0963ef1a59c
MD5 f1022ae197bbfe8dbf0ddc4a8bdadf51
BLAKE2b-256 7540dfef78a599893d725859b94c86410ed93f4fe2e71199445e105cb7262b3e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250212005636-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 9df74213483d82e89797efb4b7b61eded23b4204bedcb6c7ac426154e3f32b36
MD5 98634e6a3c3d9c0cf3d5c9e0b11dba6d
BLAKE2b-256 a8d5ea961a7b6cd55aa2711573d454389f5734a389c8cdb17d245ad4e02c0ddb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250212005636-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 c973044621841240f25a645c157d7a327c34b336e2155cbf8935eec3b4536814
MD5 b6b6a5cfb7d4421a252fea3056ef8c40
BLAKE2b-256 b65088e17a36ca7d2969e43512d0d96b658467a0599ffec1cda3e1a3871a1999

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250212005636-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 6dc74f9401218cf966f9f5e8b431658331cc33e47244ef640fca1528f5253eb2
MD5 351dbcaada00f2f857044adc68afc938
BLAKE2b-256 7e1f2a77aa1896592b12253eb0271c9a49641f21c096722fcfcd37a1f9cf18b9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250212005636-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f0cd3b2361612a000bd20a8d8a6a0524b9aba8585b01e5b61013f44fe2d15b56
MD5 641d792d5e9d15fc07edf57cecdb991c
BLAKE2b-256 4e2089d5a8ac2e82807298f41571fe05392d62a6b7954f8f8854f7fde9badc52

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250212005636-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 19630548131dc1ceaae6c8aa992b5889a753ea03062c10bc4e155f5200da4f56
MD5 c597373e9bb67cbb302d14f5b1d54268
BLAKE2b-256 3f58c91f70f5e9d7cfbe492e49062902f352ebc0f1af949785acc052d94f2d32

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250212005636-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 5073feb6297de8550a9b05520a98e987de77a09a310e12e4f3071a5e9dd87956
MD5 87c97abd44b145e22bd9cbd70da27511
BLAKE2b-256 9e7368ad88da2574c4c71bc32a215de9329eb1e3729815769b302f58437068b7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250212005636-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 121f17197b8c93d884651956e3ebb0fd159016affeca4e273383602cc9de3ecb
MD5 890f6226137e2aec024729b502b4c165
BLAKE2b-256 474faf6a597b94b63e5464ea642a55de5ea30103bbec71c162f3926f95c554e2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250212005636-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 aee63b96cc6aa0a655d5c89632d560e6af2819dce806f29a2498e44358105453
MD5 196925dec7b7f7f6c964ef4241d0851c
BLAKE2b-256 3524a03ffcad09fad8ecb6cca166888c159274215f63eeb9717e171091662264

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250212005636-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6abca848ed7813d20d379cb48d2d96536ef995d8af460d68c995e23fac8fa66a
MD5 d2183a70ec16956ef007cd945f610d22
BLAKE2b-256 fb28854c71f8a8f4b55e1e05ec49c35bf6605cf3c986a4bececdfa2a9eaef6cc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250212005636-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 22a4c02a7513e1b27734dbfd6b9e2339981c323615a3e8514394e20044fa68fa
MD5 f9ff41297aff002ff8c2980639688aee
BLAKE2b-256 3b693e28b13cf6538763d0cba403579fdf89b715c3f285bf1764ff8d86d77d36

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250212005636-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 cdc343de97ce3095b20ac5315e8a5e691af9e5b1640b7687b02b3bfb47fa5e8d
MD5 00b280ba1c24b80d6fbed19bbc71027c
BLAKE2b-256 7484b4d75435b1d4ad7c00ddde63d8236ffc3627acede59d6b665cf542724297

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250212005636-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 0b093d7ad20e9d64fed1953800cb5068072b65205001309ba820180bd4075dcd
MD5 5278fe5126384143082429d8713703a5
BLAKE2b-256 da0e34610e32b12a99d1e542d2c6b339d3303a82cb2d5dace9f677d4e1b1a3f8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250212005636-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 b561a8717965b8b0c21858965727ea520b5299ad7a728c4e7bcdbe6f8371da65
MD5 a31e0862035b2e4412dfddc47087a6ba
BLAKE2b-256 e20bdb1f2d2833e56df333dd4451ac1ae4a5c9171456f495d50cbc9cd8a33fac

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250212005636-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 2a83f9fcf02d8e49a2bae4bea1565dcb8fefb40a1b4dc86f9d982f6fbeda5952
MD5 63771563d1bd4095ced0cda5eee5cb8a
BLAKE2b-256 90b21b98587ea4e30dbc53cfd633c8c2c1af03bf79eb35f0e5e14cbda9b58c97

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250212005636-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 bc94c2b38f67f963d26622c610b1faf2c459f4751c3816df037ea3d95bfea053
MD5 e0cf5628dd1d6cd5f373d70cd5541e2e
BLAKE2b-256 792ab6d90c82bb45791aa44525c21c7f0f59e754901b21f1690e3694b7556fae

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250212005636-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 dada9f52af9f65634d0c5df96523a98ab909917b0b4983cd65b1f110bba8744b
MD5 3d1223f20d396fd76c2f3ecd18faa0cc
BLAKE2b-256 992bf2e49eb68e29fb937ae6db06b46c88dc9e289f775105d86bc54ee8c5f5e8

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