Skip to main content

No project description provided

Project description

TUnits - Fast Python Units

GCB CI status:
GCB Build Status

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 .
    

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 .
    
  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


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

typedunits-0.0.1.dev20241018223718-cp312-cp312-win_amd64.whl (705.9 kB view details)

Uploaded CPython 3.12 Windows x86-64

typedunits-0.0.1.dev20241018223718-cp312-cp312-win32.whl (665.8 kB view details)

Uploaded CPython 3.12 Windows x86

typedunits-0.0.1.dev20241018223718-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.4 MB view details)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20241018223718-cp312-cp312-macosx_11_0_arm64.whl (739.5 kB view details)

Uploaded CPython 3.12 macOS 11.0+ ARM64

typedunits-0.0.1.dev20241018223718-cp312-cp312-macosx_10_13_x86_64.whl (771.1 kB view details)

Uploaded CPython 3.12 macOS 10.13+ x86-64

typedunits-0.0.1.dev20241018223718-cp311-cp311-win_amd64.whl (733.3 kB view details)

Uploaded CPython 3.11 Windows x86-64

typedunits-0.0.1.dev20241018223718-cp311-cp311-win32.whl (698.9 kB view details)

Uploaded CPython 3.11 Windows x86

typedunits-0.0.1.dev20241018223718-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.5 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20241018223718-cp311-cp311-macosx_11_0_arm64.whl (749.3 kB view details)

Uploaded CPython 3.11 macOS 11.0+ ARM64

typedunits-0.0.1.dev20241018223718-cp311-cp311-macosx_10_9_x86_64.whl (807.5 kB view details)

Uploaded CPython 3.11 macOS 10.9+ x86-64

typedunits-0.0.1.dev20241018223718-cp310-cp310-win_amd64.whl (729.2 kB view details)

Uploaded CPython 3.10 Windows x86-64

typedunits-0.0.1.dev20241018223718-cp310-cp310-win32.whl (667.5 kB view details)

Uploaded CPython 3.10 Windows x86

typedunits-0.0.1.dev20241018223718-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.3 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20241018223718-cp310-cp310-macosx_11_0_arm64.whl (717.0 kB view details)

Uploaded CPython 3.10 macOS 11.0+ ARM64

typedunits-0.0.1.dev20241018223718-cp310-cp310-macosx_10_9_x86_64.whl (775.1 kB view details)

Uploaded CPython 3.10 macOS 10.9+ x86-64

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241018223718-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 80e7d344e6da9307f62c937aed80055eb9a290628d7a5704ba49f7d3fda10b85
MD5 db3e4d0586f8a4385e71855f2aff5b93
BLAKE2b-256 c30dc5c6f839bf0cd647ea89e6a983a51a0e8bab22da8a628a54f9189bc60259

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241018223718-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 a73583f02051363d60015345cdd7eb10276fb68c0db91ed66d44469b55437c0d
MD5 367aff360c7530c8d96c0a9f8771396c
BLAKE2b-256 5b8554a9d46e4689b346b2af3dc008d3333c055eb6442e558d939c6f36c42a50

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241018223718-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 53636b444334ed401fd65a844b3721202a3e1993ef06f29b1334f1461d8f8bec
MD5 18206fab8e51ef06b612d2a46d881a21
BLAKE2b-256 e8f4dc71ad7b1cb77e1899c5d1802b2db0a7fa290482f1bd68faafac2e9730b5

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241018223718-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 fc3a930fc62c871a7eb1d03a409ba814fd14ed69be1055d3081beab9190f4461
MD5 008b753c4380891554ef76def96e6c0f
BLAKE2b-256 9507a186ef064e860e9dd91302733500ca29570d7c34b1b1a71c56e164fa0049

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241018223718-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 f8ac0c180099d5a34cc046f34e3f39aed3f8f5f074f660808341721d35933d9d
MD5 f3d99dc96d7533dfd72765fdba777ccd
BLAKE2b-256 6952496d9f634116b88bd293bbf756d2ac86d368a1f34c49ffc6929346647dcd

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241018223718-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 3af5184095c31d1baae10f71f9668b5bf4b263730c6afb1dad5d47ca2cabd62a
MD5 a8259cbe91bd0be755b072780449446b
BLAKE2b-256 1befe6b72a6aeda51c6f1202c0fe2dde8673271b037e4c9531ac35c4df5ee2fa

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241018223718-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 f1321c2a2050e0bab6a7c38239ce84477953b7e9b53dc89214704cc11e2e7617
MD5 ff8ad12157791ffd0d2ee9ac54e24527
BLAKE2b-256 72ad7caca45676a2e189447fece8f4c94b2e97c37bdd1ab849e7ce9a105ba0ec

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241018223718-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 2932f61e2c887b64baa7bbc63f7deb23421411bff76a8f2770728a65f6f78002
MD5 eda4c09727099227427ce3bcb6c6d7d3
BLAKE2b-256 3b0b865cf051d4e28ce06339f1fb43965f4331e0f8d3d510e65cb12c1236d0c8

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241018223718-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 116d4008f6c7da29b3549ccdd91c3094b4facdc308d0bea8cb819223abd5b8b1
MD5 f70043e8652b601e459f9720d4afa7e8
BLAKE2b-256 edaa3b7c7c2b5a9cd26dccb32231d36464852d8e00e954685dac341c09ea9402

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241018223718-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 431af2dab62a41cfaaf9c9777f8b95478a447d2303318ecdf4dab709dfe96137
MD5 7af8c3d139d344ff7d2d7d84a8210276
BLAKE2b-256 ba29f224278bf746301130539131ee81d18534af0574fa248a196d26dc7c872f

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241018223718-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 006f0cd1d8731c4faa20fe821cbce637146fa16769583ec888043aab8ffee32c
MD5 b981752494c3450ba1b606dac952dd88
BLAKE2b-256 19fc255c9678c94ad9ba9749ec2ce86724a712f9e93422e602508da48cf132f9

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241018223718-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 35bd11176376b671567354b27846327ab447c9795fb834e4d423e235c995361a
MD5 a559b8dabe54e5adb7a24a916c87aec2
BLAKE2b-256 fc2d9f261a973f5f6efb2d47f06aff0812d33422d9ec6ebba5791be3a622233f

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241018223718-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f8a66e6d6ce9bc7fe0d1a53bd95938a903824f14d88d95272d3319872eb90d6d
MD5 ffdca6fdfbd4c05655bb3a9fcef85f86
BLAKE2b-256 770000f270ca7ebecc0b8be5ab1e8c70049faad0782da5c6e0879ea70492817e

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241018223718-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 021ffab55a0d6dce40aefc00259d0aae82eb39d6fdc7c86d8a6f397b5f7aaa50
MD5 71be2083e394e9651f2a2bb256121020
BLAKE2b-256 0c6aab0304efeb9e5414d7bba6fc0dcf4bb054ebc177c0d708e6ec8d904eb6e8

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241018223718-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 21d49dc0e7046440d388859fa93e639ac385e9cc41a6ad4a0cee5ab70c522fdb
MD5 23e0201953c626e46e87795b40d331de
BLAKE2b-256 5bb3aa711b727a4637cbf04dfb89511cc8d57950fc4c27c44b169bf8d9d58528

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page