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

Uploaded CPython 3.13Windows x86-64

typedunits-0.0.1.dev20250305213057-cp313-cp313-win32.whl (734.2 kB view details)

Uploaded CPython 3.13Windows x86

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

Uploaded CPython 3.13macOS 11.0+ ARM64

typedunits-0.0.1.dev20250305213057-cp313-cp313-macosx_10_13_x86_64.whl (843.7 kB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

typedunits-0.0.1.dev20250305213057-cp312-cp312-win_amd64.whl (777.9 kB view details)

Uploaded CPython 3.12Windows x86-64

typedunits-0.0.1.dev20250305213057-cp312-cp312-win32.whl (735.3 kB view details)

Uploaded CPython 3.12Windows x86

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

Uploaded CPython 3.12macOS 11.0+ ARM64

typedunits-0.0.1.dev20250305213057-cp312-cp312-macosx_10_13_x86_64.whl (852.2 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

typedunits-0.0.1.dev20250305213057-cp311-cp311-win_amd64.whl (805.8 kB view details)

Uploaded CPython 3.11Windows x86-64

typedunits-0.0.1.dev20250305213057-cp311-cp311-win32.whl (770.0 kB view details)

Uploaded CPython 3.11Windows x86

typedunits-0.0.1.dev20250305213057-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.8 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20250305213057-cp311-cp311-macosx_11_0_arm64.whl (826.7 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

typedunits-0.0.1.dev20250305213057-cp311-cp311-macosx_10_9_x86_64.whl (888.5 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

typedunits-0.0.1.dev20250305213057-cp310-cp310-win_amd64.whl (802.3 kB view details)

Uploaded CPython 3.10Windows x86-64

typedunits-0.0.1.dev20250305213057-cp310-cp310-win32.whl (737.9 kB view details)

Uploaded CPython 3.10Windows x86

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

Uploaded CPython 3.10macOS 11.0+ ARM64

typedunits-0.0.1.dev20250305213057-cp310-cp310-macosx_10_9_x86_64.whl (853.0 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305213057-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 9038505c310bb4bbd1c0f24689064c865bfa068a80f7c8ca1bba7b486d8d3e91
MD5 b0e2f5f6eca0db9c518f7cd47a6b7e3d
BLAKE2b-256 6d5422cbaf63f77de6bf42cb1cbf6eeb507ff406b307f6a7bb6a499669a84147

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305213057-cp313-cp313-win32.whl
Algorithm Hash digest
SHA256 633d877a8731c0760c4f4b9428f0b5a88d70ee0b93bddf78637654793345509d
MD5 90acf46a1254e916c68e0b335325d0c8
BLAKE2b-256 5efed390cc45d634625c1c72b393d69160f3edcad0e220a98b51a3c305c0fc45

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305213057-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 34fc4f6537d6f213784bb305021b2fd1a5d6c9e1649814fb5b99f2cfdca20fb1
MD5 1203b1a7a9abc0df74ddfe7d0c7c9646
BLAKE2b-256 d85e3d85e73671584007f5b4103dcde17095825772b27e76f525966e56600c91

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305213057-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1f08b6f603d7429b86e8f36c1fffdd12e0ff5adbd802f38eff59ac75c86d5d21
MD5 b923d3536eb20e2fdf11091c225170b3
BLAKE2b-256 2b9c8b5ac441e50588479c91d5705e5893bbe909ccd2da0823274a6038f3592f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305213057-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 6cd3945611988dac54940d3bc8d4d1c370668dda46af8c3e4fe72fef2ab1251d
MD5 65358978de5fd213c2ec0a6f29aef9b2
BLAKE2b-256 ad5e9fd430dd653702db5da5c1a893617a593f45c7154f885843232531b99926

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305213057-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 8bc5b9c3420e437410604d1283b524a18c0af395f3ca1aa95e9171bb7156111e
MD5 b81ecad4724b33c33a966877e53c86f2
BLAKE2b-256 4981baec77903815a0931b09892999a63d446531c17256a112e39b9cd00fdc80

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305213057-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 e2283708ed241acca9a24247c4262ce1f49f0189405c8fdef874c687e2631d02
MD5 8021f2492b50b2209d94b92b704570f7
BLAKE2b-256 73373c80807635faeadcc86d892cd16cced654604202f8d5798648fbea88bc22

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305213057-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b785bf2b495c7157bf39d0d6c19dc65631d44c4450bc3e673d162739b076e24c
MD5 b343ec063ad336ed67a9cc3d11c6156a
BLAKE2b-256 e5a38aa7efd375a2dc445abb19e0f35f19ed55a3fe9430d3c69888009a6ef818

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305213057-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d6559d50246efe92405567e774c7c12cded274e156d06a93bc6ec0507abde231
MD5 8447cfe626a489c7e3c9f1ade3080513
BLAKE2b-256 fa47a6a08654421cc45f7182045ab2cb5bbf03e0932704d3fb8f623022097882

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305213057-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 5e1dc8ce0bc43e31e6e6cba5d04fd5bcacb86fc81120eef110b2ebd5877b8dfd
MD5 b8e4b06e3bfccdd395530b0e153b07a2
BLAKE2b-256 35bea053438cf6a9e275c219e0211bb1b614f327850540d34646a06361820d67

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305213057-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 87ed06b0216bfebe8755768459ffb1c1866ccc7105d661d74409f383d9acac9c
MD5 f9691aef19ee43e46681009d51198645
BLAKE2b-256 af614f3cd1871544277a95372b10cb504bca99b7558f3183271c5dc11793fa10

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305213057-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 7400ccedb658671847cd6b5ce0845e5804dfe37e9ed54a7fa62bb0701a7b1ab1
MD5 4627eb2c5932695f3818f51b58b4a196
BLAKE2b-256 7d3eb05020748deb141e0b68897bc01054e7e0031e0b6504dc82ffbdf3167442

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305213057-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 64fe258405d2aec078ab46cd04a65971174c909a0021b107e1c4bdabc67e8ebc
MD5 25f244b0d5fb98fc9c6f5b655f7e7a26
BLAKE2b-256 5acca5b57261915b55910e21db3c615fa13a87575ba1fcc9f5edc4639de82109

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305213057-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7963b59003a800ce1337d10ed07f3cfa525feb7e894e60fe7542853c64b6125d
MD5 6b250eb9ce746f24577d2b264cec0ca3
BLAKE2b-256 deb10071c71eaa588ffeed675d157d11cf3a660ea1a8b6831f1d93f3967c5569

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305213057-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 68dede02766bb20197a87369a1e32cf69a2553cf96e0b14cbb220f794f345dfb
MD5 61f4d008a885916e6a4131164126641e
BLAKE2b-256 6adeeaeef55f224eb729ff67d31f904368f8b6efc592f151d178a26b62478718

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305213057-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 ebf8a83669b386930e930ce0b4bfb79f7f8527de699d9991206004240b51def4
MD5 12308f91813dcffbce9354bdae923735
BLAKE2b-256 0587f78fcb19201a1f7233d9a6d4afa7adb03471d30cf6f5db33d1417d13de6a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305213057-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 b63d2d34944fa21610121ff402851e4a6759a3335a95cf8b3ee94a3601b14e95
MD5 ad6755d6de055767bbef7d9b63fb755b
BLAKE2b-256 276fd87f538b29d58a555689e86771417a8332821283ab70c7f0c958fe579be8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305213057-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e8585f284a4d48059e7732e031e1bf47bdbeab32616391945803abf334deb865
MD5 a411b73ed1c45edc0abaa72cebaee435
BLAKE2b-256 5f440688f03ab12eeecd7cd8cf61251719bdec64fbec38acd84987cd89b938c2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305213057-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ad5b258360f04339e0064a8ee541929e43ea16e94debe07fd8696bf022825518
MD5 2a36de3969f4e80065117cc9df546060
BLAKE2b-256 8b4838bfae4d023ba2a23dc755ccab8d94a66f0e98a8213b724508b89c52d515

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305213057-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 97fcc880019a3fe20a045a1cda58a4ecdd23eb76e50cb075762d628e3d635455
MD5 7716904fb7e86c21da714a30f29cada0
BLAKE2b-256 710d8066badefaf12bb0e98c858e2ab935b0c03160f27e415856935fab2c4373

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