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

Uploaded CPython 3.13Windows x86-64

typedunits-0.0.1.dev20250903181955-cp313-cp313-win32.whl (683.6 kB view details)

Uploaded CPython 3.13Windows x86

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

Uploaded CPython 3.13macOS 11.0+ ARM64

typedunits-0.0.1.dev20250903181955-cp313-cp313-macosx_10_13_x86_64.whl (798.2 kB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

typedunits-0.0.1.dev20250903181955-cp312-cp312-win_amd64.whl (734.4 kB view details)

Uploaded CPython 3.12Windows x86-64

typedunits-0.0.1.dev20250903181955-cp312-cp312-win32.whl (684.5 kB view details)

Uploaded CPython 3.12Windows x86

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

Uploaded CPython 3.12macOS 11.0+ ARM64

typedunits-0.0.1.dev20250903181955-cp312-cp312-macosx_10_13_x86_64.whl (803.9 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

typedunits-0.0.1.dev20250903181955-cp311-cp311-win_amd64.whl (814.7 kB view details)

Uploaded CPython 3.11Windows x86-64

typedunits-0.0.1.dev20250903181955-cp311-cp311-win32.whl (748.9 kB view details)

Uploaded CPython 3.11Windows x86

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

Uploaded CPython 3.11macOS 11.0+ ARM64

typedunits-0.0.1.dev20250903181955-cp311-cp311-macosx_10_9_x86_64.whl (848.9 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

typedunits-0.0.1.dev20250903181955-cp310-cp310-win_amd64.whl (814.6 kB view details)

Uploaded CPython 3.10Windows x86-64

typedunits-0.0.1.dev20250903181955-cp310-cp310-win32.whl (719.6 kB view details)

Uploaded CPython 3.10Windows x86

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

Uploaded CPython 3.10macOS 11.0+ ARM64

typedunits-0.0.1.dev20250903181955-cp310-cp310-macosx_10_9_x86_64.whl (811.3 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250903181955-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 a64f2b2a10b222861bc31b8d429af6811c643dc51686cafdb759278ab831f97f
MD5 a44762a4ddc6cc59731c1510ce8e61f2
BLAKE2b-256 db76dbaf5542bce3922d2d3af2954446ad1ba52ea4a39246528a8aa2edc5cc25

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250903181955-cp313-cp313-win32.whl
Algorithm Hash digest
SHA256 9cdf913aa91b4730a1c2ad5aa6a7c008069e01fae19403a76721bad0be6cf12b
MD5 db87a0f0bb45538deac956a81297a541
BLAKE2b-256 c2536c1576cd27ab05f7d32b67d1900199b08f452a9ccde547917bc1502176ab

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250903181955-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 eda6c935ee318df1494ec4cd7ac66938a42c17324c929c7315fa74bba9d2629d
MD5 1fd8d8d63a67f4b3703c20b128c81160
BLAKE2b-256 8d809089862cc5ae0f3a4cdf005f38f770a6545bc4268ad066693c836ce64838

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250903181955-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 db64f54d229861670f69441259f70ff8a43de028170637dcba912d9185a575ce
MD5 25a106826bbae72208580769ebe625e1
BLAKE2b-256 998e3a5dccd57984b410ae915cacf2d0ce4f2bea27aac681cda6e9edc42e9272

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250903181955-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 de90e77ed2311fba9eff87990555304b8a2038b4b3c19f7091a11d8e6c091984
MD5 5d867503d2718b5a39e2de8683fca5e0
BLAKE2b-256 55d86dd3b60b0fe233671e70eb652063c194131546a5e0db73c63c81441de1b5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250903181955-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 47b5d8c26d6efede9aa401406119e19f2249edb838f31741bc7a143aafa41384
MD5 59cf6c959a3395650d444b4eb12fb218
BLAKE2b-256 6a2e7d2cd9769b845053a0988ebe35116bb1df82a963154511acdf31ac709b75

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250903181955-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 bd74e360186d4a1df0568d8e72556a33e051baad5b6258f88d3a54dc3d3ac968
MD5 71b7cd8345f867470ae3555a6d33f9b2
BLAKE2b-256 002185eb5bfac32d543979259fd8ea11c8cc64347b71e53ec9f681e491321bd0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250903181955-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 dcbf6f6377668ae9c696a491d667905f5c46480064b2de190a16154d63504e8c
MD5 becb6ee7d44113d4b44cee82279a9b80
BLAKE2b-256 ba748bc1a937bcec686d3a8ed8c47baff989c57a16ff4e7ffb02af0c46420787

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250903181955-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1328124c725588d9030729ccaaca701413c362feddd6af9aa1155e319c6601d4
MD5 4bd211daa05b844180e291295b22446d
BLAKE2b-256 c16b3c76a92444f7be62a51a083ac7fdecfcafbd1906b1526b1a1373b6c1d7f5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250903181955-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 32f8817d5c2b25b90ba6671039714412f1a2d0dd379913dfa19210018e206235
MD5 6ea9fdaec8b20297b2964e1872ffc0aa
BLAKE2b-256 9151e0c3017d2db94de5f6101afd31b1cbe159116e7bc3cec09b1b7ffdddd49d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250903181955-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 16b3086af0aaa980d01a3ca7018ee10665a9b941cc15e10b84ade9d36a0fec27
MD5 b9a32b2892d436bf9240501215f28569
BLAKE2b-256 f9b09a127a8e67d394028ef5a17d3fca65f929b84f6b81129e763a747bc66e86

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250903181955-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 3459f4a20fe7b1a6c97424b2d307409d8ea1316646f7b3687f8acc40518cdf67
MD5 2fae96958360b41375c448ed72eb3d74
BLAKE2b-256 9f163c6082f9da50bc64e521d1f775c18c61f020736f38a3a63ab357ff283887

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250903181955-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9168687d695ad5ceb8da7f8de8f7d3e0f86c0e410ba8d2dce8ba34bac68bdd95
MD5 284430942b7ef0c0130e2fbde719155e
BLAKE2b-256 bd73715a47d39af08c40faf3efabc03708930d21856cbc826a3af43391941e83

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250903181955-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7ac0236b6aa1bb7a4815cc7a2bec2ed99c66bc3788eb1d7863e6f779f401847d
MD5 b7d24d99f4c2f20387284f87c2b140f5
BLAKE2b-256 a6511d1538a5ee8e1649fa10eea8ba52d001df1dbe7a96734fae93e18bbbe51f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250903181955-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 812943a134994e4bf528ef78be723765bc3b8d7ed2ca782474eaee7af67645f5
MD5 2dfb5ecec222ce261c21aaeec40df03f
BLAKE2b-256 65c1c184621a55e9b1907f6d02dec7b4ee0f7dc5f5caf319ea8c03a1463aaf69

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250903181955-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 961183523ed8f76490e5e64cd4b168a070815a32dceb2b66705cc26e3a0500d0
MD5 97e6f46f83b52ae238b951ad91fa7e0b
BLAKE2b-256 de594dc3246d721cb228ca23b249b16071eda79672a4655a4cefa2b3f60af3ec

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250903181955-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 f4ad46e4cbdad267a58bd76d5db8c693a0d8dddac4f4135c1a45a6280fd4547e
MD5 b2f342d551865e949e67a588703efe93
BLAKE2b-256 f53c6cd8a6e01b9a37b5fee50273f5bd89f89dfaef3a55379ebb204a5a48efb1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250903181955-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 949ed732c9f805468f3496cdc18d697bb50601263a6468e6c38f40666f50406e
MD5 945a838ff03f6ae9d2059b6412351d08
BLAKE2b-256 f7d33982ed54b10dc24896b057a18c0d78b6e2b15a6ee488f428934c0b73104a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250903181955-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e3e61a47a230cfe251ef7420284804634df8d8f45af7ce7457a213824babea1a
MD5 204a7ef5ab2b963e8c78a160b91ff41b
BLAKE2b-256 9470b67d83a7708aad954740b1f465379306cf695f642e84d26447b5c1de8ab0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250903181955-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 e4a2db205c92e30a0c153b98d0b52bedea7e6aaa9ffe6e2577af242a0481d480
MD5 ac2a42fd1a7bdad612641bdb014e15d9
BLAKE2b-256 40eb8d5e2428f7cdd53a0658712f3ce630bbb726529c9f1555bb79630d9b47ad

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