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

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.12Windows x86

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

Uploaded CPython 3.12macOS 11.0+ ARM64

typedunits-0.0.1.dev20250116000158-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.dev20250116000158-cp311-cp311-win_amd64.whl (749.0 kB view details)

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.11Windows x86

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

Uploaded CPython 3.11macOS 11.0+ ARM64

typedunits-0.0.1.dev20250116000158-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.dev20250116000158-cp310-cp310-win_amd64.whl (745.2 kB view details)

Uploaded CPython 3.10Windows x86-64

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

Uploaded CPython 3.10Windows x86

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

Uploaded CPython 3.10macOS 11.0+ ARM64

typedunits-0.0.1.dev20250116000158-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.dev20250116000158-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250116000158-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 73e2552428a26e40008a5a6930b825402812e911b52edebdc0232d8a1f137b03
MD5 acee40fe10213fe71a4f813f96b1bd68
BLAKE2b-256 dd63f4b38833a9c696255daec3696cf414769a9239dc4b2472770d355b08c5ef

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250116000158-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 25fad27cb358df466ed262b7f6f6a834e370f04625cb98cf40458b9350491bab
MD5 e6e1aff57bf11a47b5469c528df64ca4
BLAKE2b-256 8ec32077eb60cb7e2138b38d834aed44afbac8f15521c24feb82b875f7ccb4ae

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250116000158-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ffd901be8ba8897047e9cece651a921db005c17e32a9b70cfa6d7abd5f5ae213
MD5 4f37d89b239a3c2d2b8bea8ab3723058
BLAKE2b-256 9658f5f341a1378a10bc7ea50f24b6152b276cf4120ae712a83baba3dbe05643

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250116000158-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1583c50860f0f985b0935da769e1e9138ef2f778ef223b1f78db0cb07a63d06b
MD5 c5a5fa4238ea1b0af8bd10c2b47c4987
BLAKE2b-256 e3f44603c8958afbf776574d434687005a510e89107fd09f881fa7fb286fa90f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250116000158-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 6cf4f124968f42b43e1b9f373025a3f10d283c3b1afa1a7416a5674c16ea495b
MD5 99a14d7f72208bf7b3088dc5dc16076d
BLAKE2b-256 1d5d8e4e6b763f07e14aa3228ca2776d91e3299d082fdb044b781bc4723fad90

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250116000158-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 80e382af65f498fb4287b70f11002af9b80aaba299668fa4cd871d95c90de22d
MD5 bba66a1e35fa57a4016c1dede82b8061
BLAKE2b-256 32bdadb60c0815be579803e25846483137c16ddff53d8edd13dffd17a29f6a10

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250116000158-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 fb468d9846441e1b203078bbb6cd2c244c971ffca9156def67e6d410b93ffd7b
MD5 91fc2268d8d077419285565beae207cf
BLAKE2b-256 c9ac639a2d4c12f29b50d26211551817a6ef0ed13e66ada6c36947c6aed1a922

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250116000158-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 370e1e31bdad12943c84982533fe236a3e2e759f0d7248e441ec31fc4ad4bcce
MD5 45fad6cd7ad9b7d43f60865e6837f5b6
BLAKE2b-256 c4d2f41eaa277224d1944debeec886cf0a0e5fbb035d7e5fdeb792c0b2f40cda

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250116000158-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2380b3f80cc1830c8f5646cc39fe94e23d9fa62af6f337c0833baa07efcc6666
MD5 a23605b42ca981bdfa7e1e652b81e1ac
BLAKE2b-256 9b4767cc99a2c6edd6d352850cc46027b754b0fb1c196ab2bff2b79399fa7669

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250116000158-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 f1f86cdec5953f70ba498ddff61d3d29a604b8d150b731befc34ef3de51c832b
MD5 3ce999e09ac4388b8d4c0763f3635bab
BLAKE2b-256 fa9e9111ca673652ec10518d6dd2c080edab4e6034c292d85ada601b46401839

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250116000158-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 e3163bd8937b3c02dd1ec3a2c9afb485d100b80465f7f8dd1148cac32968a5c8
MD5 a90bdf64c70fa6d1ba9a82673423284c
BLAKE2b-256 edfe53f8d21a90b53e5e341ae017cfd95b1df847b8a79c834c030f54fb036209

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250116000158-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 e0813f196f3e1699da9b1360328dd0b11f4ae111ddd25c53c2f4a5c7ee066f58
MD5 76c1b89b82c874137e0c1a0bc0d36f66
BLAKE2b-256 0136453d0581a5bef5167ea226375982a04898731c0a30b5a1b70431ce35cd70

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250116000158-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 eaa27547341fdab986255fdd05e8621e105fb70d16d3fff2cee62c849dcc4bcd
MD5 4b6fe34a4d73bc65f93b1ac787477dbd
BLAKE2b-256 0726fb8fdca06782c07580171132168ca34b16b8b13193f4f00e06b02ff8bbcf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250116000158-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d37e511156d5b1485ff05a89c4cdfc758fc33531a179d3074cde3629a8fe48ea
MD5 4c57fa26daab982d1c09db302e5f5758
BLAKE2b-256 9de0a610bf78216d7f9ca3e515c8a987f0e0f36a08a6d9234f6711e7b886b892

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250116000158-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 992f94455b353d9f6c4146dd28238d780c947013992e981a69b201c7d9b65568
MD5 09e338497ea8be1d40730898b0391487
BLAKE2b-256 ddec82c503bb2c418c9e35fd1f853a99d757ac3f1dd83ae422813f9e36a2ea49

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