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

Uploaded CPython 3.13Windows x86-64

typedunits-0.0.1.dev20250228051437-cp313-cp313-win32.whl (709.3 kB view details)

Uploaded CPython 3.13Windows x86

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

Uploaded CPython 3.13macOS 11.0+ ARM64

typedunits-0.0.1.dev20250228051437-cp313-cp313-macosx_10_13_x86_64.whl (819.8 kB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

typedunits-0.0.1.dev20250228051437-cp312-cp312-win_amd64.whl (753.1 kB view details)

Uploaded CPython 3.12Windows x86-64

typedunits-0.0.1.dev20250228051437-cp312-cp312-win32.whl (710.5 kB view details)

Uploaded CPython 3.12Windows x86

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

Uploaded CPython 3.12macOS 11.0+ ARM64

typedunits-0.0.1.dev20250228051437-cp312-cp312-macosx_10_13_x86_64.whl (825.2 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

typedunits-0.0.1.dev20250228051437-cp311-cp311-win_amd64.whl (780.5 kB view details)

Uploaded CPython 3.11Windows x86-64

typedunits-0.0.1.dev20250228051437-cp311-cp311-win32.whl (744.8 kB view details)

Uploaded CPython 3.11Windows x86

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

Uploaded CPython 3.11macOS 11.0+ ARM64

typedunits-0.0.1.dev20250228051437-cp311-cp311-macosx_10_9_x86_64.whl (863.6 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

typedunits-0.0.1.dev20250228051437-cp310-cp310-win_amd64.whl (776.6 kB view details)

Uploaded CPython 3.10Windows x86-64

typedunits-0.0.1.dev20250228051437-cp310-cp310-win32.whl (714.0 kB view details)

Uploaded CPython 3.10Windows x86

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

Uploaded CPython 3.10macOS 11.0+ ARM64

typedunits-0.0.1.dev20250228051437-cp310-cp310-macosx_10_9_x86_64.whl (829.5 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250228051437-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 6fa5affcb18ea2d265c8629aeaaeb3a6de050a0eb93f05abea647f795cdec4d5
MD5 2e711fee7eb1c0fa154ca608379ca90f
BLAKE2b-256 219e77bb42468f06a93968d71d5ff56139a58562b6d0cabb0f58ea14c712cf92

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250228051437-cp313-cp313-win32.whl
Algorithm Hash digest
SHA256 df49456f4e1cfaad3024037274f88c668ae920ea219cc544e26942a65664e36b
MD5 fcf100be2200cff0cc3f6c6fc1b6b500
BLAKE2b-256 c64704a62595b809b2f264500f2d6efdcb9e56a120f02b02e8c7113a9d0e571b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250228051437-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 4dc7528624e5a3896ab260f79a77a9fcf8211934b2d2e7204f0fcc81d2882313
MD5 a675cd25d1bdbd58c0c7d1ccd8a57997
BLAKE2b-256 86b860795c1a1ca3fe5f67eed15eee689e9c510a3fa96a0870d74f2a9f660abc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250228051437-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f3cb0ee4379bb93a43b71789d3a26b544a472c6c237b32af84873897096623eb
MD5 826d5c63d65b6c2b96d1f12278b93d7b
BLAKE2b-256 8ddb4482c29fc4e20fea8ecf1e21e462f742d27a5b3a4554161649ac5d184645

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250228051437-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 5688149eba59540dec5fcb03118019cd09f9aaf14ef534ce0777765d2fe53a81
MD5 7422ed9ac794abb536c8006a2481bf57
BLAKE2b-256 1f7c23648d17dd1db767658aeda4258682f654e75b1532e4eba130beedfa4d06

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250228051437-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 abfae3318b6a5c67ef4024d15f9aaebde3e92abf0f95aa318f2ecf3487722d2e
MD5 ebbf2556e26796aebb562444ed285411
BLAKE2b-256 7334cde222237b63dc1898483eeaa6417491b3e77e06bcb58bb5a9f0130b869c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250228051437-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 c2b119ee0e1cd083a795709ccaec02342c28bf7c97666d05bed7869a72e7325c
MD5 96087ed4d3349d8b690c698288462f7b
BLAKE2b-256 2f5260733d5d22be3623c5fe58da55f0ac69395054e41841e3243ea170a756ac

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250228051437-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 0ffdf3839e72cbe5d6f28b70c87dbb0b2142da2dc26fc3641dc2e955955fd4b7
MD5 9e2430872e6d06a45e9679f7e84a5dd9
BLAKE2b-256 b7e4d7dd181e7ad7418ee7026badf34d68878b317dcbaa5cecdd83ed5e3a4859

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250228051437-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0704f99d6dbffdc6c57b49b5ccc06d5ac95a63f586910457b0826d6842b8b8f8
MD5 bc358801b157a9001fff5ec63d65640b
BLAKE2b-256 fdffa6ae0aa786b926c554d5279537d4887c59a8e800524303fca6a6d5562bd6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250228051437-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 8ec439823f4e952c27bd8a11c6216238d9f6eef1fab5f16b745a4e2cece1bffb
MD5 af748ce39ebadd0515fc99adaba9dd49
BLAKE2b-256 f4459c8f0ae71d14c0839b2550a8478ffe6571b9a699d86c29b6917f4682444b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250228051437-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 6498ea285b12260e98b5b176f88ceb64be29586fdb91a7ce887ab9e82b631283
MD5 0b1ba09f4d1554d4d41839faa5e66599
BLAKE2b-256 6f8b598babeced92e15359631e21455e1dd2ce0c677d2fd8926d1d1b3ccc106a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250228051437-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 cd7730d40581f1d8f038abed166a461d933ef46ea416e1c68b3414c744bce622
MD5 02393122d9240e33e2b354a96351c822
BLAKE2b-256 71320918e740a60abead40ef99a400c54810fda1466d08c68d7d4ab2721ddb91

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250228051437-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 0447b7c71fd70282f099f4c104923985f11ff86b9139ccfa1ed3f6b104126ce8
MD5 7e5ec88d8b0177cbc33cfddce47469d9
BLAKE2b-256 7b063c5a99df491b82c1d1f6f40702ec38f69e4ed0ee3660657ff556743fe4a6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250228051437-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 5eec5c6356d9f0eae7f6ca54772ccb36960db42675c5847e8e2ee1bb2e99faac
MD5 92adb894da6a6918c4bea71f61706e42
BLAKE2b-256 a21cad20489cbb6ef5712b1472491eb2db4e6334dfa5cab758ef96e0103964ab

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250228051437-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 53df3944b7e7281f9eccc15c10e934751543c9dff30a662973dfb4a99178e229
MD5 8a722cde20e6687ff721ab48e729817a
BLAKE2b-256 3e84e6a891c4122c0ca77d50406f9d15a665de2273b84bdff5f91c6088020724

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250228051437-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 199792c676d5e627d21597da53c5b6b57e95f0a4faa85a8aabb1540c4a53195f
MD5 313a725b09fcfe05c934f7f09a26ae48
BLAKE2b-256 6e84dc270271b73afb033c627f7e4ffcc6ac2c92127b2c1500468d35c342772f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250228051437-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 f63a8bb322377250613046c1258a23723a2d98beed75722584570a1773a1cd6e
MD5 fc0a0f68d50ca102f215bf8470b8a69d
BLAKE2b-256 14058f3f0a959573722ccc8209680cb91620f61b0e75687fd7f4c007fcfa535d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250228051437-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 eca37b287e6ab998f51d0916b01ccf7967d6ee076e0f33d01b7af4e42763a381
MD5 9645a269dcbb1fcb1077d5d87bcb3d44
BLAKE2b-256 403f0ed8d18b90d2a0a3ce04745f1a9fa52e9847b5efc7fc3963cba8b276ca01

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250228051437-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 cd761110f4228e508574e294afaf167416d56d4388e5dfba1fc2d82452bac25e
MD5 68fa617a8921a9cf1d19d9a2c70a351e
BLAKE2b-256 1d28ceff9d3bf9161f43dfddcb66e740140b7922fbc1ce7a612240ca791a0e54

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250228051437-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 c831b991e1769aee9c4f314106259eb9a87ab2d279265c215bbfa292760687b9
MD5 db494046dfa506d696fe8ed3281ef3c8
BLAKE2b-256 d1d3a668461d5ac31093ffe2dce2c3d16faf9e38d89d7acf91cee983f3974408

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