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

Uploaded CPython 3.13Windows x86-64

typedunits-0.0.1.dev20250214195617-cp313-cp313-win32.whl (702.2 kB view details)

Uploaded CPython 3.13Windows x86

typedunits-0.0.1.dev20250214195617-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.5 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20250214195617-cp313-cp313-macosx_11_0_arm64.whl (771.1 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

typedunits-0.0.1.dev20250214195617-cp313-cp313-macosx_10_13_x86_64.whl (811.0 kB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

typedunits-0.0.1.dev20250214195617-cp312-cp312-win_amd64.whl (745.0 kB view details)

Uploaded CPython 3.12Windows x86-64

typedunits-0.0.1.dev20250214195617-cp312-cp312-win32.whl (703.4 kB view details)

Uploaded CPython 3.12Windows x86

typedunits-0.0.1.dev20250214195617-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.5 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20250214195617-cp312-cp312-macosx_11_0_arm64.whl (778.4 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

typedunits-0.0.1.dev20250214195617-cp312-cp312-macosx_10_13_x86_64.whl (817.0 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

typedunits-0.0.1.dev20250214195617-cp311-cp311-win_amd64.whl (772.1 kB view details)

Uploaded CPython 3.11Windows x86-64

typedunits-0.0.1.dev20250214195617-cp311-cp311-win32.whl (738.1 kB view details)

Uploaded CPython 3.11Windows x86

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

Uploaded CPython 3.11macOS 11.0+ ARM64

typedunits-0.0.1.dev20250214195617-cp311-cp311-macosx_10_9_x86_64.whl (856.2 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

typedunits-0.0.1.dev20250214195617-cp310-cp310-win_amd64.whl (768.8 kB view details)

Uploaded CPython 3.10Windows x86-64

typedunits-0.0.1.dev20250214195617-cp310-cp310-win32.whl (707.2 kB view details)

Uploaded CPython 3.10Windows x86

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

Uploaded CPython 3.10macOS 11.0+ ARM64

typedunits-0.0.1.dev20250214195617-cp310-cp310-macosx_10_9_x86_64.whl (821.4 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250214195617-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 d45100cb35d414b7cc141093c171fe693334b3e2814f45b8fc3e8bf838ea1c25
MD5 2081ab154ccbdda37322facf0c7ef7f4
BLAKE2b-256 7fb82a6b3570af7d6094fd6353a940311bb9e9d24f651ea4b469509de543d26c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250214195617-cp313-cp313-win32.whl
Algorithm Hash digest
SHA256 bbc09e8d6b903423817e41e24a1fffd3d3380d923d23ee67001ba93f25273f60
MD5 93a2846be38ee797115ad48b787a2dfe
BLAKE2b-256 557a93cbed1b9f19ee56d83c8cf9a9798c5cdb433ec4c06d5435ad2b18af4418

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250214195617-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5d95426e8d0aa47d887b7219d5c96180d33fbd49573cece467f03fcf50d79fde
MD5 a2fecd5592d8d1391d53ed0436964a22
BLAKE2b-256 6670146c07851bd8dd999634651adeebdf2c928034a856de351dca852cce0ed3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250214195617-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 507b2544c37baf45da0c86ffb1afc26b0015e21765f810475dfbfcc94d0867e6
MD5 44cd75e57d08156c0cde712f1ee5e0f2
BLAKE2b-256 077d207c138aa5088a95d6a4851dc91eac960f845a490624599903d4d1acad9b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250214195617-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 fc75468f62d4e48c14708ecb9f3e49a691b8c1434fd770790329a59d1c188b2f
MD5 dbaf8f3b96ad72e110d0d874f4c5e613
BLAKE2b-256 90f1aadf3df3954999be194b6ecb45f009f62c7b47605659b72be5a6c997df86

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250214195617-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 26d811c823ed2e59f5b0d49f3720c8f27041498d5c105732c762024cb02639f2
MD5 2d4a6e4705d88f77c2e8dde6924223a4
BLAKE2b-256 d03cd8a0efb2a695f0209b5fbfe57b2c8c120e219ba79e3a7ae6c10a2529c62e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250214195617-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 551f5f1c83360b1f3d9d425ffb45e519223b5f08c02eb1f90e8b2021012112f4
MD5 cd2079812fd2061a43b22b5aba86e071
BLAKE2b-256 6662dcca96ea2f98b9f0a8029b5325d1554fbbf74841df5b96f1ff284e1d207c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250214195617-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 839b34c14aa4d8c101b07be52ad8f59f83df62e464c31dacffb9fa23872f5b4e
MD5 c8f3d75c7823440b42c477349fbc1bfc
BLAKE2b-256 3a8a3e3b1da10211bf9f678e8fddada943ef52cb45dd113e2a5cb1d56710816b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250214195617-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ce3ed429f55f8d5a0cf12964833363b28a78f10cd9519ba83f4b41b57b0464cd
MD5 79620faec8b143a4f05a45b04e9d3a38
BLAKE2b-256 2992c9343e94ade9dafe5b2a19ac76af025f62602ad050c39bd3f64e207552cf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250214195617-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 7ea3e62e2772c1026a6dd466bd1f22610167839c16400a62ddc9f2b1b371d67e
MD5 e68837843228e187f6fa682510b15808
BLAKE2b-256 fa278178a36662147bf2de4562eafd424ff15e17ac499cae4963eb2accf270c5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250214195617-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 bb9bf356e9c2954fbcfecd19ced9db2446cf67c7281838ef526085b66a18a183
MD5 349e7cb35e7783a3b4429ff9302066ad
BLAKE2b-256 37f3229a99d31d9e77572a6546f51343229152b78698d6d5a48545002ba77a85

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250214195617-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 6be93265972f51e5cf5dafffa373070f3f19ae3400a3f6c2aee524a6a4ceadfb
MD5 ace3e9f2548f5498e31bbf5f48eb9f54
BLAKE2b-256 aa23aa47715ecbf30840596316261eaea1a0a8989eb40e77f615771499165418

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250214195617-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 20cd236b03e975ef4a51bd1711c8c5c7c99b4c0c8b0151d71f77cbb8fb65e0a9
MD5 b8f84eca42d202e5d1e56d6777295254
BLAKE2b-256 61b41a17f1de5499a7ffb81777b449daf9fd48597b442899850995be1bfcb0c3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250214195617-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a72af4ee1af5bd5fd9b313e13441929c1ce29670d38aca8e0c5d706890920439
MD5 1b595c52806f2547bf9b97ba7142be8f
BLAKE2b-256 3efe9e451672099a56d806f0f6119cad00fa0021d8fe316ad0def33d1a34e066

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250214195617-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 867b3770f77f214ecf2859f2c1a48c973e9a57557e4908f711a419f586b2fa79
MD5 9e71415e0434eac7a54b52e1697c172e
BLAKE2b-256 2b1ee20b762d3b5d3ce81bf84cb79cf45e57c56d2e95e14f50c4a002424cafa0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250214195617-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 318a8c4a9dc51173f83fecd46610377aae10049c581fc01d2801ddfc320cff85
MD5 65ca666bf6961071765ee19435b7bc91
BLAKE2b-256 d24f5accb3d5af352fd04e3c2aecfa6ccb00c32ab80f0736acdebb42474150d1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250214195617-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 64bcc5795c39525ba28f4ff2ccbc986985af0769077043843decf51bcc3b1b65
MD5 ed2ce4cc66402d39f949e6d9fe861712
BLAKE2b-256 30e16b29abc4ac6bf212cb602e44c831b9f1dd8bdb0134dab4ee968ef81ba175

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250214195617-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6f3ecaf6a3e1e3b7f8f3ebf595e705c69d77acc456978f7f165c9a13c7e3881b
MD5 4c8ebde06c535275cb60b6ca76725daa
BLAKE2b-256 8c88a0dbb852cfa027aa76616fd47ecc8d8b55862a7ef38a0af45ac8564b0da7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250214195617-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ecfc631ce68881663670e9a32b56ca0188567053983657dab4739e2922eb571d
MD5 c54b385a04d456116486d3771346d113
BLAKE2b-256 3c7ae72ddc64f32867d0f57d2c26f9e850313ce8f34bed428863192eda8a15f9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250214195617-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 d73245170f4228a0e4a6346355769b66efa536d0cefce935b1c528f268b3c297
MD5 b0cc3a93aa5f8030f21ef962c4177608
BLAKE2b-256 333eae7098c7b6930c484a11ef44195b0a05931e6d7c4899c5b6c1af622eddb7

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