So I’m writing some python scripts to help me do some simple calculations:
WireRadius = .455 / 1000 / 2 #m, 25AWG
CoilInnerRadius = 10.0 / 1000 / 2 #m
CoilOuterRadius = 20.0 / 1000 / 2 #m
CoilLength = 20.0 / 1000 #m
CoilVolume = 3.14 * (CoilOuterRadius**2 - CoilInnerRadius**2) * CoilLength #m^3
print "CoilVolume: " + str(CoilVolume)
WireCrossSection = 3.14 * WireRadius**2 #m^2
print "WireCrossSection: " + str(WireCrossSection)
LengthOfWire = CoilVolume / WireCrossSection / 2 #m
print "LengthOfWire: " + str(LengthOfWire)
Now, I want the script to print out all the intermediate components, so I can see what is going on. If I screw up, this will also let me pinpoint the line where my math is wrong, because that’s when the numbers become nonsensical.
However, this is clearly not very DRY, since I am writing out each variable name not once, not twice, but three times:
LengthOfWire = CoilVolume / WireCrossSection / 2 #m
print "LengthOfWire: " + str(LengthOfWire)
If I was typing this into the interactive shell, it would automatically spit out the values of the intermediate components back at me:
>>> LengthOfWire = CoilVolume / WireCrossSection / 2 #m
14.491003502
which is quite nice, because the assignment statement is preserved meaning I know exactly what the next value is. However, the problem with putting it in the interactive shell is that making changes and re-running the whole script (which is a few dozen calculations long) is tedious. Is there any way to achieve this functionality in scripts run via python script.py?
Don’t forget to set up your logger appropriately.