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

Uploaded CPython 3.13Windows x86-64

typedunits-0.0.1.dev20250301002457-cp313-cp313-win32.whl (721.5 kB view details)

Uploaded CPython 3.13Windows x86

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

Uploaded CPython 3.13macOS 11.0+ ARM64

typedunits-0.0.1.dev20250301002457-cp313-cp313-macosx_10_13_x86_64.whl (831.8 kB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

typedunits-0.0.1.dev20250301002457-cp312-cp312-win_amd64.whl (765.2 kB view details)

Uploaded CPython 3.12Windows x86-64

typedunits-0.0.1.dev20250301002457-cp312-cp312-win32.whl (722.7 kB view details)

Uploaded CPython 3.12Windows x86

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

Uploaded CPython 3.12macOS 11.0+ ARM64

typedunits-0.0.1.dev20250301002457-cp312-cp312-macosx_10_13_x86_64.whl (837.3 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

typedunits-0.0.1.dev20250301002457-cp311-cp311-win_amd64.whl (792.6 kB view details)

Uploaded CPython 3.11Windows x86-64

typedunits-0.0.1.dev20250301002457-cp311-cp311-win32.whl (757.0 kB view details)

Uploaded CPython 3.11Windows x86

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

Uploaded CPython 3.11macOS 11.0+ ARM64

typedunits-0.0.1.dev20250301002457-cp311-cp311-macosx_10_9_x86_64.whl (875.7 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

typedunits-0.0.1.dev20250301002457-cp310-cp310-win_amd64.whl (788.7 kB view details)

Uploaded CPython 3.10Windows x86-64

typedunits-0.0.1.dev20250301002457-cp310-cp310-win32.whl (726.2 kB view details)

Uploaded CPython 3.10Windows x86

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

Uploaded CPython 3.10macOS 11.0+ ARM64

typedunits-0.0.1.dev20250301002457-cp310-cp310-macosx_10_9_x86_64.whl (841.6 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250301002457-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 577af7cdedc49866b4215d2ccd10ef24053a528bae75623895be3da9c21ec479
MD5 70cc1b503f3081f21427b752015f2cbb
BLAKE2b-256 5f581b7f2235cbf9762898d3ea49c243d2eced073c1f8d7101e71ad4457d4a12

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250301002457-cp313-cp313-win32.whl
Algorithm Hash digest
SHA256 5af818a758805fb08af095581688e5f2ef076a41e40d762d6720708ac8b4d9d0
MD5 2218a7b02f47e63e0c7f90587b930176
BLAKE2b-256 bc9a2b2b12ae0fe3792c2e5f6635bf52cdf6665ded28b6e6026eb3c02fb7464e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250301002457-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6761291efe376a9b0153044dc9ed305abc7bac3aabf9553b01482484bfd2b5cf
MD5 bade887e788030c9c7000bb4e532f470
BLAKE2b-256 eb838e8c235fa81859fdf664e437f269f1c2daaa23a626d4485e43f9c3dc7019

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250301002457-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8e2d98c4cb0ff0deea48f1a23c2092695c01d15faf8cbf0a14938f18891eead9
MD5 7c093e31aeee7db75c6bb131bb37a315
BLAKE2b-256 821bca90203fb9deca54a5d4cca93c7e4faa976e3c17226546630a2b69286ef4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250301002457-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 a1706dacc893f51adb3df8ef7750858fc87b255e6a002b646aae1373b7d479fd
MD5 956caea9ca478e85e70c8ca06a0e6386
BLAKE2b-256 bbed61cae45b36235ffa61218997bc11c2f8e1a62442b524c1d87ab00e3a56e8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250301002457-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 0205b29f2814b1e9ebd74201513ebe56cef4cf4ce522cc45e5174f2372be84c1
MD5 5042622f2496647a0b3e024ee668e937
BLAKE2b-256 a52272afb8c7ef0636cef7ec127fb794aef4af3deeffece1df46d19ae6fb0f87

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250301002457-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 0f279bdf073eaada4a18c7f317cbf1d20d9731117397f4ec7e7e6e22a5bdad12
MD5 31afbefcf24c569ad9b89aa950fc78be
BLAKE2b-256 c69886bd20f47a2a5c8961495b707c9a37e0012a757d68d81cee7b8ce3c48460

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250301002457-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6e83615299bff40b1b23708dbea90c683e9b39379d35042994c93edb19be5123
MD5 3b504c55e4ab2f7d7f796ec88a2835bb
BLAKE2b-256 47f0e0b42c1775640437b2c0037ff2570f2891265fa22f489b9889314a7be76a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250301002457-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9d2f7be76a16d44c3adb23895d7167757fc832e1a41057a3926c02e448a4ab94
MD5 89878b7c6e955c508fe88ce1782749ea
BLAKE2b-256 a2a788c4eb25be22b3d6fbac125b08c6830cedb8915eba09b1f212197547cec7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250301002457-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 f2a176defa01e1af952c8b7aee5229a032b298d13922d616e6953ce73fc79939
MD5 8ebb102199bdeb6a7612362cebfbc19f
BLAKE2b-256 33bc68b24125fc0ea6a1bc70e77869a6852acd9f23cbe28f96e9f0bd5ec3ea1e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250301002457-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 1ad92b2bee01fd9d1968b201ce16c997276209f5645d14ca2f6adb558c0944ca
MD5 d75911245882af8ad39a834e5015b7e9
BLAKE2b-256 3e60c8257507a925127ad2ffdff637bb33faedbb5c7247d502908717f303b201

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250301002457-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 977c402c1d43b9bfe9b1af1574c238f7e4467b199bbf0dbb51240469533f58c3
MD5 d60988356678ad193801a0ff84481d9e
BLAKE2b-256 401868b774011f354f72f1bc8db1a7b2cb67eb12b4ee9b4d477ace0b666c8527

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250301002457-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 12aeda0d6b9184e99d994f6ba2ed7682b75975fe27aa21492bca2c3e6422a4cb
MD5 c050b0177fe36c9027304df110071760
BLAKE2b-256 7e21a810769e6d8ca0468e429f9e9596aa62b0a67d4a118ed444b8a9378885e8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250301002457-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a65fa0e7730076f605586b7fc8ede84f1b061290ca23aa9be0eba6821e5f4880
MD5 754ff9237cc49b80d56c4a332a5de7fc
BLAKE2b-256 026bbe44baf23c15e931891a78ecbb9b12796f8c8ecf4ba3be980fae403a09db

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250301002457-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 cf79833af7ba012843e44e7f44677757cdbdc7325bc7f13a076b36365e2a711d
MD5 2bad383b51ccbcfad0582e296e7ab4a5
BLAKE2b-256 367eb188abb81e267357cbf664a468458e46c4813f9eb9ba8bca8f2bd64b192f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250301002457-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 c925f2c2c3d3a83d5c63f11dc34ea122e8bc392a23e496e2ef3dd371d2d15c0b
MD5 bf0ebbddb8459a3886a4e9aa72546cb5
BLAKE2b-256 e0a4e29e64924501b9885d2f401cba486b4cd7a0f11eb79d4465349a6f3b2987

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250301002457-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 582f9b7d8c6abce5172c76956c624f220ef1cf04762ba203683ef040d7ef7fa4
MD5 cd5b538db45b55b8944e90ff9723aea9
BLAKE2b-256 d891cc7f0cba4df51667522bc9af009c4394418b0b2adea83e145304eada8055

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250301002457-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8697ed5817cebc5a27d581d7d9e7ea0c39010acec25d2209aa662070b0f21d0a
MD5 27e45fce292eaa8c2c0706e04e1957a1
BLAKE2b-256 2c47ccab4dd9fc3009154cc1974b9ad0b443e9ce9bedd11663eb3d009d3f56c5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250301002457-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 407531b564628fcfb852d3c556b1630ac51a50ff0c9b189f5a8eb34653fb8c88
MD5 1c99143ff43a93b9dac637f7ef516ac1
BLAKE2b-256 4b8504991f228c743d920b2bcbb1a8838f482bc183f492bff041899c749855d1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250301002457-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 93c77dcdb8056d16de60f5fe567b65cc525f2ac33f0944bf5e290086b11b6203
MD5 e844ccea8662a7c64651d2a690e3dea2
BLAKE2b-256 2303b4c337d5bd5b4448201ec604238e966984ee091b2a28093d900de881b441

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