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.dev20241206012251-cp312-cp312-win_amd64.whl (722.0 kB view details)

Uploaded CPython 3.12Windows x86-64

typedunits-0.0.1.dev20241206012251-cp312-cp312-win32.whl (681.8 kB view details)

Uploaded CPython 3.12Windows x86

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

Uploaded CPython 3.12macOS 11.0+ ARM64

typedunits-0.0.1.dev20241206012251-cp312-cp312-macosx_10_13_x86_64.whl (791.9 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

typedunits-0.0.1.dev20241206012251-cp311-cp311-win_amd64.whl (749.2 kB view details)

Uploaded CPython 3.11Windows x86-64

typedunits-0.0.1.dev20241206012251-cp311-cp311-win32.whl (714.7 kB view details)

Uploaded CPython 3.11Windows x86

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

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20241206012251-cp311-cp311-macosx_11_0_arm64.whl (767.0 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

typedunits-0.0.1.dev20241206012251-cp311-cp311-macosx_10_9_x86_64.whl (825.4 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

typedunits-0.0.1.dev20241206012251-cp310-cp310-win_amd64.whl (745.5 kB view details)

Uploaded CPython 3.10Windows x86-64

typedunits-0.0.1.dev20241206012251-cp310-cp310-win32.whl (683.5 kB view details)

Uploaded CPython 3.10Windows x86

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

Uploaded CPython 3.10macOS 11.0+ ARM64

typedunits-0.0.1.dev20241206012251-cp310-cp310-macosx_10_9_x86_64.whl (791.8 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241206012251-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 1a2dbdbc1c4ea598ae3267f894de4fff7d95a8cb955274ac720f0c52c83def18
MD5 f0e28ba73114e75297d2fbe1c3deef8a
BLAKE2b-256 3b318c97f8a30488f2a911425c751ad5ff52d734f1162c7c7e3d6b52c4789896

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241206012251-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 ab9bf41ed8321efcbf09e2184261fc7567ccc4d179d0a022a1f3610f70bc29f0
MD5 462de625f688c4616cb7f6106f1d2209
BLAKE2b-256 f2b8bad3e6214a3ff9850f7e14ebbb316d76824d9163ab2ef3366502c2e3b1ea

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241206012251-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a12a3700805caacee1b21956936efab05a9c50085f3276d0508407d082ceeb12
MD5 ff711837fdec1d0a8c7aa8c38d8a3a69
BLAKE2b-256 eedddbe2ec39002e9a256a46ba67f11cfb35f36b7170edf73d183b32d04ba44b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241206012251-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 52066f0de200e980e4744589e0320d45b762af76bdd0923349b2f227f0512b0c
MD5 374b685943b2245719a830d9b1fae17f
BLAKE2b-256 300dffc1593cf472b4191763b8a1cf8c091afce3d98613c39510bf3697492742

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241206012251-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 92e20f3da49c64ec6f55def4299bcc28edf8282787bb2625b6dbec15768eff1c
MD5 6bf8347a4e6de0c2d0dfa5e4b090709d
BLAKE2b-256 aed3aacaa4611c359af533df84ccf29001d74b93f2188b430cadc7dbf06e308e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241206012251-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 dfd0ebbb1a7aa0bb3218d5310a25ea9d918850cd78c6e07b5079f8a169c95ecc
MD5 e0d71c00213d6b57e8dfbbae9d170957
BLAKE2b-256 1e7a798db17c5b56d03e4f2d734dbe7d913a166e6890793f63d22bc384b5f5ca

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241206012251-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 e5132b869f52dace77c4e712ddc8ea681de30a05e20b2bf1266d2f53cc42b253
MD5 b635c10c245f19d3c38b024050875155
BLAKE2b-256 f4712e3d1e304d32e2f59a219a72ff8e69e77521a18c889e75925e2210502a39

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241206012251-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c71f61cee54b3ac60162e05cfd9fa4bd5e659d4c1f65214f1002da8040ef40f2
MD5 827de946a590786c0871d1993ebbc544
BLAKE2b-256 bf5eac61576f75efb3e31a9034cbfc4f1b3b0c7e399550e057fe44ff5c9b14f9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241206012251-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1c8e15132940ba6d5dfb6042bb4cc91802580991f1fe0839ba6c158a24fadb6d
MD5 084301725179fa28cc18337520622139
BLAKE2b-256 294a46ac7169ae61b8d41451aa585a889c56a318decf3cf326323e6256d307a3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241206012251-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 8678dfa42d44ee3b5828014aad4358aa7ad92ea080f88ee99c21bb927efa5031
MD5 9259ddbfcfe11bef5247fb6deae33998
BLAKE2b-256 5c197e6dad934d75b3567e63b85928a23c3f8e451d4f1588f1ecfb5a55ffb7a3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241206012251-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 a6836bc1945950a29c80555a2f6a6db39f9591b40f3f94857209f6e5cbce1654
MD5 af9a29886b83ec82dc59a1487f4d1c33
BLAKE2b-256 2c3240cb2ae5bf7ccedc75371d30aa9d19621644deedcb6cd11d39859a1474b5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241206012251-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 f3b966ebab57e83b67743d0b25bb9b1df36c80b137f7b5696769ea0e1318acaf
MD5 5fb237417cd968fc311142a8fd286d4c
BLAKE2b-256 e5644484356ce3be9b0dd9915ca9593e61dd0a2da4b70288f290fcd03b994a2c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241206012251-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7c40891a9e020c86ab867b5ca4ad2c8791fbb3a70bc038deb5ab38e03f47c2d0
MD5 a0d17b4542d7d5406df1d7f3a5f95bc2
BLAKE2b-256 c6675ca6189f0e78e377bf775c1bbe8377cbfa1607308aa76ec409d64ff7dbb4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241206012251-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 55b17cb2bd1b95e267cfdf94ba8f482f5d5f121486e5ac0ade1af0fd67f8a521
MD5 8f8399164af3894536e376dc665b702e
BLAKE2b-256 4f8153affa908f416674935ffa51c588b04084820b4f0542bce1045d8fc66668

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241206012251-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 9fae77f37334e75b81bd0bca1df926e8aadbe78bd5ea7cf65fefd0d66398964d
MD5 cba44e1a4ac20fadb1c53c1c3ec94bfc
BLAKE2b-256 d56943aefd489e30453e9bcf3d3ee6079394717d60b8556af7a8f692f6f54751

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