Transcript #134: Python proves Mercury is the closest planet to Earth
Return to episode page view on github00:00 Hello and welcome to Python Bytes, where we deliver Python news and headlines directly to your earbuds.
00:04 This is episode 134, recorded June 3rd, 2019. I'm Michael Kennedy.
00:11 And I'm Brian Ockin.
00:12 And this episode is brought to you by DigitalOcean.
00:13 I feel like I've been lied to. I feel like the world is not as I expected it to be.
00:18 I was taught about planets first in school, and first I was told there were nine.
00:22 And of course, I was told that Mercury was the closest to the sun, and then Venus, and then the Earth.
00:28 And yet, you're telling me Python here may be disproving this? What's the story here?
00:32 So which planet is closest to Earth?
00:35 Mercury. Wait, no, Venus, because it goes Mercury, and then Venus, and then Earth, and then Mars, and so on.
00:40 And the orbits are farther out, and if you look at it to scale, it looks like the orbit of Venus and the orbit of Earth are closer together.
00:49 Yes.
00:49 And that is true. However, they spend as much time the closest.
00:55 On average, if you take the average, how of the exact locations and the exact distance or exact ish, actually all of the planets spend more time closer to the sun than they do to each other, because that's just how that works.
01:10 And since Mercury is closest to the sun, it is on average closest to all the other planets.
01:18 There's a nice video on this link, so I'm linking to an article called, I even did it wrong.
01:24 So three scientists published a paper proving that Mercury, not Venus, is the closest planet to the Earth using Python.
01:31 It's a nice article about using the graph, and they graph it all out, and it's kind of neat that you just watch it, and you're like, oh yeah, well, of course.
01:41 As you watch these planets circle the sun, then you calculate the distance, and then you do an average.
01:48 It's close, so the average, Mercury is closest to the Earth 43% of the time.
01:54 Venus is closest to the Earth 37% of the time.
01:58 So Mercury wins.
02:00 Wow.
02:00 Yeah.
02:01 That's really wild.
02:01 I guess that makes sense, right?
02:03 Because all the planets are roughly, continuously the same distance from the sun, but they're often out of phase with each other, right?
02:11 They might be on opposite sides of the sun, which puts them super far apart, even though their orbits could actually be close together.
02:16 Yeah, and also the orbits that are far away from the sun, those take longer time, right?
02:22 So they're spending more time really far from the sun, and if you're really far on the other side of the sun from another planet, you're, yeah, it's just far.
02:30 Yeah, you're super far away.
02:31 Yeah, absolutely.
02:32 And they were linking the article and the video used a library called PyEphem, I think, P-E-Y-E-P-H-E-M.
02:41 Apparently, according to Andrew Diedrich, that contributed this for this show, has been largely deprecated and replaced with another library called Skyfield.
02:50 And I took a look at that, and it looks like a really fun library to easily get data about the solar system and stuff.
02:57 Yeah, that looks super cool.
02:58 I got to say, one of the areas that sparks the most wonder and amazement in the use of Python is astronomy, for me personally.
03:06 Yeah, it's neat to even have some of these little things of, like, an average person can go, wait, I don't think that's right, and just map it out using some Python.
03:16 Yeah.
03:16 And then you can come up with these different conclusions.
03:19 It's great.
03:20 Yeah, super cool.
03:21 Well, thanks for finding that one.
03:22 That's great.
03:23 Last week, I covered a couple of GitHub topics that I thought were really interesting, sponsors and Dependabot.
03:30 And I'm back this time, this week, with a slightly different theme that also intersects GitHub.
03:35 So I feel like I have a theme this week, and the theme is understanding languages, like the internals and the inner workings of languages better.
03:43 So the first thing I want to talk about is this library that was released called GitHub Semantics.
03:49 So GitHub, they have, as you know, many different programming languages and all sorts of cool analytics and reporting and whatnot.
03:57 So they built this library that will help them make comparisons across languages.
04:03 Like, if you wanted to ask the question of, what is the average psycholomatic complexity by language?
04:09 Studying the top 100 repos or whatever, something like that.
04:13 You could answer that across Python, TypeScript, JavaScript, Ruby, Go, and some other languages as well.
04:20 Weird.
04:20 That's cool.
04:21 Isn't that cool?
04:22 So it's not something in terms of, like, digging into it that a lot of Python developers are going to be super excited about because it's written in Haskell.
04:30 But you can also just use it as a command line tool for parsing and analyzing and comparing source code across languages.
04:36 Oh, neat.
04:36 It does some interesting stuff.
04:39 And, like, some of the things that you can do with it, or it has in it, I guess, is a flow-sensitive caching interpreter for imperative languages.
04:49 It has an abstract interpreter for generating scope graphs within given program text.
04:55 So if you want to compare scope across different languages, like, compare two different functions that are trying to do the same thing or something.
05:01 And then a strategic rewriting system for open syntax terms.
05:06 So not a whole lot of stuff that I feel like I'm going to be doing day to day.
05:10 But if you're in a computer science program, if you're trying to study languages or make generalizations or maybe even tooling, I think this would be pretty interesting to check out.
05:19 Yeah.
05:19 Is it written in Haskell?
05:20 It is.
05:20 Yeah, it's written in Haskell.
05:22 Yeah.
05:22 Okay.
05:22 Cool.
05:23 Yep.
05:23 And this one comes from Oren Carmi.
05:25 So I just want to say thanks for sending that over.
05:27 We always appreciate getting those ones passed along.
05:29 What do you got next?
05:30 Well, we've talked about black, I think, several times.
05:32 And I like using black.
05:34 One of the things that bothers me a little bit is having something change my code without me knowing about it.
05:39 Yeah.
05:40 There's a flag called black-check that will basically tell you if you ran it, this is what it would do.
05:47 I also use flake8 to check stuff, like to sort of lint my code and help me understand what I'm...
05:55 Actually, I like it that it doesn't...
05:57 Flake8 doesn't change your stuff.
05:59 It just tells you some information.
06:01 And so you can train yourself to write in a more consistent way.
06:04 So this is contributed by Nathan Clayton.
06:07 There's a flake8 black plugin that can just run black check within a flake8 environment and tell you if you ran black, it would change this stuff along with all your other flake8 checks.
06:20 I think this is pretty handy, and I'll try to include this into my workflow.
06:24 Yeah, and that probably makes it pretty easy to plug in to continuous integration pipelines,
06:28 because flake8 may already be part of some continuous integration check, right?
06:32 A lot of people are hooking black into a pre-commit hook, which is great.
06:36 You can also hook...
06:37 If you're already hooking flake8 up in a pre-commit hook, you can do this as well.
06:40 But having it, you can still slip stuff in and merges might muck things up.
06:45 So yeah, this is cool.
06:46 Yeah, yeah.
06:46 Super cool.
06:47 And you can't necessarily be sure everyone is running all the same tooling or whatever, right?
06:53 So maybe they've changed it somehow.
06:54 So this will check it.
06:56 Very cool.
06:56 That's a good one.
06:57 All right.
06:57 Now, before we get to the next one, I want to tell you all about DigitalOcean.
07:00 They're sponsoring this episode, as they do many of them, and make this show certainly possible in some way.
07:06 So thank you to them.
07:07 And they are doing all sorts of cool stuff.
07:10 You know, we use them for our infrastructure.
07:12 They have great virtual machine support.
07:15 But one of the things they recently rolled out was managed Postgres servers or as a service.
07:21 So if you want to create something on Linux and get it up and running fast, affordable, simply,
07:27 but you don't want to also become a DBA and manage your own database, your own backups and all that,
07:32 you can just plug into their managed database service for Postgres over there.
07:36 And that's one less thing to, you know, own as a puppy or to have to babysit all the time and patch and whatnot.
07:42 So definitely recommend checking them out.
07:45 They're doing cool stuff.
07:46 So visit them over at pythonbystat.fm slash digitalocean, and you'll get a $50 credit for new users.
07:51 Now, back onto my theme here for understanding languages.
07:55 If you look at the popular editors these days, Brian, we have PyCharm and we have VS Code as the two, like, frontrunners, I would say, these days.
08:04 It's kind of like if you want a big ID that's going to do everything for you, PyCharm.
08:08 If you have a lighter weight preference, then maybe VS Code seems to be like what a lot of folks are trending towards.
08:15 So there's this cool extension for VS Code called Python Preview.
08:21 If you're a computer science student or if you're getting into Python and you want to have a good understanding of what's happening as you're writing code,
08:30 I think using this extension is awesome.
08:32 So click on the link there and just look at the little picture that gets drawn if you scroll down to the second picture or something like that there.
08:40 Okay.
08:40 This is neat.
08:41 So what happens is you write some Python code.
08:43 You maybe create a list, and then you're going to loop over it, and you're going to change the variable or something like that.
08:48 And it will create a visualization of what the objects look like in memory, what the call stack looks like, the global frame is, and all that.
08:57 And it will actually show all the pointer references between the variables and how they're changing and what type they are.
09:03 All sorts of interesting stuff as it just sort of, like, explains what your code is doing visually if you were to run it.
09:10 Oh, this is really cool.
09:11 I like it.
09:11 Yeah.
09:12 It's really cool.
09:12 It's free because it's just part of VS Code, so that's really nice.
09:16 So if you're either a student or trying to learn the language or if you're a teacher, right, like, this is exactly the kind of stuff you're like, well, let's look at this code, and now I'm going to go over and draw it out on the whiteboard.
09:27 Like, oh, you could just let this thing draw it perfectly, right?
09:30 You know what I mean?
09:31 Yeah.
09:31 So I think this would actually be really good for an intro language course if you do any teaching or something like that.
09:37 Yeah.
09:38 I like it.
09:39 Yeah.
09:39 It reminds me of Python Tutor.
09:40 It should remind you of Python Tutor because if you look at the GitHub repo for this extension, it actually says, I would like to thank a couple of projects for at least conceptually inspiring me.
09:53 I don't know that there's any code sharing or anything like that.
09:55 And the first one is Philip Guo's pythontutor.com site.
10:00 Okay.
10:01 Well, that makes sense.
10:02 Yeah, it definitely makes sense.
10:03 If you've played with Python Tutor before, that's a place to go explore Python, and you type it in there.
10:08 And what's cool about this is, like, it just explores the code that you have open in your editor in a similar way.
10:13 You can't step through it, I don't believe.
10:14 But, yeah, it's pretty cool.
10:16 Yeah.
10:16 Neat.
10:17 Awesome.
10:17 All right.
10:18 What's next?
10:18 Something we haven't talked about before, huh?
10:20 Packaging?
10:20 Packaging and poetry.
10:22 This is a nice article called Create and Publish a Python Package with Poetry by John Franey.
10:29 There's been, occasionally people will say poetry is more for projects or something.
10:34 But this is dealing with a Python package.
10:36 And it walks through creating a new project and then customizing the pyproject.toml file.
10:44 And then all the different settings within the toml and what do they mean and why you pick different ones.
10:48 And then carries you through to, if it's something you were going to share with the rest of the world.
10:53 I mean, one of the great things about all this is you don't have to share your code with the world.
10:57 You might be just sharing it with your team members or just even making modularizing your own stuff.
11:04 But if you do want to share it, it does show you how to use the test server at PyPI to try everything out and make sure it works.
11:10 And then finally to publish directly to PyPI.
11:14 Yeah.
11:14 It looks like a really nice little write-up.
11:16 And yeah, it's just typing poetry space publish.
11:20 That's not terrible.
11:22 Yeah.
11:22 It doesn't talk about testing or integrating testing and talks into a project, which, and pytest, of course.
11:29 But so that maybe that's an extension.
11:31 But yeah, this is the packaging part.
11:33 Yeah, cool.
11:33 Maybe a follow-up article.
11:34 That'd be great.
11:35 Very nice one.
11:36 So the last one that I want to cover is a realpython.com article by Logan Jones.
11:41 We got to hang out with him a little bit at PyCon.
11:43 That was fun.
11:44 But this one is, I think, an interesting sort of thought-provoking one about Python and the language.
11:52 So I think there's a lot to learn here for many folks.
11:54 And it's called Python Pointers or Pointers in Python, What's the Point?
11:58 So, I mean, first of all, like the question is like outside of C extensions and maybe like inside the CPython runtime.
12:06 But, you know, in Python, the language itself, does Python even have pointers, right?
12:11 Like, what do you think?
12:12 How would you answer that question if somebody asked you that?
12:14 It's mostly hidden from users.
12:16 Exactly.
12:17 Yeah.
12:17 If someone asked me, does Python have pointers?
12:18 I would say, yeah, of course it has pointers.
12:20 But it really hinges on whether or not you define that as I can put an ampersand in front and then like plus plus it and move in the, you know, move along in like a string or move along in an array.
12:31 Like, can I do pointer arithmetic and can I actually work at that level?
12:35 Or is it I have variables that point out to things on the heap like pointers, right?
12:41 Like, which of those has to pass the bar to be like it's a pointer, right?
12:44 Yeah.
12:45 And generally, a new person to point Python and programming altogether wouldn't even notice them and would say no.
12:50 But, I mean, like names are all pointing to stuff.
12:53 Exactly.
12:54 So, what's really interesting is that could be a big misconception, right?
12:58 Because if you think what you're passing around are values, but you actually have pointers, anytime you change one of those things, you're changing it everywhere, right?
13:06 You have passed by reference, not passed by value semantics.
13:10 And so, if you think I can pass this over, that thing can change it and it doesn't affect me like a normal value would, right?
13:16 Like, whoops, that's a bit of a problem, right?
13:19 We've seen like weird edge cases where like the default value of like an empty list in a function can be all sorts of badness for reasons like that.
13:26 So, I don't know.
13:26 It's pretty interesting.
13:27 I think actually Python is more pointer heavy than most languages, more so than C#, more so than C++, right?
13:37 Because in those languages, you at least have the option to define stuff on the stack and define value types like integers versus in pointers in C++, right?
13:45 In Python, you really don't.
13:46 Everything, even numbers and booleans and stuff are pointers, right?
13:49 I didn't know that, but okay.
13:51 Yeah.
13:52 So, like true is a pointer out on the heap.
13:55 The number seven is a pointer on the heap.
13:57 So, what it means is like all sorts of, you know, it really helps you.
14:00 If you look, and the article goes through a lot of this.
14:03 If you look deeply at like the memory structure of what you get, you know, each reference type, which I'll call them, you know, thing in Python.
14:12 Each object, so everything, has a reference count it has to keep track of.
14:17 It has a type, like so, in the pi object that actually gets allocated when you say the number one or whatever, that has actually a field up there that says this is an integer right now.
14:32 This pi object that it's pointing at is supposed to be an integer, and it has a value.
14:36 So, if you think of, I've got this simple little piece of data, like the number one or the character C, it's not just one byte or four bytes or whatever.
14:46 It's many of those.
14:48 I don't know, 2025, 28.
14:50 I'm not sure exactly what the number is, but there's a lot of stuff coming along with that, what would otherwise look like a really simple little value.
14:57 Yeah, okay.
14:58 Okay.
14:58 There's some nice drawings in there about like that names are pointing to objects, and objects all have types and values and reference counts.
15:06 And yeah, this is good.
15:08 I probably need to.
15:09 Yeah, I think people should read this who haven't deeply thought about like this whole reference type scenario and what that means.
15:16 It's pretty cool.
15:17 My takeaway is like Python has pointers.
15:20 All the variables in Pythons are pointers, but they're like these safe reference type, wrapper type things that you don't directly have to worry about, allocate, whatever, right?
15:29 Yeah.
15:29 So, pretty cool.
15:30 There's one interesting little thing that I saw in the article that Logan talked about, and that was about interning objects.
15:38 So, when I said earlier before, when I say the number one, it actually allocates a Py object on the heap, sort of.
15:45 There was a Py object allocated on the heap that corresponds to one, but the first 256, something like that, numbers are what are called interned, and so are certain strings.
15:55 And that means like if you allocate a one over here and you allocate a one over there, those are actually reusing the same object, right?
16:04 So, they pre-allocate like the basic numbers and strings, and then you just change where your pointer points.
16:11 Like that's just part of like Python startup.
16:13 Okay.
16:13 Right?
16:14 So, that's called interning.
16:14 And the interesting bit is that you can manually intern things.
16:19 Like I can go to the system and say, this thing, I want to intern it.
16:22 So, if someone else tries to create one of them.
16:24 Oh, really?
16:24 Just, you know, don't reallocate it.
16:26 Just share this sort of global one-off thing I've created.
16:30 And so, it says you can intern strings, and this can be useful for the performance on dictionary lookedups.
16:36 Because if the strings that are the keys of the dictionary are interned, then a dictionary lookup is a pointer comparison versus a string comparison.
16:47 Okay.
16:49 Okay.
16:49 Isn't that cool?
16:50 Yeah.
16:51 So, basically, it's an is versus a double equal, right?
16:54 Okay.
16:54 And if you've got a big dictionary and you're like going crazy on it and the keys are kind of complicated, that could potentially make it quite a bit faster.
17:01 Like much, much faster.
17:03 It's like, are these two integers or longs the same versus I got to go through like 20 characters and compare them?
17:09 Well, so, there must be some reason why we don't just intern everything.
17:12 If you intern it, it won't expire.
17:14 It won't be cleaned up.
17:15 Oh, okay.
17:16 Yeah, yeah.
17:16 So, I mean, like if you're not going to keep it around and share it, you would basically be doing manually memory management stuff.
17:22 All right.
17:23 So, all of that is, I think, is super interesting and worth people checking out.
17:26 But now the question comes down like, can I have ampersand X?
17:30 Can I get my actual pointer or star X, right?
17:33 Can I do real pointer stuff in Python?
17:37 And, of course, Python has to interact with C, and C functions often take this type of pointer, that type of pointer.
17:42 So, how does that work, right?
17:44 So, in C++, we have like inline assembly.
17:49 You can kind of drop down to a lower bit.
17:50 In C#, you have unsafe mode because C# has reference types just like Python.
17:55 But there's a way to say, turn that off and let me actually do pointer arithmetic on it.
17:59 So, the question is, can we have that in Python?
18:02 And actually, if you use Cython, you can.
18:05 Like in Python code, you can say, this is a pointer, and you can dereference its address by saying ampersand and stuff like that.
18:11 That's one way.
18:13 And you can also work with the C types library.
18:15 So, I can create like an integer pointer by allocating a C type and then pass it around and share it and stuff.
18:22 Okay.
18:24 So, yes, if you need to dig down and you need to like truly simulate what pointers really, really are, you can actually do that in Python as well.
18:32 And there's two ways to do it there.
18:33 Okay.
18:34 One requires Cython and one is just like straight CPython.
18:37 All right.
18:38 Have you had enough language lessons for today, Ryan?
18:41 Or you could just not worry about it so much.
18:43 But, okay.
18:45 Exactly.
18:45 I do think it's worth thinking about this question and like what it looks like in Python if you haven't thought about it before.
18:52 And I think the thing before, the preview Python plugin or extension for VS Code, I think would also help with this.
18:58 Yeah.
18:58 Yeah, because it has the little lines that are really the references and so on.
19:01 Yep.
19:01 So, that's it for all of our topics today.
19:03 Anything else?
19:04 Any extra stuff you want to just throw out there real quick?
19:06 Well, I was going to talk about the PSF fundraiser, but you've got it on your list too.
19:11 So.
19:12 Yeah, it looks like I, in terms of writing it down, I beat you to it.
19:15 So, I guess I'll say something about it.
19:17 So, the PSF is doing a fundraiser.
19:19 I'm linked over to it.
19:20 You should definitely check it out.
19:21 On the previous episode, we also sort of talked about that you can buy a PyCharm license and some of that money, like 30% or something, goes towards the PSF.
19:29 Or you can just directly contribute to it.
19:32 So, I've linked to the blog post that they just posted.
19:34 Now, the deadline was supposed to be June 1st, I believe, but they didn't raise enough money, which is mind-blowing to me.
19:43 But they didn't raise enough money, so they've extended it until June 30th.
19:48 So, please click on the link, donate something, and more importantly, maybe try to get your company to donate.
19:55 Right?
19:55 If your company deeply depends on Python having a vibrant ecosystem, these are tax-deductible if you pay taxes in the U.S., so definitely check it out.
20:03 People don't know the PSF money goes to help fund workshops and conferences, and they help pay meetup fees around so that a lot of the local Python meetups don't have to pay their own fees.
20:13 They do a lot of sponsorship and financial aid of different projects that should be funded but aren't otherwise.
20:19 So, it's a good thing.
20:21 Donate to it.
20:22 Absolutely.
20:22 All right.
20:23 I think it's time for a joke.
20:24 Yeah.
20:25 Quick little one here at the end.
20:26 This one comes from Jay Miller.
20:28 And the question is, what did the developer name his newborn son?
20:34 Of course, Jason.
20:35 Jason.
20:36 That's right.
20:37 Oh, dear.
20:39 Oh, wow.
20:39 Yeah.
20:40 Yeah, that's a good one.
20:40 Thank you, Jay.
20:41 And Brian, thanks for being here.
20:43 All right.
20:44 Thank you.
20:44 Bye.
20:44 Yep.
20:45 See ya.
20:45 Thank you for listening to Python Bytes.
20:47 Follow the show on Twitter via at Python Bytes.
20:49 That's Python Bytes as in B-Y-T-E-S.
20:52 And get the full show notes at pythonbytes.fm.
20:55 If you have a news item you want featured, just visit pythonbytes.fm and send it our way.
21:00 We're always on the lookout for sharing something cool.
21:02 On behalf of myself and Brian Okken, this is Michael Kennedy.
21:05 Thank you for listening and sharing this podcast with your friends and colleagues.