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

Uploaded CPython 3.13Windows x86-64

typedunits-0.0.1.dev20250310230954-cp313-cp313-win32.whl (735.2 kB view details)

Uploaded CPython 3.13Windows x86

typedunits-0.0.1.dev20250310230954-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.6 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20250310230954-cp313-cp313-macosx_11_0_arm64.whl (804.9 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

typedunits-0.0.1.dev20250310230954-cp313-cp313-macosx_10_13_x86_64.whl (845.1 kB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

typedunits-0.0.1.dev20250310230954-cp312-cp312-win_amd64.whl (779.0 kB view details)

Uploaded CPython 3.12Windows x86-64

typedunits-0.0.1.dev20250310230954-cp312-cp312-win32.whl (736.3 kB view details)

Uploaded CPython 3.12Windows x86

typedunits-0.0.1.dev20250310230954-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.6 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20250310230954-cp312-cp312-macosx_11_0_arm64.whl (812.3 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

typedunits-0.0.1.dev20250310230954-cp312-cp312-macosx_10_13_x86_64.whl (853.9 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

typedunits-0.0.1.dev20250310230954-cp311-cp311-win_amd64.whl (807.2 kB view details)

Uploaded CPython 3.11Windows x86-64

typedunits-0.0.1.dev20250310230954-cp311-cp311-win32.whl (771.5 kB view details)

Uploaded CPython 3.11Windows x86

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

Uploaded CPython 3.11macOS 11.0+ ARM64

typedunits-0.0.1.dev20250310230954-cp311-cp311-macosx_10_9_x86_64.whl (890.1 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

typedunits-0.0.1.dev20250310230954-cp310-cp310-win_amd64.whl (803.8 kB view details)

Uploaded CPython 3.10Windows x86-64

typedunits-0.0.1.dev20250310230954-cp310-cp310-win32.whl (739.1 kB view details)

Uploaded CPython 3.10Windows x86

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

Uploaded CPython 3.10macOS 11.0+ ARM64

typedunits-0.0.1.dev20250310230954-cp310-cp310-macosx_10_9_x86_64.whl (854.6 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250310230954-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 50dba00328a4d404db54ea28e5a97504d87d97869c92c60a00f2d196c321787a
MD5 6ce33e8eb6097c1c457b61784cb775c8
BLAKE2b-256 b99f40a58c12964641b098c03a27e08b32a9e27584081bd0051c71e6aa3dd21f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250310230954-cp313-cp313-win32.whl
Algorithm Hash digest
SHA256 dd5770b6d424ee63fa95cd096e5e4906091fffec4d04a5ce1cfd00d382d61563
MD5 ca9dff3d7f21f62bf1bf1d2eb34c118f
BLAKE2b-256 d06e0c9f1a7459e6c39f530a70deca65319e3a8fb77339fe8aa3042697c04367

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250310230954-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e6a88b4bc3322ecf2aec2ee3a9a76cb093f9bc97af17e5ac873159f9fc794b4a
MD5 3a81bcd2a9729b60d5a7445591f7ea48
BLAKE2b-256 88078296efb8a29407c41e397c6276ae1a86d708785a663b2c20b8cad2ea94db

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250310230954-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 913d1d86e17c7879c9ec28d5d9472b706b4dafbac416b4ca36b21e9213d05b7d
MD5 7b85e91033f9bbc0a52d83ef1f0a2112
BLAKE2b-256 7739929c607d7156de401d81702f7228f91f51ee5d472836a6d20c543f192b64

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250310230954-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 34eb1ca7766930b34c9fb2f984db50e53b764e9d2db3b202280c09bbdcbb7fd0
MD5 b93a39750647267dea586f5f02c41a24
BLAKE2b-256 63478118668dd7d99c8a2407bcc153e740eef0eb160a2539776920c00c531681

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250310230954-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 6be24c704ac457572ff9c510e3c4ada27ea1ac7492c0ba2283fb2a3057477287
MD5 d49db85c32b8385a5fbc56f66f0fb7af
BLAKE2b-256 87e6ecdb1f19f97656cef25f9436a62c5c576d6126fde981f0ff73558d02dd61

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250310230954-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 c6983460f1f937ba720bf66f3cf7c6915b8831760c3ac78545774925ffee86fd
MD5 c6da2d87afa8422cbf3e8f1b51f64d35
BLAKE2b-256 b6e06f750ecda620dc10803371999fe6e45294c716ec3a9f7038864cc67d5316

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250310230954-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 398d9c27c7aec1c3008a0ff603a39ecec786fe271029126587a953e8112fb66b
MD5 b218aec0037803cd6d3d5d1e3a045707
BLAKE2b-256 7fd45e61df585141acd84b4563463ad79840a4b1c054e562fe8c420ce3fd669c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250310230954-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 55f6b5245908a0f8d1b88b5199003742105cbe0fa2b2085c7a621cb5ebfa3418
MD5 05d868cd770cc1c2ff06ce018b0ffe6d
BLAKE2b-256 16c48192ceb442e88cc14200f6895a730efedb12c24a71480818d85be4ae8ed5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250310230954-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 ee208bf8142514f07e21684e4bbda431aefdb537f300efc27b983a6b617ef0b3
MD5 d88472d17875bf00fc715e6b49547a0c
BLAKE2b-256 c853fc7423cf3ff7c7206d4d3feeeaf33204405096f614a23af70f6a12dcc699

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250310230954-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 2b388c21e35cb0c1b3ff5c57b0f04c4dbd244bc3af636e91f67412fac7677406
MD5 0f6fe3959362b679919d6f8f7c04116b
BLAKE2b-256 5fc7cfbc40079d133eb04552f8602c6de1d127e5df69bdb8cebb2a94124d1f4a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250310230954-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 d82349fc693ef91aaa5a743a0160e13a9cd49c54b9bd15e7cf4fc51fe78e79da
MD5 4180c4e0015f0279fcebb7ec3f7aec8e
BLAKE2b-256 05ce19a77b75fb1a0ec99c96e5b87d7185dd2268c9efd6c307cebdce05e592e2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250310230954-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 813fa7717f1116c23061cce3f216c26e94b62fb56795357078410eb348b97a77
MD5 ebff95f111ebfe91a311e7c72d144960
BLAKE2b-256 4af7c75a47fddb4801c11cf0903755a7e1d56c1eea0df5b2b97afe2b9e475db4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250310230954-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1ded893c6ac39b985073f1135d12a2128f466e5c7829d74316b34e707beb4ea6
MD5 6de5d81cba3d1a52a9b86b70e9187b35
BLAKE2b-256 e248a9ae4d88614db2c427be03d51d3009dfdd124c54c03fc51fd5dccbfd8e43

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250310230954-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 00977d98fb32d6d21ed985067d4334ccf7e70f7609885ae54d16f1c65a6a3999
MD5 b6130b5f2212f8934cecb43dcd96ed83
BLAKE2b-256 4242ab67467516a9518d6307fe44f4235ffdf162e2946b6575aeef4572a68ed0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250310230954-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 70491ce27a7a82d6ac44ea7940f4dc3a6cdc5d8e15c565cb10c9fcd3128d4f2f
MD5 abbaf1f7355d6a3093cdb6f0009f29d7
BLAKE2b-256 58b7880e524db6de5fca11f55492c20b409ea0a4e527e18fb1dd9e9bf56d4269

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250310230954-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 04c83d4763026ea2f6fa6230d4f694f69f577cd9713e387d8404570da7702a62
MD5 d6753c72bf3b03a6247b3967306f21fd
BLAKE2b-256 f9569f1b50ebac4369372997a77ee3127784bca38c96955f9916bb45f00efe50

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250310230954-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 2069607baefbdf673a59bd6c5485f6cbcc0286644694ccf15eee297a7f7e8040
MD5 26fe092b9e0b1cb99c6b2064be520228
BLAKE2b-256 072f714f4416099c7febc0f87d17bbb97ddb519b0c51599ac07cf6c3d87f3976

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250310230954-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 6b840f0065d4fc3024d9195d69863c51e7004d00e64eae0cd1fd564996cf9a1d
MD5 5c7d67b60284cee2f5f2bdef1281e0d1
BLAKE2b-256 2e0c38674eea28bf6396ee236ff178e97011349b30d660146fc56bc5020ef5da

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250310230954-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 f7c8caad28bab2139ed8ee7256f61c737303fc9a472061b9ff395fff5ce614e6
MD5 7f2949ad554a2b457a9429378ed81553
BLAKE2b-256 46e200393b4f5506ff9f68c8c58126338cbd9a990769d3cc9f19c82101ed3360

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