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

Uploaded CPython 3.13Windows x86-64

typedunits-0.0.1.dev20250305205907-cp313-cp313-win32.whl (725.6 kB view details)

Uploaded CPython 3.13Windows x86

typedunits-0.0.1.dev20250305205907-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.6 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20250305205907-cp313-cp313-macosx_11_0_arm64.whl (795.6 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

typedunits-0.0.1.dev20250305205907-cp313-cp313-macosx_10_13_x86_64.whl (836.2 kB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

typedunits-0.0.1.dev20250305205907-cp312-cp312-win_amd64.whl (769.6 kB view details)

Uploaded CPython 3.12Windows x86-64

typedunits-0.0.1.dev20250305205907-cp312-cp312-win32.whl (726.7 kB view details)

Uploaded CPython 3.12Windows x86

typedunits-0.0.1.dev20250305205907-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.6 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20250305205907-cp312-cp312-macosx_11_0_arm64.whl (804.6 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

typedunits-0.0.1.dev20250305205907-cp312-cp312-macosx_10_13_x86_64.whl (842.4 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

typedunits-0.0.1.dev20250305205907-cp311-cp311-win_amd64.whl (797.0 kB view details)

Uploaded CPython 3.11Windows x86-64

typedunits-0.0.1.dev20250305205907-cp311-cp311-win32.whl (761.6 kB view details)

Uploaded CPython 3.11Windows x86

typedunits-0.0.1.dev20250305205907-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.7 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20250305205907-cp311-cp311-macosx_11_0_arm64.whl (821.5 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

typedunits-0.0.1.dev20250305205907-cp311-cp311-macosx_10_9_x86_64.whl (880.3 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

typedunits-0.0.1.dev20250305205907-cp310-cp310-win_amd64.whl (793.4 kB view details)

Uploaded CPython 3.10Windows x86-64

typedunits-0.0.1.dev20250305205907-cp310-cp310-win32.whl (730.3 kB view details)

Uploaded CPython 3.10Windows x86

typedunits-0.0.1.dev20250305205907-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.5 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20250305205907-cp310-cp310-macosx_11_0_arm64.whl (788.6 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

typedunits-0.0.1.dev20250305205907-cp310-cp310-macosx_10_9_x86_64.whl (845.5 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305205907-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 89b0d925dd5471b59d045f5aaa1e2a68e0c5efbc1135ec2c504813bf72881d20
MD5 fe3e46328146610b24a2c62ea5e1d991
BLAKE2b-256 b0c2b01efff107c63304d1d3fc3faa2dd071de6c2ce18d6761f14c1bb8b39c04

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305205907-cp313-cp313-win32.whl
Algorithm Hash digest
SHA256 e2b6005c0860a51ed676b2a90c0f8e23feacff43d977e2e39c36e476a7b4d912
MD5 18d0c019658fe56a01c30a4e5a3e0345
BLAKE2b-256 1675613ec032a4525a575df992b456bdaf7b5cc3d20d85d35245d06089c99d95

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305205907-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 53025be671c9ea01b5797bb998643fa082380a3805e0ebf9720609d88a272850
MD5 92696c5218c44e12313d2532c4b7cfc2
BLAKE2b-256 3a85170d50b00599b0c231126663ec0ccbadceefbb582c2d6f004508bb6f5d84

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305205907-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 135b1c9930ec9471b748b8136c7d97c44cfcf3641fdabba4aca94993f1ccd460
MD5 58e1834df79d3f287457ac27aa2d48df
BLAKE2b-256 7c2779693ee89d67391f2f01eb9435f7cf8bf3fbb88bf5f7a4225ece4252efdd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305205907-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 afbdf4d5be04d621019733cb090d3f9594519a78a47d00cb67870470828e480c
MD5 5cdfbec041f158b89471e64b9b80bd4f
BLAKE2b-256 d4fc477ed9fce6b5132f59c39567198c23e354a7309c32bf7d793d109d2fada9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305205907-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 93392e0fef4e59157ef36981c1172d16ba2be243e5b28fbab03b93b66e9475ec
MD5 645b3fb2a81694e809cb842f488db319
BLAKE2b-256 1ad25db84b126b3d32dd826c86e1220db6a614b6e78b64db5c6661a33a7e3c78

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305205907-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 e4b0eba2a95c2ddccee03cd52149af7c9af43eb2e96691d8564f021d6c575c48
MD5 95b42265db47ace4e061c9a64b6c2c0f
BLAKE2b-256 14181a8b7df93b477742b58acd9fd1d55b7709f1a9711051b7fa9084ad881db0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305205907-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e0bf999453a77b1bb3b46ec9dc229930b56c086c0031dc2b8a99ddc8f575cd90
MD5 e58640c038d78de4eeb987d964849e14
BLAKE2b-256 14941426fcac5a36a51b0816d2cb8bf672a3aecabe9f919c1481779fc3112083

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305205907-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 4be541ad03a04edd025f7cdac735ee1dec33ef5db94bc5e5daeedeb67ff2c358
MD5 e3f0381731bea2b5229c5beabbacfea3
BLAKE2b-256 7618e26f98aba54ad56ce3e0ede44a11cb19e958e644d7947c0f7cf55e36ce18

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305205907-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 50442e38e9a72181f04f5a8578f970ef1ef7cda1e92ce56e57407c126f9a93dd
MD5 702843bed7879ef02afbf6d10cab3f1d
BLAKE2b-256 857a5dd556eedd011c07cd244e61f8862d95bfa28b1df1f656f4b0120f8832bf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305205907-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 4470a25e2146cf8ac8c200128dbd520141640fcfdc3e432850b8dc781d36749c
MD5 8d13e63359df6ed0fa01e25a8d52b447
BLAKE2b-256 8e502e241c8cebc1154703723acacf190ebf22404beeb7e00435d44e44c9bce4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305205907-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 97b2cd4ea0e5a6f7236f064c838c64ad02c728cd7cbe0053e0a43f4f4af25dba
MD5 7ee38b84c821921b85deb0b82d1da39a
BLAKE2b-256 d2f6188fd7474971075b351660ec80e9b316d2a49d4a72e9d41a0b31be1e0808

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305205907-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 08336ff3154f273719e09bfc4f3d05339188f463c91b74be4c999e2a9126fa83
MD5 98dcaf4d8d5b48ecef2e5ade011a0fca
BLAKE2b-256 4d28f27902342102add953769682ed2a2cca349fdf6e90e7cd637e14fcdbbc27

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305205907-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 6426b59bb213923092a0a2bbd0b0e863a61b757c193aaec88f2d033215541e92
MD5 c8b47ce3b4fae0526953d73ab5838c86
BLAKE2b-256 7498b1f886828c422fb85429e0ed9ddde5f59eeaaa9f0cff2f81e2d3e843db8b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305205907-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 4edcb3964d3bf09bc23dd11952f5101249af9ad84aca604748b0d9b547207cad
MD5 da6ad863f704ce251ac01772a3bdb050
BLAKE2b-256 0b6113cf3f453abbe3f7611b51dc1f20947b08dea0a1d7a0a6124f2498a8cfd7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305205907-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 901e6ca076dfe6b9b72ca3e8aa9e11e841ebaeef66e999a764d7c1143e45b3b5
MD5 4f7079b92c526eaee72d6cdd9fc412fb
BLAKE2b-256 5c85ff8c026c587306ddeaa0f74ac3184d8a9734409920f3a5cf16fff6fa2d9d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305205907-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 b6c60f3c15d28dbe14ea471c0869add419a32f8f5a9275e7a564cffa1bfe64f5
MD5 a051ae4c83615288e356ccb24ff31326
BLAKE2b-256 4439a0e4cb8d72c56349d97dd29e888f24c5682a94548157d42e46866287416f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305205907-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9eb0f0be4d22909a70205127c6cd0644f70655dd5c7b4e6e94aa6a7a070cba87
MD5 96708227257f013ab0011b0b5a9813da
BLAKE2b-256 9e64028c954319d7826bb9802428800c0ebd7923f6caa66c4261ae93d998ab08

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305205907-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7a16cd42656c95d745ec93e7b469c3419fbad24288755ddffc1428b01ef5be9e
MD5 113ef91a9771f9586e80a37a9eb522aa
BLAKE2b-256 fddfda94155a257d65abb5ee7ee9635f2df6a7af3adc083507679da40e744b32

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305205907-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 1badd2fb28bdf3467e98048d218eaef0638fb1ef11e30fc0cbc033ac53b53f93
MD5 8851c33e445219d49041f1d894b121c7
BLAKE2b-256 bce3c25b25dc7067dc984c473c7aa17ec5a11b8280be8f8eba38ae6704f8932d

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