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

Uploaded CPython 3.13Windows x86-64

typedunits-0.0.1.dev20250227014329-cp313-cp313-win32.whl (709.3 kB view details)

Uploaded CPython 3.13Windows x86

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

Uploaded CPython 3.13macOS 11.0+ ARM64

typedunits-0.0.1.dev20250227014329-cp313-cp313-macosx_10_13_x86_64.whl (819.8 kB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

typedunits-0.0.1.dev20250227014329-cp312-cp312-win_amd64.whl (753.1 kB view details)

Uploaded CPython 3.12Windows x86-64

typedunits-0.0.1.dev20250227014329-cp312-cp312-win32.whl (710.5 kB view details)

Uploaded CPython 3.12Windows x86

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

Uploaded CPython 3.12macOS 11.0+ ARM64

typedunits-0.0.1.dev20250227014329-cp312-cp312-macosx_10_13_x86_64.whl (825.2 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

typedunits-0.0.1.dev20250227014329-cp311-cp311-win_amd64.whl (780.5 kB view details)

Uploaded CPython 3.11Windows x86-64

typedunits-0.0.1.dev20250227014329-cp311-cp311-win32.whl (744.8 kB view details)

Uploaded CPython 3.11Windows x86

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

Uploaded CPython 3.11macOS 11.0+ ARM64

typedunits-0.0.1.dev20250227014329-cp311-cp311-macosx_10_9_x86_64.whl (863.6 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

typedunits-0.0.1.dev20250227014329-cp310-cp310-win_amd64.whl (776.6 kB view details)

Uploaded CPython 3.10Windows x86-64

typedunits-0.0.1.dev20250227014329-cp310-cp310-win32.whl (714.0 kB view details)

Uploaded CPython 3.10Windows x86

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

Uploaded CPython 3.10macOS 11.0+ ARM64

typedunits-0.0.1.dev20250227014329-cp310-cp310-macosx_10_9_x86_64.whl (829.5 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250227014329-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 a9e8ff4bf40443c4e927f2533f0b4cf04c2096eee1c2451fe0d15d1129bbf794
MD5 522b6b633accec32993797b0b881cc23
BLAKE2b-256 3e6cea83f390310daa5f40b851af6d58161ef88d76ce8437a8522c1d95b64dba

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250227014329-cp313-cp313-win32.whl
Algorithm Hash digest
SHA256 abea16aac683ade7ba33f90f2819f97de87aa1145c2f4d9faa185c9fd7c89e27
MD5 8b291f4a8924504496647a333839aea6
BLAKE2b-256 a8f5bb808a7596727ea0bd697a7cee8e78970423fc7bcc6ca8f2685bcec63e5b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250227014329-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 479220fb8686f6e04e353130911866966f12553fcf0f26c8844e5b41f07c0ab2
MD5 01e436f0e9b4c44430281260e88ecff3
BLAKE2b-256 5f08b7771075d62340cfc74631e1ece44ac73335b4389d9719e473f1d9baf888

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250227014329-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 92ad8f765ebb23cee90540cf9e01ad770d4be9d078e123204c19c2f527df67d0
MD5 b117fc33cf7c7edb61311461e2ae5940
BLAKE2b-256 6e92b0ca444a505fe413e0a1ad79ee0ba9543b9e86f5ff1bd00695dbeb17f199

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250227014329-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 25910452f65400ae315c1ba123cb21b14f2ad067b6ab77c5aff85c4fb5d79ca8
MD5 25bc961fa130668920bb2ec08fbe0ed6
BLAKE2b-256 0497a8d270c4c41a98ed8a2ab84ef7a9b823d3d32e9eb89e559ecf9b309ab083

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250227014329-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 2e4fa347cdaa0a92a63facb09a23c61706d9ac49457114a735e337510258b429
MD5 9e1a380c9ed040a2943587d845918a27
BLAKE2b-256 51ecee647ef8403a947ea6b9e1f10bb948f422fafc7e5a24ffcbfac741672ac6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250227014329-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 ba2b46a130888f2b7d662b2720570c56c8da1b1d7651fda8daa9b87dc83189cd
MD5 35938c808acbfa75e28896b60f0ffd28
BLAKE2b-256 6f2a0d3cc0337ff41a1810351359a03b2615684afb96364dea15dafcd8ecf35b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250227014329-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c8a91223ea318acfc31547b339caab4d7f52a3d7791450a9091472adb92381d7
MD5 c86d82ed33018c41e02c91eee60c0693
BLAKE2b-256 147dd6cb700d37719197a05187f9f699ce947916b1bafcfe059dc119e5e64f2d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250227014329-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a0e5772b655d9fb53e0509f50705b432e842c9a189e77298a13924ce586dd4b5
MD5 9db8884783c571c1f36a08aa705fcbc3
BLAKE2b-256 4f95eb576232d56ee86dec2424fe0dc974bf969f65b1e7925352d81ac1a712ae

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250227014329-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 d405556b322b33b81e4687ea7b49ade4d4d8f52e7fdb7fe35dd27c46d559aa35
MD5 67723035cedcd0364f8960bd69bd2574
BLAKE2b-256 6bea47ee40f45674c33e9e86660a868e49ae2529b8dc765db70f24cf86db3551

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250227014329-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 f22fa14994875028cf29ee83c5b0a88427b8061ce31f6437a97e35e1fb62990e
MD5 cf3442ea7187f8ac38d8f363ba3ddba8
BLAKE2b-256 c9976dfb06c9b62d0cf58563976d4d61067e4396259f8257e1c57c2a719cae71

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250227014329-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 22b3eb4af2dd50aceea5b5bb143bbd7bb4df0e8bfe9ff70a10a3b6c14304f034
MD5 7541fab0b623889dd5c9c1fcdc5ff989
BLAKE2b-256 50d9421dbc362fdd1e7349d104f068e7ede261007d09d995b4f97988a6102469

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250227014329-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 68916877c01a3ff89a8948fda2856006d69bdb1ab4d82ce09701352ab46ffa3f
MD5 24dd858085bc57b257a7311be86c1b01
BLAKE2b-256 bf98fa8d3c8f5d0d87b66241f86ecad79b56bff408c247977fb609924d45d290

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250227014329-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c8c1573cc14bd5343665855757e5bd1d1b5e58dbc307dd76beb2f4c539b30d41
MD5 3da7e0a47e302cd4a38dc7166e3f56dd
BLAKE2b-256 a306f3590c39a86b588ee7dfebc04c9d205c415c62d0ff5611b87ce82de360f5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250227014329-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 3b94867c2dbedc2b162286aac7260b9e2c72169c6189eb1c1c1733c1cc35daab
MD5 213c551d5eb56d0b60156ef879503765
BLAKE2b-256 3790c28a11b8cee0b3479b6701077718109637fdcc91f811e33681a0a4ef0038

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250227014329-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 716842b0be1c7620230e6c16c2f0fc22e5b57330b481b0050ec4fd733a33e7c8
MD5 6fb4093d3265c72ef114388832207c45
BLAKE2b-256 725b5d7ca24f39c2a8f37216d692068b5309acc9d8c9ac0ed09c6a64849df8df

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250227014329-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 175ee32133c2709ea9951d10df3fc04d07e3062bd2dcefb5e3ad6ef2ca357bfd
MD5 e6bb316c129f6acaec509248f2a43923
BLAKE2b-256 741fca73ef6ed52147688dee650666fced7f8ff08354280a126870b1003fa09a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250227014329-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 70a47f46e65f30cbd0be1adf9bc80b3500146ad7018f0724f812bf91a7307457
MD5 b28757b23f68a5e26d0ec64071d94908
BLAKE2b-256 8f938e6193df89b3961388f88054fa0c8269bc198eaa97825eae323afb51a81f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250227014329-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1689030a4a9e2a5fd171bff8a4941dc3074179610155a486231fd3a48558b56d
MD5 46821eb68a42a460991931a136b531ac
BLAKE2b-256 650ff7b7c24aa3c60e5716c90fcef1920b6f4f3cb6cb80053ce0d0950e435b2d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250227014329-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 b0c7c0cc1ace6ffa228e7e3585a2edaeda41c78eef410536fe6296ee3ae90486
MD5 187428ebd268bead4f163e599a2e2ce5
BLAKE2b-256 2933657fd93302b65d6af4f9a1cacc0a762b01727fe26260998a48cbcf6b5e3f

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