Skip to main content

No project description provided

Project description

TUnits - Fast Python Units

GCB CI status:
GCB Build 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

  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 .
    

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 .
    
  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


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

typedunits-0.0.1.dev20241018230039-cp312-cp312-win_amd64.whl (705.9 kB view details)

Uploaded CPython 3.12 Windows x86-64

typedunits-0.0.1.dev20241018230039-cp312-cp312-win32.whl (665.8 kB view details)

Uploaded CPython 3.12 Windows x86

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

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20241018230039-cp312-cp312-macosx_11_0_arm64.whl (739.5 kB view details)

Uploaded CPython 3.12 macOS 11.0+ ARM64

typedunits-0.0.1.dev20241018230039-cp312-cp312-macosx_10_13_x86_64.whl (771.1 kB view details)

Uploaded CPython 3.12 macOS 10.13+ x86-64

typedunits-0.0.1.dev20241018230039-cp311-cp311-win_amd64.whl (733.3 kB view details)

Uploaded CPython 3.11 Windows x86-64

typedunits-0.0.1.dev20241018230039-cp311-cp311-win32.whl (698.9 kB view details)

Uploaded CPython 3.11 Windows x86

typedunits-0.0.1.dev20241018230039-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.5 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20241018230039-cp311-cp311-macosx_11_0_arm64.whl (749.3 kB view details)

Uploaded CPython 3.11 macOS 11.0+ ARM64

typedunits-0.0.1.dev20241018230039-cp311-cp311-macosx_10_9_x86_64.whl (807.5 kB view details)

Uploaded CPython 3.11 macOS 10.9+ x86-64

typedunits-0.0.1.dev20241018230039-cp310-cp310-win_amd64.whl (729.2 kB view details)

Uploaded CPython 3.10 Windows x86-64

typedunits-0.0.1.dev20241018230039-cp310-cp310-win32.whl (667.5 kB view details)

Uploaded CPython 3.10 Windows x86

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

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20241018230039-cp310-cp310-macosx_11_0_arm64.whl (717.0 kB view details)

Uploaded CPython 3.10 macOS 11.0+ ARM64

typedunits-0.0.1.dev20241018230039-cp310-cp310-macosx_10_9_x86_64.whl (775.1 kB view details)

Uploaded CPython 3.10 macOS 10.9+ x86-64

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241018230039-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 483d43375900b3e55132a50e9b3233d9a301324c73c3819af1c99418ccf6eed4
MD5 7b9015aeb1eb3df5ea58a75f606da8d0
BLAKE2b-256 9ae2bc54410025b43cff4a223446a67f018d03c727c4de28342910010616c0cc

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241018230039-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 7ceab6634aa483e4eb4895e6fa3efcc427e08cd5f4978cc393744905641076a1
MD5 8c1adf8c76759503e69dbf323654f29f
BLAKE2b-256 18614e98cbf5fa96317ae294ec85bbc4f39a6d1bbd32995044867ef74de7f866

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241018230039-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c096e8a1520e079a4133c2b3fce215f3a87b1b99e11ce72dfe5b3e66b5fda994
MD5 9c7b3ee4d2e65049d252cc6c4831b1b1
BLAKE2b-256 7688c2f576b8482472ca2954e4154dbd0f7596980893033e89228679f53a3884

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241018230039-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b92652572439ca16cce103caf90dce245ca37d08e240b6c1b97405f788eb9a29
MD5 d4f6a2aa8a6d4a9e0d8c476b8a9fedc8
BLAKE2b-256 9255e4cff33a9528ea6ed0835db0e334c627cf2282333a905031bd54f5b25a93

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241018230039-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 14acf0ada05a9d778618942108484913f4569192c02f89f4a0e393863a582685
MD5 970905c92279abc2869739fc39354335
BLAKE2b-256 d63453cc5346ac0e1269d3f1c276aa06f750a1969828bdd4e4e25dfee0933b07

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241018230039-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 96e315bba7cf53f6dce7f28c3de7daad6bad69137ca601b42617c697ad2a8e24
MD5 1ab76ff4a07cfee82b85f39ad8a45b79
BLAKE2b-256 ca6d7e7e8092160bd609d7a0b34d6f596baeef4475083aa7aa2276e89873158e

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241018230039-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 c638569c83735400c8b9972e6770d54a5f757ccf69fa410501d778ec4391b79b
MD5 4c51e2742567917f07379d4ded357017
BLAKE2b-256 22c489ab885d2b7801cd47ae535290b899b91cd76586c4433f41a189c7e29953

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241018230039-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 eaca7bb6a1b7795a59cc241175f714202f1edae2480157165692a8530efaba99
MD5 784b6b968cfff9a74c268b638fd4be84
BLAKE2b-256 92808699aa020c0c946679fe9382f9c2b9c528ffdd6eb84791dcdbaae3f82569

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241018230039-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f852b71fbc6c43197528a459864e9bd164f3dc2678e937e09543192c013f792e
MD5 fc932a3ec18ae49bfd2e5feb5065289c
BLAKE2b-256 54959e7a1c1409ccb0b7b707c4ad3181c3349575b752b73b9cfa6dcf79f70688

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241018230039-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 7836b2bddb5ad628ca71aecd77b317ace5bc1c9eab7df3f01cf4167a41fad5c3
MD5 bb606499be9277eadff46340672f0057
BLAKE2b-256 45825121c05079d92288ff3a5fb77f8af533d23a048ea6cc77a236318cd413f7

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241018230039-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 53ada8f188e7d1d8eca56cdae24c80e8e568e40d0d61bfc9074861d54c72ea89
MD5 2c94d704d0838eb2e6610e516b974311
BLAKE2b-256 d173a3e2c14598ca84413b7c89f6df7b95dd89a9a97c137f180dbba957a0b034

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241018230039-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 3abc3953f0e9a743833802713796092357b5fa48fc160248a11009ab0480383f
MD5 4a524e8d89467db0a037be96dc72efc3
BLAKE2b-256 54d65a92a3511df78243128514460a06c6bab9a260acac632f24bb4e8b4af0e0

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241018230039-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 be705706d7bf2f0e237c71311bcc904245110fcd989290c7c52a13c54b48b3d0
MD5 f9fd0ad31639ef96740119c5b8eeb15d
BLAKE2b-256 6e123a6230d4e1247e0358b5c2b7d2ec32a3c4c121c7e95f830407c3309d0296

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241018230039-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 fcd52952bfbee1d74c47d119ba3c4b195eeae281d28f8d8db0a7768c79944058
MD5 87ded7f9480853fb0ec8b9d107a8801e
BLAKE2b-256 16a13726dee346210a4b9a7233343a18a2ea6791d61500d81d378aa4b9d0b2e2

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241018230039-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 f922d2191093711fdf260cef61dd01d08e132dfd4b4be61879a6cecd221290ae
MD5 856b21fea3a0363f4322e93a6a0ed66e
BLAKE2b-256 5bca2347a46c238a6de63ceaf16993597f05cb37a1fd13cb6c61b6d5d5400461

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page