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 .
    

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 .
    
  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.dev20241015222221-cp312-cp312-win_amd64.whl (705.8 kB view details)

Uploaded CPython 3.12 Windows x86-64

typedunits-0.0.1.dev20241015222221-cp312-cp312-win32.whl (665.6 kB view details)

Uploaded CPython 3.12 Windows x86

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

Uploaded CPython 3.12 macOS 11.0+ ARM64

typedunits-0.0.1.dev20241015222221-cp312-cp312-macosx_10_13_x86_64.whl (771.0 kB view details)

Uploaded CPython 3.12 macOS 10.13+ x86-64

typedunits-0.0.1.dev20241015222221-cp311-cp311-win_amd64.whl (733.2 kB view details)

Uploaded CPython 3.11 Windows x86-64

typedunits-0.0.1.dev20241015222221-cp311-cp311-win32.whl (698.8 kB view details)

Uploaded CPython 3.11 Windows x86

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

Uploaded CPython 3.11 macOS 11.0+ ARM64

typedunits-0.0.1.dev20241015222221-cp311-cp311-macosx_10_9_x86_64.whl (807.3 kB view details)

Uploaded CPython 3.11 macOS 10.9+ x86-64

typedunits-0.0.1.dev20241015222221-cp310-cp310-win_amd64.whl (729.1 kB view details)

Uploaded CPython 3.10 Windows x86-64

typedunits-0.0.1.dev20241015222221-cp310-cp310-win32.whl (667.4 kB view details)

Uploaded CPython 3.10 Windows x86

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

Uploaded CPython 3.10 macOS 11.0+ ARM64

typedunits-0.0.1.dev20241015222221-cp310-cp310-macosx_10_9_x86_64.whl (775.0 kB view details)

Uploaded CPython 3.10 macOS 10.9+ x86-64

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241015222221-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 ee0e4972d38c4197961660879cee74483c5b26d1b55cf4226cf68c67fb28b3e6
MD5 904a9f49c998b083cb69bdf86d6531c8
BLAKE2b-256 794a491b0a24076ed4d1a9e5dc81960aafdeb9b93f9fcd5a0bbe9c526b269b5c

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241015222221-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 f16b672ba63bca493d95e83597bc0c9af3c29188885614ef6fd1c0952603203b
MD5 37358807784cfa19d11363409375ca08
BLAKE2b-256 321b4f3a6c930dd8a228ef7eef81016151bc8ff0bbb82b7d5b80e2cd9c047098

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241015222221-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 833748dcf6d5a511ac4b6ac22520e6de03f1cd281bbe15522f03d8a1fec63ac3
MD5 677d97572237eae9638e8ba89ffbe652
BLAKE2b-256 8dad2ebb9763c53fcac29176b9e317d810a899cec3d2806b137cc2953636f68b

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241015222221-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9c294c30f940480c83677b8d4b377c9b04fe5135b8b352fc4d6984bf5a15613d
MD5 5609679c8b63ad15598e79ce2c9ab57d
BLAKE2b-256 b5b03188c3b5fcdf30e7ae3500ce8468be394d729062b6b31593964d4e111035

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241015222221-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 d5ecc3a887a8a65f42e7ad2bcd5433f51ab21fceeded85471f8cf14d97d858f7
MD5 45b92f7cdbe75c7ead3a47cc3dbadbaa
BLAKE2b-256 2863beb324db78f8fdce0ce6f754e45445282c1c1791334c48bed6b6c7c8581d

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241015222221-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 3eb8eff408787a850f1ca26a76f943041891510c25d943b92262ab57d9249fae
MD5 a5842adf52c7f35a044fb2714ffd5e94
BLAKE2b-256 1d0219b62194e1c18b703f0fd54ca5772d45aaf1a94559dfeff523d8f45cc67c

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241015222221-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 1656ce83ccef83acc16d700be6a45c07f47704008431f36874bd84ee5156df5b
MD5 1754d1e3fd481bbc648a6c28fa3898fa
BLAKE2b-256 1585263d441774ae6db90912a2ffecdd79ae4268d5561a3f71ed04bcb922474a

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241015222221-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e15232984bc75546f17d079d5d7d31a1a08f09308b1c074cda33f278b2d79062
MD5 5af6e91a3d76611689fc0cd0d3978c35
BLAKE2b-256 b29aa38ce8a5bd4c280d50d02d038de4fe7de1edee048f45df3e8e163510db1e

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241015222221-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d1149a0ca352858542c62ae68f4e02595ad0d929c262f31ec31af5507d235556
MD5 9fbe133d9861d9b16831fd15b134c94b
BLAKE2b-256 0bde847862e510fe484ee2422b1a27fdaf9d5cc6f47bf6a81f915de10dc87cd8

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241015222221-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 36ad689068ed86338f276aab3afae513ef04b50db828f60238dd74fad0c89ade
MD5 d5df9745ae049bc951cff676bedcc997
BLAKE2b-256 c51985a291a3b182de86a60a3864e7de2ac75850052683a02cc5054c96b005bf

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241015222221-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 3deba6d7ac16d8fa8a9fc6e372ff8abe6eb75719c11997263faa443b4f9715bf
MD5 38e5f959676c9e0833e66e760f7012e3
BLAKE2b-256 a80c7df382fb400039bb0de9c8310a45552708d4bca1506bb42595f3cf1451bd

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241015222221-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 83ddfb0ea0b957152d8f3cd7242711717ea3d44abb98bbc596533cb23a224282
MD5 8d3f5855fcf1fe19f6e48fffa477eec9
BLAKE2b-256 4fec9ad65cf36683a9367ea0aadfe4fd7a189c748fa7d3cf03cb130e4c0e7291

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241015222221-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e51efd6045218752cec68e9468cb9b87619097263c6dc59a1f2e275b8c6bdc3d
MD5 d6b24cca43ad060f35711053dd74cad4
BLAKE2b-256 3f6cc592464b87dbb44e4910d513f2a6e919b2e01406ca09834ccc4d2341bf3a

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241015222221-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 6bf997d486dc25c30a50ddf21affe436e332e5f6357745c5dac12c9a82c110d0
MD5 dbccfa46e9efa05de331eda396317dad
BLAKE2b-256 d8b8cb1f817734789b12dc6e488934b8353844047ff0f3b3e04cc2d0f9886efa

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20241015222221-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 41e4437c46e91805e3586869c013fc62d651415193057f658c27cf4eb477caae
MD5 e3c49c32d73bf3baf1f83720d3e8de51
BLAKE2b-256 cc5aceddff5b9b53fe884c9732fc05c27efb978c0dd32b48b5e1fe259f98792a

See more details on using hashes here.

Provenance

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