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

Uploaded CPython 3.13Windows x86-64

typedunits-0.0.1.dev20250509200845-cp313-cp313-win32.whl (681.7 kB view details)

Uploaded CPython 3.13Windows x86

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

Uploaded CPython 3.13macOS 11.0+ ARM64

typedunits-0.0.1.dev20250509200845-cp313-cp313-macosx_10_13_x86_64.whl (795.2 kB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

typedunits-0.0.1.dev20250509200845-cp312-cp312-win_amd64.whl (731.7 kB view details)

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.12Windows x86

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

Uploaded CPython 3.12macOS 11.0+ ARM64

typedunits-0.0.1.dev20250509200845-cp312-cp312-macosx_10_13_x86_64.whl (800.6 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

typedunits-0.0.1.dev20250509200845-cp311-cp311-win_amd64.whl (797.0 kB view details)

Uploaded CPython 3.11Windows x86-64

typedunits-0.0.1.dev20250509200845-cp311-cp311-win32.whl (746.8 kB view details)

Uploaded CPython 3.11Windows x86

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

Uploaded CPython 3.11macOS 11.0+ ARM64

typedunits-0.0.1.dev20250509200845-cp311-cp311-macosx_10_9_x86_64.whl (846.3 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

typedunits-0.0.1.dev20250509200845-cp310-cp310-win_amd64.whl (796.7 kB view details)

Uploaded CPython 3.10Windows x86-64

typedunits-0.0.1.dev20250509200845-cp310-cp310-win32.whl (717.3 kB view details)

Uploaded CPython 3.10Windows x86

typedunits-0.0.1.dev20250509200845-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.6 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20250509200845-cp310-cp310-macosx_11_0_arm64.whl (743.1 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

typedunits-0.0.1.dev20250509200845-cp310-cp310-macosx_10_9_x86_64.whl (808.9 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250509200845-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 e248b0d8059859402da69cea2bbf3adf87e8316ce8fcbe1d2ea75b14a05884b5
MD5 f51df547c033b4c38b18f0afcc7b45b8
BLAKE2b-256 16badadb83771bc63b73ca87525af0a8b0f51ffd2a8ab8775176b5b9db438744

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250509200845-cp313-cp313-win32.whl
Algorithm Hash digest
SHA256 9abdb60a96a72659ebc2d580296cda63430e2797dd5d9e851b37a51d5a2ce5da
MD5 c32edfd5bcbf0b02867c1a39eb6b9e7e
BLAKE2b-256 f81a57deb7c8e24fe05542ff03555ef0d40b4bee3d10cfedc73d91927289f156

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250509200845-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5bb2d6747e5eea1569a80b40f391afe5ba21d98ea4726e5626d5260fa2f198e4
MD5 b4ffefc666828320c35497b4e976d954
BLAKE2b-256 6d206c8226f86d630123634df6ffa379b5b4c21e80d6897237b7c1ef65baad1e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250509200845-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 4f02da50559ed5c223300e144282189fc4c4661e03516a3c5d2ad6ea3bcca6d1
MD5 7fd65bbe77ea4fca0be93dc6ee3084b6
BLAKE2b-256 3dc3593532fcdee00d48c71d678cc759b945f6581cc34a21facf8c14efc79af2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250509200845-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 d896e23fa99600c2aa973a4cdc7d2533d6954f86f678d8aec869778fd586ab4b
MD5 53b04f22a45d9c8264a4b03c6d9f357b
BLAKE2b-256 8b3aa4ab02060f6baf9670e6b1d26238eaaeef340605afd2894aed1745848fbc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250509200845-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 284838ff984ce9dc9a4590de6b31e24118f425d804a97fdcc4732f1e7ee10ba3
MD5 196194770f16675cb8ca0ea2f69834b4
BLAKE2b-256 8bd0629564ebfcd34c0c0ae0bcac0c4d3957f553b3c85f1c5bab2dd2f9e00f38

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250509200845-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 2092d51c76a8b0e4d21a86b4b375910be97b0842ff5cf745f1deeaf34fe43b10
MD5 4dd6e9cbacfd8d97782b0ee9cd35dfb1
BLAKE2b-256 a8fdb523570c249ea595d4408908a284a34cfeacf43c53bbade754274ba2dae6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250509200845-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 26c00049479a304c500ce4e4ba7e0919cb418bfa6af31f867ef65951c3f07c49
MD5 2d678778663e1dc84677086d73cddc53
BLAKE2b-256 06b56061f3697fe47763dd2f87c425e243dce12e2f96a6d3267be7d3d6ba7ffe

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250509200845-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e3d3e9fcba5524e8ef5fe0e35b327d161b9029f8bfde4669eaf7b27a7a38adec
MD5 5eb691874a64ac2019134cd42a85f086
BLAKE2b-256 496f556095bd550d22e4b50b599db3f4ebf8a291d439bba08195c355ee74cdbc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250509200845-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 6c643add105740b238f27b81e1eedb5ca5b7399373a86ee744c4973e5aafc9ce
MD5 52d8a2439eb9d9c24752d948fa2b0203
BLAKE2b-256 8e170dfe00845bc5646b0470862e0b7414b2d79616667ed11408fba778b3a0a1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250509200845-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 eb33b305141aace6ab10bb7746f835c11e911480ca19b36a2825056dec5aa57f
MD5 24fa6c028ce82af8fd57c0913f59c0ce
BLAKE2b-256 d1739d642f96d64fb75d0726cb05ccd32ffe977cc1e8cfc4d5811a2e0c3e48f4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250509200845-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 0dba823ba557e01520f17ccd61523246c1f79a6505e6d6b9fe22c429f0d6853f
MD5 2e1077be31f427412e2566b6147862b0
BLAKE2b-256 0b49bad0747c0f5f1b8ed0851e785efd84104eeac8a8165618b293de0fd43890

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250509200845-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6b80a595b23c4321af1560339bca1a91ea73168a48c79ad70479a109e2db6388
MD5 69cdf6e7066bcca73529db4e529eace3
BLAKE2b-256 70da0fa540c69c83ec8691ddcba7b2b0c72fcf2f47a06be069fbd39b57d59f98

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250509200845-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c2ea288d6330f06f387f244c1deec3ab081cf5af808fd47f3960d30f0360516c
MD5 9b185c8618ddbc3622ddc79b3d10cbe3
BLAKE2b-256 cba62d3fde3e53cf034d322f8ac349dfe5f38e7045487324c036d6955fac1383

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250509200845-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 76515ea205c104ae79930613283736579b229dd3535ffc6157d2353459cc09fd
MD5 f3a68068862d4bb91bd20712d39cd17f
BLAKE2b-256 c6114ebf4302e27edc211b08d069073e24fecfec9ed19a675ebb41cbaffe231e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250509200845-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 e56701ce895e5c40c7dc8af294f966bf3f05270b1fcfc953c72cdd81407ee025
MD5 0d1c7ce61a9649170c8193bdf2bcb89d
BLAKE2b-256 fa14b1b1fc39217b1c1c9b02f626d712889d2b60277698e5d603ffb9ca9d2802

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250509200845-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 6b5e1b29e07aaa334303144d5657698e543354da468d0379b5e3319b4a391521
MD5 64e98d62a3156d8b4bc52357850adb6f
BLAKE2b-256 ce172bb01490e05fde4d9c52ed581608cdd4b7b10b5bb4b228f974669af776ba

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250509200845-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 2691531e60821528e13ff45109e144b690dd1b7bad06bccfd2e6bb4248c49bc2
MD5 d17eb2a695ecd21e2d6822e77c2c6361
BLAKE2b-256 5daad42dc19c9b7c7f59fb6d6e3e751b2e43c0c116f24ddc35d442ab06435901

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250509200845-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 6e108974f76fda3785ce159e0070c5a630da0b65a9fdd0bd15b4117ca8e708e8
MD5 8f29d249c07bc98f0df0a3185b95e283
BLAKE2b-256 bbc2c81546d79d87d67d389ec8f9966e01348b804001722bc887d093764d079d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250509200845-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 51b7df21463c6803549ad022885da11799f5688c676dae1b5008ffe3f42e3201
MD5 418b684453c608d1c1ebe69208c89344
BLAKE2b-256 db091455e5afae871372729dfbed9b89ad04bdf22b71f18e74606e1b3010c4d9

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