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

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.13Windows x86

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

Uploaded CPython 3.13macOS 11.0+ ARM64

typedunits-0.0.1.dev20250129041221-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.dev20250129041221-cp312-cp312-win_amd64.whl (723.3 kB view details)

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.12Windows x86

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

Uploaded CPython 3.12macOS 11.0+ ARM64

typedunits-0.0.1.dev20250129041221-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.dev20250129041221-cp311-cp311-win_amd64.whl (750.3 kB view details)

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.11Windows x86

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

Uploaded CPython 3.11macOS 11.0+ ARM64

typedunits-0.0.1.dev20250129041221-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.dev20250129041221-cp310-cp310-win_amd64.whl (746.6 kB view details)

Uploaded CPython 3.10Windows x86-64

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

Uploaded CPython 3.10Windows x86

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

Uploaded CPython 3.10macOS 11.0+ ARM64

typedunits-0.0.1.dev20250129041221-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.dev20250129041221-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250129041221-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 f3df309a484b4d2232b1b7f47d38cebf8df87ee1872707f8697bbe1cc34c9877
MD5 e657749e6ecb9d1172b4a9babcacd95c
BLAKE2b-256 b6e2b6d16d0dcc04df2d497e8c7e60c14f61b1afabbb0c6c154adc0513b014c9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250129041221-cp313-cp313-win32.whl
Algorithm Hash digest
SHA256 6b0289b5cc474905f669c97568b2495a0be14ebcd11ea10b49fac2f081d24c50
MD5 0af9c0610819a0e22e1a1f8a6577117b
BLAKE2b-256 f00573bc481924ced4cf453c3c3191a67cbc5b1dd6a00e3553dfaab7874e1f8c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250129041221-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a27aef9b81a3f9d83b70d964f6667239ad94c92287956cdbf2b557817e7a0096
MD5 d64e9559ac502c545fe80906b3933ba3
BLAKE2b-256 da88af3d9c9a0b57def2374c30cc09d652354896743c94b74732f613bb86aa80

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250129041221-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ab14ff688d11fddfdbed3e0b3c9657f240dc6d25cc65ca2df6cf06d9f51b87a7
MD5 f71ab061980b6e777e3b103af2d9f0c1
BLAKE2b-256 0ee7f296f783072b0a9e853637d3af1d0ded4976cf759f35ebbb15ed31062504

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250129041221-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 362f6f7f37935f28203c34cde628ec7f09383af81e40bbdb4456408e34c8dd87
MD5 ca690277222ffaa29905cbc41deea802
BLAKE2b-256 ed1e0e7a3c56f5928ae25ca33a50206e263021925c64fd3730199e02841d17ec

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250129041221-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 8308713edc6839946abf2ba03e876c2b23729229fd8d59b297ed54671871d0bc
MD5 fc8b6ce0fc8728fc3ea997dad867338a
BLAKE2b-256 9db57b71f53032ac8b99bcf18abb53462e0d77b10e376a6d3ef20af2deda9557

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250129041221-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 6d961d4d4ab615461fe30b7d87ec87e8d7dbfc2f3ac3af9326d13b963a9c906d
MD5 35a3fb90430fa36fe6c6af78edd1cf75
BLAKE2b-256 2b4e1e7e99d9be05657d3096652c408861b58b3434715312538113924116730c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250129041221-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9f9d89fe51f5ba492caac906e15b9b41f8056c9935ab7cfb6b0275008a97705c
MD5 fa3efae40efce43aa7cce20730fdb6c8
BLAKE2b-256 374727fa7c8348cd6199ea2aee0294ce6bf26a5df56138ed4054446e45635c83

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250129041221-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 37d6b54711f128c856b14a63a81ff2b7975057ac73d8bc0b7ad16da700535b61
MD5 3eeb5b6c767b842ecdc5069ab7da0397
BLAKE2b-256 7e84c17fdaa0d9d9d1573c9acead89a12173463cd9800f232288ec1c0700b595

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250129041221-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 18be64c48358dcbf91ea8b99cc72d79a4fc2b7e89fce544a3ad70047a030512c
MD5 1b4abc3a956ac7d9c96cdc2848421b64
BLAKE2b-256 8efa7e6535f399d1d755a0efb65852c291cb2d9ecd6c993886ea89aa10bfb3d1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250129041221-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 6724fa751c35d4be2365f27be9be2f35d3ddfbb8eae56a810079623f104fffaf
MD5 a473e8ddeca6d980d20d200a51075b75
BLAKE2b-256 fd8abf25689d39199ac04d1a4e319669d90531015fd0c511019275b20e10b5bf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250129041221-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 4206e1faeff7da66391d86fe5d968e1e2a95951729aa2f2a59e42c0b951fcb39
MD5 07b8b7d13994e52e1db620ff57a5f597
BLAKE2b-256 bf65da2e6d268a1901133a49ebbe2f3c8d3482b34e1d44238d04d8afb7144bca

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250129041221-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7ee5e2c8d277d3c2596027d074e3a980d74d48408396853ca5eccae8e0312b30
MD5 56024dbb4391aeb68ca40efbc5322746
BLAKE2b-256 1bc90464e95be7a99bdf5c758d65b0548a0c6deda9d2e605a4c9b701231dbd12

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250129041221-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ceb5d0066ed00b372fe9b531bf9b589c3e067542be625f34d5d40c9c30fe71c5
MD5 aa473493602cdbde23673b9568c5be63
BLAKE2b-256 84f88039de24b80134199b49023b6678648a75a95575be58e61371fc4447679c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250129041221-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 ff130f2e6af4bba965bbdaf20d1e26f86cc06c55616ae71cebd4e4b8a53c3071
MD5 48da70f478ec56c32b94302acfb81202
BLAKE2b-256 026a5aa0ade3482db860f59638b3b161c2235ea020c70be17c91070d9a3d1344

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250129041221-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 e475054d4f4c5483d60d526e98b4909e7d4c2964d1735505f3f90efaca6fa848
MD5 3fe1422a9f250f2713bd5cd3df18d76d
BLAKE2b-256 d34bf6fa7b9b7f0fd84fb9116479bfe4d990a6e3ec908fcbe56562a515f5267c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250129041221-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 54fbb6114ec8b00a86fcb4b1ecc91bae76812dad129a31471a7cdebc0753beb6
MD5 b6f786559aec5d9ca9e0a6b8e41eb9f8
BLAKE2b-256 07f17c2c5673004dae878387dc26552ea4bccf96ba836353146c4780285df490

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250129041221-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8715ad1032aad46e389e4d7a960fefbade3d986992a76bd561177aeaf5ea65d6
MD5 e8b5a53fe771e714e9c8a62b04676d9e
BLAKE2b-256 7a7e23f2e700ec665166d4f91b043994f6bc611ae37300eb9e5dce2f49de0a55

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250129041221-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a4996d1e2edbc9004d73ee2de7ee2195ee62d999f8c1b3def196c850f340370d
MD5 79f12410df6a191357b3f83264051494
BLAKE2b-256 83a61b5141eed37fbf8482ad6b08c64411558145065e193aa48df3ed81ace340

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250129041221-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 d283ee28712412df99ee746be373c1230b777cc9e946f73a4cfac0ecf191bb04
MD5 b9d99a6224ca59cf5770316b18701ed1
BLAKE2b-256 24c5d2a6fd7a54063c244cbf0e9ae4dafb7fc25ee8dc287b953e37cd5ae6b19e

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