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

Uploaded CPython 3.13Windows x86-64

typedunits-0.0.1.dev20250128022528-cp313-cp313-win32.whl (681.7 kB view details)

Uploaded CPython 3.13Windows x86

typedunits-0.0.1.dev20250128022528-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.dev20250128022528-cp313-cp313-macosx_11_0_arm64.whl (748.3 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

typedunits-0.0.1.dev20250128022528-cp313-cp313-macosx_10_13_x86_64.whl (785.4 kB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

typedunits-0.0.1.dev20250128022528-cp312-cp312-win_amd64.whl (723.3 kB view details)

Uploaded CPython 3.12Windows x86-64

typedunits-0.0.1.dev20250128022528-cp312-cp312-win32.whl (682.7 kB view details)

Uploaded CPython 3.12Windows x86

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

Uploaded CPython 3.12macOS 11.0+ ARM64

typedunits-0.0.1.dev20250128022528-cp312-cp312-macosx_10_13_x86_64.whl (793.2 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

typedunits-0.0.1.dev20250128022528-cp311-cp311-win_amd64.whl (750.3 kB view details)

Uploaded CPython 3.11Windows x86-64

typedunits-0.0.1.dev20250128022528-cp311-cp311-win32.whl (715.9 kB view details)

Uploaded CPython 3.11Windows x86

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

Uploaded CPython 3.11macOS 11.0+ ARM64

typedunits-0.0.1.dev20250128022528-cp311-cp311-macosx_10_9_x86_64.whl (826.4 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

typedunits-0.0.1.dev20250128022528-cp310-cp310-win_amd64.whl (746.6 kB view details)

Uploaded CPython 3.10Windows x86-64

typedunits-0.0.1.dev20250128022528-cp310-cp310-win32.whl (684.2 kB view details)

Uploaded CPython 3.10Windows x86

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

Uploaded CPython 3.10macOS 11.0+ ARM64

typedunits-0.0.1.dev20250128022528-cp310-cp310-macosx_10_9_x86_64.whl (792.3 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250128022528-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 97a492e4a9cbc391467d897f5ec7ef630ef49baf6b419977e86d7fa0e6aa36e0
MD5 c334ee2806c5ea359f2b3cbcbe8f2be6
BLAKE2b-256 fb0b87a2598a4ed5949b1fa889f6c0ee7dd1c67b5a6cee4fc657a0ebc962179e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250128022528-cp313-cp313-win32.whl
Algorithm Hash digest
SHA256 41e59e3d3f331185e26d07cf48d7ffc635cfada12adf42ddaa72ea0976650924
MD5 eceba588f28ca7e8760ffe57682bff89
BLAKE2b-256 a1bee20203548099e1302a85fe68ba2bc5edfb61662b4e618253f7d2eedeb613

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250128022528-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e758f7e1f2b50c79e1f4d4691a0ee984f3caf2ee775692b4f609692981c634b2
MD5 18f95f4074d58781fb0bab66e3a7020f
BLAKE2b-256 28df96db439747ef4f10784508530102fbfa892572492a3483e3e0679d508e5b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250128022528-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0fb9d72d0b002e2a6e8bd6000681f319e28ce63133b261f5813cca2f743329f3
MD5 6ffc996576681705a88f8c019a0efe29
BLAKE2b-256 67188a639382871f7ce7daf3a035b71cfcec2188a869e4a3b58618693fadea5b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250128022528-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 019ec48e03924ef50011cb7b224cf4a5d98d56a149714179af436a2bd6337bfd
MD5 7668c342f1430a779e31620e5165d81d
BLAKE2b-256 f7f6ac303683644638cf460951458bc33af4b3388f22b5659875513ff0fe0697

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250128022528-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 c134594e46756c28d435f7e800967c2812af6b1b5e3980274f5797e36425c4c2
MD5 02f61b2caa96c2886a233801fb3672ff
BLAKE2b-256 3a504bc8b48e42844a195fd08b2c4e9b4c770a9dee7b810412564e38fd11b342

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250128022528-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 1d074a948769ab1ddccfbc29ddba17917847667477078ed0d24744fe22d9112e
MD5 f8ccd9e17ac48f072571e101e1cdb1ef
BLAKE2b-256 4dc2fe5f92281f66aa3024f9246854b8cbed9918f3ee0336486c918c7b52c8b1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250128022528-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ae8702c24d62b58ca2690fb1972ef34feb7cc12be1f6a2edf4c82a9b72bf4847
MD5 2a686e27435fd7ca09cf17740b106ae5
BLAKE2b-256 017a872dca7cfaeaf67779cfc3d85fa64ee2d10d3f817ddfaf8a39eb69e644aa

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250128022528-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b502e05b2c2d10bdb5af873441d1e8a46774517bbaa36366d84c492dc4a26a4e
MD5 898b86c1dcd7f4e2f2c2ab03c5072952
BLAKE2b-256 fa87659841e52e9ecbc1301d432f4319c4f9138d5d256aecae8666b97dd262bd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250128022528-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 b522f11854a7dac57e0ab4798e761220a8640f0c7ee2d8f92c055c69ba720a6b
MD5 eaf16f0a942fa8dd0fc0d3c6a21e889a
BLAKE2b-256 f1db91cfa6d6b172ac2495a74691a27e185a4b1d9301d01cc32a75a4e7660c17

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250128022528-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 c493d62aae1cdeca6f58c1042a008f5bfb53a193ab5976944391d55697ed1162
MD5 573ccb6583ca1bfdfecd5a77518e6914
BLAKE2b-256 01381141f1c0fb1d370e06b5f7120722bfe65af94c75e5ce23b2bd892658968b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250128022528-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 6c412a735a9e59ea70f61ab6342dd540cdc4d29a99bd0473deb7bd9a7de5b837
MD5 4cbd34c8039804c57ea10be9ce4f43fc
BLAKE2b-256 b3f58f39361206f3965809a559d00f8e130c47adca31fb27015576a20ca9e4cb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250128022528-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6f8ea522a05c780d8781716d5f809c3a6c348d124b7af3df2e6698e6d5cba2a7
MD5 2f8a3873c110fc2b9fd7008c05647c29
BLAKE2b-256 1aae0be432270dc5ef74596c35abd66cc2a3675b332717a41a66806abd0d0840

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250128022528-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d468e94b8b32f573958e97772ad90db000fd3d1cd0e536ae8806a030c160cdd0
MD5 33a3380857c809da79ab30b4cec44f7f
BLAKE2b-256 a0ef1c6b8c2e249dc9c0aebb63d3832eae34ae60cafcd04800d7f3fca4eb5bf0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250128022528-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 19e7c575d447ec0a4823d70295c96b4b279a6d576f8505075695de9d7706a576
MD5 0643175828ef2689d6b9e6dd29f50de4
BLAKE2b-256 d172e28b760571c8f09a96f9a75c8fc27b67ea1625c4b0fafbbc7595e61f6e20

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250128022528-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 39df1f2e058f70250c2e1cb7b60b119ab3e47a3b98ea366897857a5e4bf6b0af
MD5 0ce21e26c8f2bfa3d975e41fff1e562b
BLAKE2b-256 d61f361333f66a55f6925dc031ff36fdf8f5efe512a0d44c5852e3af13051207

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250128022528-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 789f9de2c1a667128f1dce1de6b99ad917206c26928f61d879a091ab661656c4
MD5 4c79a4f7f05ef79ea54cee35cbf36312
BLAKE2b-256 bed2079951079b6dfbbe6c5943d4645d0bcbf4c75083b08f7a5aee34c9d08c72

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250128022528-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 4525858ef391d5f6369a8826817fbcdcd66a48762f6baecd627dc8a001969946
MD5 2a6374f3954c2f496bad1c4aefa83c08
BLAKE2b-256 050651b5ea2ce4b0cb73720695338b2c1898deb1e99e47e029ddae32c7f7100f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250128022528-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a75113290a24ede32abcc94ee25381519af265c556de2501e3c5fc42a36267b7
MD5 52f941233da57ebf2dcb725575f4fffb
BLAKE2b-256 af7206587a4957f8d958557d14869a89fb77730581a8c4a75cef550224e01e10

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250128022528-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 53c8b0e2845ac3a7cfe2ecaebd5c7514f76f393529e7d879ab6610987bb8fd31
MD5 849cb51d86302983f398fc0a7b127492
BLAKE2b-256 bd3de4e94069c5817e404344f2c02304341eda880244ae72ff74aa6793e0fae9

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