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

Metadata

Release files for typedunits 0.0.1.dev20241113200257

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.dev20241113200257
File
typedunits-0.0.1.dev20241113200257-cp312-cp312-win_amd64.whl CPython 3.12 CPython 3.12 Windows x86-64 Details
typedunits-0.0.1.dev20241113200257-cp312-cp312-win32.whl CPython 3.12 CPython 3.12 Windows x86-32 Details
typedunits-0.0.1.dev20241113200257-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.dev20241113200257-cp312-cp312-macosx_11_0_arm64.whl CPython 3.12 CPython 3.12 macOS 11.0+ ARM64 Details
typedunits-0.0.1.dev20241113200257-cp312-cp312-macosx_10_13_x86_64.whl CPython 3.12 CPython 3.12 macOS 10.13+ x86-64 Details
typedunits-0.0.1.dev20241113200257-cp311-cp311-win_amd64.whl CPython 3.11 CPython 3.11 Windows x86-64 Details
typedunits-0.0.1.dev20241113200257-cp311-cp311-win32.whl CPython 3.11 CPython 3.11 Windows x86-32 Details
typedunits-0.0.1.dev20241113200257-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.dev20241113200257-cp311-cp311-macosx_11_0_arm64.whl CPython 3.11 CPython 3.11 macOS 11.0+ ARM64 Details
typedunits-0.0.1.dev20241113200257-cp311-cp311-macosx_10_9_x86_64.whl CPython 3.11 CPython 3.11 macOS 10.9+ x86-64 Details
typedunits-0.0.1.dev20241113200257-cp310-cp310-win_amd64.whl CPython 3.10 CPython 3.10 Windows x86-64 Details
typedunits-0.0.1.dev20241113200257-cp310-cp310-win32.whl CPython 3.10 CPython 3.10 Windows x86-32 Details
typedunits-0.0.1.dev20241113200257-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.dev20241113200257-cp310-cp310-macosx_11_0_arm64.whl CPython 3.10 CPython 3.10 macOS 11.0+ ARM64 Details
typedunits-0.0.1.dev20241113200257-cp310-cp310-macosx_10_9_x86_64.whl CPython 3.10 CPython 3.10 macOS 10.9+ x86-64 Details

Total release size: 16.0 MB

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

Download URL typedunits-0.0.1.dev20241113200257-cp312-cp312-win_amd64.whl
Size 711.9 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
b3b2e003ebd214a7218286a5e6d3d84819f8066d8ef8c0a6455b92e67ea3e0c9
BLAKE2b-256 checksum
How to use checksums
1b2cce2eb0af92c53ea08232a9997f9d72134b8df4d28de53e650216167c7045
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.dev20241113200257-cp312-cp312-win32.whl

Download URL typedunits-0.0.1.dev20241113200257-cp312-cp312-win32.whl
Size 672.1 kB
Tags CPython 3.12 Windows x86-32
SHA-256 checksum
How to use checksums
017c619b3564002ed1c8869bead69d0d0336782caa06955901df7c64268dffe3
BLAKE2b-256 checksum
How to use checksums
ddc902bebbb0e1b435c9ac4245b7a8d07a9c3d11c37d761fe0b24d6f3dbf95c0
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.dev20241113200257-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20241113200257-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
2c2854c56abda2a7d60384d68d6f3c9764d4f5ddfe9ff33dd3122e093db2b169
BLAKE2b-256 checksum
How to use checksums
c1ec12ce25202156bc44a555aad0cb0ea6c6bdb0c2f59a3de25365739a8e593d
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.dev20241113200257-cp312-cp312-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20241113200257-cp312-cp312-macosx_11_0_arm64.whl
Size 744.5 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
f44f1cc1b535fd51a988815cd2a01cb8d7526f2c5c5cdb7f0eb86f01132cbffe
BLAKE2b-256 checksum
How to use checksums
8b59704419879cf8bb2dcafd64b8421958d7243ff9af34808ea623c37b8a5c22
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.dev20241113200257-cp312-cp312-macosx_10_13_x86_64.whl

Download URL typedunits-0.0.1.dev20241113200257-cp312-cp312-macosx_10_13_x86_64.whl
Size 777.4 kB
Tags CPython 3.12 macOS 10.13+ x86-64
SHA-256 checksum
How to use checksums
ad40e5f7e285a93f91b84f9d8acaa81b874b129bbb4eccc6fcf79736d9888724
BLAKE2b-256 checksum
How to use checksums
ce7e0f3870c42f16f96864e02df34dd7f0213d869befdc24e3c7e673eef93b14
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.dev20241113200257-cp311-cp311-win_amd64.whl

Download URL typedunits-0.0.1.dev20241113200257-cp311-cp311-win_amd64.whl
Size 739.4 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
d089faa59b04fe1796eb46e8aacc5c794fe94f1bf331b7d1292d7a833d459b92
BLAKE2b-256 checksum
How to use checksums
74017ec3cf109e99fdfd3fd9fd6ce80aae1d66153f985fccc5dec3500d7bd603
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.dev20241113200257-cp311-cp311-win32.whl

Download URL typedunits-0.0.1.dev20241113200257-cp311-cp311-win32.whl
Size 704.9 kB
Tags CPython 3.11 Windows x86-32
SHA-256 checksum
How to use checksums
6418e45b16acf189340e24c0ee74392e491f3ac22e12ca87d6ee8008294eee30
BLAKE2b-256 checksum
How to use checksums
a3c86dc06563469f5fdb6dface66719bb7e31f68ca8e3017371c5c8c1783661a
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.dev20241113200257-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20241113200257-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
823490a19d70776b10bd4ad1db57940cfb1738941a57f6529a020665ae9c1c16
BLAKE2b-256 checksum
How to use checksums
73fcca8687c6795fd9c4616024e8cabe7538a0a42a125e5a2b569df540e5a4e8
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.dev20241113200257-cp311-cp311-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20241113200257-cp311-cp311-macosx_11_0_arm64.whl
Size 755.7 kB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
04f9aa2c36fa403423925595b6be2a6c8c9bfb2411ed7bb77e4e94130118f8b1
BLAKE2b-256 checksum
How to use checksums
30820ecde8c12edf51819d4c2d1f529c023a9d10114dc71b1ac5c15a6de6df66
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.dev20241113200257-cp311-cp311-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20241113200257-cp311-cp311-macosx_10_9_x86_64.whl
Size 813.6 kB
Tags CPython 3.11 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
b9f380e4cc6299ec5f90d99c1fcf5fdfee43d66bd939b368d7f800967c20746f
BLAKE2b-256 checksum
How to use checksums
07c59adb70fea5cf2cce0f327ad460904e2272b4f5bbc102a6f5fdbdceacbb6f
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.dev20241113200257-cp310-cp310-win_amd64.whl

Download URL typedunits-0.0.1.dev20241113200257-cp310-cp310-win_amd64.whl
Size 735.2 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
346bc368a92a31cc334f45f3caf7996c8eb5d71dfa7a209ae1033157b1aad763
BLAKE2b-256 checksum
How to use checksums
75b8f7604cf933da992f9b1986e7c24bfc7f7fa88b857b8373d4b9654918ceb1
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.dev20241113200257-cp310-cp310-win32.whl

Download URL typedunits-0.0.1.dev20241113200257-cp310-cp310-win32.whl
Size 673.1 kB
Tags CPython 3.10 Windows x86-32
SHA-256 checksum
How to use checksums
a185625ed94281cfd045c51f39934036d33c30d62cc1723fdaaca6e33e461f17
BLAKE2b-256 checksum
How to use checksums
ff256cb20e9d30e25874669930ff3c0ff9f8d8bb3bf35af2dbe842832b68cbfa
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.dev20241113200257-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20241113200257-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
59eb018bc6c20bd62e3fd3c7576407f46c5da74e1679712ab68c748f90f55a99
BLAKE2b-256 checksum
How to use checksums
0586aff143681d66b91228dd1bf7ded009b1f6f4d8c52852616b6a26c19bbbfe
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.dev20241113200257-cp310-cp310-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20241113200257-cp310-cp310-macosx_11_0_arm64.whl
Size 723.0 kB
Tags CPython 3.10 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
2c5b830a2ff9f0f29fa9bec827bb676af821f5c2de09b13c0c6a6f665d898212
BLAKE2b-256 checksum
How to use checksums
7d019f1ec4bcb934c2c0f633aeef050f3e56f2b7fd95ca4f6868b5649859001d
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.dev20241113200257-cp310-cp310-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20241113200257-cp310-cp310-macosx_10_9_x86_64.whl
Size 781.3 kB
Tags CPython 3.10 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
7974285ec9f5c5d104dd3e49f84f328edb1906ca8dff80f3a50a58fe47b1c499
BLAKE2b-256 checksum
How to use checksums
209939b69e6ca669f43dbd2407fa650c142029c9fc54f845bab16274c394d12b
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