Loading video player…

Using Pint to Add Units to Your Code

00:00 The key mechanism in pint is the unit registry, an object that wraps the knowledge about units.

00:10 Not that I’ll be diving deep into this, but it’s done this way so that you can add your own units, adjust values, and change defaults. If you’ve used the Python decimal library, the unit registry is similar to the decimals context.

00:23 With the class imported, let me create a registry. The values that I want to represent are called quantities. Quantities are a class that you get out of the registry.

00:36 The library’s author recommends aliasing the class name as you’ll likely be typing it in a lot. Let me do that. Remember, this is an alias of the class, not an object.

00:49 I can now create an object by using the alias. I want to calculate how long it will take to get to Saturn based on a slingshot maneuver around Jupiter. So first off, I need a distance from Earth to Jupiter.

01:06 This distance is based on the relative positions of Earth and Jupiter. I’m just using a typical value to give us something to play with. If you are actually planning your rocket ship, you’d have to make sure you’ve got it all timed with the correct dates.

01:19 Let’s look at the result. You can see that a Quantity has a value and a unit to go with it. pint knows about short forms, so my distance in km gets translated into the full kilometers.

01:34 The .to() method on a quantity allows you to convert to other units. Let’s convert kilometers to meters. You probably could have done that one in your head.

01:46 Multiplying by a thousand isn’t too rough. How about something fancier? An astronomical unit is a value based on the average distance of the Earth from the sun.

01:55 So by definition, the Earth is one AU from the sun, which is almost 150 million kilometers.

02:05 Jupiter is almost four and a half times further from the Earth than the Earth is from the sun, making it about 5.5 AU relative to the sun. That distance seems an awful lot shorter in light years.

02:22 Okay, that gives us our Jupiter distance. Now, let’s launch our rocket. rv is our rocket’s velocity, a speedy 35 kilometers per second. If you look at the value, pint expands it out to the full name of the units.

02:39 You can then use the .to() method on the velocity as well. ‘miles_per_hour’, “knots”, or you can even get crazy and make your own unit up as long as pint understands the distance and time units, the rest is just division.

03:00 So “parsecs / century” it is. One of the great things about pint is that it doesn’t let you do things you’re not supposed to. Distance values and velocity values shouldn’t be added together.

03:14 If you try to do so, pint throws a dimensionality error. That doesn’t mean you can’t do math though. If you want to know how long it would take for your rocket to make it to Jupiter, you divide the distance by the velocity.

03:31 Just over 19 million seconds. Okay? That puts our rocket at Jupiter. If you want to use Jupiter as a gravity assist, you’ll need some more info.

03:47 The distance from Jupiter to Saturn, again, just a sample number, the velocity of Jupiter itself, and now it’s time to apply the 2U gravity assist formula that I showed you before taking the original rocket velocity and adding two times Jupiter’s.

04:08 I’ve stored that away in gv. Now, let use the value to calculate the additional travel time to Saturn.

04:20 And so you get almost 33 million seconds. Of course, that’s a little hard to imagine so, in hours or days, or that’s just a little over a year.

Become a Member to join the conversation.