Skip to main content
Pre-release

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

TUnits - Fast Python Units

Continuous Integration

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

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

Total release size: 16.2 MB

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

Download URL typedunits-0.0.1.dev20250116000158-cp312-cp312-win_amd64.whl
Size 721.8 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
73e2552428a26e40008a5a6930b825402812e911b52edebdc0232d8a1f137b03
BLAKE2b-256 checksum
How to use checksums
dd63f4b38833a9c696255daec3696cf414769a9239dc4b2472770d355b08c5ef
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.9.21

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

Download URL typedunits-0.0.1.dev20250116000158-cp312-cp312-win32.whl
Size 681.7 kB
Tags CPython 3.12 Windows x86-32
SHA-256 checksum
How to use checksums
25fad27cb358df466ed262b7f6f6a834e370f04625cb98cf40458b9350491bab
BLAKE2b-256 checksum
How to use checksums
8ec32077eb60cb7e2138b38d834aed44afbac8f15521c24feb82b875f7ccb4ae
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.9.21

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

Download URL typedunits-0.0.1.dev20250116000158-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
ffd901be8ba8897047e9cece651a921db005c17e32a9b70cfa6d7abd5f5ae213
BLAKE2b-256 checksum
How to use checksums
9658f5f341a1378a10bc7ea50f24b6152b276cf4120ae712a83baba3dbe05643
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.9.21

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

Download URL typedunits-0.0.1.dev20250116000158-cp312-cp312-macosx_11_0_arm64.whl
Size 755.4 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
1583c50860f0f985b0935da769e1e9138ef2f778ef223b1f78db0cb07a63d06b
BLAKE2b-256 checksum
How to use checksums
e3f44603c8958afbf776574d434687005a510e89107fd09f881fa7fb286fa90f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.9.21

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

Download URL typedunits-0.0.1.dev20250116000158-cp312-cp312-macosx_10_13_x86_64.whl
Size 791.8 kB
Tags CPython 3.12 macOS 10.13+ x86-64
SHA-256 checksum
How to use checksums
6cf4f124968f42b43e1b9f373025a3f10d283c3b1afa1a7416a5674c16ea495b
BLAKE2b-256 checksum
How to use checksums
1d5d8e4e6b763f07e14aa3228ca2776d91e3299d082fdb044b781bc4723fad90
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.9.21

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

Download URL typedunits-0.0.1.dev20250116000158-cp311-cp311-win_amd64.whl
Size 749.0 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
80e382af65f498fb4287b70f11002af9b80aaba299668fa4cd871d95c90de22d
BLAKE2b-256 checksum
How to use checksums
32bdadb60c0815be579803e25846483137c16ddff53d8edd13dffd17a29f6a10
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.9.21

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

Download URL typedunits-0.0.1.dev20250116000158-cp311-cp311-win32.whl
Size 714.4 kB
Tags CPython 3.11 Windows x86-32
SHA-256 checksum
How to use checksums
fb468d9846441e1b203078bbb6cd2c244c971ffca9156def67e6d410b93ffd7b
BLAKE2b-256 checksum
How to use checksums
c9ac639a2d4c12f29b50d26211551817a6ef0ed13e66ada6c36947c6aed1a922
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.9.21

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

Download URL typedunits-0.0.1.dev20250116000158-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
370e1e31bdad12943c84982533fe236a3e2e759f0d7248e441ec31fc4ad4bcce
BLAKE2b-256 checksum
How to use checksums
c4d2f41eaa277224d1944debeec886cf0a0e5fbb035d7e5fdeb792c0b2f40cda
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.9.21

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

Download URL typedunits-0.0.1.dev20250116000158-cp311-cp311-macosx_11_0_arm64.whl
Size 767.0 kB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
2380b3f80cc1830c8f5646cc39fe94e23d9fa62af6f337c0833baa07efcc6666
BLAKE2b-256 checksum
How to use checksums
9b4767cc99a2c6edd6d352850cc46027b754b0fb1c196ab2bff2b79399fa7669
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.9.21

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

Download URL typedunits-0.0.1.dev20250116000158-cp311-cp311-macosx_10_9_x86_64.whl
Size 825.3 kB
Tags CPython 3.11 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
f1f86cdec5953f70ba498ddff61d3d29a604b8d150b731befc34ef3de51c832b
BLAKE2b-256 checksum
How to use checksums
fa9e9111ca673652ec10518d6dd2c080edab4e6034c292d85ada601b46401839
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.9.21

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

Download URL typedunits-0.0.1.dev20250116000158-cp310-cp310-win_amd64.whl
Size 745.2 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
e3163bd8937b3c02dd1ec3a2c9afb485d100b80465f7f8dd1148cac32968a5c8
BLAKE2b-256 checksum
How to use checksums
edfe53f8d21a90b53e5e341ae017cfd95b1df847b8a79c834c030f54fb036209
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.9.21

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

Download URL typedunits-0.0.1.dev20250116000158-cp310-cp310-win32.whl
Size 683.2 kB
Tags CPython 3.10 Windows x86-32
SHA-256 checksum
How to use checksums
e0813f196f3e1699da9b1360328dd0b11f4ae111ddd25c53c2f4a5c7ee066f58
BLAKE2b-256 checksum
How to use checksums
0136453d0581a5bef5167ea226375982a04898731c0a30b5a1b70431ce35cd70
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.9.21

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

Download URL typedunits-0.0.1.dev20250116000158-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
eaa27547341fdab986255fdd05e8621e105fb70d16d3fff2cee62c849dcc4bcd
BLAKE2b-256 checksum
How to use checksums
0726fb8fdca06782c07580171132168ca34b16b8b13193f4f00e06b02ff8bbcf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.9.21

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

Download URL typedunits-0.0.1.dev20250116000158-cp310-cp310-macosx_11_0_arm64.whl
Size 734.1 kB
Tags CPython 3.10 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
d37e511156d5b1485ff05a89c4cdfc758fc33531a179d3074cde3629a8fe48ea
BLAKE2b-256 checksum
How to use checksums
9de0a610bf78216d7f9ca3e515c8a987f0e0f36a08a6d9234f6711e7b886b892
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.9.21

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

Download URL typedunits-0.0.1.dev20250116000158-cp310-cp310-macosx_10_9_x86_64.whl
Size 791.6 kB
Tags CPython 3.10 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
992f94455b353d9f6c4146dd28238d780c947013992e981a69b201c7d9b65568
BLAKE2b-256 checksum
How to use checksums
ddec82c503bb2c418c9e35fd1f853a99d757ac3f1dd83ae422813f9e36a2ea49
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.9.21

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