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

Uploaded CPython 3.12 Windows x86-64

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

Uploaded CPython 3.12 Windows x86

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

Uploaded CPython 3.12 macOS 11.0+ ARM64

typedunits-0.0.1.dev20241018223705-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.dev20241018223705-cp311-cp311-win_amd64.whl (733.3 kB view details)

Uploaded CPython 3.11 Windows x86-64

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

Uploaded CPython 3.11 Windows x86

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

Uploaded CPython 3.11 macOS 11.0+ ARM64

typedunits-0.0.1.dev20241018223705-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.dev20241018223705-cp310-cp310-win_amd64.whl (729.2 kB view details)

Uploaded CPython 3.10 Windows x86-64

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

Uploaded CPython 3.10 Windows x86

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

Uploaded CPython 3.10 macOS 11.0+ ARM64

typedunits-0.0.1.dev20241018223705-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.dev20241018223705-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241018223705-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 0e1fd490a8aacfe0957774ddef2cb9fca5a9483807c3071b2d74083473b7edb7
MD5 40f549ff1e03e173b5c4468b8e2f394b
BLAKE2b-256 1c7ca6157d469c070dc51e2c58cae73f84a46f9d8eb000dff97ab9b0e1254fc3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241018223705-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 5385828ef02be96f1d738ea90675e381d0739b88e3e48c7cc811f1924e774961
MD5 88984c8faa752c189a78b14a533e4a85
BLAKE2b-256 ed61051b1d6c868d336972f0b32486c953d24409868e4491aafd74c7b8443721

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241018223705-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a3d1b1f9b5d8f7abb0dc363a0ebe0f94738646d6f5b7a56437e47e72b7687dae
MD5 16abcacea357224278f3f44489583779
BLAKE2b-256 7e41eacf87588b529a7e2ffbea2568a420f328e1f563a3039c2b9e4238568d3f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241018223705-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d9d84b3b29a0ec694078219c7bf44e7bd6f155820555d2f87e146212614bf6dc
MD5 8550a7d7c929f52ec5e02bf4a6e0fcc3
BLAKE2b-256 dc2dc469751999705e7055ad7c3f4caf512d7a8054186f2396fac059d94785a8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241018223705-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 e5e784d502d2a750df932532b7015d62bc876905f6c4f8286c24019f88c7fcf0
MD5 6321d3c02d55d0752bd96aaf45e12537
BLAKE2b-256 c26ce70aaa7d059ff3694eede7765152b3fb03d8e646ba30a1b3f156974b913d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241018223705-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 a040e6beb290c5207d45400e4b24d605bc05966de93776d75e0a4eb5be620319
MD5 c922db51d5f4b340d5517f33db7a62ed
BLAKE2b-256 98577bf0d27f20ad2f851666a8e6795eadeaf8c484adcd81dbfdaf4474864cd2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241018223705-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 b0800b9ab4cebf75e467cf60411d55c753b1e7eff47b50345498a670fef7cedc
MD5 954fbce7fb4c213887459a727502ef15
BLAKE2b-256 fac6ffde0c23d1d37b0bae32ea2c9a34206d6b1472969129ee00aba4517ecaa8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241018223705-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7daa688b478a80a7b2917d190511317617f8e8a8a94ad884f33a1b4bf57f43e8
MD5 1c095ee07eb0f44ca110f35f05ae08c5
BLAKE2b-256 6be252927ed88cd548a28eaaef34fbbe9b15e5c32e7eb44e7a850d4d54e615c3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241018223705-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e72fdffed56bd1330216271441c38632cf94926866ac832a764f0d10c27a407e
MD5 c23544093a5c8896fcc2667af1a5867b
BLAKE2b-256 f41c54bc0448a638a55dcb234b712c07c237b86a0dc95b6ee4f90a9e99153599

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241018223705-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 4816a0709e879e349f94927b36508ddb2e07434ee9347cde0018f92f3668b2df
MD5 86d712caacfdc0e7191f767b27da6dc0
BLAKE2b-256 2e19e130d29ebaec2657a39be41d51e228a79b08e6bce7c498146d5f8d204943

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241018223705-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 b45bf2f72c5e989d50237c45bcc304088c1b4a5cef4b196eb1e5ca591e224aef
MD5 ee55feff71326b64354b48ddb0204dc2
BLAKE2b-256 6775466338c08130e8e66fc2afc11dbc8f377e0438e2f3a44d62fe543a0e7bb7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241018223705-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 a60a29119443a5c4d96c954a6e630ee215f18ce5827d0dcfca0457cd0b60db78
MD5 ca851d5d0f71238c3688248bd7360fbf
BLAKE2b-256 5f9a9622b01adf9b2e704930c5526e993da37f7f016ae46be9cc968488f6707f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241018223705-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 17251c981149285937db110ed043a51efc926c23261e56c4dea1401d04b14cdd
MD5 ba9c6e83388360135aa1797653a8ecc4
BLAKE2b-256 fa31ce0fa5047e7396f166009d1a09977852aafe36a985630a59ae64d9369a8a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241018223705-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 3711c8c7c27ea0567d38b368f19aea26d9896449d35fe80b74d58014c79fccc5
MD5 d7ad4d13f872e22052081f9625b32b99
BLAKE2b-256 59fdae0a5b39edc35078593a3dafdd40c24ca694edae28dd0291f97371680132

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241018223705-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 8c137ab0d8d5225ba167a0f0d61968d80b0224ab2cbd62bf443ce964e85e1e35
MD5 7d9c7acf577ca566e5be40f86fe3012f
BLAKE2b-256 3568ed0d59be738f271cd09d19f108242639d7ae90b41801dea277c2fe149562

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