A package for dimensional analysis calculations on floating-point values
Project description
Unitscalar: real-time verified dimensional analysis in Python
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
hPais 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
UnitScalarobject, 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
UnitScalaror 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
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file unitscalar-0.0.10.tar.gz.
File metadata
- Download URL: unitscalar-0.0.10.tar.gz
- Upload date:
- Size: 10.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
909569d7357ae5f8a96aa7265e6c8c078bbca3036e1409c7e9fae4209bc82374
|
|
| MD5 |
617cc5eb37f7fe37d4eac6d82f758f5b
|
|
| BLAKE2b-256 |
c58f187bc7736ed09d0f9ae1beacfcbfd567837ad2586f7e8d7d7626d5ac1c74
|
File details
Details for the file unitscalar-0.0.10-py3-none-any.whl.
File metadata
- Download URL: unitscalar-0.0.10-py3-none-any.whl
- Upload date:
- Size: 7.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5b30011a78909a2cff649ef80a941a0b2c00dc61b34d0eb68b1a70f0dfdf4a38
|
|
| MD5 |
350642497b3feb71c121cc5ac7ed9f6a
|
|
| BLAKE2b-256 |
0a2866f22a22a775cd8e57af879a5b43f231ed1425c43baa6c07e3ca881e7f7e
|