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

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.13Windows x86

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

Uploaded CPython 3.13macOS 11.0+ ARM64

typedunits-0.0.1.dev20250218222530-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.dev20250218222530-cp312-cp312-win_amd64.whl (745.0 kB view details)

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.12Windows x86

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

Uploaded CPython 3.12macOS 11.0+ ARM64

typedunits-0.0.1.dev20250218222530-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.dev20250218222530-cp311-cp311-win_amd64.whl (772.1 kB view details)

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.11Windows x86

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

Uploaded CPython 3.11macOS 11.0+ ARM64

typedunits-0.0.1.dev20250218222530-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.dev20250218222530-cp310-cp310-win_amd64.whl (768.8 kB view details)

Uploaded CPython 3.10Windows x86-64

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

Uploaded CPython 3.10Windows x86

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

Uploaded CPython 3.10macOS 11.0+ ARM64

typedunits-0.0.1.dev20250218222530-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.dev20250218222530-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250218222530-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 de780370684a4b31a8299b39e4def12cc41f46e007ba9efc757b8b15816393d6
MD5 4ceb4a0ec2ac2229ab1dc4707bbb5c1d
BLAKE2b-256 b67499bebd09019408891d27c2f1a8b8f4d81fad6e0bc4617690b122af76de74

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250218222530-cp313-cp313-win32.whl
Algorithm Hash digest
SHA256 d764e21dd53eaec2a0d4f506aed2e35980a0160cfb8a028aa12674916069b8bf
MD5 705538aa648286e669b89997e72a5a32
BLAKE2b-256 79685147fb472a9b2e07794c204914c51130e81c54131378cc63415aac88bcd7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250218222530-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b928a22e04d661c60df4cd838dd3e7363728fe8c8fc89499bd197a410a9c523f
MD5 875d35022465173a3c6caf0b40d63204
BLAKE2b-256 6d8bdf7952a22fd9c4e282738d1b1202fd4292cb64d9b7166a7acf79c4b8bacb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250218222530-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 eb821a04cdb41776a3152281849479de3be283fc48c33b9c67d854988d987888
MD5 75f13b4416e0090dfd533ababf722fde
BLAKE2b-256 ce061ea33a9fe6cbe5959300865abf7ea0e555d2969dde7c56097684c24ae768

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250218222530-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 2b6084ef9a0a3e912740c8b1bb8ffff59288a9403bac708f0dbbb3ee26de2af1
MD5 8d33eb175bc36ab901e186e1cdf81361
BLAKE2b-256 f1db0ba5d4004808c14617e91adb2c4012188e81467f8d0091ddafbbd1b80f5b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250218222530-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 ffb9dc376de9cffda7f3dc7cb723343f6a1150b17d49f58867bed691128240c8
MD5 3634a1cd681c18fef484afd3a284893d
BLAKE2b-256 1d7c45d5cbfd05b91faf7b86e24560427d709c9e4d1b29773a2fd6a81af6054e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250218222530-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 8e5fb8748c98eff56f128078e64f034fb0b5b9f17fcd22e43d8a9a724852243d
MD5 3b20709c92a61efbd116d53606954148
BLAKE2b-256 18148454383e1bd51e48b3f5b1105b6741f2e9d78036a89fd06f0e02ba49c8f4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250218222530-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 423b8b9297c016780302469dfb4bd64b37a725d64f6cc222e88311e9c1f38010
MD5 4dac98991329f8eac37419d51f379515
BLAKE2b-256 5a897a740b14784c0c4ead18a335bc75bfbe1dad9cfa55ab89b86ad361148254

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250218222530-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c00bc44d33bbbeb6b79e5cdaaef538429f51f931076c963e6382f2e16c0c7f05
MD5 b0b83ec1e7ba8725fb1f0814fb303060
BLAKE2b-256 6866486a37a2425a845090415d70f72bc02d532194482d5be48ead3c46abd2e3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250218222530-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 f0d3f29b9889a569f154bbb43313a919754b13792067a39f213a49599513a655
MD5 0865bbf51bd467c052f0683a6d28e465
BLAKE2b-256 10073f761a158b8b6cf406b66b2606520feddc1f99c29738647245edd0395e97

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250218222530-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 1111c4c59cd9be34bf6d40c337ae108a7541ef1fa526e535a290fb40b35eaf14
MD5 96c1b8f9a3d259c37d1f518cd07b2117
BLAKE2b-256 71bad2ec5cff4c918ce06997445336e5db89181d10514a878a8076cf6e1e9763

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250218222530-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 ead526264e286ad244aa45d7c893085fd16c3b13608e262fb6dc0f855fa6fbaf
MD5 4b44baa5863d619c4f2d978883c54834
BLAKE2b-256 ccf67c3c85ddd6fcdc8bde9e293af3ff7027d2ca859d8ced12030f4a4472d7dd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250218222530-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 486891b6f487a4a7ab30826b214865e8a11d5c879ec255192b7302fab68079ac
MD5 24663399fa629b339cf47fc1be3be639
BLAKE2b-256 40bceff7262c56c7f8e535510d0af044e1443339d1e8646cc5499b1447829ab6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250218222530-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a1dc5392e87784eb4c06c0b78b4b059c9319dcbde6a2267b4d1c6acccf570b4f
MD5 619c9f2260719b067742fab43b224b03
BLAKE2b-256 04ce41cc0e3c0bbd49c1c1834c1bd22b39369d7090fc6ff9526e71a90015eb48

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250218222530-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 1b58e45722a4fa8d953f64a3a4030419093bf8071546fb9caa7919b94cf760dc
MD5 e99211f5e3978159f57e8fcb5c83c385
BLAKE2b-256 75c24ef4e98bf6ad2ae7e7e95592c05fc40cb8954f989704596dfeaba9f01b5f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250218222530-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 761bc6c6a102833ee258b19f9ac40b4fc7737f3a3aafbe35896361a57626e591
MD5 efd3888bbb7f8020542546cfb0186754
BLAKE2b-256 2f2845ebc2b195c25a9b36c1cf1e0fed7a14747c4c9c630c83a16ecb9a0123c8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250218222530-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 e39847c4d9390a3e3c773230bdcf07896fe389aa35acef96594deeb768a11c1e
MD5 ae14146704cfa0dbc643c4890dc7ce32
BLAKE2b-256 2ed7ad90d156003a4bb42b4ec70fd1a108a41b9bd200bd9eb355ee10c9ae8c40

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250218222530-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f6bdd7e3e74f245957edae70240e9b35b46bd960ed01c8c52b0b7ceee85deca8
MD5 38d21850cec3dc1c0bf653d116210de9
BLAKE2b-256 1d5315b0341e58582f04fe31ec2ae1a0d8d2dc4c5cd4a4af9567db8e16780123

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250218222530-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 6cccb6bc9bd99106d58a42fb4b329f9a3c28cabbcda35465f2ab4831cd94df35
MD5 45399a85cac6edc22d3886af55ccd2c9
BLAKE2b-256 7d8de16a2e3948a6679a2ec6c972ae957714a136520248de8d48319ab31a1f9b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250218222530-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 04cf9a1ba3d39673d60d57531dacae813950ccc8843046cefbbdeefbeb660229
MD5 04c145c748201c9a69ecd67df3763d57
BLAKE2b-256 1b5d6f04da0d44795e4257cdbf631b6748943b9e893e79ca17410bb69c1c172a

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