Skip to main content

No project description provided

Project description

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

Project details


Release history Release notifications | RSS feed

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

If you're not sure about the file name format, learn more about wheel file names.

typedunits-0.0.1.dev20250224221743-cp313-cp313-win_amd64.whl (751.6 kB view details)

Uploaded CPython 3.13Windows x86-64

typedunits-0.0.1.dev20250224221743-cp313-cp313-win32.whl (709.5 kB view details)

Uploaded CPython 3.13Windows x86

typedunits-0.0.1.dev20250224221743-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.6 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20250224221743-cp313-cp313-macosx_11_0_arm64.whl (779.7 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

typedunits-0.0.1.dev20250224221743-cp313-cp313-macosx_10_13_x86_64.whl (819.8 kB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

typedunits-0.0.1.dev20250224221743-cp312-cp312-win_amd64.whl (753.0 kB view details)

Uploaded CPython 3.12Windows x86-64

typedunits-0.0.1.dev20250224221743-cp312-cp312-win32.whl (710.6 kB view details)

Uploaded CPython 3.12Windows x86

typedunits-0.0.1.dev20250224221743-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.6 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20250224221743-cp312-cp312-macosx_11_0_arm64.whl (787.3 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

typedunits-0.0.1.dev20250224221743-cp312-cp312-macosx_10_13_x86_64.whl (825.2 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

typedunits-0.0.1.dev20250224221743-cp311-cp311-win_amd64.whl (780.6 kB view details)

Uploaded CPython 3.11Windows x86-64

typedunits-0.0.1.dev20250224221743-cp311-cp311-win32.whl (744.7 kB view details)

Uploaded CPython 3.11Windows x86

typedunits-0.0.1.dev20250224221743-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.7 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20250224221743-cp311-cp311-macosx_11_0_arm64.whl (804.7 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

typedunits-0.0.1.dev20250224221743-cp311-cp311-macosx_10_9_x86_64.whl (863.6 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

typedunits-0.0.1.dev20250224221743-cp310-cp310-win_amd64.whl (776.7 kB view details)

Uploaded CPython 3.10Windows x86-64

typedunits-0.0.1.dev20250224221743-cp310-cp310-win32.whl (714.0 kB view details)

Uploaded CPython 3.10Windows x86

typedunits-0.0.1.dev20250224221743-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.4 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20250224221743-cp310-cp310-macosx_11_0_arm64.whl (772.0 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

typedunits-0.0.1.dev20250224221743-cp310-cp310-macosx_10_9_x86_64.whl (829.5 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

File details

Details for the file typedunits-0.0.1.dev20250224221743-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250224221743-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 ecc428214c9d926e270492a6e9341136137577941853699a4c068765ed005aa5
MD5 01c8657152618f10182100a46ff0781c
BLAKE2b-256 04afcdbec1bd7a97dbbf70dbfa6b396153782f00d90a558af8d44fdc95644350

See more details on using hashes here.

File details

Details for the file typedunits-0.0.1.dev20250224221743-cp313-cp313-win32.whl.

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250224221743-cp313-cp313-win32.whl
Algorithm Hash digest
SHA256 3c1ee5a06a462d38de0ac62d99b14caea450274be819374b82ca46b2fd586218
MD5 31c6829fb493ff553da54d0ac2b7e00a
BLAKE2b-256 d0ba4877725a5d349ed1d42d72b8bcb3ca492584a653bcd26887d78f06d0ba8f

See more details on using hashes here.

File details

Details for the file typedunits-0.0.1.dev20250224221743-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250224221743-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 2ce3941766223e8665ae0d6421365021504a2a016058c10e288ad6cde35f0f63
MD5 76c34df076555176f5e9b247fdd33ece
BLAKE2b-256 7ac64848012b1262fbd63c0d9910185f153b8566bcafc5324a998ed14412f512

See more details on using hashes here.

File details

Details for the file typedunits-0.0.1.dev20250224221743-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250224221743-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9c3931840b5b846aa8860a17cb771d5dd197a6184a275f25c87519e40b459f73
MD5 e6d47a86856573d9b5c0f4d764f976db
BLAKE2b-256 5027d97fb9c0f37fe8c31b33209bd8e1ef2ab4c243a03cc4f75fa8a1d1e97745

See more details on using hashes here.

File details

Details for the file typedunits-0.0.1.dev20250224221743-cp313-cp313-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250224221743-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 bb55b2815b117a0cb2f4c024abbfe0875ff939f824a4eb7ddccd6aebb2fca8d6
MD5 ee7b8d7c049c6ab2ffb668e2e3db1ef5
BLAKE2b-256 dc26c28369f64e167d3b8526828c471f2e0312c16a845f9809c0e1c7e7bc7d32

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250224221743-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 219e13fd82ba887e6f9c3dfa4a181599e9dae1585dda4d322939398b98afe94d
MD5 531e5a2fc935de6fc7f8783f67988868
BLAKE2b-256 c3c38c8924a343674d7ac05481828a4e84c9ba893d692e20a84714e555a62e86

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250224221743-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 a5c0e14f95633190e8af63d773d3cbe7e75c0c360490236fd7c2d8bc9a800e2a
MD5 611ae900753fc9955feae9b94399ea70
BLAKE2b-256 3f82dd4e3883c3261201a555dc5752fa80babc1c4697f471d908d4596c1fbe71

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250224221743-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 0fb4cf53f50f0d6ddaa5a8600fc14aa0dc7ce43621986b20247acec8a49b1702
MD5 20f1c08da3abb9b6c3e60229f176c81c
BLAKE2b-256 a6a9c843a7437ad92c2ac8293620be55e1b54b2142effc8683b6c80cdcecb594

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250224221743-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 04d948f3ab50642dc9232cbbf71a97f0648200884c27ad627b22d6ad272ea8d8
MD5 f759584a1f215404a872c842ec450e3a
BLAKE2b-256 d044301c51cb862250aede00f495f64eb30398a2b6c1d7800c9fefcf64e0a6cc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250224221743-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 63afedbdc7c08228f1837d62e03a827f946d3bc30f09aacbc0753a58594ab3c2
MD5 e9a984d4b31e321f65f9062a6ecf91b6
BLAKE2b-256 416120b9fb364414d56ba5cc646cf3255bff6910a2e181a9466f124962277750

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250224221743-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 df28a8d3e846ddebb1abce756d415015fe2ba422b6d8bf6b288e05879218e6f1
MD5 5d8445e11c1267a7a6fe47b0045ff00f
BLAKE2b-256 c398e90fd9f3c24260c429446577e158a7e2605d0bc27ede7fa1eb1640572f9c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250224221743-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 309db3c77a5a7520f672e83ee34203385bcdf182797c2ce702e4045c0357b0f2
MD5 7fc797560a36e9a2708e96d2412ebe1b
BLAKE2b-256 5b69e3635250d2568165349859b903c9ca5c38902f725ae23b209cfe81865a51

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250224221743-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a4934c3e9190f85883770ffe10866f4a9771f15c8a54466d087f29bb76ea6ad7
MD5 3e4a994da02e06274eda279c08920178
BLAKE2b-256 8c52e3e0d2202224660c608be9e37469b0c2cee5d0a910756a2338fd91982709

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250224221743-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1898fea9099e621e70c0d1210f68ec042a7b2a031b50e76d9f4f857a279222af
MD5 0672a6a032b0293ac96382788cd945f0
BLAKE2b-256 4862cca621f4330cf43309bbd3ad63c7aba2b3a6f3e7716db06cda3266d844b2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250224221743-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 566c4f6324da29d052c80e2573f0d6b2b74eeca764ff36afc2dcc4d2140dba66
MD5 c049698addf401081cb953a261fc713e
BLAKE2b-256 265054260b50b42b5d52fbaa339c43416f0d737421fd552d001738618ba00876

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250224221743-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 6ddaf4ad18df8cba6028c99996024bad99f0395f70c78d4743f4ca05a895e594
MD5 3242ff26e34153f818b549f8b8ac4c9c
BLAKE2b-256 0625a64005f00d4accc8fb42be6fa3e430356ce58a733732aa023317f7c494e1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250224221743-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 173faa1839819eebb0fa4253786f53d9ceac7075a647a7f7d9bfb434da26fff9
MD5 14cc22aed9095fef34d7bf21e3028609
BLAKE2b-256 f3cd8633ed9b3f3acaf447310fb19d558ff2769232abae29d606ce81c6c83382

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250224221743-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e8e7960d2edcb5289a46294adee44ab7d2ec4dff90d210c8aede4051799c6ed3
MD5 7b63d7054cc0208e7a960180f934d1fa
BLAKE2b-256 63421c05e5ccadb602a319fdc57988b9cad2a3b7779b3d338d53b2116039d10e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250224221743-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 91a4c6175211ad568fcf7fded6863293f08a3e7efe70ca7b060f2d817a6965f4
MD5 732fcd4a8c7a1ff323ef4ef82097edac
BLAKE2b-256 019a2102907396892d8fca13571e6f4dd54ec45d12d906da5aeea72f91fcc0b3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250224221743-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 59a0972367f0277f27736196363f87677b5be17b9afdc59003f69cf093422b3b
MD5 0b6e2b3a72f903409be6c9a3b81cc4fe
BLAKE2b-256 7d11167d4d4f5a4dd9c31a38295a5188cceb0e98cbbd8dd536c9fca5e88e4aaf

See more details on using hashes here.

Supported by

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