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


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

Uploaded CPython 3.12 Windows x86-64

typedunits-0.0.1.dev20241112205618-cp312-cp312-win32.whl (672.3 kB view details)

Uploaded CPython 3.12 Windows x86

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

Uploaded CPython 3.12 macOS 11.0+ ARM64

typedunits-0.0.1.dev20241112205618-cp312-cp312-macosx_10_13_x86_64.whl (778.0 kB view details)

Uploaded CPython 3.12 macOS 10.13+ x86-64

typedunits-0.0.1.dev20241112205618-cp311-cp311-win_amd64.whl (739.7 kB view details)

Uploaded CPython 3.11 Windows x86-64

typedunits-0.0.1.dev20241112205618-cp311-cp311-win32.whl (704.9 kB view details)

Uploaded CPython 3.11 Windows x86

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

Uploaded CPython 3.11 macOS 11.0+ ARM64

typedunits-0.0.1.dev20241112205618-cp311-cp311-macosx_10_9_x86_64.whl (814.0 kB view details)

Uploaded CPython 3.11 macOS 10.9+ x86-64

typedunits-0.0.1.dev20241112205618-cp310-cp310-win_amd64.whl (735.6 kB view details)

Uploaded CPython 3.10 Windows x86-64

typedunits-0.0.1.dev20241112205618-cp310-cp310-win32.whl (673.7 kB view details)

Uploaded CPython 3.10 Windows x86

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

Uploaded CPython 3.10 macOS 11.0+ ARM64

typedunits-0.0.1.dev20241112205618-cp310-cp310-macosx_10_9_x86_64.whl (781.6 kB view details)

Uploaded CPython 3.10 macOS 10.9+ x86-64

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241112205618-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 4c163f5b57315c5f5f3f4f5b80b55c8f9db594d9a46d66bf97b4ad491128a66e
MD5 34b625e4e3098316ba9fc64f9105d955
BLAKE2b-256 7fe0b9954c0b648ca1de1c848eade877559156fe9e72d70ec914ce132efcc173

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241112205618-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 4cc90163a937004a388b44b586696600828f912d29251217fb5ac5c6689edc58
MD5 77fe017ff57c5066717b02e1b37e09f3
BLAKE2b-256 675ca6fd3633a5b1064d227e3d9e37937947048ba2d83542147aa9318e86d51c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241112205618-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8da3a27b3b55d707b7f6347f379162af928b6888a35b6ef260332ab7a5962b70
MD5 d8cd396f7f47f6ba45d17c2dc4567799
BLAKE2b-256 ffc7cf28aefa2174e24995f035e057211fe850f0720a401c180cbc3236ba532d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241112205618-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 78dfa108d1cd3408fc49f00032c9dce92c2b1df5348dd4224166ef568ee69ee4
MD5 40014e04636186bdb9ef4814a0836776
BLAKE2b-256 24c367e06b9e8574065a48f7eef6d400879c15095a12393cbbc753205c526d93

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241112205618-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 a6262e13b6fbf2a30addd149cf859173974238f60dac4c94b13bf39866479c6a
MD5 e6416743d0c8242abb28c623922b41ab
BLAKE2b-256 1fd7348f56dc4b4a02e36b883233858dd1f7af56895da14c2deed575b0be9a48

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241112205618-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 5a500bde6f7da537ecb65030462882045d18605596a9e608726ec83fb9242c2b
MD5 e6258d3456a8c17d21320e4c38da25b9
BLAKE2b-256 b25af64df98047d891d23fbd06359fbf2a2130c987f0e4c96a97b7442f198295

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241112205618-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 751b0bfe1ec121495192de47fa5d9982ff6a879091b06923c097d443d2d3ad55
MD5 b682ef8edd15f53a2919425aecfebb3a
BLAKE2b-256 76e673ac5da1afdabcfa0f8afa362046ace4cb4638024581b60e360419f0b20a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241112205618-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c2a34e59027d68e8dcbdeb80f5156f4f887d698d4da19fb6a49ea47974c5852b
MD5 a2323374686754bb63e07a423fe3d014
BLAKE2b-256 bec7b2247a6a3d669a70784ab8b421787ccca12fddfdabe4f134f913a987f161

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241112205618-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2047b11abc04093214a17b3a284e35dce76b39c03932d0b095cbe2cfef8b582c
MD5 24077b3c34c33ff8ad1bea35fde184a4
BLAKE2b-256 9e3f8a5a633aad5d5a035a8df8c1f53561926cd1488e795184848dbce01cc744

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241112205618-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 65662c78e78f6467f5235f12e35b330ff597828114d552eb8e6174c0092caaab
MD5 904b44cd0f72a23a0c5c5e4356f2ac9c
BLAKE2b-256 5fa91dc96e3054cb2a5c65f86694d8ef69753c523ece6bd924490362d0bd82df

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241112205618-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 e3885fd3263b2036817b33df06973d39c579a3dec1421d26d0c9b5bc510c014d
MD5 43240fd5de400f6f1e949c3420dbffa8
BLAKE2b-256 1f773b7fc5f3c2267bf2c89a28a861d546551bb61af7d00d6ec684c0901fee62

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241112205618-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 128699558a6faa985730b4c6b5d5fe1b7b5ef3fe5dd5831bd7a996c0c68954d1
MD5 deb08ec20a850e42c34b9944d71dcef9
BLAKE2b-256 d6fda6a75a439ed6ee9ecf4d69403adeeee4a5dd51947a10e5c6afb755b93afc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241112205618-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 89d858954003ac548a18d6123aa016a00b90226301d938c11471355b26e2902c
MD5 b40458c238f38469579c5fd37aaa9cb0
BLAKE2b-256 4be1b032a2489885a47abff2777d38c7cacecbaf8784704b8eabfe3a11f94662

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241112205618-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 486c59c53a0948cff9661ec82b37d3a598a9b1bec51cb7d1b1aa7a213076bf65
MD5 19b8b2f8b9183c56b4ae3cb115ccb00d
BLAKE2b-256 25e30383ca08066a44fd9c4c6a4a8f983c3f122b2ba1edab2843c21be24e1ed2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241112205618-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 76515c61571d388674abe3bf60f605b4a9db4d3ed5546b03deeb1ebfb570e411
MD5 e1724b11daf70e92bd00b68cc29387c3
BLAKE2b-256 2aa7b972e83eb7aab2f7891dd841bfcc9426d491cda59feb1f3b42e361684d5a

See more details on using hashes here.

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