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

Uploaded CPython 3.13Windows x86-64

typedunits-0.0.1.dev20250422140511-cp313-cp313-win32.whl (741.4 kB view details)

Uploaded CPython 3.13Windows x86

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

Uploaded CPython 3.13macOS 11.0+ ARM64

typedunits-0.0.1.dev20250422140511-cp313-cp313-macosx_10_13_x86_64.whl (854.7 kB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

typedunits-0.0.1.dev20250422140511-cp312-cp312-win_amd64.whl (784.4 kB view details)

Uploaded CPython 3.12Windows x86-64

typedunits-0.0.1.dev20250422140511-cp312-cp312-win32.whl (742.7 kB view details)

Uploaded CPython 3.12Windows x86

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

Uploaded CPython 3.12macOS 11.0+ ARM64

typedunits-0.0.1.dev20250422140511-cp312-cp312-macosx_10_13_x86_64.whl (862.4 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

typedunits-0.0.1.dev20250422140511-cp311-cp311-win_amd64.whl (813.0 kB view details)

Uploaded CPython 3.11Windows x86-64

typedunits-0.0.1.dev20250422140511-cp311-cp311-win32.whl (778.1 kB view details)

Uploaded CPython 3.11Windows x86

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

Uploaded CPython 3.11macOS 11.0+ ARM64

typedunits-0.0.1.dev20250422140511-cp311-cp311-macosx_10_9_x86_64.whl (898.6 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

typedunits-0.0.1.dev20250422140511-cp310-cp310-win_amd64.whl (809.9 kB view details)

Uploaded CPython 3.10Windows x86-64

typedunits-0.0.1.dev20250422140511-cp310-cp310-win32.whl (745.1 kB view details)

Uploaded CPython 3.10Windows x86

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

Uploaded CPython 3.10macOS 11.0+ ARM64

typedunits-0.0.1.dev20250422140511-cp310-cp310-macosx_10_9_x86_64.whl (861.8 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250422140511-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 29801c78b4b4c8d80a459905f3c283a4d3b49f1079abf875f30f9d6eb8dd6078
MD5 a24df0eac03d394367b11966f16e3605
BLAKE2b-256 bfd3cc716c83e065ded46c7beebb6858d8de9dea89539333f0df21b71fd70109

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250422140511-cp313-cp313-win32.whl
Algorithm Hash digest
SHA256 03811af157b9cd062c54c840183aa72298d18535fa762d39edde4a9d446df34a
MD5 aa4a2f8f3148e8cfe0be1f363bcf1d15
BLAKE2b-256 35ae616968ece2723542d96d10e311ede7148bec6c19c8b3c2f3d9cc295da161

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250422140511-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b92782315e52e900854d984a693130dae4e1a44083fb9ad689453fb22bc3ae4d
MD5 215bcc67a35c574c477e340bc2bd0922
BLAKE2b-256 4ea3c8959ed4b8124fa56431f8c3e523d22c4adbc21886585fc910b11a767289

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250422140511-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b20fb4293f40b6ed541f5dc66d8be2ad14d3fd8363e6a0f436ec324a55f2f9fe
MD5 63033daa9b7b6bc7552bad1f2a2f679e
BLAKE2b-256 202715c1ab57cbe9e49a6620a1a9dbd062d59a9bb448afe332f4163bc6ad9fcb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250422140511-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 e5b8b6775012ce7628ad523a9043c51c4ab4ed8c91fe8f2e8d6ed5bb52fb6a9d
MD5 37a944bc6cc33d050fe70b0875098312
BLAKE2b-256 a7afc09de5f0f3ba31f8ce3b65f3e065913781c80d6acf45cade79137611d8b5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250422140511-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 e6170153064297500002d4c44d52fbed7d16c7b27c8f43f86573f8f1472c5a13
MD5 44e7417aba4f8f020d79de4530d8a0ef
BLAKE2b-256 abb379e82d30bc01ec5bcb1976b86ea5552a7d3294936aaf441bb8ce352ff7ec

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250422140511-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 9e26a72a8b3a5ba4bc051f4e29a7891522d455287d136a06fe34a85198fcf176
MD5 f299abf291fefd0878fc9c961fef2548
BLAKE2b-256 4db3cca75d9a421c4c4ec78954eadf009dc9a40ae027aa8afdf6ad91bf2234b5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250422140511-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 48eff2f6a738e46f927a496b5443d720ca55a8896e5d13ca1e20063c10af9c14
MD5 4dec2754b0eea2bffc9f4cf94314748e
BLAKE2b-256 725ccb4793adcc9a2df23198953e3ecf8f71d18c5947e7e466b663f7e4d0ada7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250422140511-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1b9edd07f75e2ebd42adfa4ad1de7a83f6d935cfebf8cce3a7d55a2dede27be6
MD5 cab3fb80fcf3b90601a3c2a0b355e7f4
BLAKE2b-256 271100d79b442568d1266e272b30ab36a3214e11cff3c548070f620fb50fb992

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250422140511-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 316dcd72a9025655806d0dcd769574555016ac5579a0798c3fc7575a9e0602ac
MD5 09f50d812a994654bcbfcc27aad38f40
BLAKE2b-256 00b312d3f685fb9ffe179f73481f49d05d9c8946d7ae9fd3b588d1579f69aae5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250422140511-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 f5e8f30990da6d0c448493c8753cb9c50c19192b8b2789aef984472b966407cd
MD5 60b4346a3d0e49c8f629bc37496d7a2f
BLAKE2b-256 931e7bcfb9bf3a86ea9442bfc728d1fa221da4aa1fd5c538ef1f1820cd91a73a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250422140511-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 ee7159bfbf3847b1ab2abdad3b83b7ff2d55b8e4d9482d3c72680448e2de5eb8
MD5 e64969c7adbb225ffe92e9321a6bb713
BLAKE2b-256 b2df2c153922866b6d5dd59f451d726d3d235023cd42eee2e28f8a7563747885

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250422140511-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 55133b1268398a36798676b7136ceb67e955a7a4e1872f3368ec13bba50bf147
MD5 d3852f1d46b846313636c1ed85e1dd44
BLAKE2b-256 315eaa136a904c81e31bdd2af481eb740a14c4483fa4fbef01c89dcc62da6517

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250422140511-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 bee155c8240882ce61d01da02f96693b5663f675d08b005fe63599d6b9610e9c
MD5 55f2d2788285425ae86db393f9a24e74
BLAKE2b-256 887d8d670c67ace2da4d75c727433f3b98aafa1ac6f2c765ec91b2233327d50e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250422140511-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 716fa947f8f03a68a19fbc418aef3d5ea66ac59fc074a0d85eeecd77f1e545d5
MD5 9955ba12285b0b510e8766f69b4f956f
BLAKE2b-256 7867e3402191b7fb8408470a45e143a3758eed312d2b8e6b6983c91cc336a76d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250422140511-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 1521698a0bcdbfdebddae2d10516e8fcbddedfa4ab42e1d7085d3e8e765aef47
MD5 367fa370f47dbe84832b116b23d51277
BLAKE2b-256 7e31dc93cf7831d4d81e3c7bed845b9a3e0f7313472d1a8ac1773ce3219ac2c0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250422140511-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 3730c78e35bf5d23a5abec1cb8d70ce75f36781db150ad649b65450274bf2f1e
MD5 cba5ef0e09e6f3b0a614159eae9cc0ea
BLAKE2b-256 f32df74d7abe5e5792ea1c93fcd8eae74ec462e09d03f95b418ed15497cf7455

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250422140511-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 4ce637557eb40e06a290612fe70ab11fc026bc0b9538ed505f2c6537170f07bf
MD5 2cb54656c805076e50f37f1621ca70eb
BLAKE2b-256 885d9ace07667a6f54811247ed77eae9d60fd2baa4e41e4c60a259ae0c635af1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250422140511-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 4dd6592ff24b11f020300b1b983ae25addc7a13ed400549e4e67733d53911efd
MD5 c822e14c7ad5d3245ebae346fe42c057
BLAKE2b-256 246964cf06b58ec65087d55913a0016729ca4d3ffdda158ce521fca8f90c07ed

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250422140511-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 1ede7254e45d64512802dd3b980f50973dc2b84e9d659d9c2161f7d38cee9c7b
MD5 2b21dbfd49e79d4f28c634f3137fb992
BLAKE2b-256 1ce81ac2cfcf2e426e972b911571c29ac0e39011487a9cd00fa5e34724dc3834

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