Python Bytes - #131 Python 3 has issues (over on GitHub)

Episode Date: May 21, 2019

Topics covered in this episode: * PEP 581 (Using GitHub issues for CPython) is accepted* Things you’re probably not using in Python 3 – but should The Python Arcade Library Teaching a kid to co...de with Pygame Zero Follow up on GIL / PEP 554 Extras Joke See the full show notes for this episode on the website at pythonbytes.fm/131

Transcript
Discussion (0)
Starting point is 00:00:00 Hello and welcome to Python Bytes, where we deliver Python news and headlines directly to your earbuds. This is episode 131, recorded May 15th, 2019. I'm Michael Kennedy. And I'm Brian Atkin. Hey Brian, how you doing? I am good. How are you? I still have a little bit of a conference hangover. Two conferences in a row and then some parties definitely took it out of me. How about you?
Starting point is 00:00:20 Oh yeah, you went to Build afterwards. How was that? It was good. I did a bunch of podcasting, met some other i got to see what like a a different type of conference than pycon looked like and uh yeah it's pretty different okay so anybody out there from build listening you can invite me next year that'd be just fine yeah absolutely absolutely so uh yeah it was fun it was fun to be there and it's but it's good to be back home from all the conferencing yeah it is so let's get back into python let's get into it so before we do though i want to say thank you to digital ocean check them out at pythonbytes.fm slash digital ocean more about that later uh i feel like we kind of have a little bit of a a pep roundup for this episode brian we got at least three peps yeah
Starting point is 00:00:58 we got at least three peps making an appearance maybe four okay well the ball is rolling now with the uh the new steering council in in place and uh they're kicking butt and taking names and it's great so we'll start off with um i just saw this today pep 581 is accepted and that is the github issues for c python so c python is has traditionally had its own i think it was its own custom-made defect tracking system or something. It hasn't switched yet, but it will be switching to GitHub issues. We'll link to the PEP, and also it has discussion of pros and cons, but it has been accepted. But the follow-on PEP is 588, and I'll quote Barry Warsaw here. It's, the migration will be a large effort with much
Starting point is 00:01:45 planning development and testing and we welcome volunteers who wish to help make it a reality i look forward to your contributions on pep 588 and the actual work of migrating issues to get so 588 is okay now that we've decided to do it how are we going to do it how are we going to do the migration and once they figure that out they'll probably need some help to do it? How are we going to do the migration? And once they figure that out, they'll probably need some help to do it. That's cool. And you spoke to Barry on our episode, our live episode at PyCon, about becoming a core developer and ways you can help.
Starting point is 00:02:14 And here it sounds like another way you can contribute to CPython without actually writing C code potentially. Yeah, I'm not afraid of C code, but I know, I mean, especially in the Python community, there's some people that aren't involved with C code. So there's I'm not afraid of C code, but I know, I mean, especially in the Python community, there's some people that don't, aren't involved with C code. So there's other places, ways to help.
Starting point is 00:02:29 And this is one. Yeah. Yeah. And it's, it's one thing to write C code. It's another to write on the core of C Python itself. Right. I mean,
Starting point is 00:02:36 that's like, it's a super highly polished piece of software and any change you make is like massive ramifications. So, right. I can see depends on it no pressure you're going to take down the instagram influencers if you mess this up so don't yeah maybe maybe some science and other important things as well i think this is
Starting point is 00:02:58 good you know um brett can i was key in getting c python the source code over to github originally and i feel like this is well overdue, right? Having the issues there means you can reference them in check-ins. It means that you get that integration for pull requests and all sorts of stuff. It just makes a lot of sense to do this. Yeah, and I have no idea what the migration path looks like. Yeah, I was wondering, are they going to copy every single issue across?
Starting point is 00:03:33 Or is this a chance to kind of clean house and go, well, if this bug comes back, it's important enough to worry about. If it doesn't, maybe it's not. I've been on projects like that, that we've gone to a different bug tracking system where we just said, well, we'll just leave the other one around, and if anybody really cares about moving them over um we will but those sometimes cleaning up is a good thing yeah i totally agree so how about cleaning up some code yes let's clean up code so i'm a big fan of design patterns in general the solid principles all these things i really enjoy like thinking about how that influences code and one of the things that makes me crazy when I read code is like ultra nested, indented conditional stuff. And when I see it, I feel like people write code like that because they don't believe there's another way. It's just like, well, this code is complicated. So it's like indented, you know, 16 spaces or whatever it happens to be indented as right like it doesn't have to be this way most of the time so i just wanted to like call out a super easy to implement design pattern called guard clauses or guarding clauses okay okay so this is if you have like a nested stuff i have two little examples like a bad one and a good one here in our show notes and people can check that
Starting point is 00:04:43 out and it's like a little checkout for a user. So a user, they've got a shopping cart. They've got some items in there. Some of them are available. Some of them aren't. Some of them are selected to be express shipped. Some of them are not. And things like that.
Starting point is 00:04:56 And so it's got like, if user is not none, go through their carts. If the item is available, add it. In addition to that, if the item is selected to be express shipped at it, and that's like just, what is that? A 30, 30 degrees down, 45 degrees down line of code, right? It's not vertical. It's like, you know, at an angle, probably 45, right? And every one of these is asserting a positive thing. The thing I want, if the user is good, I want to go through them. If the thing is available, I want to do this, right? And so on and so on. Guarding clauses basically check for the negative and bail out as soon as possible. So you could rewrite that and go, if the user is none,
Starting point is 00:05:32 return empty stuff for their stuff. If it's not available, just skip through the loop with a continue and so on. And it's much simpler. And it's not just about visual code. It's not just like easier to read, but it's easier to reason about. Like if you get into one of these super nested conditional structures, then it's really hard to think about like, okay, I want this case and that case, like, where does that go? Do I need another branch in this if, and so on. I feel like it's much easier to maintain and add to with these garden clauses. And in the example you're showing, and we're going to have in the show notes, the better one actually ends up being more lines of code. However, you're visually going to skip
Starting point is 00:06:10 over the top part because you're like, oh, I'm just making sure that things are the right. And then in the middle, I've got highlighted just like three lines of code where the actual work is and highlighting where the real work is instead of dispersing the real work across lots of if clauses. I think that's a great idea. Yeah, thanks. I super love it. I'm linking to Martin Fowler's original little article on it, which is like C or JavaScript or something. But it's if statements. It's basically the same idea.
Starting point is 00:06:36 As well as to one from a Go Medium article about line of sight programming, talking about like you can just see right down the line. Anyway, it's all pretty cool. I definitely, definitely think how those can be used. I find it to make it a lot nicer, a lot easier to maintain this code. It'll also reduce the cyclomatic complexity of your code. Yes, yes, it will.
Starting point is 00:06:58 And that also probably, you know, that's cool because it'll make Anthony Shaw happy and his Wiley, but it'll... But it also means, like, it's cool because it'll make Anthony Shaw happy and his Wiley. But it also means like it's less cognitive overhead to maintain, right? You can say, okay, I cleared out the stuff that's not good. Now we're in the good spot. It's not indented. It's not a lot going on. You're right.
Starting point is 00:07:16 It's so simple that I don't, you know, if you're not using it, just check it out. It's great. Good reminder. Speaking of if you're not using it. Yeah. So Python 3 is a thing. We've talked about it a few times in the past. There's probably a lot more people.
Starting point is 00:07:30 Well, I don't know. There's probably more people, some people now still converting to Python 3 or starting to get used to it. The easy hurdles are just to start not using the stuff you can't anymore and some of the string changes and whatever. But here's an article I ran across called Things You're Probably Not Using in Python 3 But Should. So there's a lot of new items in Python 3, but this article goes through a handful. So there's some obvious, I think obvious, of course, items like fstrings, pathlib, and maybe data classes. I'm using them a lot, but maybe Fstrings and Pathlib, definitely everybody should be using those.
Starting point is 00:08:09 Those are awesome. I'm warming to type hinting a little bit more. I think it's feeling more natural. I'm not really a zealot about it, but when I start trying to see myself start typing a comment to say what kind of stuff should go into a function or what should be in a variable i'm like i could just put a type hint on there that exactly yeah exactly yeah comments are
Starting point is 00:08:31 deodorant right they go on to bad code to make it smell better but maybe you should just make it better by putting yeah type it maybe type it yeah so especially when it's i'm only intending it for like the the reader i'm not using some testing tools around that. But there's some stuff that I knew about that I just kind of forgot. So I'm glad that I'm going to list three that he listed. Enumerations with the new enum package and enum and auto methods and classes. Yeah, I love enums. They're really great.
Starting point is 00:09:01 I don't use them enough, but, yeah, they're super. Enough. I want to use them a little bit more after this reminder. Also, LRU Cache is built in. It's a decorator in FuncTools that you can use really easy memoization. So if anybody's not familiar with LRU Cache or memoization, the gist of it is you just throw this decorator on a function. If it's really functional programming type stuff
Starting point is 00:09:26 where you pass in some value and that has no behavioral side effects and it just returns some value that's a one-to-one correlation between input and output and it's called a lot, you can use memoization with LRU cache to speed it up and it just remembers the old And it just, it just remembers the old stuff. Yeah. It just says like, if I see this argument come in, say you have a number in your example, like if the number 72 comes in and the answer was 7,000, the next time it's called, it just goes, that argument is 72. We know it's not changed. The answer is going to be 7,000.
Starting point is 00:10:00 Anytime you have a function that basically, you know, is deterministic, you give it the same input, it gives you the same output. This is a super good option. Yeah, especially if it's something that's like time consuming, it has to do some data crunching or something. Yep. The last one I'm going to highlight that I totally forgot about is extended iterable unpacking. And this one, you kind of have to see to get it. But basically, you can, when you like, for instance, if you're unpacking a list with a three element list, you can assign it to three variables. We know that. But if you have more than three, you can put a star on one of those things and it'll catch all of the rest. And that's cool.
Starting point is 00:10:40 There's lots of places that I could use that that I forgot about. You know, you can even do the star in the middle, right? You've got like head, star, body, and tail for five things, and the body is three things. Pretty awesome. Yeah. When I look at this and I think about it, you know, obviously there's a lot of people moving to modern Python
Starting point is 00:10:56 and using it in their code, right? But just like you could come from C and write non-Pythonic Python, you could move to Python 3 and write non-Python 3-ic? I don't know. You could write code that's not idiomatic to Python 3. You still do everything the Python 2 style, not taking advantage of any of these things. type hinting async and await you know like you said enums you could like do none of that and still be quote using python 3 so i think it's cool a cool reminder an example of things you could do to be more idiomatically python 3 yeah actually when i was thinking about that the author of this even says some of his old articles are written essentially for python 2 and i think that's a great place for people if they were if they're new to they want to start doing technical blogging
Starting point is 00:11:45 and they don't really have some ideas on what to do. You can go look at some common and popular articles that are written in Python 2 style and kind of make them your own and do a similar article. Don't copy it, but do a similar article with Python 3 syntax and you'll probably get some hits. Yeah, for sure. Or even if you really love that resource you're reading,
Starting point is 00:12:07 you could send them a note like, hey, I'd love to upgrade these three articles to Python 3. Could I help you? Oh, yeah, that would be much nicer. Do that. Yeah. Well, they both have their own merits, right? Yeah.
Starting point is 00:12:18 Speaking of merits, let me tell you about Digitalution. Digitalution is powering all of our things, which is awesome. And we talked about GitHub at the beginning. One of the big things GitHub is doing is GitHub Actions. So kind of automated workflow for things that happen, you know, check-ins and other stuff on GitHub. And DigitalOcean has a GitHub Actions for DigitalOceans that you can install and plug in there.
Starting point is 00:12:42 So you can take your workflow that's happening on GitHub and automatically use that to do things like create a new virtual machine or push a new version to a Kubernetes, manage Kubernetes cluster based on a push or something like that, right? Or maybe snapshot it when an issue is filed, who knows? But all sorts of cool stuff you can do with GitHub Actions
Starting point is 00:13:03 and DigitalOcean mixed together with that. So check that out at pythonbytes.fm slash DigitalOcean. Get $100 free credit for new users. Definitely can recommend them. They're doing good stuff. So how about some fun, Brian? Yeah, we got a couple of fun ones. Yeah, yeah. Let's play a few games here. It's awesome that these came up just right by each other. So I want to talk about this thing called the Python Arcade Library, which is at arcade.academy, which I didn't even know.academy was a top-level domain, but apparently it is. That's pretty awesome. So this is a library for easily building 2D games in Python that run at like 60 frames a second on OpenGL. Oh, neat.
Starting point is 00:13:48 That's pretty cool, right? So it's really about, like this is by a guy, I believe his name is Paul. Paul, hopefully I'm getting that right. He built this so to help teach, he teaches at a college and it helps teach his students a more visual way to learn programming.
Starting point is 00:14:03 So it's not to teach game development, it's to teach programming, but because it's got a visual aspect and not just like a terminal version you can you know see what you're creating more easily and see see it working so that's pretty cool you can create like minesweeper games hangman games and in particular platformer games right so if you wanted to recreate lemmings or he wanted to do like Joust or whatever, you could totally use this for that. Ooh, neat. Yeah, and you can check out the sample games made with that. So there's like a tower defense game.
Starting point is 00:14:33 I'm a sucker for tower defense. There's like a little angry bird thing made. There's all sorts of fun stuff. There's tons of examples here. I think mostly because these are students who are taking the class but then submitting their projects oh that's a great idea yeah nice right yeah and i i like these uh 2d games i do too yeah it also includes things like physics which is nice because it's one thing to get the graphics on the screen but hit detection physics sound all these other things are super hard and i believe sound is still
Starting point is 00:15:02 like a little bit of a iffy feature here. I hear that that's tough in Python. So maybe that's like a cool C extension somebody should write. I don't know. But anyway, it's all based on OpenGL, and it looks pretty cool. So definitely want to recommend that people check that out. Yeah. And you guys, similar one. How about that?
Starting point is 00:15:20 Yeah. So I wanted to highlight an article called Teaching a Kid to Code with PyGameZero, written by Matt Lehman. So this is just this guy with his kids. He likes to play video games with his kid. And thought, you know, I should try to teach him how to code. And they tried. His son did like a version of Scratch. But the worry is that Scratch is really far removed from actual coding and that skills you build might not be transferable really easily.
Starting point is 00:15:49 So I went ahead and decided to try Pygame Zero using the Mew editor. And I guess Pygame Zero is already pre-installed in Mew, as is Python Interpreter. I didn't know that. That's cool. Yeah, that's great. And so a quote from somewhere, Pygame Zero is intended for use in education so that teachers can teach basic programming without needing to explain the Pygame API or write an event loop. He worked with his son and they came up with a 29 line of code, including blank spaces,
Starting point is 00:16:26 little game doesn't really do much, but it teaches a lot. So he said that my, that his son learned about naming things and variables and mutability and fiddling with constants to see how those are affect the screen size and stuff like that. Red writing functions that have side effects and interacting with mouse events.
Starting point is 00:16:44 So he learned quite a bit with that with just this little bit of code, and it's actually Python. So that's kind of neat. And then one of the things I really like, because I do want to start, I haven't worked with my kids with coding yet, but the article also includes some tips on how to behave as the adult when you're working with kids with coding. So this is good.
Starting point is 00:17:03 Yeah, it's truly tricky to set up the right balance of it's interesting, but like quickly becomes too hard or it's easy enough to get kids started, but it's, you know, they have expectations of something they can do with their iPhone as a game. And what you build does not necessarily match. Right. Yeah.
Starting point is 00:17:22 And I'm, I like the tips at the end because I'm one of those kind of people that would just say, okay, you just sit at the side and I'll do it and you watch. It's not really teaching. Yeah, true, true, true. It's cool though.
Starting point is 00:17:33 I think these are both really fun options for teaching kids programming and building a little game because not everybody cares about building games, but a lot of people who do, like a lot of people's introduction to programming was they wanted to build a game. Programming was just what was required to make that happen, right? And you can go do something else like a useful utility that somebody might need.
Starting point is 00:17:54 But, I mean, how many six-year-olds really need a useful utility written in Python? Yeah, true, true, true. Anyway. Cool. While we're talking about games, like if kids are not quite ready for a game but they're ready to do adventurous stuff i guess i want to throw a shout out to codecombat.com that's a super cool place it has a free version where you basically go into these dungeons and you solve the dungeon by writing python programs oh neat and the editor has like autocomplete like
Starting point is 00:18:24 nobody's business it's super super nice so you have to like have your hero move around you say like hero dot attack and select like an enemy if you type the letter a it'll auto complete hero dot attack i mean it's like really really beginner friendly so definitely uh you know that's maybe a first step and then like one of these two that we just spoke about would be really good as well i'll try that yeah i was doing that one with my daughter and she was super into it. She got like 50 levels in or something.
Starting point is 00:18:48 50 little dungeons. Cool. So, all right. So last one, let's round it up with something a little more serious. So this is, we talked before about whether or not the gill will become obsolete with the introduction of PEP554.
Starting point is 00:19:03 And this was on episode 128, which is cool. So Anthony Shaw wrote a cool article called, Has the Python Guild Been Slain? That really digs deeply into that idea that we were touching on back then. So the answer is kind of, maybe, I don't know, probably, but for a limited case. So we've got a multi-threading in Python, which is pretty easy, but it's not actually concurrent because of the GIL. We have multi-processing, which is harder to exchange data and stuff.
Starting point is 00:19:33 It carries a lot of overhead, but does escape the GIL because it's all these separate processes, right? So PEP554 introduces this idea of sub-interpreters. Remember that from back then, Brian? Yeah. And we speculated that maybe the ability to have multiple sub-interpreters. Remember that from back then, Brian? Yeah. And we speculated that maybe the ability to have multiple sub-interpreters would remove the problem of the GIL,
Starting point is 00:19:51 because the GIL is not a process thing. It's an interpreter thing. It's a global interpreter lock. So if we just take and run our threads on multiple sub-interpreters, there would be no GIL. Things would go potentially faster, at least some of the time, right? The problem is, if you actually try to share data across those sub interpreters, things get pretty tricky. So Anthony's article really digs into that and talks about how you might use like shared memory and IPC, which is also another feature coming to multiprocessing,
Starting point is 00:20:22 but that's also kind of slow and challenging. So he highlights another PEP. So here's our fourth PEP, PEP 574, which proposes a new PICL protocol. Is that surprising? Yeah, a little bit. A little bit, because like people say, don't use PICL. It has all these vulnerabilities and versioning issues and whatnot,
Starting point is 00:20:41 except for it's a nice binary format. And if your goal is to just literally exchange data from sub interpreter to sub interpreter through shared memory, well, then like, that's fine. That's like within the runtime of a process, it should be okay, right? You're not going to hack yourself. And if you do, you deserve it. Yeah. So this is a special protocol version five that has support for allowing memory buffers to be handled separately for rest of the pickle stream basically so all these things could be combined together to get us a cool faster more concurrent python and he answers the question when when will maybe these
Starting point is 00:21:17 things be here so pickle version 5 and shared memory will probably make it to python 3.8 which will be october of this year 2019. And sub-interpreters may make that, but they might take another version 3.9, which I believe at the time, that's going to be 18 months later. I think we're still on the 18-month release cycle here. Yeah. What I really appreciate about this article isn't that there's suddenly stuff that I can use now. It's that we have been talking about the sub-interpreters, and I haven't really got my head around really how that affects us, and I think this is a good jump in, a good discussion about it, so that if you're curious about this,
Starting point is 00:21:56 he talks about all the kind of the backstory and kind of where we're going from here. The where we're going from here, I think, is still kind of up in the air. I would like to see something more around like a shared memory. One of the discussions is having shared memory objects that are owned by one subinterpreter at a time. So you could say, well, I'm creating this data to have another interpreter take it over. Right, I'm handing it off to you. You can have it now.
Starting point is 00:22:23 Yeah, and it would be nice if I didn't have to pickle that. If I could just, the data I'm creating is just normal data, but it happens to live in an area that I can hand over to a different process or something. Oh, that's interesting. Basically dereference it in the current garbage collector
Starting point is 00:22:39 system and re-reference that information in the other one. Yeah, somehow. Something like that. Okay, cool. There's, I'm sure, lots of smart people working on the problem. But it is neat. It's neat to see it going forward. Absolutely. Plus, there's a really amusing video of a breakdancer where six guys come out.
Starting point is 00:22:58 None of them are doing anything except for one who's spitting on his head. Yeah, it's a very, I don't know even how to describe it. It's a very different interpretation of how the global interpreter lock kills concurrency through breakdancing. Yeah, through breakdancing. I knew it all along, man. The rap beat was off.
Starting point is 00:23:16 That's why we can't get this in a run in parallel. Yeah. All right, cool. Well, that's it for our main topics today. What else you got that you just want to quickly chat about? A couple of things. PyCon seems like it just our main topics today. What else you got that you just want to quickly chat about? A couple things.
Starting point is 00:23:25 PyCon seems like it just got over, but the videos are already all available. And I've already started watching them, including Ant's complexity with Wiley Talk. And the other thing I wanted to bring up is I'm reviewing, I'm not reviewing it, but I bought the new Pragmatic Programmer book, the 20th anniversary edition. They rewrote it, and I'm reading the testing chapter starting there. And they bring up PyTest, and they're using PyTest and Hypothesis, and that's pretty cool. Oh, yeah, that's super cool. And Hypothesis, that's pretty interesting, that one.
Starting point is 00:23:59 Yeah. All right, well, I have a couple quick announcements for you all. So first of all, a quick and easy one. I've been creating a bunch of different things lately. The iOS and Android apps are both out for the talk Python training stuff, but I just released new versions that have a couple of cool new features. So if you have it installed, make sure you update it.
Starting point is 00:24:17 If you don't have it installed, well, check it out at training. Talk about that. That's cool. So Brian, I've joined you in the, uh, the journey that is
Starting point is 00:24:26 writing a book yes you're an author now yeah i apparently somehow somehow i don't feel like i've gotten any like official stamp or like any letter or no no um little pamphlet that makes me officially an author but yeah so we have a book called effective pie charm that's out and it has a digital version has a print version, has a print version. It has sort of bundled stuff with some of the courses. So I'll link to that in the show notes. People can check that out at effectivepycharm.com. So that's pretty cool. And then also we just released a new course, 100 Days of Web. It's pretty neat. I've started watching it. I like it. Yeah. Thanks. Thanks for taking and checking it out. That's awesome. So it's kind
Starting point is 00:25:03 of intense. It's like 28 little different web projects. So one time you build an API in Flask, and one time we build like logins with Django and migrations with SQLcamy. So all these different things you might want to do all over. It's kind of like a super sampling of all the web stuff. So check that out. You know, the links in the show notes
Starting point is 00:25:21 that you talk about on training as well. All right, well, that, all I got for now. Do you got a joke for us? I don't, but I wanted to bring up the, something about the, your course. Oh, thank you. My first concern was that like a hundred different web projects might seem overwhelming, but it isn't that, like you said, it's 28 projects. Is that right?
Starting point is 00:25:40 28? Yeah. And most, there's four of them that are super short. So it's kind of 24. Yeah. You get more into it than just some quick thing that you can do in one day i appreciate that pacing so yeah thanks a bunch yeah we're trying to find a balance there because 100 separate projects too small yeah one huge project you don't get in you know enough variety of other
Starting point is 00:26:00 things so 24 that was the slicing we came up with. All right. How about this? So there's a programmer going to a coffee shop looking to get out of the cube. All right. Can't stand being in the cube more. So I'm going to get out and there's a waiter that comes over and says, Hey, welcome to the restaurant. Would you like coffee or tea? The programmer says, yes of course have you done this no but it sounds like it might be sort of fun yeah i mean i haven't done it with coffee or tea but i definitely think that sometimes if somebody gives me an or question i'll answer with yes yes yeah they mean exclusive war, don't they? Yeah, pretty awesome. All right, well.
Starting point is 00:26:47 Thanks a lot. You bet. Good to chat with you, and I'll catch you next time around. Okay, bye. Bye. Thank you for listening to Python Bytes. Follow the show on Twitter via at Python Bytes. That's Python Bytes as in B-Y-T-E-S.
Starting point is 00:27:01 And get the full show notes at PythonBytes.fm. If you have a news item you want featured, just visit pythonbytes.fm and send it our way. We're always on the lookout for sharing something cool. On behalf of myself and Brian Auchin, this is Michael Kennedy. Thank you for listening and sharing this podcast with your friends and colleagues.

There aren't comments yet for this episode. Click on any sentence in the transcript to leave a comment.