by ahupp · 2y ago · 0 comments
>>> speed = 5 meters/second
>>> (2 seconds) * speed
10 meter
I'd been using Jupyter notebooks as a calculator for engineering problems and was wishing for the clarity and type safety of real units, but with the succinctness of regular python.It works in both Jupyter notebooks (with an input transform) and stock python (with an import hook). The actual unit checks and conversions are handled by the excellent `pint` package.
>>> speed = 5*meters/second
>>> (2 * seconds) * speed
10 meter
Given the ecosystem problems and other problems extending the syntax gets you, I don’t get why you’d do this at all, when all it gets you is the ability to write ' ' instead of '*'.> I don’t understand why you’d go to the enormous trouble
But more importantly, it was just really fun to get it working.
Units are inherently exactly multiplication.
5 meters is:
5 (unitless) * (1) meter
You could also use python type hints for this stuff.
Here are some links about units in python:
https://socialcompare.com/en/comparison/python-units-quantit...