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

Uploaded CPython 3.13Windows x86-64

typedunits-0.0.1.dev20250404020538-cp313-cp313-win32.whl (733.0 kB view details)

Uploaded CPython 3.13Windows x86

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

Uploaded CPython 3.13macOS 11.0+ ARM64

typedunits-0.0.1.dev20250404020538-cp313-cp313-macosx_10_13_x86_64.whl (843.4 kB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

typedunits-0.0.1.dev20250404020538-cp312-cp312-win_amd64.whl (776.4 kB view details)

Uploaded CPython 3.12Windows x86-64

typedunits-0.0.1.dev20250404020538-cp312-cp312-win32.whl (734.3 kB view details)

Uploaded CPython 3.12Windows x86

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

Uploaded CPython 3.12macOS 11.0+ ARM64

typedunits-0.0.1.dev20250404020538-cp312-cp312-macosx_10_13_x86_64.whl (851.1 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

typedunits-0.0.1.dev20250404020538-cp311-cp311-win_amd64.whl (804.1 kB view details)

Uploaded CPython 3.11Windows x86-64

typedunits-0.0.1.dev20250404020538-cp311-cp311-win32.whl (769.4 kB view details)

Uploaded CPython 3.11Windows x86

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

Uploaded CPython 3.11macOS 11.0+ ARM64

typedunits-0.0.1.dev20250404020538-cp311-cp311-macosx_10_9_x86_64.whl (887.8 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

typedunits-0.0.1.dev20250404020538-cp310-cp310-win_amd64.whl (800.9 kB view details)

Uploaded CPython 3.10Windows x86-64

typedunits-0.0.1.dev20250404020538-cp310-cp310-win32.whl (736.4 kB view details)

Uploaded CPython 3.10Windows x86

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

Uploaded CPython 3.10macOS 11.0+ ARM64

typedunits-0.0.1.dev20250404020538-cp310-cp310-macosx_10_9_x86_64.whl (852.1 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250404020538-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 0c6a5ab804aa6f78df5e0d18e19e23670c001fdd87440791e1abb90b5e01b273
MD5 cac1b7304724d5cf08b7057f9b899e5b
BLAKE2b-256 3085a71cd8974f9c0c93e4ac05a5777c4bb748c0525fa1fa254d3d0a7ecf8528

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250404020538-cp313-cp313-win32.whl
Algorithm Hash digest
SHA256 95f6fcf10568d9e68d8bb564bee880c150b55d98c767a8a0596ab47119c43e37
MD5 8a6fd7395c1429bc3dadc51224f8894b
BLAKE2b-256 bd8be21fdd9d14e697b18fb1317a56bff72a40a43959862bfd4fbf81de8d4eed

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250404020538-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 2a918bc1ba219cb781f3ba69920ec459c9840c217ebccfa1c7107f1e2f260a8f
MD5 81ed9defa21e4d118c0b0ab49114eae5
BLAKE2b-256 8ee543d1e7bfcf16f71b2e3e82fcc572c63eba2797015bd17f4a1b8985e58093

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250404020538-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 64687be55059a4264a4ebbd028306e8e685d0fbe5300c0bcad2e2d2a949c40b7
MD5 10fef43a2b6f6ac34f1fca9491b7f80a
BLAKE2b-256 e41a974190948d12a89912c8810e808be36f39061c2cfab0c638e3a3c8277ef6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250404020538-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 c9b1d2804f02449c866e43432027627296da6dcaad6ace09cc04fd0bc697fd5e
MD5 8d6c9d77674fb3bd83a8904cd7f23a0e
BLAKE2b-256 5f92abc855868945f6be4f0a49bb6cf3df6c3d18d8ed4a434dc236b69b05bca8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250404020538-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 822fabac27164d09b8a2b2994cdc2e5d735bd820ccd8de95425279b1ab3cdbcb
MD5 b60118c771d747e537a286c26e304458
BLAKE2b-256 af784c00b1aff73cfa1615f90487eb127c956a53c451ae92d33f290e96a1a44c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250404020538-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 f0791606029cd00bc55c741e27bff516271749fbde12654c171db4fa716f020f
MD5 bf5e9c65636c130233de29a8121c98f5
BLAKE2b-256 759e153ed2800d804cf5597fe8f799284bd3ec228f6b71580472d78737cfd667

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250404020538-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 4ad5cdc1de1694be1f6ecbd5f261f08eed894ad769c2dacaf52fb17a9256d125
MD5 d637ca6f4722d3b83a34e20c94376a07
BLAKE2b-256 c6867017eb5bd7689aa79e67933ec26e094b19b326923f33aa57437c88c12fb6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250404020538-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 53a7fe51a296e9fd2fed8a90d48c3ce7d91550abd6bce348efe0fe9f9e5edfcb
MD5 cfd7ddb5595c2ca3858b5d2722ee38f2
BLAKE2b-256 9b6e809857cf60e598f3e997a4ce43bc729da0d29aa33c891f53706e4aa2250c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250404020538-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 c34457c684d5dfb0bd39a703abbd2c05c3ef0044a0dbe0392a55895b21bd117c
MD5 21c8f03f9d79c9e90ea0b5180f75ea2a
BLAKE2b-256 fd1986c7fef1957fdcf5b99d11602190070e16620aa8afe94d7ae27be7371315

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250404020538-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 a509bb3e296f2f084d1e9617beb1e1db927fa8015bbea28d0779fa0e26d43c30
MD5 f317dca5f74f93cef13156e9542976a3
BLAKE2b-256 5123497eebd16eb85b82b80161064a5ae2d05b105d6b28697c2c9eb9d9702047

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250404020538-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 4966e591e2234f01e488e1d7981cfa4ca0c967c2c779e8c4a29f6a373b9784e6
MD5 31ca580c48e63723490b29c13a997e75
BLAKE2b-256 5f8ccc52d5df12aa880b91fca6a37c8933eb1d4cac9f832bd15b55f0be0192d2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250404020538-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 31691b7d535685116668403f6e37aedb5e1899a6d24bbe8d0297ef8f583637b6
MD5 946cd4a45c029f7513797a835068af74
BLAKE2b-256 ba1128ee29c49345bb0870a1534f4850736ba409504dcbc1882221cf7b3e5da0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250404020538-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ceaa2def52bb7040478a8373d08e8be50d205122243902d83935c067d727fa89
MD5 923152e0fbf9c0256d6d0ad9a94cca9c
BLAKE2b-256 b2247dcc4075d0cc6db23cb24f2c461f7a62d653b6a69dd5eb6dfec3bb770a44

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250404020538-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 c953caded41e2b0b3b2510e363f5bbd1c6a7ffa40745c89f81395a5758e0e544
MD5 148bf8cced304a804d4791c28f880e66
BLAKE2b-256 8a46b886bc6133ccb79700d7ab6c4d6176ab5f0a108723490fd2501bb1e426ed

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250404020538-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 73bd5efe72880ca8c4f7148f3731f0854f83309a7221027497dabe412143d30f
MD5 5d0d90efdc89d88d0b930685f34f632a
BLAKE2b-256 e720a6d5240ea6697b0bae3634852a4629730f053342e8699562901751248acf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250404020538-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 d5b4d8ec55955a51aa9414129f7259222973be2297975ca3080fab46d39ac829
MD5 bfbc81e9ca6315efacc9b8f0f14fb4ea
BLAKE2b-256 9337d5545c1575b7a02f3673443fea62be57367e52dc356d04c55ec55a385e67

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250404020538-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 446941f1fa5b46a1e4fdb9aa809ca45958dca99dc23d3c627c340eef16204b1a
MD5 256eed4cf8b17d00195548516efe9321
BLAKE2b-256 25248b13c52e3ac13aa6c19f127ff3b5a150d89347744f70414e0c8f1be7bfdb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250404020538-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f9065eab2bbe14c0b2926d39619d625bc045b94bfcfec8a116dc92696bc78232
MD5 6a77049938cfd435fcb1dd4d71f42591
BLAKE2b-256 ece41291a56d442352e1ace44abcd6b40f91ba32bd6c483dd9554ee9e3a8585d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250404020538-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 eda1d5cd00aee6e000280c392e1c4e054f33c479f4ca7da4fa87cda61895ba63
MD5 b2a3a8b2f131d237c001427499b488cd
BLAKE2b-256 8fc6b8da5ca3d2591646df93e529e816146afb629bd689c2d73f2f8084fddc54

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