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

Uploaded CPython 3.13Windows x86-64

typedunits-0.0.1.dev20250125001019-cp313-cp313-win32.whl (681.6 kB view details)

Uploaded CPython 3.13Windows x86

typedunits-0.0.1.dev20250125001019-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.4 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20250125001019-cp313-cp313-macosx_11_0_arm64.whl (748.2 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

typedunits-0.0.1.dev20250125001019-cp313-cp313-macosx_10_13_x86_64.whl (785.4 kB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

typedunits-0.0.1.dev20250125001019-cp312-cp312-win_amd64.whl (723.2 kB view details)

Uploaded CPython 3.12Windows x86-64

typedunits-0.0.1.dev20250125001019-cp312-cp312-win32.whl (682.7 kB view details)

Uploaded CPython 3.12Windows x86

typedunits-0.0.1.dev20250125001019-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.4 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20250125001019-cp312-cp312-macosx_11_0_arm64.whl (756.2 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

typedunits-0.0.1.dev20250125001019-cp312-cp312-macosx_10_13_x86_64.whl (793.2 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

typedunits-0.0.1.dev20250125001019-cp311-cp311-win_amd64.whl (750.2 kB view details)

Uploaded CPython 3.11Windows x86-64

typedunits-0.0.1.dev20250125001019-cp311-cp311-win32.whl (715.9 kB view details)

Uploaded CPython 3.11Windows x86

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

Uploaded CPython 3.11macOS 11.0+ ARM64

typedunits-0.0.1.dev20250125001019-cp311-cp311-macosx_10_9_x86_64.whl (826.4 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

typedunits-0.0.1.dev20250125001019-cp310-cp310-win_amd64.whl (746.6 kB view details)

Uploaded CPython 3.10Windows x86-64

typedunits-0.0.1.dev20250125001019-cp310-cp310-win32.whl (684.2 kB view details)

Uploaded CPython 3.10Windows x86

typedunits-0.0.1.dev20250125001019-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.3 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20250125001019-cp310-cp310-macosx_11_0_arm64.whl (735.4 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

typedunits-0.0.1.dev20250125001019-cp310-cp310-macosx_10_9_x86_64.whl (792.3 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250125001019-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 d3715f1b01b9447363b4626bb46d6742400ebd39c190f2de5dcb99a2223ac7c7
MD5 2ea8507f465fe755f7a64501a2f2ef56
BLAKE2b-256 3d05a822350ab331f74474474c5385fdaca19fcf6e1e69a8e4322d7bf39b9120

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250125001019-cp313-cp313-win32.whl
Algorithm Hash digest
SHA256 33892f56b8a6a4f3e9fbbd5a6e2319bec4ad4cc5ff9e5a1ce6f867ea5fe8e5a5
MD5 d09ed472aed15f93d1d4bba2e5da965b
BLAKE2b-256 c916f5ee80d0e2bb0862c8f7a55337c3929ed9d2f296893ea85440cd05601e19

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250125001019-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 151a15fdbaf191fa41603e2e20c82d2f2c5ebafaae17e66954d7c64f4e03025f
MD5 d3641477c8461e1daa379f47bcb77fe9
BLAKE2b-256 931f4558065f2b5a387dc11006896f6936f2d7768d809402acd8842455dc4214

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250125001019-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 6c3bcbc4b637570f7ef9d0e8d1848de6e016ef0aeecf828d070e87ff5800ab13
MD5 29bbf86dcf79511a61e8c595e5dd3d19
BLAKE2b-256 57a0f2a4a75bde53ced05020b0c19f6867befd0fbc50006b9e09fd2421531708

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250125001019-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 924ac15672e7cd211e50a7f0e5f186149e6c1a2e22d1f8fa7aeb594ab6b6ed44
MD5 378094a31b37685933d31bc26ef0c1a7
BLAKE2b-256 b811b7d270247775fa5021b21c2741e4d7df3c427490a4da3c9a8d6147d10ca6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250125001019-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 3cb197ddf8b7ceccbacb36c14489b356adb331950cb5c4d51adc25436e822b94
MD5 7bbfe0091dd574593f984b2cb75d9cdf
BLAKE2b-256 4a089bdc8c86d2d3e063d42e50664fc7a5c2123e02c0acf9922941d6bbffd1b8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250125001019-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 8f87d68c380c7fac623550289ffb3f74e3b947bcfde8620971ece0a103bf13c4
MD5 7d755f71ff2597e69a0dd2a1e5603ece
BLAKE2b-256 4dae9ab2ec5815dfbd0ce22da4fd1b4b859b5faa28aa015080d02b6fafe8aa62

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250125001019-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 74d575ff5a6b1e6f4ebb3d0408ef48cfc90d43fe73c2892111a27e346af3b877
MD5 b250ea690e2de68e2a0ed73850e8f4fc
BLAKE2b-256 d459e883ed25ad3b4e610d3577b46d2199aed7dc6dd2ec80966e642d48bc9465

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250125001019-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0e6f268d607531cd06225d99bd80156cef50dcbdad45a69853c6543105ec9210
MD5 7ec68686d27f7a3d1d4395bbbc5a736b
BLAKE2b-256 0b2105e1e33b142156fcfb1b54ec4a6bd254f327737f86b4f7a0d5a1a6951987

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250125001019-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 93a856b95e1ced852113088e29ea73395c1bf54ae55a36c4d555967922b0f119
MD5 e41b68ae68d744fd91676a5d0687f8cc
BLAKE2b-256 dbb318af8870b86e40fab0aa947ac2bc5cc9976dda86cc2685a1a23c3e842915

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250125001019-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 acca9bad8d3a8a9a9d69485b29fd27c1f20a98911e822ed3dfa64526a66874cc
MD5 38ed6a1361979f0e0449a340ed78a7c1
BLAKE2b-256 07ae906fa6180f30d0ef5f44f0bb4887e290c82119b3f22c2685d7d6921f08ad

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250125001019-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 2598ae44e3653dc17fb46f87b93561be897bb9bba9c50dcf9a949598d52652e4
MD5 5410e4abb651abbdd335551a8c182279
BLAKE2b-256 b51275c3459bd5a853759730aaa8a11cb6b26c91133d944de77892138ef4fa13

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250125001019-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b1a887f9cbebafdb53f1629032594496ac10d46103c0a45d8522edef4183efe4
MD5 94719e976fd2f86a81033bb11d2216df
BLAKE2b-256 160e86ce68bce4d2802f3ac27768cc55bafaf29c2a8756e4c95b5e9f68ae6ae4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250125001019-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 6759efd64e122d14b44ae212fefb4c9811757dc32a9c902ba4946667affee34b
MD5 b95fef8a78ee98841b6f39c48e232da7
BLAKE2b-256 042d8150aacff5844985d34e2e2caf87f28b65ad64c8cf89162e68cf8bb9161e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250125001019-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 43b8baca1594432655db2565f0e65bb7a565f9d1454bed70d1941249d68a5565
MD5 e4764dce9c3cefdf78755aac98291d70
BLAKE2b-256 a9d5f8e111d05ede4e17086415e828699850470c90a8a6270a8d4b01de70bda2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250125001019-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 722da83b975cc6e958acdebb55a96b13b1556f776d658c5ef215d9057327326a
MD5 d28bf9a6edc8565f692595423181f78d
BLAKE2b-256 4dc6ff46ae84610e24b8581515bcc3db86326dceb8b0274220f9fa7f978748cc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250125001019-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 e12ede91c249ec7845f560fd58a5c0d17784ec4373cd8566728275da6c454e13
MD5 3495f4216a85051d8c647953e9b7b617
BLAKE2b-256 fac0270bc02588f8002826557abab505df987417dd6e0270371b5d4d92840619

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250125001019-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ee64aed05fdb283eb593caec12dc54f5bb28ad4a19229da2dbb040ae386f9c0f
MD5 0406f8ee53085336e7b17e4d33a32ac1
BLAKE2b-256 69ad0a253432ce53299d17c948322129abf3ab0dcc4bc74596eb20648aff93b3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250125001019-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 aa04245c56569ac6ff538daae4f390e7d1929fdb4849d48e99e54e20d52a3d4d
MD5 c943980ba53827a610531818a39f4ebb
BLAKE2b-256 5e21256dba1db50810141494cba887a571d48b17263d736134462882da11af83

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250125001019-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 06167d8d09c2b7b6c70c0eda825e511e6569c5d2beaa1538e6bd88c56c0ea3de
MD5 5d71348e17d1db24729de3eb5ccc3247
BLAKE2b-256 d2eaafda264031fc23be1322776653e5afa67ed070fa918c0b85c915059032bd

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