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

Uploaded CPython 3.13Windows x86-64

typedunits-0.0.1.dev20250314004035-cp313-cp313-win32.whl (732.3 kB view details)

Uploaded CPython 3.13Windows x86

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

Uploaded CPython 3.13macOS 11.0+ ARM64

typedunits-0.0.1.dev20250314004035-cp313-cp313-macosx_10_13_x86_64.whl (843.3 kB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

typedunits-0.0.1.dev20250314004035-cp312-cp312-win_amd64.whl (775.9 kB view details)

Uploaded CPython 3.12Windows x86-64

typedunits-0.0.1.dev20250314004035-cp312-cp312-win32.whl (733.5 kB view details)

Uploaded CPython 3.12Windows x86

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

Uploaded CPython 3.12macOS 11.0+ ARM64

typedunits-0.0.1.dev20250314004035-cp312-cp312-macosx_10_13_x86_64.whl (850.3 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

typedunits-0.0.1.dev20250314004035-cp311-cp311-win_amd64.whl (803.5 kB view details)

Uploaded CPython 3.11Windows x86-64

typedunits-0.0.1.dev20250314004035-cp311-cp311-win32.whl (768.0 kB view details)

Uploaded CPython 3.11Windows x86

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

Uploaded CPython 3.11macOS 11.0+ ARM64

typedunits-0.0.1.dev20250314004035-cp311-cp311-macosx_10_9_x86_64.whl (889.5 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

typedunits-0.0.1.dev20250314004035-cp310-cp310-win_amd64.whl (800.3 kB view details)

Uploaded CPython 3.10Windows x86-64

typedunits-0.0.1.dev20250314004035-cp310-cp310-win32.whl (735.1 kB view details)

Uploaded CPython 3.10Windows x86

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

Uploaded CPython 3.10macOS 11.0+ ARM64

typedunits-0.0.1.dev20250314004035-cp310-cp310-macosx_10_9_x86_64.whl (852.9 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250314004035-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 0f587793c325bea0fb47d33666a5b1f2b501df82d389e5c2cd1cfc8d8a914571
MD5 a7d722447f8e91b3252a137845b2d5d2
BLAKE2b-256 5eec23d4a041ae5e5910cbd3d246c0eac9a2c5b061c007d42c3ff02f363d2b13

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250314004035-cp313-cp313-win32.whl
Algorithm Hash digest
SHA256 766a6c82e63313d94014cab837f320301fe0df062130c214e049356a5a8d5368
MD5 89ae8792f98a0b8e877f61f94e74bc9b
BLAKE2b-256 5478e7468027f988089afeba9847ce7034b181a8fe62cde5a98eaac89970eeeb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250314004035-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a126f42aa8ee70650d6c2847052d07b3316eafb288ff8fa6d34cfe6682229841
MD5 00d3a62280fb172857b35e47323e5737
BLAKE2b-256 f33e5979c96c6d4136f589c460202b271132b2875e231844cc8abe3bb6487d74

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250314004035-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 902f0b37929fc338d023fc41d926947f5ce00626f0f28bf1d1cb8251d539d69c
MD5 4ae73ae91d8f7556f6f79e120eb87aba
BLAKE2b-256 d9fdd11f5f271d5ed6e93c3a6dc92c6f2bd5303df328d90f1f6e8088e1bf01de

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250314004035-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 00aa16d009013dba853474bfaecdd346d430d71ccec3ec15d2050a16d6556824
MD5 4eee31476c17197caf5d43f6b1f1ed4d
BLAKE2b-256 e002d784ccb9f73e2546b170ef59501829bda43211cdb6e874fe339353a11e57

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250314004035-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 b1b401e498c7528e8784c4b3716e2233704f0a82fac0d1e7062a3235e7693f0a
MD5 6a29e3b61d1cf0656269c78363d48f86
BLAKE2b-256 32b1f30477a6c8bc1068fb1a8fc2dbeff8967be4ca6d88a8ad74f5b3f33a6866

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250314004035-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 baaa9ecebe8c7c213b98aecff81aa007fc206751e18edd1630ad881c9443448d
MD5 ce0c7253a5f522ecbabbc6ea5ce5a7e5
BLAKE2b-256 8b0b860e66ccee5ceb2309c855d107007e6344b6a9caf772750fb9f9e8217f07

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250314004035-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 3d52f824f833b17c16bdd53d5e9653cddcd0a9d6a0db87e23aeceb94d5ffa6e6
MD5 bac62d46b8ad7bcf82bf5ec587031fd5
BLAKE2b-256 f336833b012bb47211ca24f28d56b7456d66f426af985e91b950be72f34b1333

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250314004035-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e5352507a7aa1c2d3eaaefd8b4bb2e189a7d7af163099223442043c0cc2b45c5
MD5 6fe6b11ab17421c3c4d9eb1dbc6f6a8a
BLAKE2b-256 1b243b191652788b01cf5f279a2a7b0ebbb4ec77085d2eb445752106df180523

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250314004035-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 a8bbb79107d30a8eeef0cd0a7516efa795bf8bc26fcd5801ce41cce2471a4363
MD5 b3993c49d101e10e41d071aadffeae12
BLAKE2b-256 28700e45e9ca429963db9fd6cbb09ea00ca66b513bf17b8874a100494c5b6825

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250314004035-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 00fd90fccc8ed9616b2e6c97365698492a23d7ca086eb862c9195388a9aa4904
MD5 9988610844a5be698e2cb4dc87578971
BLAKE2b-256 3cff5d36d20c2c2384a5405727a948b9d57dd4e3dd50a7229c822db3c5a476a1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250314004035-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 415b5c32d47e3463703eb1db9e2ebfeff603f24624a3e826edfc2965ee365465
MD5 3a83dea06f7a3aa5ef5881342b9a65ca
BLAKE2b-256 8d66e40e574ab1c697f8b4e76b0a6298c2ac01d7349c713bab2b29bdcad53762

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250314004035-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8a160ecd8ac674bbadcd453138b5cf930ffab2c813a8b97d3fe5b7489274c3d4
MD5 b6105acb9744921556fe11cc7d9021f5
BLAKE2b-256 9e87a204ede26369f6ec20488e0259d6d7f3c8e36c5efb8eccd3d96af8a3a85d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250314004035-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8ec9791c7d315d06db79216217217c08e8b8b1672b8faba8a5d5979be7a160cd
MD5 0be783d41e19ed38e1ccd6e364496b01
BLAKE2b-256 894f9563dc6b83e6b0459ebe9b67db47dc559d845de03271b1ddcfd657c495d8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250314004035-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 8df8f1696e2cfd7a2bc997cc47149938a851b04f5d0bdb91dec8e7c78b5ee473
MD5 7da5b63a47befc6fa6fcaf62057ac679
BLAKE2b-256 2af0e1fc02aab24138b48e6566e1d912d3748371bffa2bc55059ace53304900c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250314004035-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 2fccc4afcdfa2e21a3c89f981160cab8422870a8bbb5b05f0455c24151198a6f
MD5 6ab892671059576d238d8b037d3eec27
BLAKE2b-256 6dd03f1169a3acd1d82314f2eec89124d036fac6bbff0f00952e336678079d42

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250314004035-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 756ae931df6e8b1be532eb3db5dfb5c06f0d85667f315760e6c5cdbe101d9ae9
MD5 ebdb18e1f02a5664a62b4d04d31a4d5b
BLAKE2b-256 b7c604fc8067c1664064dffbc55b4aee30b7e107dd3066e4cfd474329c824d9c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250314004035-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 2e5d079ae18850c668bb4582946e0ed01bfc8c9ba79961c1799f083a91156f0a
MD5 3ca0b1dfdd9db2f9cf10ca0535ce56fb
BLAKE2b-256 24f7b355052c9486ca9c5463282d173c6fc31f74debfb5c8f0911309d41c4521

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250314004035-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 4273b9b518639921b685248d1f36f1ec8734fea7ab7753d0bf1a03c8d5092e62
MD5 6d335175a0072c63c7a13caaa47700fc
BLAKE2b-256 726682c0b301653044e11ab99744ec2d7c2c2186ec7b3296f630a8f8945d4aff

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250314004035-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 f46dcb931f93822be14eb6692c9a367d380df0aaaec2c91e6a0ea0c5f8485e5c
MD5 a790df9701df7b104d9c1d15c03cef0d
BLAKE2b-256 cb0245df45136cc18bb1e6150693434e511a27e5fba3c079e8977d1b2e121af4

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