Brought to you by Michael and Brian - take a Talk Python course or get Brian's pytest book


Transcript #134: Python proves Mercury is the closest planet to Earth

Return to episode page view on github
Recorded on Monday, Jun 3, 2019.

00:00 Hello and welcome to python bites where we deliver python news and headlines directly to your buds. This is episode 134 recorded june 3rd.

00:09 2019 i'm michael kennedy and i'm brian akin this episode is brought to you by digital ocean.

00:14 I feel like i've been lied to i feel like the world is not as i expected it to be i was taught about planets first in school and first i was told there were nine.

00:23 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.

00:52 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:19 There's a nice video on this link, so I'm linking to an article called, I even did it wrong, so three scientists publish a paper proving that Mercury, not Venus, is the closest planet to the Earth using Python.

01:32 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 If 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:59 So Mercury wins.

02:00 - Wow, that's really wild.

02:02 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, yeah, absolutely.

02:32 - And they were linking the article and the video used a library called PyFM, I think, P-Y-E-P-H-E-M.

02:40 Apparently, according to Andrew Diedrich that contributed this for this show, has been largely deprecated and replaced with another library called Skyfield.

02:51 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 gotta say, one of the areas that sparks the most wonder and amazement in the use of Python is astronomy, for me personally.

03:06 - Yep, 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, 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, that's great.

03:23 Last week, I covered a couple of GitHub topics that I thought were really interesting, sponsors and the PandaBot, and I'm back this time this week with a slightly different theme that also intersects GitHub.

03:36 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 So the first thing I want to talk about is this library that was released called GitHub Semantics.

03:50 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 psycholimetic complexity by language, studying the top 100 repos or whatever, something like that.

04:14 You could answer that across Python, TypeScript, JavaScript, Ruby, Go, and some other languages as well.

04:20 - Weird, that's cool.

04:21 - Isn't that cool?

04:22 So it's not something in terms of digging into it that a lot of Python developers are gonna be super excited about 'cause it's written in Haskell, 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:37 interesting stuff 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. It has an abstract interpreter for generating scope graphs within given program text 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 in a strategic rewriting system for open syntax term.

05:06 So not a whole lot of stuff that I feel like I'm gonna be doing day to day, 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, is it written in Haskell?

05:20 - Yeah, it's written in Haskell, yeah.

05:22 - Okay, cool.

05:23 - Yep, and this one comes from Orin Carmy, so I just wanna 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:33 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:40 There's a flag called black dash dash check that will basically tell you if you ran it, this is what it would do.

05:48 I also use Flake 8 to check stuff, like to sort of lint my code and help me understand what I'm, actually, I like it that it doesn't, Flake 8 doesn't change your stuff, just tells you some information.

06:01 And so you can train yourself to write in a more consistent way.

06:05 So this is contributed by Nathan Clayton.

06:07 There's a Flake 8 Black plugin that can just run Black check within a Flake 8 environment and tell you if you ran Black, it would change this stuff along with all your other Flake 8 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 'cause Flake 8 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, if you're already hooking Flake 8 up in a pre-commit hook, you can do this as well.

06:41 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, 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:55 So this'll check it.

06:56 Very cool, that's a good one.

06:57 All right, 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 and they are doing all sorts of cool stuff.

07:10 You know, we use them for our infrastructure.

07:13 They have great virtual machine support, 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, but you don't wanna also become a DBA and manage your own database, your own backups and all that, you can just plug into their managed database service for Postgres over there, and that's one less thing to own as a puppy or to have to babysit all the time and patch and whatnot.

07:43 So definitely recommend checking them out.

07:45 They're doing cool stuff.

07:46 So visit them over at pythonbytes.fm/digitalocean and you'll get a $50 credit for new users.

07:52 Now, back onto my theme here for understanding languages.

07:56 If you look at the popular editors these days, Brian, we have PyCharm and we have VS Code as the two front runners, 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 what a lot of folks are trending towards.

08:16 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, 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 OK.

08:41 This is neat.

08:41 So what happens is you write some Python code.

08:44 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'll 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:12 - Yeah, it's really cool.

09:13 It's free because it's just part of VS Code.

09:15 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?

09:21 Like this is exactly the kind of stuff you're like, well, let's look at this code, and now I'm gonna go over and draw it out on the whiteboard.

09:27 Like, or you could just let this thing draw it perfectly.

09:30 You know what I mean?

09:31 So I think this would actually be really good for a intro language course, if you do any teaching or something like that.

09:38 - Yeah, I like it.

09:39 It reminds me of Python Tutor.

09:41 - 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:56 And the first one is Philip Guo's PythonTutor.com site.

10:00 So--

10:01 OK.

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 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:15 But yeah, it's pretty cool.

10:16 Yeah, neat.

10:17 Awesome.

10:17 All right, what's next?

10:18 Something we haven't talked about before, huh?

10:20 Packaging?

10:21 Packaging and poetry.

10:23 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, and then all the different settings within the toml, and what do they mean, and why you pick different ones.

10:49 And then carries you through to if it's something we're 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:58 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, does show you how to use the test server at PyPI to try everything out and make sure it works.

11:11 And then finally to publish directly to PyPI.

11:14 - Yeah, 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, 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, but yeah, this is the packaging part.

11:33 - Yeah, cool, maybe a follow-up article.

11:34 That'd be great.

11:35 Very nice one.

11:36 So the last one that I wanna 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:55 And it's called Python Pointers, or Pointers in Python, What's the Point?

11:59 So I mean, first of all, like the question is, like outside of C extensions, and maybe like inside the CPython runtime, 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, yeah.

12:17 If someone asked me, does Python have pointers?

12:19 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 a ampersand in front and then like plus plus it and 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, and generally a new person at Python and programming all together wouldn't even notice them and would say no, but I mean, like, names are all pointing to stuff, so.

12:53 - Exactly, 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 pass by reference, not pass by values 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 weird edge cases where the default value of an empty list in a function can be all sorts of badness for reasons like that.

13:26 So 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++.

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++.

13:45 In Python, you really don't.

13:46 everything, even numbers and Booleans and stuff are pointers, right?

13:50 - I didn't know that, but okay.

13:52 - Yeah, 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, you know, it really helps you, if you look, and the article goes through a lot of this, 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, each object, so everything, has a reference count.

14:16 has to keep track of. 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 this pi object that is pointing at is supposed to be an integer and it has a value. 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 it's many of those I don't know 20 25 28 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 yeah okay okay 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 and yeah this is good probably needed yeah I think people should read this who haven't deeply thought about this whole reference type scenario and what that means.

15:17 It's pretty cool.

15:18 My takeaway is Python has pointers.

15:20 All the variables in Python are pointers.

15:22 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 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 Pi object on the heap, sort of.

15:45 There was a Pi 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:56 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 Python startup.

16:13 - Okay.

16:14 - Right, so that's called interning.

16:15 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, so if someone else tries to create one of them, just, you know, don't reallocate it, 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 lookups.

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 (laughing)

16:48 - Okay.

16:49 - Okay, isn't that cool?

16:50 So basically it's an is versus a double equal, right?

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:02 Like much, much faster.

17:03 It's like are these two integers or longs the same versus I gotta go through like 20 characters and compare them.

17:10 - 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:17 So, I mean, like if you're not gonna keep it around and share it, you would basically be doing manually memory management stuff.

17:23 All right, so all of that is, I think, 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 pointer, that type of pointer, so how does that work, right?

17:44 So in-- - I don't know.

17:46 - Yeah, so in C++, we have inline assembly.

17:49 You can kinda drop down to a lower bit.

17:51 In C#, you have unsafe mode, 'cause C# has reference types just like Python, 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:06 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:12 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:23 - 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:34 One requires Cython, and one is just straight CPython.

18:37 All right, have you had enough language lessons for today, Ryan?

18:41 -Or you could just not worry about it so much.

18:45 -Exactly.

18:46 I do think it's worth thinking about this question and 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 plug-in or extension for VS Code, I think would also help with this.

18:58 Because it has little lines that are really the references and so on.

19:01 - So that's it for all of our topics today.

19:03 Anything else, any extra stuff you want to just throw out there real quick?

19:07 - Well, I was gonna talk about the PSF fundraiser, but you've got it on your list too, so.

19:12 - Yeah, it looks like I, in terms of writing it down, I beat you to it.

19:16 So I guess I'll say something about it.

19:17 So the PSF is doing a fundraiser.

19:20 I've linked over to it.

19:21 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:30 or you can just directly contribute to it.

19:32 So I've linked to the blog post that they just posted.

19:35 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:44 But they didn't raise enough money, so they've extended it till June 30th.

19:48 So please click on the link, donate something, and more importantly, maybe, try to get your company to donate, right?

19:55 If your company deeply depends on Python having a vibrant ecosystem, These are tax deductible if you pay taxes in the US, so definitely check it out.

20:03 - People don't know that 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:14 They do a lot of sponsorship and financial aid of different projects that should be funded but aren't otherwise.

20:20 So it's a good thing.

20:21 Donate to it.

20:22 - Absolutely.

20:23 All right, I think it's time for a joke.

20:25 - Yeah.

20:25 - Quick little one here at the end.

20:26 This one comes from Jay Miller, And the question is, what did the developer name his newborn son?

20:34 Of course, Jason.

20:36 Jason, that's right.

20:38 Oh, dear.

20:39 Oh, well.

20:39 Yeah.

20:40 Yeah, that's a good one.

20:41 Thank you, Jay and Brian.

20:42 Thanks for being here.

20:43 All right, thank you.

20:44 Bye.

20:44 Yep, see you.

20:45 Thank you for listening to Python Bytes.

20:47 Follow the show on Twitter via @PythonBytes.

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:56 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:03 On behalf of myself and Brian Okken, this is Michael Kennedy.

21:06 Thank you for listening and sharing this podcast with your friends and colleagues.

Back to show page