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

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.13Windows x86

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

Uploaded CPython 3.13macOS 11.0+ ARM64

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

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.12Windows x86

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

Uploaded CPython 3.12macOS 11.0+ ARM64

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

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.11Windows x86

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

Uploaded CPython 3.11macOS 11.0+ ARM64

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

Uploaded CPython 3.10Windows x86-64

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

Uploaded CPython 3.10Windows x86

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

Uploaded CPython 3.10macOS 11.0+ ARM64

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250228032247-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 8241aad06c173b84dc92ed26beb0f0973185481a8a71f50e1c7f9a2ccffee4c4
MD5 8790e6e53c975733aebc4d7139481785
BLAKE2b-256 2f177ee0098d3ced5cbe5fa0ce33ecc41c21b1eace0a14c3c7adb7d455a6b3c0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250228032247-cp313-cp313-win32.whl
Algorithm Hash digest
SHA256 86885b6d3def2e73f0090832297e9d7895eb97224b709c920b2b3f4e2d604731
MD5 422b8513463a266c0388100d409bef28
BLAKE2b-256 be982a15771b88873107dd88ce4a900135fe4391a3b2834caaa21200a6abbd47

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250228032247-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c33c860b7215661e502639b070777ab3aea8371da5bd24d18df9ec7a7785d69c
MD5 15ee5834e58e6e7bec5d33902bb6c974
BLAKE2b-256 08e85b662960e483bcfb21bf9e789668fcd1920dec6d1b721eeac6848c26bee6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250228032247-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 356b5444fdbba108f318193bd6b84e7956c365e693250d05ea4b8464ae35a5da
MD5 9c317deadacde9abd7fa07147cf0c0e8
BLAKE2b-256 0320f8f84b6da10b34f91f3da305bb54624b38c3ae6659ba858c1939d0f3c6f5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250228032247-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 9c007b27174035eaa1ea1f995a962447cd4815f5e72c46462654e5c8a386a9f7
MD5 5236d1967ca1da2e48dcbc35fbfe5828
BLAKE2b-256 74b5609c5fe4f92b44bb4e90628579b5955a172f384598f48b953f05758cc103

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250228032247-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 bae691d229e02a03cd00a9bbb87d3017d14729aacd5514e25273ec269365c4ff
MD5 76343420c20d4801fbea94c34afcc124
BLAKE2b-256 2013a79a20c2803e4d002f81cb9e2461bd73f0a42e717ecf9b6e55bcf8f62391

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250228032247-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 27326b8e289fce1bb2ac22f7461f8da887ea12c6e0c703a33bcb561e1f873940
MD5 aa7ae353bdd64771985d25ee057c7153
BLAKE2b-256 ce9921ea970d8b189366df45d86f1c6ed923796bf8a6c5d9b015b86a9c8c7da2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250228032247-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 213d0d5a0a9bd145712a9180e4029f2dce646046554756fc6bd96569a8681632
MD5 cc32fd56861961c5b8bfc31acb9848cc
BLAKE2b-256 8631ed2942290a17e0e16c5afc9287a1c1f645d2f6ad414d37eba7f791156f3e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250228032247-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 4462f82e8a50bc52ba53a4f0fd05a430a242c3111c42adab07ae72075b7faf77
MD5 242d8cb5fb9f93ff3294f0e4c03ad5ff
BLAKE2b-256 6c0a78d9483213f6da641fc0133494316240854c8005f77e72d1c8ec3388129d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250228032247-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 ab29a7099cb5ba2ee756882e90ca4a442054387e4e663472681837882bd38230
MD5 d2c5ead6b059062f14450837f8d2a78b
BLAKE2b-256 700d1d24bd80cb39a2bf06d68052ea0a18c77e34b44248b88b38f265bd5bf617

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250228032247-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 97cc2b3f3d103cae6e836888d45a3006fc70f23c33d743967372d8068b201f0a
MD5 dc65a764c6b4ef5391c6ec80c3c1a898
BLAKE2b-256 b7af82769cc454fe63d7c3e96108091e2cb953fd13a48fb1c0cae3e37a22418e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250228032247-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 17b5037efad3b548fb68045c1aa8f9e629134814123d2ac4aa8f48684d730a83
MD5 eac60988437e16c07926cde41396b82e
BLAKE2b-256 88205b8b41c30e898c54434d7abc11f3b2c779413d55988adb9a5bef82b64fd0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250228032247-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9d7c2bca48565d442091f76961a3553fc48859efffff980715093b020d025fd1
MD5 e4a67f4dc6e6133d39a798c75ef5c406
BLAKE2b-256 d49d6f7b6bbeb53a0099b517ac630c556db5d5dc9105681c0805f048cbac2cc6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250228032247-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 68597643290fca942f3d98b69ea670d5cb7dd5c8ae49bbb5951cab48d65cb5f8
MD5 2c9dac6327bac29be0f684482c515f46
BLAKE2b-256 9547fe16b7d81887bc7ef0391244321f1b5275553b60f59e91eb7d3738c4437a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250228032247-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 58d7b289fd5b919400f14b44f710a129b4814d91e076d46511cd8dc3707b578a
MD5 17e746459863d615718d36c5e368ce37
BLAKE2b-256 43d3a27981507120a1e1b85121f3cee20bda2c264ab1251625892e4bd1206700

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250228032247-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 943f63da20a1e5e2882c821916ce6db6e8294ffa6acd815d1c496ba02ae7fa22
MD5 8279fa207d05e7a8d41557e6cbef768a
BLAKE2b-256 5fb498c9f13f77eb47e03f9f554fdb774cef3f04edd11c2204f91ec2cd9dfa2c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250228032247-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 70b0a6696d0733199f9306459e8a00d3c96ed597a970aad45d926c545b82333d
MD5 996fe7f58fa4836cadc253d71e37dc19
BLAKE2b-256 e02eb1fc6ce135778c7b135773541d3c6ab9d0769b88bc8b3aea5b2bd00ac24a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250228032247-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 88834ab32b4dfd16f1a42ec27e5d65933f248698997ca47669cac2a29612a713
MD5 73847e1c2b210301c1c892f73cebe927
BLAKE2b-256 f7229178d03ebf54d00624e494c2340ae376ab4df12a86c16a07815d429b5f80

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250228032247-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 fe66b96e0c8599cbff3540e8b0eaf6252f7950644b28a0a6d42f6fef7d210d21
MD5 3eb033958da52551d50b6fda2e981f85
BLAKE2b-256 06dae6a76cd8921cdb1fb6e4fcde6201d2849d8d0f05ec0d89e1d6aaf085e40f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250228032247-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 395071225744b95505d629f3e0ee592b2928a8db68c32bc4587673da3ef193f4
MD5 23b9e8e38960be64f0dda1d3ba7470ad
BLAKE2b-256 b23e7da4c8a30c67257b6bd24f2d198493ae184b4819e0bd33e114218487633d

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