3 ms·
I've used astropy.units in an intricate but medium-size (~10K LOC) Python simulator and it was really quite nice. You can do things like: import astropy.un
by mturmon 7y ago
I've used astropy.units in an intricate but medium-size (~10K LOC) Python simulator and it was really quite nice. You can do things like:
import astropy.units as u
import astropy.constants as const
m1 = 1.0 * const.M_sun
m2 = 150 * u.lb
F = const.G*m1*m2 / (1.0 * u.au ** 2)
print F.to('newton')
where it has auto-converted the solar mass and the other mass to commensurate units, and then reduced the force into newtons.
If you get the units wrong (e.g., use the wrong "g", or forget a mass when multiplying), it will be unable to convert to newtons and throw an exception.
As you supposed, this proved to be a big benefit in a scientific code. Units checking removed a lot of worry when combining quantities from across the simulation, e.g., you didn't have to worry about how you were storing distances. A Matlab code shadowed some of the Python functions, and the difference in complexity/reliability was apparent.
https://docs.astropy.org/en/stable/units/ https://docs.astropy.org/en/stable/units/