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

Uploaded CPython 3.13Windows x86-64

typedunits-0.0.1.dev20250305020553-cp313-cp313-win32.whl (721.5 kB view details)

Uploaded CPython 3.13Windows x86

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

Uploaded CPython 3.13macOS 11.0+ ARM64

typedunits-0.0.1.dev20250305020553-cp313-cp313-macosx_10_13_x86_64.whl (831.8 kB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

typedunits-0.0.1.dev20250305020553-cp312-cp312-win_amd64.whl (765.2 kB view details)

Uploaded CPython 3.12Windows x86-64

typedunits-0.0.1.dev20250305020553-cp312-cp312-win32.whl (722.7 kB view details)

Uploaded CPython 3.12Windows x86

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

Uploaded CPython 3.12macOS 11.0+ ARM64

typedunits-0.0.1.dev20250305020553-cp312-cp312-macosx_10_13_x86_64.whl (837.3 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

typedunits-0.0.1.dev20250305020553-cp311-cp311-win_amd64.whl (792.6 kB view details)

Uploaded CPython 3.11Windows x86-64

typedunits-0.0.1.dev20250305020553-cp311-cp311-win32.whl (757.0 kB view details)

Uploaded CPython 3.11Windows x86

typedunits-0.0.1.dev20250305020553-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.dev20250305020553-cp311-cp311-macosx_11_0_arm64.whl (816.8 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

typedunits-0.0.1.dev20250305020553-cp311-cp311-macosx_10_9_x86_64.whl (875.7 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

typedunits-0.0.1.dev20250305020553-cp310-cp310-win_amd64.whl (788.7 kB view details)

Uploaded CPython 3.10Windows x86-64

typedunits-0.0.1.dev20250305020553-cp310-cp310-win32.whl (726.2 kB view details)

Uploaded CPython 3.10Windows x86

typedunits-0.0.1.dev20250305020553-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.dev20250305020553-cp310-cp310-macosx_11_0_arm64.whl (784.0 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

typedunits-0.0.1.dev20250305020553-cp310-cp310-macosx_10_9_x86_64.whl (841.6 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305020553-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 b28c9cbe3977af4220ca81c037bf7743fa8ad4414fd88a796520e8c20dd10e85
MD5 a1459226a78c3359bb207e719d23094c
BLAKE2b-256 0af90e4c85c3b4df4782b9f26d2c56a4ceb7330724f56a121fc0c417cf582d39

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305020553-cp313-cp313-win32.whl
Algorithm Hash digest
SHA256 6ef26ac9ac6794914eb8591cb70ebd4b71c004f5807c96837e06c7bd04ddea5c
MD5 4b32fa35c779b1dd82ef6622c859dfde
BLAKE2b-256 aeed9427cb893d93179ec9043add9cfe31369b19198a0644a42c3a652ad6f2ba

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305020553-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 cf055355749095f738ea2bc112d7f090b54e883df5e7c52d40331b98915e341e
MD5 86ff2d449a24385db2a0ce5d680ad209
BLAKE2b-256 aa7757e550f324b70fc3d18658fb740cdd97327ea482f05c0970059f168dcf43

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305020553-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d2a8aa89fc10816c648560dc7586f2ea7085b9fd8729f335509c2d96c321d4cc
MD5 abd47efcb52c4ca12f1831a04d7cb4e4
BLAKE2b-256 f7d43f0d188514d282825aaf056c569cc811e5a66fd2705beba244e6f9d9d489

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305020553-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 eb7712bce01b411ede3511d1dcf84d0a18da624f384b49dc32412e99d34b8842
MD5 217324dc567963ecc1a5739d10a51a01
BLAKE2b-256 eb8be08bd25c58ba94a529135ddbc2a8b1cdfc8da79e3cd3db25b9607fb2d035

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305020553-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 c67a87a4d3008c94966a0949d918cc7b1c2e1265b20570123c7a85bf8f28a3bd
MD5 27ac7579704f90cabf921a5d31a06c35
BLAKE2b-256 589d7942001b9c361816cad97c5d62412e7f1cc0bbb495d60de32406dbb315ff

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305020553-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 43139e7fb98521c09bf1cd19fa596d8ada4c9c9ab703062c5bd9f1e5e134fd99
MD5 b66f16853fa2b558b1f08612098fd5d7
BLAKE2b-256 652a407d2089039e7c3eb844a19954c95ccd82d4bdeaaab18b47c4ac975a04dd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305020553-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d1d4dd319b887c87c49df02cf64c866fa6e1c63be4382b3111f3d0d896385006
MD5 028b3abd93c5e2cb3737dc61f0cbc790
BLAKE2b-256 4171f56a90aa433542b6b0b1f4b625d51adff9ab5705dd8b73d4531bb1b24cbe

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305020553-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 661d32dc5a41ff73a621974ea4ff5fd0301923d24c8cc096a2f915b4f6b43cdd
MD5 675baa446bc38b1c4d74e628de5e84ef
BLAKE2b-256 df652e7a5afe01fbae0ad82bc5c04507cb56e1627856e920fc72e39eebaa7b8a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305020553-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 266199fef579515707f5b56a3963e1cd5b1e74e74042dab7d5fb064f3773c45c
MD5 83e320314139ceca59285b96666b859a
BLAKE2b-256 1dc12045d9562d09de1043e1ddab4568fa18fe526545e4dcbf4777a51bf3414a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305020553-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 ffd200d9c7a135d9cb7d859847910ccdaab885dc23fe7552422af6cbfb1d8ed9
MD5 02f74c1e9e435b483228003c4c4152ae
BLAKE2b-256 c1ea7cc1bc6618fddf92257e487f37bf07335f9d8e2837d9fd5a46c902faaab1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305020553-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 1334691ae324edf874914bba07bc5afbb2bebf57319e9415e24c63c5adee60d2
MD5 f289bf7c10664ac160fc96acee0fd8a3
BLAKE2b-256 73dfd80e57677c5401988851f9231a0584e92b6bb2855f93e2fea77151f55d83

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305020553-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d26cc602e98734e5944b6ac51bb80a1c1f4945c3a2ea8e6d614ca76618cb65a1
MD5 391ed9501431083842a73aed30ccfcfb
BLAKE2b-256 7fa4901f69b82c3d8acc70eec3b9b5a4cf9ef19ba01c1878e41024545c7f3988

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305020553-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d6746ab9a86f3caa0279c33d1bf1c5e72f7c4d156e75e5a54c05e617eb522e92
MD5 a3bdd308bdd276009ed1e5f680a78e2d
BLAKE2b-256 6fabadd45d7b63080a81db12f8d6c43e38504cd7de273ad350747039b9d5615d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305020553-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 26f791bd3c3fb8e7382b6bdc8f1741d8526565aa07dfec4a35cd09a113f83ee6
MD5 2edaabb8105abbe0fe10ef3a884d4b6b
BLAKE2b-256 8f761cc68342a1302ef25396b16f60f686270e2035cd60838c259fe11e57778f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305020553-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 283e47e14bbd38403eca8a8ff9dc8880b5072943e8c35fd1715d972699f87109
MD5 c8c45241a9d57135826f0856fb05af20
BLAKE2b-256 f3bd91c0122c35046a31fdff4c41cd821dc47ee0c56fafb55804de13271477d1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305020553-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 67c81e2d21b66b03cbd60a7baaf6712b9672472c809cabebebaa6c5910fbe0fc
MD5 0bc16f79033e27d9de76fa8720f56e29
BLAKE2b-256 d1e50b61fbde7e41574f63d805cf3f9b8a10750665180614adcb5f371a6c8a9e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305020553-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 603b75c5c3c2aea02bcbdb721a64a48de606606f1c329bc4b6e14ac15d07092c
MD5 d5c22832ca8666bccccd086c6596b6fc
BLAKE2b-256 780595d7f7ae92f1c18163edfc579605c4481db473d0a778002cb3e9b34f2db4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305020553-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7d3d625a95c6c43528963137b23ef5296a123182749f7a3293d6b49270ada810
MD5 82e3f523ff19b5b5eb1bfdc4a888cbcf
BLAKE2b-256 442429cf85c5d87165070ed1eff0805cc8ee221cf00e5a1114babdef301b841c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250305020553-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 c6fda2a2c92b425fe8808b986606eeaf52f23130a26a5f00fcc5271deb5e174a
MD5 d9c9e58d735c21b0003d1e491c914b8d
BLAKE2b-256 9356a12f2f4aff945fb2f16a530c657465d30081650212fc0a46f4eb5d724035

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