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

Uploaded CPython 3.13Windows x86-64

typedunits-0.0.1.dev20250211014127-cp313-cp313-win32.whl (699.6 kB view details)

Uploaded CPython 3.13Windows x86

typedunits-0.0.1.dev20250211014127-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.5 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20250211014127-cp313-cp313-macosx_11_0_arm64.whl (769.8 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

typedunits-0.0.1.dev20250211014127-cp313-cp313-macosx_10_13_x86_64.whl (809.1 kB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

typedunits-0.0.1.dev20250211014127-cp312-cp312-win_amd64.whl (743.2 kB view details)

Uploaded CPython 3.12Windows x86-64

typedunits-0.0.1.dev20250211014127-cp312-cp312-win32.whl (700.6 kB view details)

Uploaded CPython 3.12Windows x86

typedunits-0.0.1.dev20250211014127-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.5 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20250211014127-cp312-cp312-macosx_11_0_arm64.whl (776.9 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

typedunits-0.0.1.dev20250211014127-cp312-cp312-macosx_10_13_x86_64.whl (815.2 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

typedunits-0.0.1.dev20250211014127-cp311-cp311-win_amd64.whl (770.3 kB view details)

Uploaded CPython 3.11Windows x86-64

typedunits-0.0.1.dev20250211014127-cp311-cp311-win32.whl (735.5 kB view details)

Uploaded CPython 3.11Windows x86

typedunits-0.0.1.dev20250211014127-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.6 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20250211014127-cp311-cp311-macosx_11_0_arm64.whl (791.0 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

typedunits-0.0.1.dev20250211014127-cp311-cp311-macosx_10_9_x86_64.whl (852.3 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

typedunits-0.0.1.dev20250211014127-cp310-cp310-win_amd64.whl (766.9 kB view details)

Uploaded CPython 3.10Windows x86-64

typedunits-0.0.1.dev20250211014127-cp310-cp310-win32.whl (704.0 kB view details)

Uploaded CPython 3.10Windows x86

typedunits-0.0.1.dev20250211014127-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.4 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20250211014127-cp310-cp310-macosx_11_0_arm64.whl (757.7 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

typedunits-0.0.1.dev20250211014127-cp310-cp310-macosx_10_9_x86_64.whl (817.0 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250211014127-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 5b9234b0f03567b24e765e1cdd7f65d1900563a47e207092957b81f992a7b9dc
MD5 16d06c4d4a8d4ddf4906845fcecb5f2b
BLAKE2b-256 acb84438354aab664a54646ff40480994ad1075aa58e3b50204092f19eb299c7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250211014127-cp313-cp313-win32.whl
Algorithm Hash digest
SHA256 e924d1c34605cf97f179cb46e8fd7acbb4219ba6aac53a726040e33764339380
MD5 2a639933d1ccd098fa6e00216a32e6c7
BLAKE2b-256 e3eea68bf1a122fcba8e19b2b43e10d697de82c57ccf5685d76642274f7a97c0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250211014127-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d760d5360b6df6c14011b69446e3146aca5e990d857f249125c0f0720dea1153
MD5 43bc1c6ed6481eb04caf100fce90769b
BLAKE2b-256 b5d3e13fd94261306a052efb14ee81d511c3c7d978704d1c726cdfb0ee7f3d96

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250211014127-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 4a7338431984cc528befb3a9b99f230cdaab754d284d69e3d7f599fb7b1194e9
MD5 297bdfb37bb237f8a9675030f7237cc7
BLAKE2b-256 567acf662af9ff191f3cf7af6759369d7cdeb25149dd342685a896da36cfd412

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250211014127-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 6e3662c9318c66c1d617005b0e920cd4dafed68f72e4ddfb80ffbc4d30fa4d59
MD5 7bb5592987d3ba428668f1bc28cff0c9
BLAKE2b-256 ee440c17d129a2c441a14a2dca21def31a902ac27b27b731564a9863c619386a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250211014127-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 58fc544eed41039f70ea556b1be0a5f7d7e3fde1f152f643840a3b844f9ef565
MD5 fa9fed672158a9df51220879d41b8b6d
BLAKE2b-256 436157f1fda14f0c472b969a41e49abe99a754e7f4c78ba7ac0ce700750c7a5e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250211014127-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 55d3678cf23eadcef899e77684e7ecd8c08ea8e99ac8d2411f4684bd50a0fc26
MD5 eb0415fdf1a58037f316b330b7ace410
BLAKE2b-256 2b1bebef2b3b79894b63236f527875fa71a6890485e9b984f4728d723aeec7c7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250211014127-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 751630383f74be8aba4dd60365dcafabfb6b1ba7b5490bf84ec47ad15f7fc346
MD5 eb986a39240fe6967d15fe5bc41c70d8
BLAKE2b-256 7fca2c7954ebcd45cb8a42bb330ba781129bbcda54d267049e8ae717cad00f4c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250211014127-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a81b3a6fdd3b3fd9a89f05699214cd3795aaf53f6025bc58eb290646d9cee98a
MD5 c9afe1a801c1010ae5f4e1650d2b5789
BLAKE2b-256 70549cae935263c3bdd579e7d37b400610286b78c489fd6a9e5150d9b3ea31cb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250211014127-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 35c606b561528e71454ef92987050156ba7c71a9c6f0a8f76e1bc66660776418
MD5 b9176577749456b7249a6541044d1d05
BLAKE2b-256 89c35631708e429d928e44d2609109ecb20a02650ad77b74860154f91ffb258c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250211014127-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 600ecf80e3b25d19c74ed16369c406e5bcb8a53429222a89765f311a357d6738
MD5 85e8d0159a85eba5b7aa2cff9f0c16c4
BLAKE2b-256 83ce0e041c2b44c8a38c4b17634cc2b22be934eb3e8b1bde483199c49c0343a4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250211014127-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 eb0b6543c8ce488b59b84d64b95a70f24442895b8c8b1926a4668abcce122f4c
MD5 947febf907de0ddbcfe597fe808ebac7
BLAKE2b-256 700878e18bd2020ea5df3604d5fd32c08cf3518785d9bd2adbd9d62744dda61f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250211014127-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 588914c27411abe464f61e679a60efe19b1c6ab60acd1d4342ba1bb36957c452
MD5 a9ccc828af845507bd6670d45957867a
BLAKE2b-256 2b5028e18af38455d260f64c86d5d299a2229dc0727e3ae91557e60e48cebb61

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250211014127-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 771aa74e13155f1dc84f3074a5adb7e5b0ee7b2c9219759dc028190634b3ded7
MD5 586f12e487422137bf73874dafe9c126
BLAKE2b-256 9cb148bdf843ddf83a82614647a70693d6a7fd762c18b5548bbd190e60840ecd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250211014127-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 ee390ac6031574298b170dd5d3c956549ff6071e68817dda6aec0158368911b5
MD5 8a6111d5a2ac397d2414ac69b4aaa8fc
BLAKE2b-256 4751621bc7d7c7a327d7c471c1f19fd71be4fb427d8117e955bf1e9b806ac774

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250211014127-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 e5238ea84b861089fdb3bac66b31a68261ee6590dc382ecdae2deb3584f8bbd9
MD5 4e9c113fecfa75f94680c92091a9fce1
BLAKE2b-256 9bd745c276fea67d2499ede6d540fda95800200141d26c55c3fffa42dc175f2a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250211014127-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 35ed3eec425fd1c4ebca613498aa1ab21ce382fc008db71eba5cdae6457ed085
MD5 fa628a77227cec81095d7ec0fba1464b
BLAKE2b-256 82dec6c6faa71d2b2748085477c2633f447622d76c8d320212f668bd2857b258

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250211014127-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5b8153dd9ba88b7672618db0c565b92ee5fa95d9e593f715898dad9270a0d50d
MD5 8c59c791d4eb42ebc8e961ee531fad5e
BLAKE2b-256 28e9c0a1d448a97733d06f88d7a462ca2e77fd237bdc56026b1f1105fc48010c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250211014127-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c0792c88eaf89485d92bab7698735257bd1c63d294c564c0f66497d2e582b0be
MD5 7009ec83d986e3ace482e4a8144184d1
BLAKE2b-256 2caed054577331cf13025e8e12fad1bdd1f0a508a1ea4bc38bed67522efec8d6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250211014127-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 4402ac2e5fd7491b20e28f53e57aa6a74d7c82c10e4dc161758eef08078483f8
MD5 5f4f78345bc1bac6c7ac56e3b55595aa
BLAKE2b-256 70f243cb2adeb526a9a3c1f83cfe922c3213781a6221fc74d9244ca349429543

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