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

Uploaded CPython 3.13Windows x86-64

typedunits-0.0.1.dev20250416214736-cp313-cp313-win32.whl (740.1 kB view details)

Uploaded CPython 3.13Windows x86

typedunits-0.0.1.dev20250416214736-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.7 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20250416214736-cp313-cp313-macosx_11_0_arm64.whl (810.3 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

typedunits-0.0.1.dev20250416214736-cp313-cp313-macosx_10_13_x86_64.whl (849.6 kB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

typedunits-0.0.1.dev20250416214736-cp312-cp312-win_amd64.whl (782.7 kB view details)

Uploaded CPython 3.12Windows x86-64

typedunits-0.0.1.dev20250416214736-cp312-cp312-win32.whl (741.3 kB view details)

Uploaded CPython 3.12Windows x86

typedunits-0.0.1.dev20250416214736-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.7 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20250416214736-cp312-cp312-macosx_11_0_arm64.whl (819.8 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

typedunits-0.0.1.dev20250416214736-cp312-cp312-macosx_10_13_x86_64.whl (857.6 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

typedunits-0.0.1.dev20250416214736-cp311-cp311-win_amd64.whl (811.4 kB view details)

Uploaded CPython 3.11Windows x86-64

typedunits-0.0.1.dev20250416214736-cp311-cp311-win32.whl (775.5 kB view details)

Uploaded CPython 3.11Windows x86

typedunits-0.0.1.dev20250416214736-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.8 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20250416214736-cp311-cp311-macosx_11_0_arm64.whl (832.6 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

typedunits-0.0.1.dev20250416214736-cp311-cp311-macosx_10_9_x86_64.whl (894.8 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

typedunits-0.0.1.dev20250416214736-cp310-cp310-win_amd64.whl (808.5 kB view details)

Uploaded CPython 3.10Windows x86-64

typedunits-0.0.1.dev20250416214736-cp310-cp310-win32.whl (743.4 kB view details)

Uploaded CPython 3.10Windows x86

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

Uploaded CPython 3.10macOS 11.0+ ARM64

typedunits-0.0.1.dev20250416214736-cp310-cp310-macosx_10_9_x86_64.whl (858.5 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250416214736-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 35ffc742ca80cf5611c700558b025c4a523edabd4701a324716e24ca84bf1a2d
MD5 55dda30fc46046c45c842be24e167086
BLAKE2b-256 251fcbffede4c8d83986ebb7722d6b3fa40595d4aa348a116638276834e3ee0f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250416214736-cp313-cp313-win32.whl
Algorithm Hash digest
SHA256 60ab22e58329a8d5e6ae2b85775551fe5e889a0496fe43e754f3ea6e2588c9d0
MD5 2dabd4f56b55cb6ac61d25f2fc584949
BLAKE2b-256 d14cc2aaa2cd5d04acc15cf1a6a0eee31f1101fe3f37f10f9211dd030217ce52

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250416214736-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f6bc090b3344795f7093feb7214056a0d3da8ed7755468eaf4f2691dbeedde19
MD5 4399d19cc74a7bd573d13988c3a633fb
BLAKE2b-256 8bf50ea46e9801b3ea89d26a0fa5607a750f7931fff5537df13c4a19c41cd8d2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250416214736-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 67cfa512df8c01c075aadb38c14c83e5c59a0381091975d2b1e5b628664e5861
MD5 e7808c38c0ace829cb15bd599c2670af
BLAKE2b-256 778a6679fa70150cd57a11600452234ed736f1b58952759f7eac1aab3a48a347

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250416214736-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 30ab7965a90265f42cfe33b80a3d9d93abf36e33e9996b9646642badf07e5ccd
MD5 e39d475d5e4889029f78d677362f3a47
BLAKE2b-256 d368fa15ce5e74a2847f30fefc92e9141e1ab5c6d71d8a2bdc851f9204439c15

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250416214736-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 9802d61e3743b81d4526b37b16c0d451db2b4854d0ddde202af9cd7e58a29681
MD5 9014e34142250f646a3fa6fa87a778b6
BLAKE2b-256 377a090f5928baf9fda1b11793d2934c8caa6293e156e20a854d12ad33b0b522

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250416214736-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 89ad303c7bdbba50bbbe655b253c8346944e9d5372e3ed8a252992ee66630834
MD5 6209ece0e035984ecd83be12b6d1cfc0
BLAKE2b-256 21d0d91826b8f35f8c3d0f387f67bbf5878ac87017d61e757824d38d73afeadc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250416214736-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 0b85bfacaf7d518aab4cc93a37f2ec6a61d913fb957fd8db435c66c766b5855c
MD5 324ffe28ca8c5e5025c827424966bda0
BLAKE2b-256 7d97dae918fa0dd88e9eb2da09b9ba17516b9a0f423204f13ea3af670cdfc5fb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250416214736-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a4947eada379ef96eaf9b8a761f21a155e4a9dc6e38480570f513d9a57ddcd71
MD5 dbe1f4418a38cf74d7b954c304d8896f
BLAKE2b-256 ab8bae543db95500bdd54ab1e11aba3b8f407fdf3e103d5eaee92cbb72cc2871

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250416214736-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 c0155b184330ac60311d6878a9d658ac1c3d75f48f70941e11c7ad2a0a259335
MD5 d89a520c14fa23ec3a295dde583807d6
BLAKE2b-256 0bc5de9203003295294efed4eb85784e0ff540af4fbfa4db231fc7d0a10c11c6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250416214736-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 e016f5ade8589fdca2bb6e6dd45e00b412c7906d18ec8b5bb4d047ad3fd769a3
MD5 2b26d9881ef809f1cc2e024318a6cc24
BLAKE2b-256 c9007763e62ac3f60f9a06e6b270e94853c62f47446b133cb7e33f733ec05d54

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250416214736-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 4f14f9bd128d9057e87548c31b9c99aa1640eee2c794101ea465183f4a980dc1
MD5 e21f62bbebbc1c289cf3cd703bdb1db7
BLAKE2b-256 6a2ce8d1b58908c0d8787ba3686f22d6b3bacae9c1440f838ad24797b9c293cd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250416214736-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 3746bac4894435874a65362f8f4dbcfa6737b1c88557fdf88986b4f72f0093b2
MD5 b5a0990ce213eb441142415897ccc09d
BLAKE2b-256 c9c0916086a31330ff86c4e9e1de8632f864ed2cecda1a3a34a4fd8bfd46c298

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250416214736-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e98186ac1023c9f5e1514b11eb1e3eec30b0ce5a99c3fd783f284b41843ad789
MD5 2d657443669eae908f51425464e35592
BLAKE2b-256 3869c3af7d764e23748650927ec10bc97d89af2d911dbc4c9ccd634575ee0dac

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250416214736-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 738a7b4485a2e18f9381ff34d2a2517038ef495d6b7285f4796d95752d399461
MD5 9e1b81cfdd086752851abb095799aac6
BLAKE2b-256 0bb774cf103577b59f45b358d69c23ada2fab0f8c3243b9396d1507f298abbdc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250416214736-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 21581f9fa0388aebc27dc59211838ba2df908ad73a99479785eaa5befe8ac0dd
MD5 40af52c4a70f51631dc78819de12e444
BLAKE2b-256 2ea13f1c87a3404b4b9eb9e0456666c81e4582fa3ed46d77577b5d8d63c16c4b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250416214736-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 88843f74b740b9c338d00052a2e6ef5c98506cc83e60011e2badd7c08d9c4303
MD5 1aac443f431435416bee8dd2dcdc43f7
BLAKE2b-256 14e541f4f2f9b02dffa6e4623b5bbc94a13839e7304a5d193a25c79f56150427

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250416214736-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e0ed98674cde9ad62778ba1aabeed30dd9796e8485f563f6492536a654c798fc
MD5 043a4fd563005613b419eded803c4638
BLAKE2b-256 2f0921da652e15cd1be9aea1b334013a5e1ac4a345da481c6cf96a434a524ba5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250416214736-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b3d2dc5757ec6e79361c526c9295008db58767543d69bf41318ef9013d1753d2
MD5 ac5882cbb33dfbf29d9a3aa1a76ca3d7
BLAKE2b-256 0af5c30c5ad7b98f55427164f58234048a044ddbb1d3edffa14cb46c5884ba8d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250416214736-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 8685220f404be97bb20a5d1fd84c61cdbb5ef9afb941a4fe6c49e004c41c4e64
MD5 12d78a8e27e44bab0bdfaa75a7c80214
BLAKE2b-256 eaee901bde9379e5ac05b5e69e375cbf5dc6b2e2d246c03135cf5fd995068c7a

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