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 -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.dev20241206010532-cp312-cp312-win_amd64.whl (722.2 kB view details)

Uploaded CPython 3.12Windows x86-64

typedunits-0.0.1.dev20241206010532-cp312-cp312-win32.whl (682.0 kB view details)

Uploaded CPython 3.12Windows x86

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

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20241206010532-cp312-cp312-macosx_11_0_arm64.whl (755.5 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

typedunits-0.0.1.dev20241206010532-cp312-cp312-macosx_10_13_x86_64.whl (792.0 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

typedunits-0.0.1.dev20241206010532-cp311-cp311-win_amd64.whl (749.4 kB view details)

Uploaded CPython 3.11Windows x86-64

typedunits-0.0.1.dev20241206010532-cp311-cp311-win32.whl (714.8 kB view details)

Uploaded CPython 3.11Windows x86

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

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20241206010532-cp311-cp311-macosx_11_0_arm64.whl (767.1 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

typedunits-0.0.1.dev20241206010532-cp311-cp311-macosx_10_9_x86_64.whl (825.5 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

typedunits-0.0.1.dev20241206010532-cp310-cp310-win_amd64.whl (745.7 kB view details)

Uploaded CPython 3.10Windows x86-64

typedunits-0.0.1.dev20241206010532-cp310-cp310-win32.whl (683.6 kB view details)

Uploaded CPython 3.10Windows x86

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

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20241206010532-cp310-cp310-macosx_11_0_arm64.whl (734.4 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

typedunits-0.0.1.dev20241206010532-cp310-cp310-macosx_10_9_x86_64.whl (791.9 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241206010532-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 6f188bf2889a0d8a29640ab1bbdb7989aedc532c99b185690eb5ea49708cef73
MD5 bc8eda118ed2c5378644bbad4b0cc2d4
BLAKE2b-256 fc8c470434cd6be72f9924726607b347e6a98d5b2a9659a890c1230ee658b2aa

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241206010532-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 45ae7fee3cdfe84bcdb3a7482baea107f83f5c2d8b296e6e70b6dd4c2ce86394
MD5 34d95d85e6d17527b1670b592eeb9f00
BLAKE2b-256 52a54a1596fdf4582d773fd25a02c50d65a8ae6ea82556bd3d8245316130b0d3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241206010532-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 4a34375669d190a87d2d94b18a5afca406e7147f8b8869b1c1fbb779f2b0982d
MD5 8bdb47fe0682f4fa49e617b75f0d0a9b
BLAKE2b-256 3e0aff194dade674adce2a5a7c87a65b0325d3df348f2429ea123f33cf46313c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241206010532-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 4915336509131ed2e8a051b61a0e26d6f8d0d5e997780ed81c1a4b6384a55806
MD5 e6aa1b01e08277e9978bd818778eadd4
BLAKE2b-256 20c2f381a3bc883e5ad2d87b4b8de668fb713d0b00e3468a8a40ad104a9800ea

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241206010532-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 4532d42f4a94d6b7fc30da18b50a9e9ea5f5900f1ac8f9ba56531f57199b086d
MD5 c4b99ca811b10c75101cd9fd70e10f8a
BLAKE2b-256 107963949218f0084c63fe95f502ab7a873525904c45f34c7cd82cae3df58942

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241206010532-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 0996c44a23af961f4a8a1fb064f223f20945f043187420020c93097d230d22c7
MD5 3325924473ed833674baff86b61e7de0
BLAKE2b-256 d4f9d3b1130e3d187f77c3269a3e44075829af1af7cbed6efdf4d27fe22e0f26

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241206010532-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 e0f24435df42e22aac7090e829a5a62b5788c4f4fc29bb89a66e8b988b078238
MD5 65b99e1ffeb4d58c97ca9e58491e636d
BLAKE2b-256 41bcdb29d3b73259ab09b85ab053d2463249c3c53b5805ce098a69b8aaaaca9a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241206010532-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8434103d5fb071c42743e7cfcac548e45ce0e7324d7a0cd910462ec9087a5846
MD5 1e08f1c2ea43c6ef52f83696b74da6ef
BLAKE2b-256 b6439d83c41d2c7c4141db421a0247c5034402d6aeb23dd6c7f3cc07cb64f1a1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241206010532-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a1d10867eb3817956353481d4a0811fa3d098fd192755ad6194d00d31176231e
MD5 ad361d040aaff1446a25058c92c620d5
BLAKE2b-256 6f3ca64cb770c8ac55f6306b7759f4422ead070a7314329bb217f4f9253e0dc2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241206010532-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 fd928f45d456fc16a2999c336766a9cd5ffb14a0d88470ca0ae46b4222f35bfc
MD5 2ebc3d93271a4d881e6eed426f609cdd
BLAKE2b-256 49dc1c068bc31e3be35b6a9269fcbf8ddbb5cb0557fb6faf45e3afd7492b75f7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241206010532-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 c673f388306994be1a6cede09aab8a9fa934bba24b001a0e758f58e16bd81a0f
MD5 47c547c48b63e66cc47b7e700086f2e4
BLAKE2b-256 054b0fd0edd84f9f7e7eb01cdd72516ac3f7365ff665f7c0df78b8d429dc42a8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241206010532-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 eb01c5e96b3b4d660a55deffcd87ad8b4d8592e7b254c5081d2b9b9791f126ed
MD5 1620219227a3866160008242e148fd48
BLAKE2b-256 54a78cfec8d7d44b3e8cc73850ae28e3e5f65c9520454d0eb1d37442f1af7951

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241206010532-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 06a53dc741d49b3ad68d586a5c0bfbffff873631b6d7e99d078cb76602ee25d4
MD5 a70dc9a9ea9289374a2a1975cee1a9db
BLAKE2b-256 ca3a3ebab6e12b8de8ddc8e214286b7884518ef80ae4f817b3800eacc214d6da

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241206010532-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7443e4c6964c3d2ddc0a3b17a7a584633dca7d421c8e7dca3ead804c50b42d25
MD5 1d72d90ce7b2e82792d607944387dd32
BLAKE2b-256 abbdf5ad5a4608df081f8464935aab9dde36dbe62f6b788ba76e76c0a43dc16c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241206010532-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 71b01147cb385fd7b5a27d330817c78db159be3484c1db821c86721e8738894a
MD5 ccbd5afe2b623402f5144b0a6ca65bed
BLAKE2b-256 20a3b72dfd3cf4d8d787339170ed835d8535aedac0f8abb5b59d6047996dbdd9

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