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

Uploaded CPython 3.13Windows x86-64

typedunits-0.0.1.dev20250421203746-cp313-cp313-win32.whl (741.2 kB view details)

Uploaded CPython 3.13Windows x86

typedunits-0.0.1.dev20250421203746-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.7 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20250421203746-cp313-cp313-macosx_11_0_arm64.whl (813.9 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

typedunits-0.0.1.dev20250421203746-cp313-cp313-macosx_10_13_x86_64.whl (854.3 kB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

typedunits-0.0.1.dev20250421203746-cp312-cp312-win_amd64.whl (784.1 kB view details)

Uploaded CPython 3.12Windows x86-64

typedunits-0.0.1.dev20250421203746-cp312-cp312-win32.whl (742.4 kB view details)

Uploaded CPython 3.12Windows x86

typedunits-0.0.1.dev20250421203746-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.7 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20250421203746-cp312-cp312-macosx_11_0_arm64.whl (821.2 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

typedunits-0.0.1.dev20250421203746-cp312-cp312-macosx_10_13_x86_64.whl (862.1 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

typedunits-0.0.1.dev20250421203746-cp311-cp311-win_amd64.whl (812.7 kB view details)

Uploaded CPython 3.11Windows x86-64

typedunits-0.0.1.dev20250421203746-cp311-cp311-win32.whl (777.8 kB view details)

Uploaded CPython 3.11Windows x86

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

Uploaded CPython 3.11macOS 11.0+ ARM64

typedunits-0.0.1.dev20250421203746-cp311-cp311-macosx_10_9_x86_64.whl (898.3 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

typedunits-0.0.1.dev20250421203746-cp310-cp310-win_amd64.whl (809.6 kB view details)

Uploaded CPython 3.10Windows x86-64

typedunits-0.0.1.dev20250421203746-cp310-cp310-win32.whl (744.9 kB view details)

Uploaded CPython 3.10Windows x86

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

Uploaded CPython 3.10macOS 11.0+ ARM64

typedunits-0.0.1.dev20250421203746-cp310-cp310-macosx_10_9_x86_64.whl (861.6 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250421203746-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 ccd3847aa2563073e93f46878257aa8a125372ff2a84e4cb0f2f88795ffc3887
MD5 90d684f40767f511071496bcde3cd13a
BLAKE2b-256 6f076638116bb6bc498ed17867bd59572ab667eca3bc4295a063e54d78a118e0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250421203746-cp313-cp313-win32.whl
Algorithm Hash digest
SHA256 b39d012c73fca8cb51ebebc3b1eb72f2f765b140f1f2c4698fa2295560a58c1b
MD5 2359570fda0ea6d97d3660ec7381cfcc
BLAKE2b-256 d9e7243295cd38b1bc5939d516b3a0e73af18ef92c66daa560ec8d8923857cd3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250421203746-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 bd3304776158a387217d76077cc9a3cbac5cd396278bc1cbbd145f468d0b0466
MD5 b13de215e6a164463399c0efd5cb499f
BLAKE2b-256 33dc543e39d26402bb9daf6e2493182166c91a1722a7241fb25a4a5856813563

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250421203746-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 5a1c20a5cfcf9978e3e56acc188f4496b9407449691d6781228435b0493f3e0a
MD5 5535d5da2102d61852c31219fdf4a347
BLAKE2b-256 25ab955c36ab6fd9d85a93a8e1ad737c322876e7014a858fd7bfc812b0fadd99

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250421203746-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 4d3964bce2587f00e73400860bd48ff61723c8ce9bfcc261e08ecc803dc79787
MD5 3a65198f1669c0637d75c420022449dd
BLAKE2b-256 e847f3605a74ecabdb7772421a8851d5cad13f52a174243099f059ac75dd9ac8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250421203746-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 3659852b84d595d787876668571ea3c61cdbf3753444f7b17b73beda854fafd1
MD5 b70403010316b8ec89679d8c2542b805
BLAKE2b-256 0bb4edcf3516f3dac2d38075c35dec1ccf6dd9e42dc389e07f619e9333734f32

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250421203746-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 87205f00c8cf7179814723784711d6cdcddb9e792d69e73046f35d750ed511a5
MD5 4a74c5c7158ea63608b2878fa78477f2
BLAKE2b-256 58dbb082f845f5194a9f549546bf10849646d11bdf526351b7aab4541e4331e6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250421203746-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 562c0c15a9b836200bcab80759d285ff8c9a2e9f6f1bf4e32bb984be1d319ee0
MD5 a33fe412fc21b7e846d1cd077e992328
BLAKE2b-256 94407063cc50a6cc2c91acdaec8b72d7619d24bf4f81d6c6412472862cbe35a0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250421203746-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b4915e6a44459c471be05e85885b6c894fa4c35da17789e59ad14d218dd959c7
MD5 8bbca2d0572412f13e6549dab8d06833
BLAKE2b-256 3ce7ea8f98acda8f9ee7a9e1a558c7471e4bfd7596fd62eb168936233e1790f1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250421203746-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 75e5b9754182c9ac9ccac74bbcd249dadc2288fca119f13d4706f9a7377112b4
MD5 148b17e53fd81e4fda5c687a79f61372
BLAKE2b-256 212ddbb717c236debb52623e8471c0dca36fae3648e4da08f78fb241b4ff7d86

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250421203746-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 e8f48e1116cb5bba5949b05b3b3fad6ce3753f0119897d167f0503a139b53cbe
MD5 8fc87bded911c9f2b65a027dace46d01
BLAKE2b-256 c3cc4cca18d5f2d1c5a0ebcf470e40c894ba1e622b145b0b78695a6166a78f0c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250421203746-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 81d6735ddd521f0868dba1677152e3a9dc2b7722de524840a7afb57ac805aa25
MD5 e33d16d0777c23ce3af30c3e4f5f2294
BLAKE2b-256 b5b556aeddb87962db8752acb796464bef1581171dc9e9481a17cd17a577c51c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250421203746-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 125e6fa8c5fd3a7e9004742b89712b82eb65ed0ba59e2ad73605973ac5e21e31
MD5 1792cfae73b4d8d35d840480e0cae669
BLAKE2b-256 83070274b113d12600c8a6fea78af32bc1589000bca960446b0a4913dc091658

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250421203746-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c0214222c13aac2e630d22fee148867dad48c6f4666926afcf3a1c317ac20b54
MD5 3419a36598c7cd841266b0eb20b3dc05
BLAKE2b-256 9c65483bffefcf31a09a763f6dba91b421f93b70250a135148ef319d21a78a92

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250421203746-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 63e5b554963e6eed5030e0af8366f4faa74eca04223513c41af5a501faae377d
MD5 b15b668bc142f09115ed02d392fbc98a
BLAKE2b-256 afc245a3212f90e6ec2f422b065c7da670013adf0824a0bf8d3650f9599bd5c7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250421203746-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 67877fb9ab43fbc985c73123daa0dc076bb33e39e2773f1128276871a987eaf3
MD5 5f1f4737e51b31d5d13c3447627d6a32
BLAKE2b-256 38fd350f1ce541cb123906a9426847763b65037e5ced47c60cf23f369fb38639

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250421203746-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 00a304c4ce6cc4ff6c2cb8544dc1fdb858fd190b09aa6f497e11bf0e644884d8
MD5 5f66ac8d4cb151c9aeae1ba8d7542e4d
BLAKE2b-256 32580f14d93bd0db4c8d13713b790175ff26b8330da5517742e99cc1c40fb407

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250421203746-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 387d31e6a69a4d5038e30edb2a3b76db092366579f8c66414ebda1e0187b2854
MD5 6df15115dfdd28187f3c7a8c1f432c32
BLAKE2b-256 431c1ce26e9d5912b46c0e85b9f9c4d584bef04aa1039dce9ee5576145edbb34

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250421203746-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 53bc81fe6670fc2fc6cde1481e0d16177aa26be6f92820e73636b304367d1582
MD5 d6b317a98e889ff7936ec36fa1379a6d
BLAKE2b-256 4389c536fba877811549ec33191f59b4d71a75d6d24a43aaa9a310d0f8873794

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250421203746-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 d9e70cae42f444af4d7bc6f87bd2444eb1587a0295647da9233c0c3bd1549bf2
MD5 40bb66e79cc878e33c6f7a0b239e0d96
BLAKE2b-256 9d0fa6c1748c1b00890750948bd346791b5cd53f7f1fb62493c82cc991197d7b

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