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

Uploaded CPython 3.13Windows x86-64

typedunits-0.0.1.dev20250425170239-cp313-cp313-win32.whl (741.6 kB view details)

Uploaded CPython 3.13Windows x86

typedunits-0.0.1.dev20250425170239-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.7 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20250425170239-cp313-cp313-macosx_11_0_arm64.whl (814.3 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

typedunits-0.0.1.dev20250425170239-cp313-cp313-macosx_10_13_x86_64.whl (854.9 kB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

typedunits-0.0.1.dev20250425170239-cp312-cp312-win_amd64.whl (784.6 kB view details)

Uploaded CPython 3.12Windows x86-64

typedunits-0.0.1.dev20250425170239-cp312-cp312-win32.whl (742.9 kB view details)

Uploaded CPython 3.12Windows x86

typedunits-0.0.1.dev20250425170239-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.7 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20250425170239-cp312-cp312-macosx_11_0_arm64.whl (821.6 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

typedunits-0.0.1.dev20250425170239-cp312-cp312-macosx_10_13_x86_64.whl (862.6 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

typedunits-0.0.1.dev20250425170239-cp311-cp311-win_amd64.whl (813.2 kB view details)

Uploaded CPython 3.11Windows x86-64

typedunits-0.0.1.dev20250425170239-cp311-cp311-win32.whl (778.3 kB view details)

Uploaded CPython 3.11Windows x86

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

Uploaded CPython 3.11macOS 11.0+ ARM64

typedunits-0.0.1.dev20250425170239-cp311-cp311-macosx_10_9_x86_64.whl (898.8 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

typedunits-0.0.1.dev20250425170239-cp310-cp310-win_amd64.whl (810.1 kB view details)

Uploaded CPython 3.10Windows x86-64

typedunits-0.0.1.dev20250425170239-cp310-cp310-win32.whl (745.2 kB view details)

Uploaded CPython 3.10Windows x86

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

Uploaded CPython 3.10macOS 11.0+ ARM64

typedunits-0.0.1.dev20250425170239-cp310-cp310-macosx_10_9_x86_64.whl (862.0 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250425170239-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 685877b998a1ee42be97257c58fbd80f6cb649104d8eb0200181fbfd61d3018c
MD5 74040ba2eecd426c2ae902aaa02d1e75
BLAKE2b-256 e5f56a757471a4236a9ecbc28b33979c548c9a0c7c7246be3a7a5cd0fa753007

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250425170239-cp313-cp313-win32.whl
Algorithm Hash digest
SHA256 222517b0e2e457928a5013c83a2f6323ef2b757fd384f304cc6383f4c9628ce9
MD5 7c8e50b28115f4bbe057b43ead656e1f
BLAKE2b-256 d732507220f53d3dc3bfd435b7702ebbd870ac2a93727ba6c12a0f284f966d44

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250425170239-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e4ae2c4f8610ef1ecf0eeb6d66a94f5468b6351baacaea835700b0639ab071ce
MD5 d11b8643308c04d049f30b6c85251763
BLAKE2b-256 78bc4267d4e04c67c2c8c9f76df6c8244b2d6f9ae5d6c0d01cdff6b131bc06df

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250425170239-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 99b3420f3647c6b05d9340e1edef8307d8d1e7150177d985ba5d22ada2560705
MD5 c7c4237f04e418c4e2c3b03cbfc6ae70
BLAKE2b-256 24a2ca55aaa41ba2328d04def9b597827536d75aab3db9d9ecc9ea5198de29da

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250425170239-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 92f980577b2f5db334ed178e2f93764c8bd6b427e37651c2d56e9b2565de7db4
MD5 e85645344e3f0edb1ec4ca80d78cbaef
BLAKE2b-256 d004ce0c1607d598e5c36b611eb9deae2780167711da1ae2a65311852c5a55e5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250425170239-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 5ec374e6557477263f91a7dd720c20c4003390fcbb0246a517054dfd817218af
MD5 41ddad23a8167e960f34ba20cf15dd32
BLAKE2b-256 c44013de151eba0288ee8814f1a13ac00f266ed1c61f1d1818a9fb33d4227f00

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250425170239-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 9963462c08f51a2bf6dee6af53e836f50bdd5028d8586f9c1a52a4853f5f2d0e
MD5 63da929601939aad2ce190d9b2903702
BLAKE2b-256 fb87df0c2295129464ec3de53fe7948c8b05e0e4a7abe0da84f989fdb8b80261

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250425170239-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8126cbc86d8d515ada935732d535ba5e0eedb20da8b2adcc9f0692c1748d4a80
MD5 63dbd1d083ad3596c6749677e94518f5
BLAKE2b-256 3ef75f8cee8800b7edec1556403de3edbd11f8e225f647501df7b65fdbbeb556

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250425170239-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d578a742553777101f9a6bd79b1f3f4d6e393b5a2930d958b44b7d31ba4ec985
MD5 7d2da93ecb16899e0c12ee21e8f23b51
BLAKE2b-256 feeb944349ec3870d38f2e5b039d4784eab6552d9e2020d20ae5dfdf538bb55e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250425170239-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 82892ae7992e2bb92fa8783e1866846da56b13189361867a19ba855bb6cda310
MD5 21855a736b377f5f08d1741493496db9
BLAKE2b-256 c84b881f08513cbe7cebe2f50de59860ddbf29d7278566f72fc503a8f23bd3c6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250425170239-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 507287f2cd619542a8cd81c676221a9aedfa09e9fca42ace8d88b5b795a644be
MD5 4ab7da9d3ac7740915ea28d2ea4dbdc9
BLAKE2b-256 2ecba5cac98880f77c0f2b98ddcdc47b00dd6e88d0d206e4981df973c4935ffc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250425170239-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 047b648cdb8c111ffbd83b921e75b7764b9d9538133a8155af671552e99a584d
MD5 0cf8fc045df69887f06af746baaa0f01
BLAKE2b-256 28e1b9803e4428a9113f34c1af00196a91ff704df51b11189d6ec1f0350edf6b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250425170239-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 85f9eb6e7553fe6526bf7c2f2a5ea3438e665595de7c94c5de08456141acffb7
MD5 673d43e2afa6f42b45a5304671208ba8
BLAKE2b-256 c363a7c716b56b0dd94f440afc998d7779b021a59e8d71434e8f958c52e50d9c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250425170239-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 6ee1c5b3e84d7bbce014d9ea226fd9629f6e077b89a6d8232075e707159f77da
MD5 0aad3c8b220377d43d35bca73739aced
BLAKE2b-256 3d458c98f62dfc24edfae42761233b7d3011dfefb0db9b5354cf2902c4690695

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250425170239-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 ad9da1dde91d93f9f9aa2efd760f25d7e6d9bc596e5c1592c40fa41490b0835c
MD5 e847e0a84ec141f931d725dc1a23b58d
BLAKE2b-256 6ce89c62defa4e6dc8dd27bfb3301df481af0a3eff1a5bdf9d1eed45451ba813

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250425170239-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 28291b9ccdbdbf0556299da854d15347da03760a0bd7b05c3120d6ce201f1fb7
MD5 6421763cf8f491447e784267219a2703
BLAKE2b-256 e169271764dea2fbf245000390b10ce889e93e12424ed25db450ed723de4c82e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250425170239-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 8df55e8185cd7e883aaccf495a281ed3081f902c42031dc69c3ba169950487a4
MD5 5a8719a9b77463a43af31d87b162e290
BLAKE2b-256 a035f88cbc246d7bfc21b3805cbc6c19f5569d24a6e64526c528250ed085c39e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250425170239-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ac816885b398dc29594a4c2593e1c4e1d89ac16868eddda778788f00b9bbb93c
MD5 09f4cbf4a9642d42e39f9935d785796d
BLAKE2b-256 296c1d4be12293c4475350967b49e9e35d39b01ac4384eb45021f375ab79c365

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250425170239-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b9cb9c6326ddbc3e083a0dea02cab6b43a5aeaf92be72ba61ee851b0643fa219
MD5 bcdd750aa930bb0a9ad2b887f65f65ad
BLAKE2b-256 0a0c704e9ce5eff94d22aa0da8294a76a05428a5a9f96472f70b74be4f204c37

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250425170239-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 ca3a9a35363820319cf96e3f73955611c5c8c3defbcd2d1043c9a7f4f04d2435
MD5 827374b6bb5d3d70004ee2aac4bb932b
BLAKE2b-256 8c0cd32274963e744b8fd3c561800e1f8afc2bab06fff5dd3cb6976463bbdf9c

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