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

Uploaded CPython 3.13Windows x86-64

typedunits-0.0.1.dev20250122222505-cp313-cp313-win32.whl (680.7 kB view details)

Uploaded CPython 3.13Windows x86

typedunits-0.0.1.dev20250122222505-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.4 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20250122222505-cp313-cp313-macosx_11_0_arm64.whl (747.4 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

typedunits-0.0.1.dev20250122222505-cp313-cp313-macosx_10_13_x86_64.whl (783.8 kB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

typedunits-0.0.1.dev20250122222505-cp312-cp312-win_amd64.whl (721.8 kB view details)

Uploaded CPython 3.12Windows x86-64

typedunits-0.0.1.dev20250122222505-cp312-cp312-win32.whl (681.7 kB view details)

Uploaded CPython 3.12Windows x86

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

Uploaded CPython 3.12macOS 11.0+ ARM64

typedunits-0.0.1.dev20250122222505-cp312-cp312-macosx_10_13_x86_64.whl (791.8 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

typedunits-0.0.1.dev20250122222505-cp311-cp311-win_amd64.whl (749.0 kB view details)

Uploaded CPython 3.11Windows x86-64

typedunits-0.0.1.dev20250122222505-cp311-cp311-win32.whl (714.4 kB view details)

Uploaded CPython 3.11Windows x86

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

Uploaded CPython 3.11macOS 11.0+ ARM64

typedunits-0.0.1.dev20250122222505-cp311-cp311-macosx_10_9_x86_64.whl (825.3 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

typedunits-0.0.1.dev20250122222505-cp310-cp310-win_amd64.whl (745.2 kB view details)

Uploaded CPython 3.10Windows x86-64

typedunits-0.0.1.dev20250122222505-cp310-cp310-win32.whl (683.2 kB view details)

Uploaded CPython 3.10Windows x86

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

Uploaded CPython 3.10macOS 11.0+ ARM64

typedunits-0.0.1.dev20250122222505-cp310-cp310-macosx_10_9_x86_64.whl (791.6 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250122222505-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 fe6a50b22e4cdcda59073c7605b3ed2d2ace415ce7688eae58bbdfb32f5b09ed
MD5 d232ed84ff1f6745113d64aed91892a0
BLAKE2b-256 c181a1fce8c1e0e47e769ba258910e03053e01f781d13975d22afb468fbfb2f4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250122222505-cp313-cp313-win32.whl
Algorithm Hash digest
SHA256 8f73c5793b4263fc818120ed0b2ae720f2baf527f3e4f39e7dc727292ad1c766
MD5 a597c92e486c7a3bbac8342a5fc23e3d
BLAKE2b-256 b009b7761f6bb980d7000af23fdb77ad0a348aa19303aa061cc1c5db9b1e57a7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250122222505-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e5f9755bcae53c55c034f497b3fb9fab07b98c7456d864532eb77df81cc75eaa
MD5 ba5b872eef240062341f99203c2bdd3c
BLAKE2b-256 d598db4a7ea2a502e25cd72c3a8d75b6c660bfce0f67bc02b2c57b4ef4be8391

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250122222505-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 293974f6ec4545debf0a500171e4d2924eb8524de869bac620cf1a9df0561b2b
MD5 afcfa97758da1fa587202418e7e8bb1b
BLAKE2b-256 43dde3d8e9189aa1149f3b534d3da862e92701cc915ab745e1f1100a4f3613f4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250122222505-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 647434591c8e20f0954980fb406204543beef41c7247ce87fca71c815a972f26
MD5 fd47aa8b3861d37c44fa7e5064ed258d
BLAKE2b-256 18a5d0607a0ee22ade1ea3ef4f9a9fd62cfc381929e821114fdf75f63f65be55

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250122222505-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 32af38084d0718e60112f5980352f26271cbb5dfc8daa84bd4d57b8e8fef8396
MD5 f9d9539957aecbe7808dc21c63a00d0f
BLAKE2b-256 f74b2d5a8b89d229b057cc93e7a932680403a330dde16bda25d2e69d527b3af4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250122222505-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 01b1202f23f078846147fe2b77feeca671217ff5e60c3521ef14039d21b239bb
MD5 bf6229129c58292bea349805f24ed323
BLAKE2b-256 de4c4ddf37d2df0696996e0e19a27063169f0b82ca48239ac650ed17e63c8e56

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250122222505-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a5d78da6b61a4413eb76574cfd8aa77d9ac6b36b5d387fb4fcc52e4cfad92a5e
MD5 4ef7f9dfc159845ea78e1dd4d41c4a45
BLAKE2b-256 8b4b338e7de18cd6535b30bfce7551c0e45ff90495c211a1295220cdc90f3388

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250122222505-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 bf443256b79b3a4329480ba555b617c98e28e61d1a0ae26566b4dd52b3601c16
MD5 4e8675e84aa9cc6fcf4cb1ebdd473f96
BLAKE2b-256 b170d8ddf4dfc52494ba877d62106e50cc79f25ae7c8cb1cd1288f70235b777e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250122222505-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 a605ead289232e557b45202aca8180b0829ed9720166d670ef8ed6ea30681194
MD5 f770f6f98d6fb1b68ca142396039f1d7
BLAKE2b-256 be85866dd8ca3ea3c2bc78bd5f0bd96a5af309341e1d3c802ab2e943b7ec0ce9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250122222505-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 0764eabd07435349bcc8f6f2ae3289f2fffd0a70db35cf5ce5c4cabbcc4acb2a
MD5 1f53389231567274c9e292434b2bda3c
BLAKE2b-256 251ddf927b0b1f053dac479ed1af73375b35dbee7204d0d585c6b1d7033e13e0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250122222505-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 6779ce42492ebc217f30663394de2765de7e63fbf60b1324582270f82ee17b8d
MD5 3279aa87696da83e5cbc9299f3f9c48d
BLAKE2b-256 98be60f2fa263ba58495e7650ed2229236257aa5058d55079347e78c5a7ebb9f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250122222505-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c59dea672c938c644a0b650bc4c1078c94c5f9cf720e6182783ce02b0c95862e
MD5 2d57cae9758a06304ce9f27716302fa1
BLAKE2b-256 2af18063912a20c6690f39f608d8d69b4f27547792da14f0397cc18e8c5f7541

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250122222505-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a6f1fb513e82ac2030e5feecc388bf73c6620e1c9e9f7f51a76af0e0828d3a30
MD5 2cc3b0471fcc31fdd4fbb80c2a1d1bbd
BLAKE2b-256 4879ab1b1bcc0dae7749a7ca9aa5029609c125a7bdb5a0019178582d00ce355b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250122222505-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 c7ede82c924760da720f275b86361b855469853b893096042e92c5ad7643830a
MD5 a75296450bcd5edaeb8401fbc80d8053
BLAKE2b-256 47a15dfeafafe998e7470632450dff2b6dd576702de5da70689740f237d17625

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250122222505-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 6ca4a59ff6182d4dc6d56a7eb666f129a196c41e3daeabf69a1f81e927224258
MD5 cbb3d65f88117f8a611629d128992dc8
BLAKE2b-256 e8524cdce63197a9a9c932c74d2c4046d50fcdee400ad3a94f979d8c56645d16

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250122222505-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 e2c08e9702e5fc990cd6581220412688a9b3de2cced272cb3d31b11e0ff7dc5f
MD5 26067bd62bf617aff5e1dc68ff5c2405
BLAKE2b-256 8ef789f7fbefdb4f2e2576c14ba58ff1ca55bde56c3cd77f23effcefd74f0a60

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250122222505-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 4b912d2b7218b97c61304f82a2bcaa9afb200dabeed7e6dc5965e9c9a51d6615
MD5 2ab4c5f193bb9376e8941599ec65063b
BLAKE2b-256 66a956f87e50e6bd8d3d2b1511e0af30566b9c3fe38391a8e31723c0af44bacb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250122222505-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 37fdc6660deab395a45fb7607190b008b6a9cbc856b33025d93ffddbf8620cf9
MD5 2d73834cdeb84bc11acb515f00c94b61
BLAKE2b-256 242fc11454a3907903768273543021ffe9383d18e50d0bd0ed9b839d4417fd08

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250122222505-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 301afc4e77d31afd82b12a83dff0b171fe96fc36aea6a71e2c2519e89b1a76e7
MD5 9d96ccc9ebcd0392a301e7ac666cad04
BLAKE2b-256 531ba462d6a8aabbc238e364c8d05d0b997363ba0f28e270c707209f2e5548c4

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