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

Uploaded CPython 3.13Windows x86-64

typedunits-0.0.1.dev20250224204949-cp313-cp313-win32.whl (721.7 kB view details)

Uploaded CPython 3.13Windows x86

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

Uploaded CPython 3.13macOS 11.0+ ARM64

typedunits-0.0.1.dev20250224204949-cp313-cp313-macosx_10_13_x86_64.whl (831.8 kB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

typedunits-0.0.1.dev20250224204949-cp312-cp312-win_amd64.whl (765.1 kB view details)

Uploaded CPython 3.12Windows x86-64

typedunits-0.0.1.dev20250224204949-cp312-cp312-win32.whl (722.8 kB view details)

Uploaded CPython 3.12Windows x86

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

Uploaded CPython 3.12macOS 11.0+ ARM64

typedunits-0.0.1.dev20250224204949-cp312-cp312-macosx_10_13_x86_64.whl (837.3 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

typedunits-0.0.1.dev20250224204949-cp311-cp311-win_amd64.whl (792.7 kB view details)

Uploaded CPython 3.11Windows x86-64

typedunits-0.0.1.dev20250224204949-cp311-cp311-win32.whl (756.8 kB view details)

Uploaded CPython 3.11Windows x86

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

Uploaded CPython 3.11macOS 11.0+ ARM64

typedunits-0.0.1.dev20250224204949-cp311-cp311-macosx_10_9_x86_64.whl (875.7 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

typedunits-0.0.1.dev20250224204949-cp310-cp310-win_amd64.whl (788.9 kB view details)

Uploaded CPython 3.10Windows x86-64

typedunits-0.0.1.dev20250224204949-cp310-cp310-win32.whl (726.2 kB view details)

Uploaded CPython 3.10Windows x86

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

Uploaded CPython 3.10macOS 11.0+ ARM64

typedunits-0.0.1.dev20250224204949-cp310-cp310-macosx_10_9_x86_64.whl (841.6 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250224204949-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 3c0f724976e06eb7a5ede4eb70b38a818beab0a1258695889cb68dd5e62b7bba
MD5 a9ce923628518228e52e49df88b318bb
BLAKE2b-256 1393e1112b151a68f6c1688482b6d8a17b4126a0299b0e8b004f0424249eab55

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250224204949-cp313-cp313-win32.whl
Algorithm Hash digest
SHA256 a5c9022b1721997fc274321a56ece940888da705d184bfd44cbc1562a15ac842
MD5 ceb15a60e7b4264757516d17f91a3403
BLAKE2b-256 07237504a3d928fb72dddfb85f9b2b80954dd3297a7c4e4072c45975e2a0ebb7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250224204949-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7e0f7ba740aec0a66e4521a3f8fa17c6cb661dc2f7da93845a5ffb62e3d7668d
MD5 53b203c5114701cd46718b75f0bc9bb8
BLAKE2b-256 fad9ebe488b18b8fba7db96dd3e4087b5d4a4af8c445824660b0048597ca43f9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250224204949-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 bb5e646c20620481fd3b6d2577fcaadb58e7899fb5f2c9ebfc4379a2c6153ca3
MD5 c126e4152bdfcb2a1a0e5cb56ff872c8
BLAKE2b-256 bb25ff63ac8d9b862c6aa8ad8e6e9c8a048577508d10ec1944284957bf13956a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250224204949-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 cbbe6b17a3576e990bc27cf9f9f2bb3f88b8b34fbcdd3f7f77699ee6e5ec1a99
MD5 b6de00f335010ca2a04021421206a8a7
BLAKE2b-256 ddbd9d2aad2d48f228102a40495bb4d7fdf706b1648b24e65ecbf7214ddb8507

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250224204949-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 34773b7f306849cb1adaa4abf9da920e59766682bca8d3897dba2242f553dbfd
MD5 a66918584d05c9efc419beece58c0416
BLAKE2b-256 32bf845629d176cdb1adbbc9e0e5634499ec38d364248de3b11af1058cd8b47b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250224204949-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 164a9e35d856efff8b3ce923c8b96ff7d0ddf500f6570d06104b0b9c8b1147e1
MD5 9e6f8bf23c755550cf8353a91d7275c9
BLAKE2b-256 3785e40047c395ed387c1fc57641ca9a7040f468ed5fa6f8b22d5591e15cfa2d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250224204949-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 41b59e35a6e7ebeb57808da8ce624885aca40a390bba6a7aac570efdc8822f19
MD5 76ce23b6ecacbd5242df95bc6e236039
BLAKE2b-256 159fbc29f6b4e10ddf469b9ae2e17588dc89840956c4aa0fbdf995f930968d82

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250224204949-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c35d51093748f3d5aaa2a2bcf906c975b06c160aeae1993aafb0ab30be28cb4a
MD5 30ff40aeefc475b50fbcb7e31a14f80e
BLAKE2b-256 8bce90092468da270e871a7df4919bdf4a53f541901b4afb20613579be564a3d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250224204949-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 388a5530c386a1ad1f478418df65da6c3ee8097fff076c005eb6677258a39514
MD5 37d9feb48a404963d82892f01cd5475a
BLAKE2b-256 a412374bbb5185dd6cdf81e7dbe5ca53c1dd82793b8934ff424cb49e777c50d4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250224204949-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 fb7d3f5265d28f496f26354ad74a87e9e3845378d25d73d265e677c24fb5dfbd
MD5 c713cc9957fd186bb1efa9e657b08ee1
BLAKE2b-256 fe4b13a2380c34316106cfa60edf1d5c14974f6fd9680a8155497dda94632134

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250224204949-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 63c39abfa5bdca8b32471c4d04f6e9eb63f2e177c8d09a5cde8f52e1d5a817df
MD5 993a614c9137e0132503d126b2e5544e
BLAKE2b-256 0e64077580e1b09dd19de83ce8e76c7f11a6f7fb9a4f6b7a4b93e1b9d408ab8e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250224204949-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1ab08659df3d5eb9819cb26832112d1b19f1dd6171cf831a11794939ba5f7b57
MD5 e7cfb6ce96b0e5d96b894d00ac6d9f0f
BLAKE2b-256 6fb97364606a3d9fb65525a63a3263339b3729f59b17ba4aed183bab08c40ddf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250224204949-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 5dd462d7c17f28c869281622e1a14227ba792200ca349d20cd6c60ff6f42ce0f
MD5 82314683bd77450b560493242272145d
BLAKE2b-256 a9dc91c40d28d5623ac6f21a48d66b65b5219c125123eaa8a4b7f053cad8364d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250224204949-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 473d05d50960709bc9ce18399cb8ef94294466febf699fa7f7ba2623c2e6cd24
MD5 ca441f488ad288422862a1f03abdab37
BLAKE2b-256 ae53394c003a762a847c9afd92bbbfeb1edcf9fb68b5114c3d07757def9f4049

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250224204949-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 d5c18261c30ab3c180c2fe79a04879ee34a6c0a12404ec5e445db697e693524e
MD5 132f0453a6b247c0df51b756bfd1be6a
BLAKE2b-256 fe238ea6aef0c4346acad87d24797377c64e12ee96967049d7a53653d88e5eac

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250224204949-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 3211503c1df8fce55132ed44a1555b72dc4094fb1b62dd6319189a4f53f4fa4e
MD5 0aeb74d46972d7e34dba1c30dc5dc10e
BLAKE2b-256 59b12fb767e851d535ec90e6ddb2e94d0c12fd4c89156128aade059981e14a0b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250224204949-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 df58d1fa7b85be3ec90c8ac9e4177eb9e8f7b7ea66e833d58c03a98116cbe2ab
MD5 83b159175bf0c7ec924deb0f469128ad
BLAKE2b-256 8fc630f918a20891c20e43a79782fee5de74d42f0887b29e3e495c2dbfaef178

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250224204949-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 88058c0f5c26f574e50b41ac9d07ad8c2eff4fab78baff4995620125e44c0dc0
MD5 ca86d78d824e30ef975dfc73e049c23d
BLAKE2b-256 1c4515dd821eed970b513a3bd39fa52b4c3ebc4e60aa29d3ecb9524d2d67260f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250224204949-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 b4f8423355bde5be9c8d61fc2fc622a3ef3d88c42f9deab042594e6f21f59e0a
MD5 68e50f287822d77c99dd67bd2843cb91
BLAKE2b-256 5599aaef5ae41197583b93e9bb23731df69ea3d1c2dc52f55b4846e8f19a8e79

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