No project description provided
Project description
TUnits - Fast Python Units
GCB CI status: |
---|
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
-
To install a precompiled wheel (add
--pre
for prelease version)pip install typedunits # [--pre]
-
To locally build the latest version from the main branch
pip install git+https://github.com/quantumlib/TypedUnits
-
For a local editable copy
git clone https://github.com/quantumlib/TypedUnits cd TypedUnits pip install .
Development and Testing
-
Clone the repository.
git clone https://github.com/quantumlib/TypedUnits cd TypedUnits
All future steps assume you are in the repository's directory.
-
Install dev environment dependencies.
pip install -r dev_tools/dev.env.txt
-
Install TUnits
pip install .
-
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
Built Distributions
File details
Details for the file typedunits-0.0.1.dev20241015220558-cp312-cp312-win_amd64.whl
.
File metadata
- Download URL: typedunits-0.0.1.dev20241015220558-cp312-cp312-win_amd64.whl
- Upload date:
- Size: 705.8 kB
- Tags: CPython 3.12, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5d67acc53359941e00461aa544339e4874fef01b5b500b9231c41b2791a1bd81 |
|
MD5 | 1d5b65d289e67c4aa19f9aebf8ad231b |
|
BLAKE2b-256 | 1ba75257247a3b528df7d3b9ea07ddded73d004685ecad5afd8f0457e6a718be |
File details
Details for the file typedunits-0.0.1.dev20241015220558-cp312-cp312-win32.whl
.
File metadata
- Download URL: typedunits-0.0.1.dev20241015220558-cp312-cp312-win32.whl
- Upload date:
- Size: 665.7 kB
- Tags: CPython 3.12, Windows x86
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4edbb705a929615a1e6ec7717617c6640d8e4334bd0beea5896cf7e47a31c27a |
|
MD5 | a2cac9a3d36b1ee30cea6b402cab1d16 |
|
BLAKE2b-256 | 2d8373af4310503ca1989d9b4030939fe7702298c34b9f8939eb0d2880010f7c |
File details
Details for the file typedunits-0.0.1.dev20241015220558-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: typedunits-0.0.1.dev20241015220558-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 2.4 MB
- Tags: CPython 3.12, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 94c91c507a796c75703be2667b287c47c862c0b1f7707c449488f70dc8bcdf26 |
|
MD5 | 044391715d53bb95aeae6a850cd2fb3a |
|
BLAKE2b-256 | 3f8977cf72b40305cf886b68ac3d59ff39b1059a5af501105521192320486207 |
File details
Details for the file typedunits-0.0.1.dev20241015220558-cp312-cp312-macosx_11_0_arm64.whl
.
File metadata
- Download URL: typedunits-0.0.1.dev20241015220558-cp312-cp312-macosx_11_0_arm64.whl
- Upload date:
- Size: 739.4 kB
- Tags: CPython 3.12, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 69006bb6ef3efa6db17393ad32976821681a33980af6d014e8207a121f7d2c69 |
|
MD5 | 6227775038e51c4f14f967b4eff0e57a |
|
BLAKE2b-256 | 55874fb4437a49345a1b583ea2064dba220b866cefe6239967882eab2934e396 |
File details
Details for the file typedunits-0.0.1.dev20241015220558-cp312-cp312-macosx_10_13_x86_64.whl
.
File metadata
- Download URL: typedunits-0.0.1.dev20241015220558-cp312-cp312-macosx_10_13_x86_64.whl
- Upload date:
- Size: 771.0 kB
- Tags: CPython 3.12, macOS 10.13+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5e1494b6c4ab2b5d1ee9851981045777b6af81096da1ab39a4288c1af4923615 |
|
MD5 | e653e827e57827fee58abf1cbc10d516 |
|
BLAKE2b-256 | 6a558677aaf7dbb89cc75d5fdd2a4f505c1ebc18c9bb99a3eec9a4fca98914b6 |
File details
Details for the file typedunits-0.0.1.dev20241015220558-cp311-cp311-win_amd64.whl
.
File metadata
- Download URL: typedunits-0.0.1.dev20241015220558-cp311-cp311-win_amd64.whl
- Upload date:
- Size: 733.2 kB
- Tags: CPython 3.11, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3aa069409f943195ee046c14e72de53f467d583fd3427583cb6dedd5fe1af2b4 |
|
MD5 | 17ee9a6f4d6b3a7b7d6d1c854a9aa86e |
|
BLAKE2b-256 | 79fb9f95f0af06b75e01ae34d38046a428d8dc8fcee74fc34dcca4c94aee2cef |
File details
Details for the file typedunits-0.0.1.dev20241015220558-cp311-cp311-win32.whl
.
File metadata
- Download URL: typedunits-0.0.1.dev20241015220558-cp311-cp311-win32.whl
- Upload date:
- Size: 698.8 kB
- Tags: CPython 3.11, Windows x86
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3a5ceac381f29c6ebfbb953712ae23e6a124e93b02782f5f4f832f8c99895d12 |
|
MD5 | bf8d784347cd0010a249bcf7a306270e |
|
BLAKE2b-256 | 6ea18b20c53c0eea827e88c835711b5e1f00b5ef97a69dd4e8194028557aeb47 |
File details
Details for the file typedunits-0.0.1.dev20241015220558-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: typedunits-0.0.1.dev20241015220558-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 2.5 MB
- Tags: CPython 3.11, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1475d6d3fd8c65170b1e93174d3e80bc3159818bf113eb415a54b010fcd5b7f3 |
|
MD5 | 18f0fc86c61c7c776e6b2a3c89f08925 |
|
BLAKE2b-256 | c35c02d60ba1cea1b1b2dd9f6ba02ef694ab3280d1bc7d644f2b3f580a4e6e77 |
File details
Details for the file typedunits-0.0.1.dev20241015220558-cp311-cp311-macosx_11_0_arm64.whl
.
File metadata
- Download URL: typedunits-0.0.1.dev20241015220558-cp311-cp311-macosx_11_0_arm64.whl
- Upload date:
- Size: 749.2 kB
- Tags: CPython 3.11, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0a2e4def5f7951e269f6b90863184239adc432ab43503bc6b1da22a7c6def51c |
|
MD5 | 758bb3c6ca22276d4ae7e215f7f28e51 |
|
BLAKE2b-256 | 18fb51896e2d366d5185e20a41e4f889245384f65c4c30f02001266403fe7151 |
File details
Details for the file typedunits-0.0.1.dev20241015220558-cp311-cp311-macosx_10_9_x86_64.whl
.
File metadata
- Download URL: typedunits-0.0.1.dev20241015220558-cp311-cp311-macosx_10_9_x86_64.whl
- Upload date:
- Size: 807.3 kB
- Tags: CPython 3.11, macOS 10.9+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6d88f93f4caa190b8af430c472e82840fe5e612e6325cf0c3d7712d519b68aa8 |
|
MD5 | 1bbcfac6d0882115835551aabfefa9a4 |
|
BLAKE2b-256 | beed1acbf8fec86b89db7078b2a910a434b685d45481ea920e012a3380d03feb |
File details
Details for the file typedunits-0.0.1.dev20241015220558-cp310-cp310-win_amd64.whl
.
File metadata
- Download URL: typedunits-0.0.1.dev20241015220558-cp310-cp310-win_amd64.whl
- Upload date:
- Size: 729.1 kB
- Tags: CPython 3.10, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e05ddf1ac665b48bbb0dd19e96bbfe65d934390c6fb0431e7dd520f73873c195 |
|
MD5 | da7afb5acd3a45c04f260da0aacc783e |
|
BLAKE2b-256 | f81b44c3e91cd22bee70d2952f6c395c804e793b0aa4c8353049d910281d6224 |
File details
Details for the file typedunits-0.0.1.dev20241015220558-cp310-cp310-win32.whl
.
File metadata
- Download URL: typedunits-0.0.1.dev20241015220558-cp310-cp310-win32.whl
- Upload date:
- Size: 667.4 kB
- Tags: CPython 3.10, Windows x86
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2cbed10620335f3e5d5932e23849fb775d397123c1ab7c406b1a9f9406e09ad8 |
|
MD5 | f68217dca13caed14422c99398ebee7a |
|
BLAKE2b-256 | ede0f598340b8453c5c63092b4671b143b3ea8b8f7e9b6267e224119d5a6e3ca |
File details
Details for the file typedunits-0.0.1.dev20241015220558-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: typedunits-0.0.1.dev20241015220558-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 2.3 MB
- Tags: CPython 3.10, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f3feb09b48aefb99dbb6cdcd4947be0518c2569cc00c2b362cc2de3290709b2c |
|
MD5 | 6d73eda1c4c05776387b971ed1e650cd |
|
BLAKE2b-256 | b9362c7a6481f32c84c78ec2ab23a2abd7ab2e60407d1a096d8359dc077195bb |
File details
Details for the file typedunits-0.0.1.dev20241015220558-cp310-cp310-macosx_11_0_arm64.whl
.
File metadata
- Download URL: typedunits-0.0.1.dev20241015220558-cp310-cp310-macosx_11_0_arm64.whl
- Upload date:
- Size: 716.9 kB
- Tags: CPython 3.10, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3fd99f2481b4a3d29e5a67ef608f49a5f3a6c8c7fde74e7de53065299bc81a47 |
|
MD5 | 680374b7fbd77bfd61c29acff5ec0ee5 |
|
BLAKE2b-256 | 59957b6f830b8a967bb68ea6d55268e070862d341dd5db8c6c020ad83eec89aa |
File details
Details for the file typedunits-0.0.1.dev20241015220558-cp310-cp310-macosx_10_9_x86_64.whl
.
File metadata
- Download URL: typedunits-0.0.1.dev20241015220558-cp310-cp310-macosx_10_9_x86_64.whl
- Upload date:
- Size: 775.0 kB
- Tags: CPython 3.10, macOS 10.9+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5276e319c66d907194660f0e2ad637fce06e7b2539e148aa1d732293b7ac48b5 |
|
MD5 | 08a5b5266e702bf94f44bc80da54b4d8 |
|
BLAKE2b-256 | baa53772125846a5f0dc7f09844ca507f1b1deb85a00447fd29469f6d69dc1ed |