🔬 Python Dimensional Analysis (Unit Class)A powerful, standalone Python class (Unit) for representing physical quantities with dimensions, enabling arithmetic operations while automatically handling metric prefixes and verifying dimensional consistency.
This library is ideal for students, engineers, and researchers who need reliable tracking of units (like N, m, J, s) and their corresponding prefixes (k, M, m, µ) during complex calculations.
✨ Key Features:
- Dimensional Consistency Check: Automatically prevents illegal operations, such as adding Newtons (N) to meters (m).
- Metric Prefix Handling: Seamlessly convert between prefixes (e.g., 1 kN + 500 N works and returns 1.5 kN).
- Auto-Scaling (.auto_scale()): Intelligently adjusts the displayed prefix to keep the numerical value in a readable range (e.g., 12000 N becomes 12 kN or 0.005 A becomes 5 mA).
- Full Operator Overloading: Supports standard arithmetic: +, -, *, /, and exponentiation (**).
- Complex Unit Simplification: Automatically combines and cancels units in multiplication and division (e.g., N * m / s^2).
📦 Quick StartThe class is defined entirely in Unit.py. Simply import and begin calculating.
- InstallationIf published, installation would look like this:
pip install unit-dimensional-analysis
- Real-World Usage (Structural Engineering Example)This demonstrates calculating the total design force and checking a column's resistance, handling conversions between units like $\text{kN/cm}^2$ and $\text{kN/kg}$ automatically.from Unit import Unit
Define input data
from unit_analysis.unit import Unit
N_Ed = Unit(750, "", "kN", "") # Design Axial Force: 750 kN
f_y = Unit(23.5, "", "kN", "cm*cm") # Steel Yield Strength: 23.5 kN/cm^2
H = Unit(540, "", "cm", "") # Column Height: 540 cm
Cross-section area (A) and linear mass (G)
profil_slupa = {
"A": Unit(164, "", "cm*cm", ""),
"G": Unit(83.2, "", "kg", "m"), # 83.2 kg/m
}
# 1. Calculate Self-Weight Force (N_self_weight)
# g_accel is needed to convert mass/length [kg/m] to force/length [kN/m]
g_accel = Unit(9.81, "m", "kN", "kg") # (9.81 m/s^2 is used to match kN/kg)
# Linear weight: [kg/m] * [m/s^2] * [factor] -> [N/m]
g_kslup = profil_slupa["G"] * g_accel
# Total self-weight force (in base units)
H_m = Unit(H.value / 100, "", "m", "") # Convert height to meters
N_self_weight = g_kslup * H_m * 1.35
print(f"Force from self-weight: {N_self_weight}")
# 2. Total Design Force (Demonstrates Addition with Different Prefixes)
# 750 [kN] + 5.95 [N]
N_Edmax = N_Ed + N_self_weight
print(f"\nN_Ed,max (Automatically Scaled): {N_Edmax.auto_scale()}")
# 3. Cross-section Compression Resistance (N_cRd)
# Resistance = Area * Yield Strength
N_cRd = profil_slupa["A"] * f_y / 1.0
print(f"Resistance N_cRd (Automatically Scaled): {N_cRd.auto_scale()}")
🛠️ Running the ExampleThe included if name == "main": block demonstrates the library's features.python Unit.py
🔮 Future Roadmap
- Implement a Unit.from_string("100 kN/m^2") parser for easier instantiation.
- Add support for common non-metric units (e.g., "in", "ft", "psi", "lbf").
- Add the capability to define and use Aliases (e.g., treating N*m as J).👨💻
Release files for unit-analysis-package 1.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| unit_analysis_package-1.0.2.tar.gz | 5.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| unit_analysis_package-1.0.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 12.0 kB
Release files / unit_analysis_package-1.0.2.tar.gz
| Download URL | unit_analysis_package-1.0.2.tar.gz |
|---|---|
| Size | 5.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f9a7847831405649d87041d4eeda45227cee0329d9d102398e519dc02c541ce8
|
|
BLAKE2b-256 checksum How to use checksums |
b480b6ff2e5273d819d02e4eab9963846c30501159529e51610b00de960d8050
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.0
|
Release files / unit_analysis_package-1.0.2-py3-none-any.whl
| Download URL | unit_analysis_package-1.0.2-py3-none-any.whl |
|---|---|
| Size | 6.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
03cb71e77b7c8651878c2750e8e443da977ceb0d76f73ca31c3f7ee0bfa25d15
|
|
BLAKE2b-256 checksum How to use checksums |
e5d42918c9c16bcfd0bfeb7a9276dbdf3b05be3558413d5e2ea3be9476fb42ba
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.0
|