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

Uploaded CPython 3.13Windows x86-64

typedunits-0.0.1.dev20250422050902-cp313-cp313-win32.whl (741.6 kB view details)

Uploaded CPython 3.13Windows x86

typedunits-0.0.1.dev20250422050902-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.7 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20250422050902-cp313-cp313-macosx_11_0_arm64.whl (814.4 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

typedunits-0.0.1.dev20250422050902-cp313-cp313-macosx_10_13_x86_64.whl (854.8 kB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

typedunits-0.0.1.dev20250422050902-cp312-cp312-win_amd64.whl (784.6 kB view details)

Uploaded CPython 3.12Windows x86-64

typedunits-0.0.1.dev20250422050902-cp312-cp312-win32.whl (742.9 kB view details)

Uploaded CPython 3.12Windows x86

typedunits-0.0.1.dev20250422050902-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.7 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20250422050902-cp312-cp312-macosx_11_0_arm64.whl (821.7 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

typedunits-0.0.1.dev20250422050902-cp312-cp312-macosx_10_13_x86_64.whl (862.5 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

typedunits-0.0.1.dev20250422050902-cp311-cp311-win_amd64.whl (813.2 kB view details)

Uploaded CPython 3.11Windows x86-64

typedunits-0.0.1.dev20250422050902-cp311-cp311-win32.whl (778.3 kB view details)

Uploaded CPython 3.11Windows x86

typedunits-0.0.1.dev20250422050902-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.8 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20250422050902-cp311-cp311-macosx_11_0_arm64.whl (837.2 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

typedunits-0.0.1.dev20250422050902-cp311-cp311-macosx_10_9_x86_64.whl (898.8 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

typedunits-0.0.1.dev20250422050902-cp310-cp310-win_amd64.whl (810.1 kB view details)

Uploaded CPython 3.10Windows x86-64

typedunits-0.0.1.dev20250422050902-cp310-cp310-win32.whl (745.2 kB view details)

Uploaded CPython 3.10Windows x86

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

Uploaded CPython 3.10macOS 11.0+ ARM64

typedunits-0.0.1.dev20250422050902-cp310-cp310-macosx_10_9_x86_64.whl (862.1 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250422050902-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 88dd976e207d4f1399eb535af39eb772ef52759be7384bb38b330e85d82735b6
MD5 34e159b8f36099a78f33ab789a3db987
BLAKE2b-256 e12f187db1b774578aad74904e630a833ee2d5aa6c0c766d76cdb903e849cc0e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250422050902-cp313-cp313-win32.whl
Algorithm Hash digest
SHA256 3e8248c54b408c209db3abffbaf1bebe4de7db6f93d69bd5dcee10193b88fa29
MD5 67601c07b671c640f9e74bdab572e595
BLAKE2b-256 0ffde40374c3cf66aeb0089b4a7572eedbb2e5d1bd76a39023ae59c5170dd115

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250422050902-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 58e194fbd829bf825e1ff29ff65101dd0bc009cc6751f53c3fa2fbc2d2749ab8
MD5 565e50c1dfc6498d28a80a2a6f63e17f
BLAKE2b-256 4ef334f43a6442ebdc0659457ebf610d40d271035a8e549c5735d6d6958d918c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250422050902-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8d6f3d011c50486cb68415312f05e8d1482defa9eb366e85f4fb097556bd7dce
MD5 fbf627e63ead3b5c383c3c2a692efde9
BLAKE2b-256 5a51e98c9fe94db869e7ce1fabccb0f424828bbbe98c39e26594ada474f0bf0a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250422050902-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 f4852b7c8317c1fa5d956699cbed7eab06a2fb85451c2e14aea65b172f4ae3a5
MD5 adb2889808d515c2524c5656bfe6c092
BLAKE2b-256 f0b7b5a1017bab69fd623073b9425e97fc4d6b639bfeee21d178e87b96e4ab87

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250422050902-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 2080bc818776d9561fc2591cd6adbfe5301de4b421494a232ec3dc8936970838
MD5 723becebe82da45ed1060046c936bb30
BLAKE2b-256 e4a3285174f6da328a3688f475febefffa47308671c9400bd88bf1d76236dac0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250422050902-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 35abf06d94b1840f8a50c463a8327dbe0d9d838fd2f5c00deca82ee2ab916201
MD5 d5596c87387b153d8fe993b18cb7a15a
BLAKE2b-256 32abdca95a86ed7d6ca25594c93e0612ccb2127cf6df6ce3592a6511a2662ee0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250422050902-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 545f8ae7b66fab8cf39f766124554ffcfccc4354f4d8285215de566bfee24e5e
MD5 e57bb66917fc0b4d1df227488f7b9ab7
BLAKE2b-256 d8224ad4f9182c38a71dcdbe31bf9e771be8fdddac444f2ba5f5313b12586e7f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250422050902-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2c48810255cbe4fca95338cc697c6940208afa9dc9cfe5fb4b4afb83fecfdf78
MD5 0824ea7df69a84d82bd3fe3f20e11e0e
BLAKE2b-256 36119c698ba2ee7bff38bd537bcec6695d83501271707adf3eb4167a900fae25

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250422050902-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 d69a54c3d0732e13e9bd97a31547c6f80c8f0f9fc995ea2be64b6094cd00dd8e
MD5 d6715923835e6234a20e959947e35e65
BLAKE2b-256 f499411812cdbb94634442ead2f8aa831363ff84bade6b4ab0f78f9bd48de8e6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250422050902-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 9bdedccfa533e8542173e750849b846d0e8345824e71d3717b57e927bd82b3c4
MD5 9e8cb1760da29e279eb1bfc0bdd4a910
BLAKE2b-256 a8aac478736d351a55162657fe6de5926e76cb1ec2b5be5214f8f37d5c185826

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250422050902-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 15362f0ad916c9fc196a1484e2db509b2951f670fbe6dd2b490cc2dc7e7cd60c
MD5 23ee044e469d990aa3af85fed5e237bc
BLAKE2b-256 f25e9a41e20286656cace819f944b76c114186f4fd32fd8f4b10fbde57fa6e99

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250422050902-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ea630ccca9656599de7c8062bc488aab1602d0933e098fb4783be5a6b785fd91
MD5 5d283f017003ca92c5eb114c6d4b9b42
BLAKE2b-256 d5981cb2b058fa06d61d5e4413a72ffb72fcbba0dc43ca860872a78fcc1798ef

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250422050902-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d8db2c38e9084b11e218be5abbc1a5a783b8bf690dee7a8f14e5a3cd3ce285e1
MD5 71f487a353835ce09380e82f7147f71c
BLAKE2b-256 5966777dba043d347819c2ee90b89b32e1e73a3f60f8f1da9f10d9fb4e0c8376

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250422050902-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 556ffd65c954e5c4e8c64ef973bd4000d77d242b4f676650e6705a9719164b5b
MD5 9eee7d9efee268b6bda0ef850d87c370
BLAKE2b-256 22c2cb5d3140c3bf3f9fe36377662ea6fe406b41769ef0812989db2d05b0b2a8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250422050902-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 eacf886b2330c1495156d5d091c444528f1c8528793d0557846ba0c2aa73d377
MD5 2ff4562054fe74c1c62d4a3a4d4444a9
BLAKE2b-256 48f294bacfdd9f47ef5ba7b8f461a0cc0a43c475bd9c03a667e2ff7539d9d7fd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250422050902-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 c533081d0cdc8c4358498bb8823d8e1574bef290bd2deaf5aaf64e74acc5bd1f
MD5 1fe014186d380e3de602498f99434d12
BLAKE2b-256 82a40da2de8925ff59d43703e2896e83123d974c7805c43fcccb2e497dae8427

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250422050902-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e1782bb6371489660e6b4134cb9792fa6cb682be5822aa33d72aad5e2418b850
MD5 12e7de46ee32f083b6253d19c533c18b
BLAKE2b-256 df8299c8a148fedbba5e8469d48da3554c7a9f17d6121ec1d5b8712c65bdbc54

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250422050902-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 55b9ae0750a9bd447837123940e7530f849a0be942d3abf391111956ffe2c75f
MD5 b07a6ba72014ba1e2eecb9e8f77c2c4b
BLAKE2b-256 42add332f0cdb1a05c9b511ffd02697de8eb84d708a5ebc4cecfc4b272d49e4c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250422050902-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 21b0988767a4d6d2fc482327949f1a87308c1839b3ea112ac6847e0c9f742c23
MD5 6e7e4811b2e1098283c51d2125ce08d7
BLAKE2b-256 bf8a80f228112f9ee2110052267d871b7f8228ba2c668c1136bd1f5f5c2045d2

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