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.dev20250313205936-cp313-cp313-win_amd64.whl (778.6 kB view details)

Uploaded CPython 3.13Windows x86-64

typedunits-0.0.1.dev20250313205936-cp313-cp313-win32.whl (735.7 kB view details)

Uploaded CPython 3.13Windows x86

typedunits-0.0.1.dev20250313205936-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.dev20250313205936-cp313-cp313-macosx_11_0_arm64.whl (807.4 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

typedunits-0.0.1.dev20250313205936-cp313-cp313-macosx_10_13_x86_64.whl (848.0 kB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

typedunits-0.0.1.dev20250313205936-cp312-cp312-win_amd64.whl (779.8 kB view details)

Uploaded CPython 3.12Windows x86-64

typedunits-0.0.1.dev20250313205936-cp312-cp312-win32.whl (737.0 kB view details)

Uploaded CPython 3.12Windows x86

typedunits-0.0.1.dev20250313205936-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.dev20250313205936-cp312-cp312-macosx_11_0_arm64.whl (815.4 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

typedunits-0.0.1.dev20250313205936-cp312-cp312-macosx_10_13_x86_64.whl (856.4 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

typedunits-0.0.1.dev20250313205936-cp311-cp311-win_amd64.whl (808.0 kB view details)

Uploaded CPython 3.11Windows x86-64

typedunits-0.0.1.dev20250313205936-cp311-cp311-win32.whl (772.2 kB view details)

Uploaded CPython 3.11Windows x86

typedunits-0.0.1.dev20250313205936-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.8 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20250313205936-cp311-cp311-macosx_11_0_arm64.whl (830.4 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

typedunits-0.0.1.dev20250313205936-cp311-cp311-macosx_10_9_x86_64.whl (892.8 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

typedunits-0.0.1.dev20250313205936-cp310-cp310-win_amd64.whl (804.6 kB view details)

Uploaded CPython 3.10Windows x86-64

typedunits-0.0.1.dev20250313205936-cp310-cp310-win32.whl (739.2 kB view details)

Uploaded CPython 3.10Windows x86

typedunits-0.0.1.dev20250313205936-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.5 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20250313205936-cp310-cp310-macosx_11_0_arm64.whl (796.7 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

typedunits-0.0.1.dev20250313205936-cp310-cp310-macosx_10_9_x86_64.whl (856.9 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250313205936-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 af55b1213eef491dcb440531392a30f71d1e67eb1859bed46402d277de05599c
MD5 46ea35334c7c015b8f6f506999b1fdc7
BLAKE2b-256 698ba0996036f50bb212f26ea5ea877e0ce3e49b55ea4793f55e248cea1388eb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250313205936-cp313-cp313-win32.whl
Algorithm Hash digest
SHA256 2655a9a722c662f9189330fe3280f7eca0fffd75575f9ef246b71c3045131017
MD5 21d232567605a5a0ef861600fca747f2
BLAKE2b-256 5ef467acc5b99b1f93d1901a2877c83733c442defa440e324d38b451b471d242

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250313205936-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a33164ff52db840d292787425e608f8e03f7a7e352707e4a68da049109702f55
MD5 8f2ab8422f1e9e11ed47d8b218080c1a
BLAKE2b-256 064c334d187eab3cd54b9088ebd768bbd5747f75dc1e56feebc95caddfdd265c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250313205936-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 566f63b7ea5120f92e40332609899169f6761a29dc74acf7ef27344f983da91c
MD5 033a826f39ec31e94f6a7fe0ea39645a
BLAKE2b-256 f07802e453fed59b93c2cba6b05c1a1fb9553543181cb2172cbfbd322f5cf70c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250313205936-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 d386dd4123cbce2eb1bff460a0e9a5dbb75c09e9a47773144ca3e85764b61bce
MD5 447836ddf517d870675367acddd5a06e
BLAKE2b-256 14a93f441204ae1c3dba5253a25572193633d934e671871b13e0e9481e656eba

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250313205936-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 9e8df6fb708475703204a405a15e2659e2fe6be07eea891c23923af5ecc9e821
MD5 4419abc89ccccc5a60f0e298954672c2
BLAKE2b-256 b82a6d38d49afe89a25c0d585bbcf452155666958a9389d5a39f1b2fc3373583

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250313205936-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 a2dd7cae4231bb5bfafcf07964507beef5fa54c85eb92db9a24e34f2928ba4b5
MD5 a74e7afbcff6532b87f85ba7935187f3
BLAKE2b-256 a1d2981d07238cd661da1fb162619c34796bc00fc19b6e31248463e5ce84d984

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250313205936-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f8c607421b379017bbb76b357e5f180971257aa744ff48f7e744ede6b0e4b819
MD5 2e81f97101b3ca680475986e098a63a2
BLAKE2b-256 45fef330283a7e7d7a6685dd74055908eb2d7a064ed6cddee0c5ba7e36f4381a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250313205936-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e1d4a5f66828501228594bfa740d140b190e3f2686d19fa864d7a0917c7b7a55
MD5 2366f1cd36b8774cacc68f529fc2a22d
BLAKE2b-256 7a9a8e470152176081e39e5919606bfe1f774a5f5c40635e1a637acc331bcb30

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250313205936-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 c75f66afc6b18f504891f9c7370e9ba6ee8fd0c7cde962ed018dbf11fd1a1cc2
MD5 49768c5c85917afeacfb2492ae8b8e68
BLAKE2b-256 2dcf5b896dbf945e8bb0c0495c2d101475daf77527074a00f9589385749c274e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250313205936-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 ecf12e6821ea89ba0401c8932e5041301e0fa1c2462754ee73912dc8692b3bd9
MD5 f5946fc1d609e32b6ada87adb22fab02
BLAKE2b-256 9fe2bc5ba62c8fb2c552b350949834b968fa79697192dcae5ced29ccaa5d1ded

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250313205936-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 05a8208ce5c85067c55a5c3c5d3b84ae21c0c3a393a5009dac2c66f7042379d0
MD5 58235b2bfe47c075547116db8c0ae2cb
BLAKE2b-256 355efc03788013e286678eef0242330606035e7ffa8154ec044ff91b53dfbf0a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250313205936-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 13dcbb76920eafca3741cb525475aa599447f1cffdd2cf8b1bf1ca175ce6f28f
MD5 339427ece693909ea0d772c69ca77bbf
BLAKE2b-256 c54933745c792dcee958407cf67f076b745148733436a7a6dfc6432adf9b6625

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250313205936-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 759046de96cc86355d1c25d4c1fac0ef35b4052f1999b7942407cae339402c37
MD5 ad236c6cbac501e64445c20cd92dd442
BLAKE2b-256 4af34746dfc05361813c7d9fc7fbc5c910164c6e186fb771632f02c8a7c5a8df

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250313205936-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 f47dc25c44080efeda9b014faa974a58cc27fbdc5557a75d3507d9d31abad176
MD5 ff75c719447cfab69dc110beeb181c17
BLAKE2b-256 fdfc925829344896b70444e097bff38daec30c8febf4ff117532ed5a70341d05

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250313205936-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 a52b749e0770b75c47f6036809636e906eb3f17222d01ac64f1001c8cfaae155
MD5 d3ce6dab9144796055f1bc186ca06139
BLAKE2b-256 53fcf13a973452d0534c824b160b8a097773197893c7e730b73bb9bb3c902879

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250313205936-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 f27aebcac19237d816dd105033a24bb46a0b5fb43ccf9e370d66f237f2fc9244
MD5 6d61638f25757c423fb3ffb40af6df13
BLAKE2b-256 249aa564504b73857a908af527ba746bf5e795340e65052e5a084da620c8b116

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250313205936-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b8d5ac7fc7f28436c8310a3a789e2b7eb516591ec11126537033b552bf76645c
MD5 4063c92be8edb89dc8d33b62d2b6fb07
BLAKE2b-256 ad7500f97af1ce3dc58cecf7c6710f0dead0ce78977e912a119e1fa3fae339af

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250313205936-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1a96fae59a1433a252860728f10bbe5024dc47267a44ea2acbaee9098701c730
MD5 50593932faaf6f00f1181068f6e2ae55
BLAKE2b-256 cd9241238978470cc1fe76e6ff6a1941b031e8cf37a8886b5511f68b82379b8d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250313205936-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 cf7d2538abfc37ac450648cb573fa77a6f9735969422b0aa63ce9d3057410703
MD5 e28c43c26370d0ec307bdb9511b5f6b7
BLAKE2b-256 332286441068553d70805042798aa1c721de459d228047fa483fba6d51da41bf

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