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 .
    

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 .
    
  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


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

typedunits-0.0.1.dev20241105175138-cp312-cp312-win_amd64.whl (707.8 kB view details)

Uploaded CPython 3.12 Windows x86-64

typedunits-0.0.1.dev20241105175138-cp312-cp312-win32.whl (667.8 kB view details)

Uploaded CPython 3.12 Windows x86

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

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20241105175138-cp312-cp312-macosx_11_0_arm64.whl (740.5 kB view details)

Uploaded CPython 3.12 macOS 11.0+ ARM64

typedunits-0.0.1.dev20241105175138-cp312-cp312-macosx_10_13_x86_64.whl (773.6 kB view details)

Uploaded CPython 3.12 macOS 10.13+ x86-64

typedunits-0.0.1.dev20241105175138-cp311-cp311-win_amd64.whl (735.3 kB view details)

Uploaded CPython 3.11 Windows x86-64

typedunits-0.0.1.dev20241105175138-cp311-cp311-win32.whl (700.5 kB view details)

Uploaded CPython 3.11 Windows x86

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

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20241105175138-cp311-cp311-macosx_11_0_arm64.whl (751.4 kB view details)

Uploaded CPython 3.11 macOS 11.0+ ARM64

typedunits-0.0.1.dev20241105175138-cp311-cp311-macosx_10_9_x86_64.whl (809.5 kB view details)

Uploaded CPython 3.11 macOS 10.9+ x86-64

typedunits-0.0.1.dev20241105175138-cp310-cp310-win_amd64.whl (731.2 kB view details)

Uploaded CPython 3.10 Windows x86-64

typedunits-0.0.1.dev20241105175138-cp310-cp310-win32.whl (669.1 kB view details)

Uploaded CPython 3.10 Windows x86

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

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20241105175138-cp310-cp310-macosx_11_0_arm64.whl (719.0 kB view details)

Uploaded CPython 3.10 macOS 11.0+ ARM64

typedunits-0.0.1.dev20241105175138-cp310-cp310-macosx_10_9_x86_64.whl (777.3 kB view details)

Uploaded CPython 3.10 macOS 10.9+ x86-64

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241105175138-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 f7e6756b234a2d074a1988084adf149fa8669037860bd000effd015875367d97
MD5 5417ec61778e3877ed20aeec7f5dc61a
BLAKE2b-256 78894135b22093d299b70a75e2bbe46d561caa5957299ac3b0691759df4fe0c4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241105175138-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 00063a38452950af0a7c2f312115822e75e1dca8076bce2bc9b025416d2d091f
MD5 a20e1e8aac2450314c660fc992ce57b3
BLAKE2b-256 26bdf71319a42054f28c1461f2ceb26e1a3affb1f1d1d49e8c0112c33e3f113e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241105175138-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 fd4889d8813a532ee420191a21ae6a889d85fff4039181d0324eb13b0cb5532d
MD5 28ed5a2ad0713ec9c4a5e250faead02b
BLAKE2b-256 770b0fc528532ef257ae5e80ff112a5f5b3bdf6e2af75d770b7f1899f68dd1c6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241105175138-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f6a13200f572cf5e231481f686cc4cccc9a71873626d835aa646c86e01784ea3
MD5 443774458ba9df3295c1f3ab17bc8d26
BLAKE2b-256 4732c225ede9218446a358bc27a13cc44ca8236f8311be341ca89fdde787d764

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241105175138-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 c0d0e8dc142e3738e31e376e57708e4820f1f8552d2aa19178ce26a07cfc1e38
MD5 cfb008bd654fe1fbd00becd8147bbb1f
BLAKE2b-256 475f97ec063dce33909f52574b7bcfc20bb0f7440a5d5067953f4a5c2c94407f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241105175138-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 3f284c9464b36396625b9655a8709948271219af9d139d241496fdf20260c062
MD5 b3afd50e593ef9e2a68a546b9885f3f0
BLAKE2b-256 51189fd470e32b7de3c46d3c43e8dad66b507a562bd8edacd78c9c0e2a2bbe39

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241105175138-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 4500cd36ae3f4516fa912b315dfdc4c10afacb67ffeec74367ce35607ccfc780
MD5 265241d2eccd332eec4cde820f1f6d79
BLAKE2b-256 de3caced327572e7f57d4b3fc863d737f85e09b2a2ccab0a05b238dd04bd7876

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241105175138-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8ad05003173f3cd741775e3ca4245198c61fe26056a5aa09e7fe17ce19ff3357
MD5 8ca62a7831e5860d82829161d52f9831
BLAKE2b-256 58496f554d2e594bcfdcdf71eb225f900e47bd5e9268e996411552ae1b28d42c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241105175138-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c9f0886888ab9065d8830a387e27fc5385cabbc66fb2238ea026ac3990367ffb
MD5 03ed0c1ee80303ff89aae481a2f9ed18
BLAKE2b-256 0c38be829c59c99c115c6eb07e5a22707f094ee7cceafdc1a2aa4dfa2b124a2c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241105175138-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 3e84d260cccc1524f93ea828f89890f97d79284adff654287302526869c8f31b
MD5 2aca28b2e7664b176944f5ce4ec6213a
BLAKE2b-256 692cd31dfdd29dc2e79a0e0932dc9eb2356acb6079d46bca81e57866226476aa

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241105175138-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 129486eb59e1fa88475be9cf0922552926a961ef6a5df59a71af9d1d6abb4923
MD5 48878d8fb11dd29f77097f4acaa38ef8
BLAKE2b-256 7ce8fabaec150fbbc10bb05d4444d3d9448ba64143adc93b92e7283c775d79bb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241105175138-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 0d5547f7fc49eb1ff61e4d853559d1d3c7b8263734bccb753c63ef35061a86ad
MD5 80643c3eb31b481de0d7ed41ef32031d
BLAKE2b-256 8ace9c59f590825e8ec3b3dabf0a6e3dc5f71943636ae459e3af646d76e44f19

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241105175138-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 802c73d653c53efc4414ad7638ce77a508d8d188224d865d026e65d5bbc18b3d
MD5 2ecc5a12bdfb27a055abd9538dee67c7
BLAKE2b-256 63f38caba4d04783d3914afd8ef2e86a649a25912b672fe720f284eba1c35ceb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241105175138-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0a9c19205547edbb7628fdb1ced7564e4e60e58b4dcbd2a4efa06f595133ccf5
MD5 07742c73045113c78d00fc4d3fece4a2
BLAKE2b-256 53e416d429a0eac2e9ada2affed549f285d639f0187d093a00c9bd3f37bb4f80

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241105175138-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 5e52ea2ac38264df039bbaae6cee01c5e8967777bcad709a759c0a6970c1f429
MD5 da98304c2e064d41687e198fe8505985
BLAKE2b-256 58280f1637473d81e1b150ae9bea740e536986f1826cab09197233e0eaf31f54

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page