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

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

Download URL typedunits-0.0.1.dev20241015222221-cp312-cp312-win_amd64.whl
Size 705.8 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
ee0e4972d38c4197961660879cee74483c5b26d1b55cf4226cf68c67fb28b3e6
BLAKE2b-256 checksum
How to use checksums
794a491b0a24076ed4d1a9e5dc81960aafdeb9b93f9fcd5a0bbe9c526b269b5c
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.dev20241015222221-cp312-cp312-win32.whl

Download URL typedunits-0.0.1.dev20241015222221-cp312-cp312-win32.whl
Size 665.6 kB
Tags CPython 3.12 Windows x86-32
SHA-256 checksum
How to use checksums
f16b672ba63bca493d95e83597bc0c9af3c29188885614ef6fd1c0952603203b
BLAKE2b-256 checksum
How to use checksums
321b4f3a6c930dd8a228ef7eef81016151bc8ff0bbb82b7d5b80e2cd9c047098
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.dev20241015222221-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20241015222221-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
833748dcf6d5a511ac4b6ac22520e6de03f1cd281bbe15522f03d8a1fec63ac3
BLAKE2b-256 checksum
How to use checksums
8dad2ebb9763c53fcac29176b9e317d810a899cec3d2806b137cc2953636f68b
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.dev20241015222221-cp312-cp312-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20241015222221-cp312-cp312-macosx_11_0_arm64.whl
Size 739.4 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
9c294c30f940480c83677b8d4b377c9b04fe5135b8b352fc4d6984bf5a15613d
BLAKE2b-256 checksum
How to use checksums
b5b03188c3b5fcdf30e7ae3500ce8468be394d729062b6b31593964d4e111035
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.dev20241015222221-cp312-cp312-macosx_10_13_x86_64.whl

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

Download URL typedunits-0.0.1.dev20241015222221-cp311-cp311-win_amd64.whl
Size 733.2 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
3eb8eff408787a850f1ca26a76f943041891510c25d943b92262ab57d9249fae
BLAKE2b-256 checksum
How to use checksums
1d0219b62194e1c18b703f0fd54ca5772d45aaf1a94559dfeff523d8f45cc67c
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.dev20241015222221-cp311-cp311-win32.whl

Download URL typedunits-0.0.1.dev20241015222221-cp311-cp311-win32.whl
Size 698.8 kB
Tags CPython 3.11 Windows x86-32
SHA-256 checksum
How to use checksums
1656ce83ccef83acc16d700be6a45c07f47704008431f36874bd84ee5156df5b
BLAKE2b-256 checksum
How to use checksums
1585263d441774ae6db90912a2ffecdd79ae4268d5561a3f71ed04bcb922474a
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.dev20241015222221-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20241015222221-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
e15232984bc75546f17d079d5d7d31a1a08f09308b1c074cda33f278b2d79062
BLAKE2b-256 checksum
How to use checksums
b29aa38ce8a5bd4c280d50d02d038de4fe7de1edee048f45df3e8e163510db1e
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.dev20241015222221-cp311-cp311-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20241015222221-cp311-cp311-macosx_11_0_arm64.whl
Size 749.2 kB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
d1149a0ca352858542c62ae68f4e02595ad0d929c262f31ec31af5507d235556
BLAKE2b-256 checksum
How to use checksums
0bde847862e510fe484ee2422b1a27fdaf9d5cc6f47bf6a81f915de10dc87cd8
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.dev20241015222221-cp311-cp311-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20241015222221-cp311-cp311-macosx_10_9_x86_64.whl
Size 807.3 kB
Tags CPython 3.11 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
36ad689068ed86338f276aab3afae513ef04b50db828f60238dd74fad0c89ade
BLAKE2b-256 checksum
How to use checksums
c51985a291a3b182de86a60a3864e7de2ac75850052683a02cc5054c96b005bf
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.dev20241015222221-cp310-cp310-win_amd64.whl

Download URL typedunits-0.0.1.dev20241015222221-cp310-cp310-win_amd64.whl
Size 729.1 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
3deba6d7ac16d8fa8a9fc6e372ff8abe6eb75719c11997263faa443b4f9715bf
BLAKE2b-256 checksum
How to use checksums
a80c7df382fb400039bb0de9c8310a45552708d4bca1506bb42595f3cf1451bd
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.dev20241015222221-cp310-cp310-win32.whl

Download URL typedunits-0.0.1.dev20241015222221-cp310-cp310-win32.whl
Size 667.4 kB
Tags CPython 3.10 Windows x86-32
SHA-256 checksum
How to use checksums
83ddfb0ea0b957152d8f3cd7242711717ea3d44abb98bbc596533cb23a224282
BLAKE2b-256 checksum
How to use checksums
4fec9ad65cf36683a9367ea0aadfe4fd7a189c748fa7d3cf03cb130e4c0e7291
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.dev20241015222221-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20241015222221-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
e51efd6045218752cec68e9468cb9b87619097263c6dc59a1f2e275b8c6bdc3d
BLAKE2b-256 checksum
How to use checksums
3f6cc592464b87dbb44e4910d513f2a6e919b2e01406ca09834ccc4d2341bf3a
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.dev20241015222221-cp310-cp310-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20241015222221-cp310-cp310-macosx_11_0_arm64.whl
Size 716.9 kB
Tags CPython 3.10 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
6bf997d486dc25c30a50ddf21affe436e332e5f6357745c5dac12c9a82c110d0
BLAKE2b-256 checksum
How to use checksums
d8b8cb1f817734789b12dc6e488934b8353844047ff0f3b3e04cc2d0f9886efa
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.dev20241015222221-cp310-cp310-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20241015222221-cp310-cp310-macosx_10_9_x86_64.whl
Size 775.0 kB
Tags CPython 3.10 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
41e4437c46e91805e3586869c013fc62d651415193057f658c27cf4eb477caae
BLAKE2b-256 checksum
How to use checksums
cc5aceddff5b9b53fe884c9732fc05c27efb978c0dd32b48b5e1fe259f98792a
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