Skip to main content

Unitscalar: real-time verified dimensional analysis in Python

Basic validation

This package implements a unit-aware number data type that keeps track of units as an inseparable part of the number. Arithmetic operations strongly protect against inadvertently combining incompatible units. Heavily inspired by Steve Byrnes' numericalunits and Dylan Walsh's unitpy

Implemented Features

  • Instantiate with a floating-point number and a string representing a unit (e.g. "kg mm / ms2")

    • Most common SI units and some imperial/customary are supported
    • SI unit prefixes from femto to tera
      • hPa is implemented specifically because the hecto prefix is not used very often
    >>> from unitscalar import UnitScalar as us
    >>> list(us.UnitScalar.VALID_UNITS.keys())
    ['m', 's', 'kg', 'C', 'K', 'in', 'L', 'Hz', 'rpm', 'g', 'lbm', 'J', 'Wh', 'mol', 'N', 'lbf', 'Pa', 'hPa', 'bar', 'atm', 'psi', 'W', 'Ah', 'A', 'V', 'ohm', 'T', 'F', 'H']
    >>> list(us.UnitScalar.VALID_PREFIXES.keys())
    ['f', 'p', 'n', 'u', 'm', 'k', 'M', 'G', 'T']
    >>>
    
  • Format as a string

  • Get raw floating point number

  • Get raw integer number (truncated)

  • Compare units with another UnitScalar object, or a unit string

  • Get raw floating point number in other (equivalent) units

  • Format as a string in other (equivalent) units

  • Fundamental algebraic operations (operands can be UnitScalar or integral types)

    • Add / subtract
    • Multiply / divide
    • Raise to power (frational powers allowed)
  • Instantiate with custom literals (see below)

Valid Literals

UnitScalar uses custom-literals to hack support for custom literals into the language. These are defined for certain (arbitrary) unit strings as needed. At present:

Literal Unit String Example
x "" (N/A) 10 .x or 10.x
gMM g/mol 101.1.gMM
inch in 3.90.inch
psi psi 10.0.psi
lbf lbf 0.0.lbf
K K 1837.22.K

As a consequence of including this feature, unitscalar depends on the PIP package custom_literals. The latter mentioned warning about stability shouldn't affect downstream projects if the literals feature is not used.

Fair Warning

Briefly quoting the custom-literals README section on stability caveats:

This library relies almost entirely on implementation-specific behavior of the CPython interpreter. It is not guaranteed to work on all platforms, or on all versions of Python. It has been tested on common platforms (windows, ubuntu, macos) using python 3.7 through to 3.10, but while changes that would break the library are quite unlikely, they are not impossible either.

TODO List

  • Vectorized artithmetic?
  • Write example code and fill out README

Metadata

Release files for unitscalar 0.0.10

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for unitscalar 0.0.10
File Size Uploaded
unitscalar-0.0.10.tar.gz 10.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for unitscalar 0.0.10
File Interpreter ABI Platform
unitscalar-0.0.10-py3-none-any.whl Python 3 none any Details

Total release size: 18.7 kB

Release files / unitscalar-0.0.10.tar.gz

Download URL unitscalar-0.0.10.tar.gz
Size 10.8 kB
Tags Source
SHA-256 checksum
How to use checksums
909569d7357ae5f8a96aa7265e6c8c078bbca3036e1409c7e9fae4209bc82374
BLAKE2b-256 checksum
How to use checksums
c58f187bc7736ed09d0f9ae1beacfcbfd567837ad2586f7e8d7d7626d5ac1c74
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.10.12

Release files / unitscalar-0.0.10-py3-none-any.whl

Download URL unitscalar-0.0.10-py3-none-any.whl
Size 7.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5b30011a78909a2cff649ef80a941a0b2c00dc61b34d0eb68b1a70f0dfdf4a38
BLAKE2b-256 checksum
How to use checksums
0a2866f22a22a775cd8e57af879a5b43f231ed1425c43baa6c07e3ca881e7f7e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.10.12

Release history Release notifications | RSS feed

This release

0.0.10 This release

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page