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

Uploaded CPython 3.13Windows x86-64

typedunits-0.0.1.dev20250122224152-cp313-cp313-win32.whl (680.7 kB view details)

Uploaded CPython 3.13Windows x86

typedunits-0.0.1.dev20250122224152-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.4 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20250122224152-cp313-cp313-macosx_11_0_arm64.whl (747.4 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

typedunits-0.0.1.dev20250122224152-cp313-cp313-macosx_10_13_x86_64.whl (783.8 kB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

typedunits-0.0.1.dev20250122224152-cp312-cp312-win_amd64.whl (721.8 kB view details)

Uploaded CPython 3.12Windows x86-64

typedunits-0.0.1.dev20250122224152-cp312-cp312-win32.whl (681.7 kB view details)

Uploaded CPython 3.12Windows x86

typedunits-0.0.1.dev20250122224152-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.4 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20250122224152-cp312-cp312-macosx_11_0_arm64.whl (755.4 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

typedunits-0.0.1.dev20250122224152-cp312-cp312-macosx_10_13_x86_64.whl (791.8 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

typedunits-0.0.1.dev20250122224152-cp311-cp311-win_amd64.whl (749.0 kB view details)

Uploaded CPython 3.11Windows x86-64

typedunits-0.0.1.dev20250122224152-cp311-cp311-win32.whl (714.4 kB view details)

Uploaded CPython 3.11Windows x86

typedunits-0.0.1.dev20250122224152-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.5 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20250122224152-cp311-cp311-macosx_11_0_arm64.whl (767.0 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

typedunits-0.0.1.dev20250122224152-cp311-cp311-macosx_10_9_x86_64.whl (825.3 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

typedunits-0.0.1.dev20250122224152-cp310-cp310-win_amd64.whl (745.2 kB view details)

Uploaded CPython 3.10Windows x86-64

typedunits-0.0.1.dev20250122224152-cp310-cp310-win32.whl (683.2 kB view details)

Uploaded CPython 3.10Windows x86

typedunits-0.0.1.dev20250122224152-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.3 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20250122224152-cp310-cp310-macosx_11_0_arm64.whl (734.1 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

typedunits-0.0.1.dev20250122224152-cp310-cp310-macosx_10_9_x86_64.whl (791.6 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250122224152-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 9b81b1101a02960c7323401cbf11c1e57e721bcce540032d6be6899d94b274a4
MD5 3d49b8c9be71d414371d98e2a3564d1a
BLAKE2b-256 c7f25d28068256c41ef51fe49cc63ee6b2e25f4bfa611dba6b653575e266746d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250122224152-cp313-cp313-win32.whl
Algorithm Hash digest
SHA256 588a061883e3cdef834bfd62602c1b3b993d95e33f524411c30a835c7d6dde8c
MD5 fb3a76a341209db8d5d3f889b354d932
BLAKE2b-256 81cf4e715b92f2e20e0cadf9f01c23f6f35bd87d85d6fdc9fe7865f83258201a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250122224152-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 3988414baab00cc7cb46c518b3674e7b9303140d23f9cb4a573b25a457a0050c
MD5 af80c1cfc63a3537fbe3459c85360292
BLAKE2b-256 884f4cf872da5546cb6258c95cffb70159d910f75a6224946ae1182198646746

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250122224152-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e8d3e1029f7fbb5f8ede803ba75e43cd0372bcdcc7992df04415c4f023499179
MD5 022bb17413bc08772ebf5283c2506318
BLAKE2b-256 ccf55c93097458e4fb4fe43f7d2c9c2478e0b687fd9c94cba3cd48a3749e20f1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250122224152-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 f8358c965b7c687dda7fbb46df93532f7f766729814ec5e67230a53bcf7567c7
MD5 607640daa256263ee4e9ee123a3602f1
BLAKE2b-256 758f1a24741496a58db5a5c406d827317471dc8c14c6b6f80d4ec382610979e5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250122224152-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 9e557c3d4f42cefe8a168f6549670c1420c8f5a3fb7e3ba8ed0037e8109ae2a5
MD5 c0131ece887882999642095a5756e1ee
BLAKE2b-256 a43dd0be5894c15b45ac0d7a7a2bce5c537f4e7666537864b1db6fb4f41a13be

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250122224152-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 283c2c933044d6609ce398d9b862ee4ef58beb528ab62a2d7f665f9698b5d64d
MD5 2e6f952519e20cd6800c62df9fa3b7f0
BLAKE2b-256 984e29476029eac08153d9453efdca0e9bdd0b126b3085d5129182902cce9cfe

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250122224152-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7d18039e19808888fa51ea6538cad4b6bfb9a5bf2fd6af333865e7b0d3f2a4d8
MD5 70a21d21dd07b8d3fde3a9741fd218a8
BLAKE2b-256 98b83eee5084a7d8110096f37d1508f6d01979733b11b3e39a19d3b0bb7c100e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250122224152-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 cca3e058834053526ea53db0b42c542c657a68fb4d10f1fcbbaa273685340615
MD5 f9650be4c12f7e5ad51b7643e8506c23
BLAKE2b-256 7e9dbf0b2175a50edae34aad76dea7911086060f8f51928636550f36ca0df3ba

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250122224152-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 b23a31ba514536b8819616a481527c383ea0b92747da2ab7d0152ec896ee61c6
MD5 37476411f3c3ab02437a5a70c2410c20
BLAKE2b-256 0a3364391c4ea29a92050aacd701bd88a559c38e30aedb29cb4c23c46937d62a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250122224152-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 681eb642702fd953fbe74a547c0c1710c482bea43faff52b00810f81c5ff4a26
MD5 940076d9c5aeb1670fbc104fe1d3949d
BLAKE2b-256 648290c61ad5ed95f927759625c26dd60cb155c3f3d457a8156871443bad4d2c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250122224152-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 2076dcbca068aab724085c1496bc14101447f3951753bd7a07df78d62ecd79cb
MD5 e78d2e0e86a593f9ab10ca52b8f8e190
BLAKE2b-256 f1d594ee76f246be821b3132844cee2540bfe20c3aca0fa573e8fcfd65d3726c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250122224152-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 aaf68c3db9a53ca58bb323fbca21b9c1c1a163493f6dbc676cde5bb0792552e4
MD5 01ae7df834f0b3e39e44050dbe3fc94b
BLAKE2b-256 8a10d51659ce07483850342d66327f7908831ebc614940e95b95427134861229

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250122224152-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a745d62973d2bc57c681daa48864cbe6cee91654d8a0e6286f3ff25e4642b56b
MD5 e203648989e6c6d46c42f337166f6425
BLAKE2b-256 9eabc27204a966a9d9d51627bebf1fab9b87dd81e014ea989c58bce4c00d1cef

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250122224152-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 d0a9cb27b798fb9d1aa569bf31eebf8fcb8d18d902ecb4eedd6bb879e302314e
MD5 702e64357904a4eadd9450b22a14e96f
BLAKE2b-256 d9bda93f2862603464425ef9f6af95335b922fcad7e352c33c5dbb14585e2d50

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250122224152-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 2d0498e193c36cc3e0a1e868cd5099437917d04cac2b0eb32961b2b46c49d1ee
MD5 c6124a6fcb11990f8dc0a7b490839fd4
BLAKE2b-256 7a3da47fbd11e623a2f527a90f77fe1ac8fc894343aaa11e635e2fe41a4fec81

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250122224152-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 e1ab5752af287aa0943f80450f6b540862b651ffc93779607364a4b560ef10b3
MD5 3059595268f3a0a550b8f951a07e488c
BLAKE2b-256 4c2365e80748c62ef088aa201d14e359ea8070ab8dd6e6a7f4fbd7cd7c488450

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250122224152-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 518131793c80fb39a3443f9b8be72b672af5f15842b55769ffd52d80ff8e06ae
MD5 ca7a26e7f747d8ebb73c87e96a38e86a
BLAKE2b-256 60f24f382ad29beca9513dd983e1c285149f46c6c7604d24e922ff86b50dcd45

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250122224152-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9c87bfc51c81cb1cbba21db7d0c0f4465f78fe896a37475508ceafdf0ef38467
MD5 aad65afcc064d621fcb7b105c33525ab
BLAKE2b-256 68d15f83bfd800cd113fd3912ee24b3c78a0a32b5aa4e769bb1f9909e7b1cdb0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250122224152-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 7910c93d714590939fd3ad8e12e12233c5a307ca16237c91a59145fb5f57726c
MD5 b957d9b1fc58214066845c467893b081
BLAKE2b-256 748bf1dd5bf4241ae4c819d929376fbb0599722d4f5aaf2cba0986e9599a54fd

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