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

Uploaded CPython 3.13Windows x86-64

typedunits-0.0.1.dev20250213042250-cp313-cp313-win32.whl (699.1 kB view details)

Uploaded CPython 3.13Windows x86

typedunits-0.0.1.dev20250213042250-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.5 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20250213042250-cp313-cp313-macosx_11_0_arm64.whl (769.1 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

typedunits-0.0.1.dev20250213042250-cp313-cp313-macosx_10_13_x86_64.whl (808.3 kB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

typedunits-0.0.1.dev20250213042250-cp312-cp312-win_amd64.whl (742.6 kB view details)

Uploaded CPython 3.12Windows x86-64

typedunits-0.0.1.dev20250213042250-cp312-cp312-win32.whl (700.1 kB view details)

Uploaded CPython 3.12Windows x86

typedunits-0.0.1.dev20250213042250-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.5 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20250213042250-cp312-cp312-macosx_11_0_arm64.whl (776.3 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

typedunits-0.0.1.dev20250213042250-cp312-cp312-macosx_10_13_x86_64.whl (814.4 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

typedunits-0.0.1.dev20250213042250-cp311-cp311-win_amd64.whl (769.6 kB view details)

Uploaded CPython 3.11Windows x86-64

typedunits-0.0.1.dev20250213042250-cp311-cp311-win32.whl (735.1 kB view details)

Uploaded CPython 3.11Windows x86

typedunits-0.0.1.dev20250213042250-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.6 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20250213042250-cp311-cp311-macosx_11_0_arm64.whl (790.3 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

typedunits-0.0.1.dev20250213042250-cp311-cp311-macosx_10_9_x86_64.whl (851.3 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

typedunits-0.0.1.dev20250213042250-cp310-cp310-win_amd64.whl (766.2 kB view details)

Uploaded CPython 3.10Windows x86-64

typedunits-0.0.1.dev20250213042250-cp310-cp310-win32.whl (703.5 kB view details)

Uploaded CPython 3.10Windows x86

typedunits-0.0.1.dev20250213042250-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.4 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

typedunits-0.0.1.dev20250213042250-cp310-cp310-macosx_11_0_arm64.whl (756.9 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

typedunits-0.0.1.dev20250213042250-cp310-cp310-macosx_10_9_x86_64.whl (815.9 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250213042250-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 23f0c1dad75982642d9268165d2699b18e7ab5b009705e50823202dcc805dd63
MD5 3010b1db02b1c1703c09ce2ff103141b
BLAKE2b-256 7ca3007e4544d072e38440390cad2f9cce05293823e6f3eba974519555b6c08d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250213042250-cp313-cp313-win32.whl
Algorithm Hash digest
SHA256 d764efeed081677cabfc26602093a3bb224f8aa2288926f8a971f1cca8455030
MD5 80cf88ed5f657cc8a75be711ef3ca78e
BLAKE2b-256 248ebad522603c7b4aec8e1fa7dc9987f407ddd428d92a1fc1d6f64398bd9f83

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250213042250-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 69caaa96da3854f57ff9a4137d980f854219e0564150e00ae155efceb3afe196
MD5 13b9a1226d0475463004fc6a2276d120
BLAKE2b-256 266600ea06494addab69b3ce1f06909b02ad10361b710dc7d77fc1b4a74492e0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250213042250-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 6600e5d56c8fe258a992a2d5378080ae06de801b4862a4a0a2236c8d8777adea
MD5 996f4693c1e491af47e81d51f2f236fb
BLAKE2b-256 ecfee6d537113c8d23fba3b58be60f2a612c09718fe359b8fbf9fd1cb2f6a692

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250213042250-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 9cc8366c2d29d777bbf6aae20db02afa3c7feaa098d09a3d16f84eeb44df634c
MD5 1c5bb7919370e23d6d144ef206af3ee7
BLAKE2b-256 f6fb28520ba234abcaa35df0520a91a86b0ce416072f0d9875bf7caa147daab5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250213042250-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 eb8d4d36adaa2962646040a48a85965b6ff84a83abbde7c06715c8c6beba74c8
MD5 91b4466773cfa18a6954927583ff73c3
BLAKE2b-256 8c11fe3f0a96dc22b60b91308b5902157d3117e2bb64639544bf30031b6f2086

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250213042250-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 4b713a63c9effedc49f4cfe42c6339909f33e3485db4fc37e7f555602933ec1f
MD5 a7b82d204497370ee7795127cd93294c
BLAKE2b-256 f96c15a88afafd023b1cc926c595e141e0cb3f5cc691465c9dfbd06907d4dd25

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250213042250-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1707e7e3f59ffe8e59f21ecdb4cb72383f65e27876bf7a8b7ad682cbdb2634b3
MD5 fb6619fb854b1dad8fd9847573a44301
BLAKE2b-256 6f59d1d93e17c05c33e7b8b8e7d8adfb5f7e9dfdab1505d426aa1294248817bd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250213042250-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a66a7133c70219d812ec3c65372e342a197a0c79e3a59d58bf3ee0904d282f00
MD5 6ad3abb4ceaf2afe7e383bf603ae381e
BLAKE2b-256 9a7466f584f9a3d9610c8096acb77bc31d3106998f8de6c6e647a3fbd4928218

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250213042250-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 5e5276c4a1199a0f6ebf82fb9d76d66ba3ff57eb0fcb1ab306e424f4246fc5b7
MD5 cfd7064f16be4d5c2af0c1a7c72e338e
BLAKE2b-256 a22b4a987924457b2e97f91b3f427a6bb615adce6b1e379fac4a7de839da2b5b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250213042250-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 759b46350331eadc0fbf96042a090ac2a227350af3bf7dd457d05425876ab284
MD5 42f597839862940afc625a05f7410ee8
BLAKE2b-256 992879514056261c1edd07d996247fa7cc2bb517810e70adf6e86414524c9b6d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250213042250-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 7ce3b904df76c4d105fb9e28029face5f557ed7731d00776bd77f61640a2362a
MD5 b016d1f87f66bd848148c254eb5ed84c
BLAKE2b-256 ae481aeacdbe90b1f603718cbf32b60fca8b83dbfe734a52b821103b2179f1b6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250213042250-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 37f577ca5a50f66c4a7e88bc496f774da81e6b27070d76c3cabb23d0009cf04f
MD5 0ae2f32929bd4744b7f18bb684d64621
BLAKE2b-256 9dbf7a1226527bd1d28ef299fb324710bb49470106cc12355fcb503b58d8c487

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250213042250-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e2cf221457d6cb57e14a98a09d1a269f61dd6ba29bdc964509a30fe77857c891
MD5 36ea5da002cd54575a6dfe5464335711
BLAKE2b-256 670c88544c600fff134c218de1efcd5779425d6cea46abc264832fa5cc7cb922

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250213042250-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 70e4487a94d6285956fa694620bae5e855f971afb9177c8c4779664751af31d5
MD5 71ef6d9c9b08aefae1fc20c044ef138e
BLAKE2b-256 2b8e0573f01b8f483df816debae9f0fce41e5bd66caecd92f8d2c1928e0fafa2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250213042250-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 514db2999ecfe1316ce40340eacc9741ffcd4e1717f1492ffbd75542838c4d3f
MD5 57c4c25f25905111d656c4e6083870aa
BLAKE2b-256 4c04f6a42e206c9703e4bc31301c08e034f9b01303a664378a25f3231a0bb9cd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250213042250-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 7a91a8fa9118e2ab7acba5bdfa815cb555de16a7529f75e494db8199bc139b08
MD5 a4d1c82c9c51d461914485def7940f13
BLAKE2b-256 00ca184b6e5c0273b64e20bdafaf1e2a9dbcbd723005b27ea40dd9b1d33a1add

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250213042250-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ec6b35017a191358e2a71785bcb26035ea8509af645797d99d3d54cd172b6928
MD5 fa8385e4df255651206a046a3bbbd1a6
BLAKE2b-256 5e3f5cf6cbeb21a301cb86d644e7b9c22784c77947ebb4a67c1977db0f310f6c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250213042250-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 22da70e59455549c5b922f11bc29a42e0401dad20cbca0ab7807677dbb67586b
MD5 9e7433b63d52f6222ffbf0548e3212e6
BLAKE2b-256 a60ab2fa432106ec2b8f59762bfc4a884900bb0b266d9d1fb82971423b3d9e1f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typedunits-0.0.1.dev20250213042250-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 e3e3bb591a0223650cd3f52e9b1c180fdf75061465f6d5eecf4a9f5c7a6b8dbe
MD5 7ca44441221f51c119aaf62ccd890892
BLAKE2b-256 c51451f040a7ae5ac892697f5c0e36315bb4ff670427f154920d13a5e8129c12

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