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

Uploaded CPython 3.13Windows x86-64

typedunits-0.0.1.dev20250214023029-cp313-cp313-win32.whl (699.1 kB view details)

Uploaded CPython 3.13Windows x86

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

Uploaded CPython 3.13macOS 11.0+ ARM64

typedunits-0.0.1.dev20250214023029-cp313-cp313-macosx_10_13_x86_64.whl (808.3 kB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

typedunits-0.0.1.dev20250214023029-cp312-cp312-win_amd64.whl (742.6 kB view details)

Uploaded CPython 3.12Windows x86-64

typedunits-0.0.1.dev20250214023029-cp312-cp312-win32.whl (700.1 kB view details)

Uploaded CPython 3.12Windows x86

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

Uploaded CPython 3.12macOS 11.0+ ARM64

typedunits-0.0.1.dev20250214023029-cp312-cp312-macosx_10_13_x86_64.whl (814.4 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

typedunits-0.0.1.dev20250214023029-cp311-cp311-win_amd64.whl (769.6 kB view details)

Uploaded CPython 3.11Windows x86-64

typedunits-0.0.1.dev20250214023029-cp311-cp311-win32.whl (735.1 kB view details)

Uploaded CPython 3.11Windows x86

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

Uploaded CPython 3.11macOS 11.0+ ARM64

typedunits-0.0.1.dev20250214023029-cp311-cp311-macosx_10_9_x86_64.whl (851.3 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

typedunits-0.0.1.dev20250214023029-cp310-cp310-win_amd64.whl (766.2 kB view details)

Uploaded CPython 3.10Windows x86-64

typedunits-0.0.1.dev20250214023029-cp310-cp310-win32.whl (703.5 kB view details)

Uploaded CPython 3.10Windows x86

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

Uploaded CPython 3.10macOS 11.0+ ARM64

typedunits-0.0.1.dev20250214023029-cp310-cp310-macosx_10_9_x86_64.whl (815.9 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250214023029-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 20c026280f065d6e436907ca57a3b76ee81fe23b526bdaf62341ba06735e3913
MD5 6e350181f987a6a25811738cfbb8d1b4
BLAKE2b-256 a95e8cacbb45b65956c845650d8df8019f8fcd11c09c06b7b33ee77f204cd032

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250214023029-cp313-cp313-win32.whl
Algorithm Hash digest
SHA256 b890938fd47b9a291aec516ca37303c5ed83b4e631e7a83b5f6bb0645b77f94c
MD5 81742609a3b17928f9a4040049cc9529
BLAKE2b-256 f25bea505698b61e4512aad80e0fba34d9ddca8d88bbd0236d2fb3bbf18ced1b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250214023029-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 3b05a453cc28feb2442b37eef396a16d4012d0a5f74fa0a99d6ade6ca8f859fb
MD5 ae7136c42049904b83f5e8a3e4a14e1c
BLAKE2b-256 32157ffc41774eed548861b4d3d11a270511d73083a117a2b893406a19ca2027

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250214023029-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 46589c97ddcb59ae0888700093987e4b20672d7e4523b51479a7829a52f3d0d7
MD5 cc279e3deb228a8516dece77e2c09d13
BLAKE2b-256 02eb7a2836f789f6dec7824a2bdeaf11eaf94c8af20cee13da0789a098f0e3fc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250214023029-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 eaa0ca7ea4233ca3d50a5cde4b37a9086a8c4cae4c55f58a58e11063fb91cb2b
MD5 40125d0de137be87f6d8ab9a7e65c7ae
BLAKE2b-256 146e628ff4aa0c61ac1f844a2e284975e6119eb1c849776e617517a3ddb705e4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250214023029-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 164746121b088c743409d843cf525777e3b3eede2ff3b1f5d59821dddd6e7ea8
MD5 fac1651dc1fe2bb7f373c3eab85eec2d
BLAKE2b-256 f6eaaa1b5ed9a16e7f71915a7686f3171c57126e0f6de2a2c9432460e53fa589

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250214023029-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 e4656bed36b0c65ba35c8f497342786026de0bb932f957bfb1fe5550cd721e96
MD5 ca087ecfc4c1baccea523a22f7f78ec8
BLAKE2b-256 fbf3107dae24e2b52f4814b79a3b18f9f4b6a5aaef3b68130ced423deb5c55d7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250214023029-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 560d064d9b159c84ba32ce5ae5dfde6035161007a6520f61810721b7270eebe6
MD5 6bdcba411b51efbed4cdc108ad97f106
BLAKE2b-256 baf7ef6a8c15e2d05b39a4154f3dd3443a4fff1706bcb7b382a9feffc761895b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250214023029-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 3248f5c2b06a3595e086abee0e3c3f9f5e839b934b70a4c53c75565ed52e87f9
MD5 aa988513d5de503924bd78dd9e81b766
BLAKE2b-256 6dc3232ead86dbe7887355edc3a60609b28adc0f63b0b338af77ea4368ec2f62

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250214023029-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 23a4ebf89cc807afee53aebab725505cdca4524db4ed5ed1f82852943370c6e7
MD5 bf80d09cd144456c6fadee049ecf4942
BLAKE2b-256 37a2870f68c5200329b37c1be4e4260408501edafb71c1209b68db6faaff067a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250214023029-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 92de1263185227c5eea3d9c97abd588762152868b295f6b6decce2c8ff727a6c
MD5 f1ab52ebd550c183f39b2cea3363fd68
BLAKE2b-256 aead05bbea94361622db20d53e0037041141fda64aa0c9ceff71fee9486584e5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250214023029-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 1619fcf905b3dffd4b2990712cbe52d90e6a394cb87be96b9d65353efa66fc3e
MD5 563341efdf8180ee57e3aaf764b4e2d0
BLAKE2b-256 9e12c5c1038f4939144975e07e75503ea0df255146a0c96510175636af1caf34

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250214023029-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 25798551a4916158455c88dd70beafc6dccf7da8fca1106895117983e82dbee4
MD5 e7f0687a6e286947a4accbaee9662992
BLAKE2b-256 4bb03041fb0079bd4a0a49f46bd5d5ace87ad247aaad3bceb1aed3255bbfff2e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250214023029-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 4fd834297de9be67e16eab2daee6ba8f96727a8235f5f27e1ec6b20ca1957ebd
MD5 e1342a47c705d67c8c6b85165aa60d3c
BLAKE2b-256 771d82fc3f8afcd407f2b2522c55e154bcc50edf9c2519066928c07e4b2ac2bf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250214023029-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 53251f0ddd1acb1d4a326605d0ca5a275d7bccec199cf6902ef84dc9dd27838c
MD5 21e5b719922bf311adf1a2fe75291d0a
BLAKE2b-256 435e98547cae4ecabea21ee61eaea2b097795466822f8e8c5b5cdb21760c9eb8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250214023029-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 32b6f7a3523d452cabc4a5bc99706835a059649c3a919712c6b0a38070a74f81
MD5 d8386ff9f3d343cb89e942c50733b90f
BLAKE2b-256 5edf35f2aa879454cbeeff54b360e6aec93e4be1c270abb37ba79f0dcc01a35f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250214023029-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 e509ad6f0bce136bcc92f3be8cf08cda5e917aab1d80380170b5133d3c3a8328
MD5 99cb515cb145bcfbc3b2cff78697bbce
BLAKE2b-256 feddacd028884a82a09741149138faca7d9324d59c79f313f74153a947732b0f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250214023029-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d94595b08d298349e4ac2010ca723e43eef400b70c279dce4381e369c71575a8
MD5 ea76e0b4e730d1e45d4850c0e35cedc3
BLAKE2b-256 f2f35ff924451174a5b5603a468f0ab6646b6d28970457d0f905e80173622593

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250214023029-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 576fa5c51b94402d7d34b486f10a0edec5479735f65b11420aaf67162c75afe2
MD5 482ca1f5539406613ad4cfbf4dcb33f8
BLAKE2b-256 9bd10e0f7fa99c4a93d0cf420670003a369a956e1910370ddad20d718af7eec5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250214023029-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 c97523eeacf4ae88fa77281758ddb041c8c84af05e4020f25935a4e6d1be5758
MD5 418b430c15493e295430e12838830a39
BLAKE2b-256 ddf29b0a2398e788257b11b76ec77e08e8dda5497aa74638f9500a089db840b1

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