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

Uploaded CPython 3.13Windows x86-64

typedunits-0.0.1.dev20250212002305-cp313-cp313-win32.whl (699.1 kB view details)

Uploaded CPython 3.13Windows x86

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

Uploaded CPython 3.13macOS 11.0+ ARM64

typedunits-0.0.1.dev20250212002305-cp313-cp313-macosx_10_13_x86_64.whl (808.3 kB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

typedunits-0.0.1.dev20250212002305-cp312-cp312-win_amd64.whl (742.6 kB view details)

Uploaded CPython 3.12Windows x86-64

typedunits-0.0.1.dev20250212002305-cp312-cp312-win32.whl (700.1 kB view details)

Uploaded CPython 3.12Windows x86

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

Uploaded CPython 3.12macOS 11.0+ ARM64

typedunits-0.0.1.dev20250212002305-cp312-cp312-macosx_10_13_x86_64.whl (814.4 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

typedunits-0.0.1.dev20250212002305-cp311-cp311-win_amd64.whl (769.6 kB view details)

Uploaded CPython 3.11Windows x86-64

typedunits-0.0.1.dev20250212002305-cp311-cp311-win32.whl (735.1 kB view details)

Uploaded CPython 3.11Windows x86

typedunits-0.0.1.dev20250212002305-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.6 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20250212002305-cp311-cp311-macosx_11_0_arm64.whl (790.3 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

typedunits-0.0.1.dev20250212002305-cp311-cp311-macosx_10_9_x86_64.whl (851.3 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

typedunits-0.0.1.dev20250212002305-cp310-cp310-win_amd64.whl (766.2 kB view details)

Uploaded CPython 3.10Windows x86-64

typedunits-0.0.1.dev20250212002305-cp310-cp310-win32.whl (703.5 kB view details)

Uploaded CPython 3.10Windows x86

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

Uploaded CPython 3.10macOS 11.0+ ARM64

typedunits-0.0.1.dev20250212002305-cp310-cp310-macosx_10_9_x86_64.whl (815.9 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250212002305-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 1872811fc1c2a95668ff8682e3eacc4df89437bb9ca4ebbcb74d319dea2e0098
MD5 2eb2f1f1985f8d16e04737ab868bff6a
BLAKE2b-256 828e862a086d31228d85467eb5c983d5a257956b602d84ca790ef64313e4439a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250212002305-cp313-cp313-win32.whl
Algorithm Hash digest
SHA256 d7694926d083be1e37dd6fa94abd301e53debb44a4a7e91fd51b217ef6cdbb99
MD5 4d518589c0947c8178ab7bbe8ce5f73a
BLAKE2b-256 bc3e7618cd32b963a8703b1be3c18b8aa47460f2684632f10bc62ed05787b00d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250212002305-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c8d9ba9a34ded6f17b17307dfe5bd4b777ebf31e209ef79970fc585fc835812b
MD5 a785b21ea069afcf27e143615ef0e1e2
BLAKE2b-256 ff2f07b4e93ff62eaeda6ae80f409313418ec36e8053136ba89db2650c5ae32b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250212002305-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 67dd2c5f9f2ce78888d992f26d4cbff85983032bcfcc069b0d731d2079b548f3
MD5 6f67bf8f725425dff9660a6c3279fded
BLAKE2b-256 de9cc4ed0a9e4fa7ed9dd5b102afb45d836482d497834a5127f43d617fff78f8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250212002305-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 4b0ae95a4607db6e3f295ede005134b3d626ce4d1c580ef5e92d5c599817bd05
MD5 041947e53043fae332fdfe56b7d18d2c
BLAKE2b-256 705362e31f56b82500597d7230adb40a9b6094ed7480da26176537af83e66c9d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250212002305-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 8392634bf1f4661bb2f331baaa50a35b50e46ce277c7d8e1da9be0f1e8fce367
MD5 35795d79725c00c866bcfd5d1db22bb8
BLAKE2b-256 ba751146ef3ce70c3c9c83ccd84d753b89f35c55f23ec4e1bef7b022e5e44645

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250212002305-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 f8cb744cfae938c9a20f4e3fdb4c5b8ad7374b4b54fd19bb0aa4822ff274374f
MD5 3935b2aa0a1bacd399efbe85421c9319
BLAKE2b-256 682edd76c344b4b2c9bde368b9ca810bf8b2a6a6d5eab156b9978addff47c636

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250212002305-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 878450ee53c9379f328afde4304bf98ef7e972e24191180578edc4b780eff61b
MD5 fb0fff19182c48030a37f65920bf1e24
BLAKE2b-256 c8fad506fad559b47f679107b78e17a5178cd7eb5e2cc52d6798d86efd88cb76

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250212002305-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1ace4fc49512629991808f494ce19cc751ef1c5581f0279d86a077962cb0e0cf
MD5 b33b16fe6ea4aad817ef4f638b94d90e
BLAKE2b-256 b23ca5cea736e1b8c338d9f5f539bb9db57ec54d1e4d4a72434d3bece4278d49

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250212002305-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 e12f9ef64c5fb75f39aabe5f2ab728af786bb8ee028f51409f4723e646715115
MD5 e57567f7894a7ffe784bf493afad6f48
BLAKE2b-256 562638efba64b4386561d6463a0c4ad42e9da4e875b17b7617249d0f1f3baed8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250212002305-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 a781d118759cad8b9340d4785330a2b28217daa95abf808762bd7bba403cee30
MD5 03102bd325cc3ace1e128c9bfbad3cc5
BLAKE2b-256 57527a83cf528d383e009677911e23305c5556771e8aba5a9185b5d1e680adc6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250212002305-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 2ff781fce302db88d9a36b5c8ac85ec878ce3c3ce0c4c38747599f3d8c92fff5
MD5 ac7cc096a403d6cbcbce89f07a29a4f6
BLAKE2b-256 a298e45376216ca86d57ffbff84cf88a0e290b6574206164ea248976cbb9907b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250212002305-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 eb67bf74e684fd5c3fe16a5bd6cc8c2eb5f6c129d9b0e61e4903cd2ec2742c45
MD5 62d107e25f7d203506fa5d3789885cdb
BLAKE2b-256 89a718175b22bf2a5b957c58fa333aba2daa8c4335a87418b621d75576479f1c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250212002305-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 97b21881014eca256131c951942638e5e867aea7d079eb2b6699eaab99e66a37
MD5 a550382e59d5e54b0654487357afdea2
BLAKE2b-256 8bdd4a9da993ec27b4236b406eeba549858b68a43cfb4b41c60d5dd675123073

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250212002305-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 d5d0c82e35eb32219ff6e20a9bbf88f2f26ea42494eaee11e0bc6ef868d20214
MD5 dec4dd5d4e71c17ff8f78870a6af6203
BLAKE2b-256 e63c1cc45809b240f280f6379e428166739d983597e6e8dc75d111b73ffa3a4d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250212002305-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 056b58278de4c1460d6bf2e6b399f027aa61f2c668f7a9a66fa8b5743ffe1231
MD5 2b63da786ac1705282ffbac275e3f7d8
BLAKE2b-256 db8f6a582a291e139bc13f3ff578852b54b891a913d3abd8d0d5c8b303399bde

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250212002305-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 03e9705d1e8dc8bee4750102487fe8f94775a583770e85f07a472045e8bf5a66
MD5 5dcd3215af61c53c7500fa69cca775d0
BLAKE2b-256 fb123e312b35ba155a9770132ca82fc65683533a16f109b59cba2ec4927065f3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250212002305-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 88b89abdafb526537b15f460c94b04634f441661f6c5df7081b36f28d8a4c791
MD5 feb6ecabea7df58d1e14a0ce40ab6913
BLAKE2b-256 8ae44255d03918b64af1f4e1dca10f066b8e33d03eebdc7d7224f8a58e77bb96

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250212002305-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a29d89114d1f381ce96380e9e292b8dfa82dc14d5b1b61ae5ba18db21bbc1407
MD5 0734121f482a9b611f275f028f9e2c67
BLAKE2b-256 a71addbb3cc6f8f9a32cd862d98edd60bb7053db69e9ee29a8fd6bd8690f19a6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250212002305-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 eec5909caccca533899f004da4001de253c4de546fe002ddc16f281337b61181
MD5 73ecb9441362755a06f244928c53aeab
BLAKE2b-256 745ca5bd18452ff1da33a24ed75ae8b8f0759131c40406994500dc2387c208dd

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