Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

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

Metadata

Release files for typedunits 0.0.1.dev20241105175138

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distributions (wheels)

Table of built distributions (wheels) for typedunits 0.0.1.dev20241105175138
File
typedunits-0.0.1.dev20241105175138-cp312-cp312-win_amd64.whl CPython 3.12 CPython 3.12 Windows x86-64 Details
typedunits-0.0.1.dev20241105175138-cp312-cp312-win32.whl CPython 3.12 CPython 3.12 Windows x86-32 Details
typedunits-0.0.1.dev20241105175138-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.12 CPython 3.12 Linux glibc 2.17+ x86-64 Details
typedunits-0.0.1.dev20241105175138-cp312-cp312-macosx_11_0_arm64.whl CPython 3.12 CPython 3.12 macOS 11.0+ ARM64 Details
typedunits-0.0.1.dev20241105175138-cp312-cp312-macosx_10_13_x86_64.whl CPython 3.12 CPython 3.12 macOS 10.13+ x86-64 Details
typedunits-0.0.1.dev20241105175138-cp311-cp311-win_amd64.whl CPython 3.11 CPython 3.11 Windows x86-64 Details
typedunits-0.0.1.dev20241105175138-cp311-cp311-win32.whl CPython 3.11 CPython 3.11 Windows x86-32 Details
typedunits-0.0.1.dev20241105175138-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.11 CPython 3.11 Linux glibc 2.17+ x86-64 Details
typedunits-0.0.1.dev20241105175138-cp311-cp311-macosx_11_0_arm64.whl CPython 3.11 CPython 3.11 macOS 11.0+ ARM64 Details
typedunits-0.0.1.dev20241105175138-cp311-cp311-macosx_10_9_x86_64.whl CPython 3.11 CPython 3.11 macOS 10.9+ x86-64 Details
typedunits-0.0.1.dev20241105175138-cp310-cp310-win_amd64.whl CPython 3.10 CPython 3.10 Windows x86-64 Details
typedunits-0.0.1.dev20241105175138-cp310-cp310-win32.whl CPython 3.10 CPython 3.10 Windows x86-32 Details
typedunits-0.0.1.dev20241105175138-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.10 CPython 3.10 Linux glibc 2.17+ x86-64 Details
typedunits-0.0.1.dev20241105175138-cp310-cp310-macosx_11_0_arm64.whl CPython 3.10 CPython 3.10 macOS 11.0+ ARM64 Details
typedunits-0.0.1.dev20241105175138-cp310-cp310-macosx_10_9_x86_64.whl CPython 3.10 CPython 3.10 macOS 10.9+ x86-64 Details

Total release size: 15.9 MB

Release files / typedunits-0.0.1.dev20241105175138-cp312-cp312-win_amd64.whl

Download URL typedunits-0.0.1.dev20241105175138-cp312-cp312-win_amd64.whl
Size 707.8 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
f7e6756b234a2d074a1988084adf149fa8669037860bd000effd015875367d97
BLAKE2b-256 checksum
How to use checksums
78894135b22093d299b70a75e2bbe46d561caa5957299ac3b0691759df4fe0c4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.20

Release files / typedunits-0.0.1.dev20241105175138-cp312-cp312-win32.whl

Download URL typedunits-0.0.1.dev20241105175138-cp312-cp312-win32.whl
Size 667.8 kB
Tags CPython 3.12 Windows x86-32
SHA-256 checksum
How to use checksums
00063a38452950af0a7c2f312115822e75e1dca8076bce2bc9b025416d2d091f
BLAKE2b-256 checksum
How to use checksums
26bdf71319a42054f28c1461f2ceb26e1a3affb1f1d1d49e8c0112c33e3f113e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.20

Release files / typedunits-0.0.1.dev20241105175138-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20241105175138-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.4 MB
Tags CPython 3.12 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
fd4889d8813a532ee420191a21ae6a889d85fff4039181d0324eb13b0cb5532d
BLAKE2b-256 checksum
How to use checksums
770b0fc528532ef257ae5e80ff112a5f5b3bdf6e2af75d770b7f1899f68dd1c6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.20

Release files / typedunits-0.0.1.dev20241105175138-cp312-cp312-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20241105175138-cp312-cp312-macosx_11_0_arm64.whl
Size 740.5 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
f6a13200f572cf5e231481f686cc4cccc9a71873626d835aa646c86e01784ea3
BLAKE2b-256 checksum
How to use checksums
4732c225ede9218446a358bc27a13cc44ca8236f8311be341ca89fdde787d764
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.20

Release files / typedunits-0.0.1.dev20241105175138-cp312-cp312-macosx_10_13_x86_64.whl

Download URL typedunits-0.0.1.dev20241105175138-cp312-cp312-macosx_10_13_x86_64.whl
Size 773.6 kB
Tags CPython 3.12 macOS 10.13+ x86-64
SHA-256 checksum
How to use checksums
c0d0e8dc142e3738e31e376e57708e4820f1f8552d2aa19178ce26a07cfc1e38
BLAKE2b-256 checksum
How to use checksums
475f97ec063dce33909f52574b7bcfc20bb0f7440a5d5067953f4a5c2c94407f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.20

Release files / typedunits-0.0.1.dev20241105175138-cp311-cp311-win_amd64.whl

Download URL typedunits-0.0.1.dev20241105175138-cp311-cp311-win_amd64.whl
Size 735.3 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
3f284c9464b36396625b9655a8709948271219af9d139d241496fdf20260c062
BLAKE2b-256 checksum
How to use checksums
51189fd470e32b7de3c46d3c43e8dad66b507a562bd8edacd78c9c0e2a2bbe39
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.20

Release files / typedunits-0.0.1.dev20241105175138-cp311-cp311-win32.whl

Download URL typedunits-0.0.1.dev20241105175138-cp311-cp311-win32.whl
Size 700.5 kB
Tags CPython 3.11 Windows x86-32
SHA-256 checksum
How to use checksums
4500cd36ae3f4516fa912b315dfdc4c10afacb67ffeec74367ce35607ccfc780
BLAKE2b-256 checksum
How to use checksums
de3caced327572e7f57d4b3fc863d737f85e09b2a2ccab0a05b238dd04bd7876
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.20

Release files / typedunits-0.0.1.dev20241105175138-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20241105175138-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.5 MB
Tags CPython 3.11 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
8ad05003173f3cd741775e3ca4245198c61fe26056a5aa09e7fe17ce19ff3357
BLAKE2b-256 checksum
How to use checksums
58496f554d2e594bcfdcdf71eb225f900e47bd5e9268e996411552ae1b28d42c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.20

Release files / typedunits-0.0.1.dev20241105175138-cp311-cp311-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20241105175138-cp311-cp311-macosx_11_0_arm64.whl
Size 751.4 kB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
c9f0886888ab9065d8830a387e27fc5385cabbc66fb2238ea026ac3990367ffb
BLAKE2b-256 checksum
How to use checksums
0c38be829c59c99c115c6eb07e5a22707f094ee7cceafdc1a2aa4dfa2b124a2c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.20

Release files / typedunits-0.0.1.dev20241105175138-cp311-cp311-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20241105175138-cp311-cp311-macosx_10_9_x86_64.whl
Size 809.5 kB
Tags CPython 3.11 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
3e84d260cccc1524f93ea828f89890f97d79284adff654287302526869c8f31b
BLAKE2b-256 checksum
How to use checksums
692cd31dfdd29dc2e79a0e0932dc9eb2356acb6079d46bca81e57866226476aa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.20

Release files / typedunits-0.0.1.dev20241105175138-cp310-cp310-win_amd64.whl

Download URL typedunits-0.0.1.dev20241105175138-cp310-cp310-win_amd64.whl
Size 731.2 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
129486eb59e1fa88475be9cf0922552926a961ef6a5df59a71af9d1d6abb4923
BLAKE2b-256 checksum
How to use checksums
7ce8fabaec150fbbc10bb05d4444d3d9448ba64143adc93b92e7283c775d79bb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.20

Release files / typedunits-0.0.1.dev20241105175138-cp310-cp310-win32.whl

Download URL typedunits-0.0.1.dev20241105175138-cp310-cp310-win32.whl
Size 669.1 kB
Tags CPython 3.10 Windows x86-32
SHA-256 checksum
How to use checksums
0d5547f7fc49eb1ff61e4d853559d1d3c7b8263734bccb753c63ef35061a86ad
BLAKE2b-256 checksum
How to use checksums
8ace9c59f590825e8ec3b3dabf0a6e3dc5f71943636ae459e3af646d76e44f19
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.20

Release files / typedunits-0.0.1.dev20241105175138-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20241105175138-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.3 MB
Tags CPython 3.10 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
802c73d653c53efc4414ad7638ce77a508d8d188224d865d026e65d5bbc18b3d
BLAKE2b-256 checksum
How to use checksums
63f38caba4d04783d3914afd8ef2e86a649a25912b672fe720f284eba1c35ceb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.20

Release files / typedunits-0.0.1.dev20241105175138-cp310-cp310-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20241105175138-cp310-cp310-macosx_11_0_arm64.whl
Size 719.0 kB
Tags CPython 3.10 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
0a9c19205547edbb7628fdb1ced7564e4e60e58b4dcbd2a4efa06f595133ccf5
BLAKE2b-256 checksum
How to use checksums
53e416d429a0eac2e9ada2affed549f285d639f0187d093a00c9bd3f37bb4f80
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.20

Release files / typedunits-0.0.1.dev20241105175138-cp310-cp310-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20241105175138-cp310-cp310-macosx_10_9_x86_64.whl
Size 777.3 kB
Tags CPython 3.10 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
5e52ea2ac38264df039bbaae6cee01c5e8967777bcad709a759c0a6970c1f429
BLAKE2b-256 checksum
How to use checksums
58280f1637473d81e1b150ae9bea740e536986f1826cab09197233e0eaf31f54
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.20

Release history Release notifications | RSS feed

0.0.2

30 release files

0.0.1

24 release files

This release
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page