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

Uploaded CPython 3.13Windows x86-64

typedunits-0.0.1.dev20250131003844-cp313-cp313-win32.whl (699.6 kB view details)

Uploaded CPython 3.13Windows x86

typedunits-0.0.1.dev20250131003844-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.5 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20250131003844-cp313-cp313-macosx_11_0_arm64.whl (769.7 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

typedunits-0.0.1.dev20250131003844-cp313-cp313-macosx_10_13_x86_64.whl (809.1 kB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

typedunits-0.0.1.dev20250131003844-cp312-cp312-win_amd64.whl (743.2 kB view details)

Uploaded CPython 3.12Windows x86-64

typedunits-0.0.1.dev20250131003844-cp312-cp312-win32.whl (700.6 kB view details)

Uploaded CPython 3.12Windows x86

typedunits-0.0.1.dev20250131003844-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.5 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20250131003844-cp312-cp312-macosx_11_0_arm64.whl (776.9 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

typedunits-0.0.1.dev20250131003844-cp312-cp312-macosx_10_13_x86_64.whl (815.1 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

typedunits-0.0.1.dev20250131003844-cp311-cp311-win_amd64.whl (770.3 kB view details)

Uploaded CPython 3.11Windows x86-64

typedunits-0.0.1.dev20250131003844-cp311-cp311-win32.whl (735.5 kB view details)

Uploaded CPython 3.11Windows x86

typedunits-0.0.1.dev20250131003844-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.6 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20250131003844-cp311-cp311-macosx_11_0_arm64.whl (790.9 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

typedunits-0.0.1.dev20250131003844-cp311-cp311-macosx_10_9_x86_64.whl (852.2 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

typedunits-0.0.1.dev20250131003844-cp310-cp310-win_amd64.whl (766.8 kB view details)

Uploaded CPython 3.10Windows x86-64

typedunits-0.0.1.dev20250131003844-cp310-cp310-win32.whl (703.9 kB view details)

Uploaded CPython 3.10Windows x86

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

Uploaded CPython 3.10macOS 11.0+ ARM64

typedunits-0.0.1.dev20250131003844-cp310-cp310-macosx_10_9_x86_64.whl (816.9 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250131003844-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 44289dc848840995e035d43d165f18883285a44abb383b515ba44c8b502b018e
MD5 d5dfbe30e58954c97bdde39354abb7d0
BLAKE2b-256 38614cc6fec7c03bc70b749f6d5ce3370b6ee936f23c580cfea6ec956dc3b952

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250131003844-cp313-cp313-win32.whl
Algorithm Hash digest
SHA256 724275f04775d3339b74580b32c5e253566eb2ddb48afe29c925a66b52ae4dfb
MD5 250826d66608d083bd0918e1f964b1f0
BLAKE2b-256 53b85b01fce8036d25142b40c0931a2f64033222bae580c46b403b00acb165e4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250131003844-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 36bc75084c52ef95880193a951a7afa4726a602d7bb9c82e354b274afc05939b
MD5 cb7340cc2b784e478ea658f0358706a5
BLAKE2b-256 ea2c456ed7fe535e28186e1e76b2cdb32b618eccc615d2161781e8b197a33683

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250131003844-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 5b5f82fbb3ee129f4d003edabd4c8a4715d745995863d1dc96a0dd0685856b34
MD5 81749789fae7bd3448050d40d855b4b6
BLAKE2b-256 87b7bb22ceda821bc272915587bac1eadae6fc3abad0733c75d16c98778c013f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250131003844-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 35047c62e185327915d05980a0fdfa37bcb3954dc9f0a79e29e8245ddadcc1b7
MD5 07b719a6e755a068fd3fac0e38df455d
BLAKE2b-256 f5170fd6f36a9dcb4f0389534d546f6e69e0beb31507a0e0f5455cd20c6374bf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250131003844-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 db545ca7a2a4af1a9fb9b829b3694a310505a811f0f232ca44e6a3855ef8efdc
MD5 0bbdb1363e0952b6944dde2f2c1f2617
BLAKE2b-256 e25e7be17696f3279df85de4e142f1e8ad8a616d15645e8c9838446568faa6e3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250131003844-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 fa1a67f05ee40c9df279ae2d6f6415986d699e75ae9039cd1f8a06e99272ce00
MD5 c37b68f704913b98f6f373e046ec2fab
BLAKE2b-256 973982539fe1af86ed5131fed8d99e4c71998d478234b01c6ef0b7385bfe6660

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250131003844-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 dcae13faace854a07bebcae808eab6b524f363b2d30863a67e0b73077062c0b2
MD5 43646a4d9ad953600d17f42907d5155f
BLAKE2b-256 531a3eab8c13d113d4779d689e249ebe9c4442607ea35f7260ee8cc88e681fd5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250131003844-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9460d7cf4cad039e2e43107e83e59a314dc707b603f8a0d9dd92c34bb9f48a09
MD5 a3cee55a8a02a1876e3663bfc8be8de0
BLAKE2b-256 ee7c846b469fc22c56d2f0cc8bf0efc6b1b03be2665fcbc5e684e8b76a5836e4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250131003844-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 db2face9afe0ba62b84b8f3c6153380c3c0b3794e64e80c99459c712b2a3c9f1
MD5 94df0d9a0a1d504c34cc6660121ad411
BLAKE2b-256 4aca1aae71c58d1e89ce993770d6396a256cc995a0e246ec3392bd87bab8960e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250131003844-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 903cd71f134a72fc41e919941734bb43b2255e4c2315eb044ba61c7aef6c624f
MD5 57ae433c2bd7f8b8f61114201287634b
BLAKE2b-256 35d64f56f296114ab7a4323f59112e70b8bd272750cc495dfad496ed273e5f35

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250131003844-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 9daae16f3eae1ae96689d91f7dece913917c7a46730901818b8d0a40ee6d8610
MD5 ab4765f96bab601f301722647510a543
BLAKE2b-256 4e35c32402e1bc9243e4a18c537ac0deba58040466edf808a12362e9022e4e9a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250131003844-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 0fa038e4f84b5cea30bae034a1b10c40028ef9e836d8d9f2444f1e4abb4ddf87
MD5 c54eff7ecc2f753e1c7ceab3bd97890d
BLAKE2b-256 468d1b085a68c3c8ccdf4ec3c5c2959711db103325d9db72648923e35ea35c45

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250131003844-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a353b619c609cc24a716dbf0c1b07f345f847e05daade3c719fbcda724b9204f
MD5 2b94a0a5d2a04e03d50242828822f4c3
BLAKE2b-256 2ce4f6b69ca0dd2d0363ac4ecb0b67287f08108de51de5a88a62dd49e2ce87f4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250131003844-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 142f4b82058cb1d6709cab2ca7cf7c9d9616a3d55b11e129ad8f51fbe0d0bfed
MD5 59b2b7225e602cf8b58de7b5a6f23405
BLAKE2b-256 1f8008e195f4156f4e73afeb299d9482d19341a73ef253d552eec969a62ba17f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250131003844-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 3135975258c1e93fe6a0733e1069d32ebc96574d0d5d65570853b273801e398b
MD5 2a58ae64c8f6926a40711b08f345c3e6
BLAKE2b-256 f28fdf445d6fb2957e4366d6acac42c82cc8c3f234146ada7ba8f9f8ac885e61

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250131003844-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 f5aa7c38c46d7d6f675cdb811fde555e9351551d8d2cd86a30322f2d2939dd8e
MD5 76f0acc95b6498576fe2a1a7fa979d9f
BLAKE2b-256 64315945fcd1660de11872d51ee8cc3ce9ae3190eb88c37408c0947c39283bde

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250131003844-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 bd124cc898a679dca27a298283d4b5f3e8d255ebe2df8d2f748bf5b8b1de73a1
MD5 d6c0f9e2e2bcdd5ffd76658bd12f6e91
BLAKE2b-256 75d26beade2e4d740c1b2bbb754fee2699614c3843a0a3379233bc31fc6c2f77

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250131003844-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2723f96a22fb06812c80835b0c80b9d6f329c13f8ef8ac9e02d8ca85b80f643c
MD5 19ce02be1bdbce0ff9211c5a90700702
BLAKE2b-256 391a8d3cf166cb86676f0353c8a76401b4c75ac6a118e2006834cf2300c67988

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250131003844-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 7a2e83fac100a40b05b01d8045dbd88d0b3b3e86852d381b88a47a2efe1a04ce
MD5 d4a0970cb0f351dabbeb1bb6efa1cea1
BLAKE2b-256 92e2421867e9234bdcea3439b52c5bea9ebc00b8660546c7d9894f0e424d5d3b

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