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

Uploaded CPython 3.13Windows x86-64

typedunits-0.0.1.dev20250305185237-cp313-cp313-win32.whl (725.6 kB view details)

Uploaded CPython 3.13Windows x86

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

Uploaded CPython 3.13macOS 11.0+ ARM64

typedunits-0.0.1.dev20250305185237-cp313-cp313-macosx_10_13_x86_64.whl (836.2 kB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

typedunits-0.0.1.dev20250305185237-cp312-cp312-win_amd64.whl (769.7 kB view details)

Uploaded CPython 3.12Windows x86-64

typedunits-0.0.1.dev20250305185237-cp312-cp312-win32.whl (726.7 kB view details)

Uploaded CPython 3.12Windows x86

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

Uploaded CPython 3.12macOS 11.0+ ARM64

typedunits-0.0.1.dev20250305185237-cp312-cp312-macosx_10_13_x86_64.whl (842.4 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

typedunits-0.0.1.dev20250305185237-cp311-cp311-win_amd64.whl (797.0 kB view details)

Uploaded CPython 3.11Windows x86-64

typedunits-0.0.1.dev20250305185237-cp311-cp311-win32.whl (761.6 kB view details)

Uploaded CPython 3.11Windows x86

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

Uploaded CPython 3.11macOS 11.0+ ARM64

typedunits-0.0.1.dev20250305185237-cp311-cp311-macosx_10_9_x86_64.whl (880.3 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

typedunits-0.0.1.dev20250305185237-cp310-cp310-win_amd64.whl (793.4 kB view details)

Uploaded CPython 3.10Windows x86-64

typedunits-0.0.1.dev20250305185237-cp310-cp310-win32.whl (730.3 kB view details)

Uploaded CPython 3.10Windows x86

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

Uploaded CPython 3.10macOS 11.0+ ARM64

typedunits-0.0.1.dev20250305185237-cp310-cp310-macosx_10_9_x86_64.whl (845.5 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305185237-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 b199d5560802e636a365a753d7a1bc845fab5cf87d7ee83d3db16d69912268d5
MD5 fafc4bb94dddbe18f800b69a27cd8192
BLAKE2b-256 167699767e7d8875718d68543259153898c41b23ab8d84faeed230d4e47d9ab0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305185237-cp313-cp313-win32.whl
Algorithm Hash digest
SHA256 6768823153f1a0a7543f55ffed722ef1200d716bcdae736753a074b798c87ee0
MD5 108c752b3a04747b18505c9b71468182
BLAKE2b-256 033ba6f52e65bbcf1f6aa0dfdc6722a238990152403f85266902f160c91c5d29

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305185237-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6aba85ea4b7b4dc579b282705f988e6e9a2ca4b8785d2bcc7abbd6fdfb47a536
MD5 3c1eb4cd4b0dba5ffd61e91e05dfe0d6
BLAKE2b-256 ffd8199f64ce6d8f5751ce76060a3274d6d3b7842bf8bcf81a2e1202c8edc6e8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305185237-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 3fa17f24dc33c14ff5848a68a515c7405b5ff7c47ae312ed8afba330d83c827e
MD5 5fd61a03d9878f0d648dac9fbb4add33
BLAKE2b-256 03c8d0387aef901509e0aa564b8934076c604d98345d7f068bda9db52f78902f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305185237-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 a8c7b07154d0fb140c7338346141c8e77939148b2be240a757215c438781a05c
MD5 c29f895a6394a42a0f264daeaeb7557b
BLAKE2b-256 7b34acffb18e92e0db8630f240f9dc998aee1de8911c07653e346180110f1883

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305185237-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 731d6a15fd4763adcb5d2e88ff80ed00c930ae2f419eb8256571c3fcca59ac9e
MD5 3e5d05fa2657f790ab5270c4f8484756
BLAKE2b-256 69608d9debf7b5e67435b12c1fd2ec3a7b48f0cf061f939e55d909157463e4f5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305185237-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 d996e837208cf741aa75dde81df9da1363496c7edfef661a6301c6cf2b478f19
MD5 f145024119f33b216ab2707a11a8c429
BLAKE2b-256 4a1f6cd2add1e5179f0afd1ac4b330da97371c0ef985694b0f37a064f701e781

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305185237-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a7686232dd7c1c8b28e9c657ac0ce1578566269dcaef7e8f9b2fad40d5eed7ec
MD5 82bf1067f7d59d23dff069a6216e9a0c
BLAKE2b-256 78fa923c29dd44530a08b10381980eff0c1b9ddd66838e8456a39dddbf449354

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305185237-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 6075352546fad06fdca6457bcfb44689c17e02aceceee90c802f29aa185b313e
MD5 65da043df2cd77a7dd5bae8b7f7fc296
BLAKE2b-256 ed683b918ecae62f6c070fb31985507f21ab62ff0f073a5b12956ed9ef26ae86

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305185237-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 b5a49a96bf319768cb7e52026619af56a193cc82bc4f2f961ccafdebdfdf0b18
MD5 c605fc4cdff4ab1604d35500400adef0
BLAKE2b-256 93d17dce8668b288ca2824cabb5ca92d76629a725ac14fe2ae4f8a8057aadbe1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305185237-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 11f9dd4639d161c1de9cf68f80fc66560295437b89a19b5acab2482049ab4b67
MD5 f827d0846f96b5c0d5baa2ab453a8656
BLAKE2b-256 d4aeaa7f2ee55473d5ba14c0eccb1b74f0227fb27f9e64f98af10cb30e94d89e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305185237-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 59bc0d8f8e1e88e5b50ec38b36f60ebee096661258509cde14bfa078ea0d40d2
MD5 dba0080d850d467e75dd6e18e467d263
BLAKE2b-256 ce367cf9c64e08fcbac473715542a086b26ab9d688d510a6739ac7085054eb7d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305185237-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d782166fd51f5e3d9a7c60743f534c9b273d4ec4472b451616fd98666a17e768
MD5 04d0bfcfe2f549c781d25530aa5d8c5f
BLAKE2b-256 b29743bac23b42c4e08713a2f275d60aaf71ac2165537b5874265a25d7d5e420

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305185237-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0b9e228fe38a48c31a2ce010ac261f3ef7e862eacbddcad935f2f0025cfe737a
MD5 84b1b7644b126e9b9f9714eae0bb80ea
BLAKE2b-256 ce7e35fcbcfa944a65913354044fe86e5617d33b72caf3d1ca625ea08f83fbdd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305185237-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 65dd1550b08139bfe1deeb49f15acd1001c5c90055f57491fad5655f88e579a2
MD5 abe36c4c75c4d3d4638941c835fe8ca8
BLAKE2b-256 c798fdf18ec5451bb047a556fe8d40ed8ecebb6d78e6e8133198b8c73661c734

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305185237-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 de733c8b30a75c1fc99da34b4201fe5964c0cb0a527c4255bb59b645dfd1d980
MD5 045c3835332d380ec85cade9d06692e4
BLAKE2b-256 5aeea0a2454d2ca27400a5688fc1fcdac0b217bcd25ebbf5bad98082ae71baf8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305185237-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 a44ab79f9dce71a2d80b4f10e71ddd6d0be3179865db9a026e8e6c113802b9be
MD5 7107bb0e92b5d909e8dc6e7253aaa379
BLAKE2b-256 19f524bc0da2f78d7073eb24b9be8e57ccfe6c7a40dbe4e5629a1d2c7d159b4a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305185237-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 24ee366306f947b9125f3ef6a859a4732c5e5e11b439393195890dd20cd9a59d
MD5 ff772ecb51d9a27b1822a88789a9bdd6
BLAKE2b-256 0a765fea762f75f99d45342599df6cd2a423df90442a045f731ed6266320fe23

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305185237-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a49ce6fecdb9db90ce5ba9370e1aac2e63cca21ead781eda744048ec3ee1a9a0
MD5 5e7b52a9c2cb4a973e59e666e8884371
BLAKE2b-256 28c4ecf842163f3f3cc6408c8fe3ec030b2170f9a515865a721f5136472dfd16

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305185237-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 01c01481e4cdb31bdb2e679cda54802181fd39e11503d6dc8efcf145339040fc
MD5 62c1e577ea92bda97ff1ecdb5a20ab41
BLAKE2b-256 eb301695faa8070dd2bbb0f0a1183a55275250c2b4f49219d0e2c464d184d938

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