Skip to main content

No project description provided

Project description

TUnits - Fast Python Units

GCB CI status:
GCB Build Status

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


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

typedunits-0.0.1.dev20241113200257-cp312-cp312-win_amd64.whl (711.9 kB view details)

Uploaded CPython 3.12 Windows x86-64

typedunits-0.0.1.dev20241113200257-cp312-cp312-win32.whl (672.1 kB view details)

Uploaded CPython 3.12 Windows x86

typedunits-0.0.1.dev20241113200257-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.4 MB view details)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20241113200257-cp312-cp312-macosx_11_0_arm64.whl (744.5 kB view details)

Uploaded CPython 3.12 macOS 11.0+ ARM64

typedunits-0.0.1.dev20241113200257-cp312-cp312-macosx_10_13_x86_64.whl (777.4 kB view details)

Uploaded CPython 3.12 macOS 10.13+ x86-64

typedunits-0.0.1.dev20241113200257-cp311-cp311-win_amd64.whl (739.4 kB view details)

Uploaded CPython 3.11 Windows x86-64

typedunits-0.0.1.dev20241113200257-cp311-cp311-win32.whl (704.9 kB view details)

Uploaded CPython 3.11 Windows x86

typedunits-0.0.1.dev20241113200257-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.5 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20241113200257-cp311-cp311-macosx_11_0_arm64.whl (755.7 kB view details)

Uploaded CPython 3.11 macOS 11.0+ ARM64

typedunits-0.0.1.dev20241113200257-cp311-cp311-macosx_10_9_x86_64.whl (813.6 kB view details)

Uploaded CPython 3.11 macOS 10.9+ x86-64

typedunits-0.0.1.dev20241113200257-cp310-cp310-win_amd64.whl (735.2 kB view details)

Uploaded CPython 3.10 Windows x86-64

typedunits-0.0.1.dev20241113200257-cp310-cp310-win32.whl (673.1 kB view details)

Uploaded CPython 3.10 Windows x86

typedunits-0.0.1.dev20241113200257-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.3 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20241113200257-cp310-cp310-macosx_11_0_arm64.whl (723.0 kB view details)

Uploaded CPython 3.10 macOS 11.0+ ARM64

typedunits-0.0.1.dev20241113200257-cp310-cp310-macosx_10_9_x86_64.whl (781.3 kB view details)

Uploaded CPython 3.10 macOS 10.9+ x86-64

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241113200257-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 b3b2e003ebd214a7218286a5e6d3d84819f8066d8ef8c0a6455b92e67ea3e0c9
MD5 b13584e0db9440e7669280195a70280b
BLAKE2b-256 1b2cce2eb0af92c53ea08232a9997f9d72134b8df4d28de53e650216167c7045

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241113200257-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 017c619b3564002ed1c8869bead69d0d0336782caa06955901df7c64268dffe3
MD5 e936f8ab59ed0f9bbe75a165a7fae7df
BLAKE2b-256 ddc902bebbb0e1b435c9ac4245b7a8d07a9c3d11c37d761fe0b24d6f3dbf95c0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241113200257-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 2c2854c56abda2a7d60384d68d6f3c9764d4f5ddfe9ff33dd3122e093db2b169
MD5 24725cc884241913352e2d97c7d9a919
BLAKE2b-256 c1ec12ce25202156bc44a555aad0cb0ea6c6bdb0c2f59a3de25365739a8e593d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241113200257-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f44f1cc1b535fd51a988815cd2a01cb8d7526f2c5c5cdb7f0eb86f01132cbffe
MD5 e86ed699d359a54ffa2644d0dd659d2b
BLAKE2b-256 8b59704419879cf8bb2dcafd64b8421958d7243ff9af34808ea623c37b8a5c22

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241113200257-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 ad40e5f7e285a93f91b84f9d8acaa81b874b129bbb4eccc6fcf79736d9888724
MD5 3589d7cb3331dfad5892d80758a5c061
BLAKE2b-256 ce7e0f3870c42f16f96864e02df34dd7f0213d869befdc24e3c7e673eef93b14

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241113200257-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 d089faa59b04fe1796eb46e8aacc5c794fe94f1bf331b7d1292d7a833d459b92
MD5 be4567516f08563d2115689ab75f44cf
BLAKE2b-256 74017ec3cf109e99fdfd3fd9fd6ce80aae1d66153f985fccc5dec3500d7bd603

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241113200257-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 6418e45b16acf189340e24c0ee74392e491f3ac22e12ca87d6ee8008294eee30
MD5 e383a2024f1f717b0ceb618e87adc7f6
BLAKE2b-256 a3c86dc06563469f5fdb6dface66719bb7e31f68ca8e3017371c5c8c1783661a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241113200257-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 823490a19d70776b10bd4ad1db57940cfb1738941a57f6529a020665ae9c1c16
MD5 3bc839eef3b7fbef377794ec9124d4ac
BLAKE2b-256 73fcca8687c6795fd9c4616024e8cabe7538a0a42a125e5a2b569df540e5a4e8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241113200257-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 04f9aa2c36fa403423925595b6be2a6c8c9bfb2411ed7bb77e4e94130118f8b1
MD5 adc5212cfe55a659d1a1536da537dcdc
BLAKE2b-256 30820ecde8c12edf51819d4c2d1f529c023a9d10114dc71b1ac5c15a6de6df66

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241113200257-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 b9f380e4cc6299ec5f90d99c1fcf5fdfee43d66bd939b368d7f800967c20746f
MD5 0bf201e486e27e3a5a96a5610503a1b1
BLAKE2b-256 07c59adb70fea5cf2cce0f327ad460904e2272b4f5bbc102a6f5fdbdceacbb6f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241113200257-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 346bc368a92a31cc334f45f3caf7996c8eb5d71dfa7a209ae1033157b1aad763
MD5 8383fa6c151368adcc6254990d81e94a
BLAKE2b-256 75b8f7604cf933da992f9b1986e7c24bfc7f7fa88b857b8373d4b9654918ceb1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241113200257-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 a185625ed94281cfd045c51f39934036d33c30d62cc1723fdaaca6e33e461f17
MD5 20eaed9906144ab32e53516c67a0c51c
BLAKE2b-256 ff256cb20e9d30e25874669930ff3c0ff9f8d8bb3bf35af2dbe842832b68cbfa

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241113200257-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 59eb018bc6c20bd62e3fd3c7576407f46c5da74e1679712ab68c748f90f55a99
MD5 1d6386183290be81f78008cf5bd751dc
BLAKE2b-256 0586aff143681d66b91228dd1bf7ded009b1f6f4d8c52852616b6a26c19bbbfe

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241113200257-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2c5b830a2ff9f0f29fa9bec827bb676af821f5c2de09b13c0c6a6f665d898212
MD5 b47c4d6d4b20e94b1803b2f30f81f721
BLAKE2b-256 7d019f1ec4bcb934c2c0f633aeef050f3e56f2b7fd95ca4f6868b5649859001d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241113200257-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 7974285ec9f5c5d104dd3e49f84f328edb1906ca8dff80f3a50a58fe47b1c499
MD5 a739bce66a0861231b11f37654d7a711
BLAKE2b-256 209939b69e6ca669f43dbd2407fa650c142029c9fc54f845bab16274c394d12b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page