Python Bytes - #55 Flask, Flask, Flask, 3x Flask

Episode Date: December 7, 2017

Topics covered in this episode: Django 2.0 Released The Big Ol' List of Rules requests-staticmock PEP 557 -- Data Classes have been approved Quart: 3x faster Flask Extras Joke See the full show ...notes for this episode on the website at pythonbytes.fm/55

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 55, recorded December 6th, 2017. I'm Michael Kennedy. And I'm Brian Ocken. And Brian, can you believe it's December? Yeah, it's getting cold out. It's getting cold. I look outside, it's the middle of the day, and it's still basically dark. So I guess we're getting there.
Starting point is 00:00:21 So before we get into our picks for the week, though, let's just say thanks to DigitalOcean. They have a ton of awesome servers for you. The websites I run run on DigitalOcean. So we'll tell you more about that later. However, one option, I guess one of the servers I have actually runs Flask. And Brian, I hear you're kind of digging Flask these days. Yeah, I am actually going through Miguel Grimberg's Flask mega tutorial. So I'm pretty excited about that.
Starting point is 00:00:50 And I got actually from the, I think I took the advice from you to try something simple like Flask at first, not to slam Flask, but it is pretty low barrier to entry. And I knew Miguel was rewriting this mega tutorial. So I begged and pleaded and got an early copy of the rewrite. So I'm partway through it right now. But he did a Kickstarter to try to rewrite it. The first one was in 2012. And his Kickstarter was very successful, I think. His part one of the rewrite is available right now today. Yeah, that's awesome. And I know Miguel's been putting a ton of work into the rewrite. I was so excited to see his Kickstarter be successful. He added a bunch of stretch scrolls to do additional sections. He has an ebook version and a video version coming out of it,
Starting point is 00:01:41 coming out as rewards from it. He hasn't done the videos yet. He and I were actually just talking today about the videos, so that'll be fun. But yeah, so if you want to learn how to get going with Flask, his work is really great. And so definitely check it out. He does have, what he's going to do is he's going to release one part every week, but if you can't wait that long, you can buy his ebook. I think it's just like 10 bucks or something. Yeah, totally affordable. And that's what I'm reading right now. And yeah, his video, he says he's planning on January for the video version. Yep. Very cool.
Starting point is 00:02:10 Very cool. Speaking of new releases. New releases and the web. Amazing stuff. Django 2.0 is released. And this is a huge, huge change. It's been many, many moons since major point release of Django has come out. I mean, after all, it's only version two, right? This is a huge deal. And it's, it's a lot of cool new features. One of the
Starting point is 00:02:33 things that they added that I really like, and I don't know, it's always made me just crazy when I looked at Django is the fact of writing regular expressions for the routing, which is I want to take this URL and figure out which view method that goes to. That used to be a regular expression, which was painful. Now it's much more like Flask and Pyramid. You just put little identifiers, like variable names in cutout URL, and then that's how it maps over.
Starting point is 00:02:59 And you even have types. You can say it has to map to slash users, slash user ID colon enter. I think enter goes first. But there's this nice routing syntax. There's some nice responsive design changes, better querying over some of the query sets. These are all cool. They have a new versioning, what they're calling loose form of semantic versioning. So if you look at the
Starting point is 00:03:26 possible versions, we have two, maybe it'll be a 2.1, and then a 2.2. And then that 2.2, maybe that's something they're calling stable long term support LTS. So it might be 2.2 LTS. And then if they go anything beyond the LTS, that's a three, then a three, one, then a three, two LTS. So like anytime you go into like new territory past the LTS version, it sort of is a major version increment now. Okay. It's interesting. Yeah. So I suspect that we'll see major Django version numbers coming faster because of that, but I'm not sure. I guess we'll have to see. And then there is some exciting thing about Python 3. Yeah, it's very exciting. The legacy Python is dealt yet another blow. So Django has had a significant disproportionate influence on the adoption of Python 3. For example, when they
Starting point is 00:04:17 switched their tutorials by default to use Python 3 versus Python 2, that dramatically changed the usage by numbers on PyPI. And so now this, they've actually dropped support for Python 2. It's the first version of Django that says, you know, Python 2, that's, you know, thanks, but that's not for us. It's Python 3 only going forward. Yeah. And because of that, I've seen a few people mention on Twitter that working with the code base is a lot easier now because there aren't a lot of backwards compatible things in there. They were able to clean up the code base quite a bit for this. So I think it's great. I think it's great as well. And yeah, it definitely makes working on new features easier because you don't have to write them twice in some sense.
Starting point is 00:04:59 And there's a bunch of small changes. I don't want to read them all off to you, but just to give you like a sense, down in Django contrib.auth, luckily they're doing password hashing and folding. So not just hashing with salt, but then you take that and you hash that and you take that and you hash that. And then they used to do that 36,000 times. Now they do that 100,000 times.
Starting point is 00:05:20 So it's more computationally expensive to guess the password if somehow the database were to leak. And so there's just tons of little cool changes like that throughout there as well. But probably the biggest one people will notice is the simplified URL routing. Yeah, that's nice. So you got a bunch of rules for us or something, huh? I do. What's up with that?
Starting point is 00:05:37 I'm usually somebody that doesn't follow a lot of rules. But one of the things I embraced when coming into Python is the notion of that there's kind of a coding style that everyone follows, or a lot of people follow on open source projects, which is PEP8. And then it's extended. So there's, when I started using type checkers like Lint, or at the time I started it, the way to check for PEP8 was a tool called PEP8. That's now been changed. The name has changed to PyCodeStyle. But now I usually use Flake8 for my linter. And there's a...
Starting point is 00:06:14 So Flake8 covers PyCodeStyle, which is PEP8. And then it covers PyFlakes, which does a lot of traditional lint stuff to catch bugs. And then a McCabe complexity checker. And that one, I actually have tried to figure that out several times, and I don't know what it does. Nice. Yeah, cyclometric complexity is a pretty interesting metric for code maintainability.
Starting point is 00:06:38 So the idea is how many different decision paths are possible through that code. So if you had a method of cyclometric complexity five, there's five separate execution paths that could go through there. There could be one if case that doesn't early return another, that's an if LFLF and taking all the possible ways in which you could go through those conditionals and loops and whatnot, there would be five possibilities. So meaning basically you need five tests minimum to cover that. Okay. I'm not sure what the check is for McCabe, what the complexity number is that they're flagging for, but I usually turn it on anyway,
Starting point is 00:07:13 because I want to know if my code's a little too complex. The issue with it is a lot of these spit out an error message with a one-liner explaining what it is. And so what I have for us today is called the big old list of rules, which translates all of those errors and warning numbers into very nice one-page descriptions of what they are with links to more information. And I really like it. I'm going to be using this all the time now. That's really cool. I feel like there's an opportunity, you know, first of all, well done Grant for writing this and putting this all out for everyone. But I think there's an opportunity for editor plugins, whether you're using Sublime, Visual Studio Code or PyCharm or whatever, you know, you could probably get a plugin that would turn that into a hyperlink that
Starting point is 00:07:58 shows the details from this list and that would be awesome. Oh yeah, that'd be good. Yeah. Yeah. I'm using, so pycharm does this checks for all this stuff and um yeah and i usually turn it on for py test too i have my py test plugin to check flake 8 once you find an error trying to fix it it's good to know where what it is yeah especially when it's just e112 like what the heck does that mean right yeah i mean you may be really good and know them, but I don't know. Awesome. So before we get on to the next item, I just want to let everyone know that this podcast
Starting point is 00:08:31 and really all the sites that I run are coming to you through DigitalOcean. I have, gosh, it's just a growing list. I think I probably have eight servers over there now doing all sorts of hard work and working together on various services and database connectivity and whatnot. So super excited about working with DigitalOcean and talking about their stuff because it's really, really been great to work with. So if you're looking for cheap, reliable, fast servers that are simple and not a huge mess of a thousand features like you might get somewhere like AWS or Azure. You just want to
Starting point is 00:09:05 have a server and work with it in a really nice way. Check them out at digitalocean.com and let them know that Python Byte sent you. Nice. Yeah. We could probably contact them with requests as well. We could probably like do some sort of API and talk to them. But if you want to test it, you need to mock out your request, right? Definitely. One of the challenges, I think, there's a few things that are really make testing sticky, tricky, whatever. One of them is time. The other one is the network and external services. Some of that being requests type things,
Starting point is 00:09:36 some of that being databases. So any chance you get to cleanly sort of mock that out is really nice. And so this one actually comes from a friend of the show, Anthony Shaw. And he has this thing called request static mock. And I think we were recently talking about something with mocking requests. And he's like, you should check out request static mock. And so I did. And it's pretty cool. So I decided to make it one of the things we're talking about this week. And the idea is you can create a request session and then mock that out like,
Starting point is 00:10:07 hey, I want that return of 503 service unavailable. Or I'd like when you make this request to this URL, return this JSON file as the response. So really easy to swap out the testing behavior, like if your code somewhere deep down calls into requests, but you can do it without monkey patching. Yeah. That's the neat part is it's without monkey patching or doing a lot of these test-based mocks. It's pretty cool. Yeah, it definitely is. Yeah. You don't really mock stuff as much. You kind of just plug in the session and you know, if for people who don't know the session object is a thing that comes from requests, which is actually pretty
Starting point is 00:10:42 interesting. So suppose you're going to start talking to a service and every single request has to have an off header. It has to have maybe a user agent. It has some other details, some kind of token type thing, who knows a lot of shared stuff, or if you're going to try to submit a form and then you need to take a session on the server, like a cookie-based session, and then go and do other things. You can't do that with just straight requests so easily. So you create one of these sessions, and it keeps a persistent connection. It handles the cookies across all the requests and stuff like that. So that's really handy. And what Anthony's
Starting point is 00:11:20 thing does is create a sort of testing session variant of that so it's pretty cool so you can mock that thing out yeah and the way you put it together too is uh the data that's coming back is just in like a you can just set it up as like a tree structure in your in your file system it's kind of like your old school html directory try with some index.html and yeah all that kind of stuff they just put it in there and it traverses that. That's cool. It's a nice interface for the developer as well. It's cool.
Starting point is 00:11:49 Yep. Well done, Anthony. So you're going to give us a bit of a preview of Python 3.7, right? Because there's some pretty awesome stuff that just got approved or finalized. Data classes, which I didn't know it was on the fence for a while, but these are data classes have been approved by Guido and it's PEP 557. And these are kind of a different form of regular old classes,
Starting point is 00:12:13 but you can put a decorator on there for a data class and then you can sort of say what your, some data elements and what type they are and you can assign defaults. And the cool thing about that is you don't have to write your own init statement. It kind of generates one for you. So the first time I saw these, I'm like, wait, that's not valid Python. What is this? What language is this? Yeah. So you could say like class C colon, and then just A colon int
Starting point is 00:12:40 new line, B colon int new line. And you just start out with a class when you create it that has an A and a B and those are both none, right? Or you can even set default values. That's pretty cool. It lets you do more of the defined as part of the class structure instead of the self dot attribute equals value through the dunder init. But like you said, it still generates that dunder init and then moves over the default values and all that. I kind of like the syntax. The first time I saw it, like you said, it's bracing and it's like, this isn't Python. But it's kind of nice that you can just put that in one place and not worry about it too much. It's pretty clean. Definitely like it. Yeah, I find myself doing this sometimes. And I'll just have to set everything to
Starting point is 00:13:21 none, or to zero or something like that, because it won't work otherwise. But guess what? It does now. It's cool. And I also just found out that there is a 370A3 developer build that's out that has this in it. So if people want to play with it, they can, but I probably wouldn't do much production code with it because 3.7 isn't scheduled until June. Okay. Yeah. So it's a little ways out, but still exciting to see this coming. I think this is pretty nice. So one of the things that this feels like, I think is compared to and looks somewhat similar to is Adders. And Adders gets a lot of attention as well. What's the story between those two? I don't know the history of like how much I know that, hi, Nick. Oh, he's going to clobber me again for getting his name wrong. But I think he was involved in talking with the core developers when talking about this data class, but I'm not sure.
Starting point is 00:14:12 But anyway, there's a few. Adders is still great, and these data classes don't do everything that Adders does, and it has more validators and converters and a whole bunch more stuff that you can do. So it doesn't completely take the place of adders. But for simple cases, I think it's a simpler interface. Yeah, okay. That sounds good. The best example that I heard of why people wanted it in there is because the core developers wanted to use it on Python itself. And you can't use non-standard library stuff within
Starting point is 00:14:46 the core of Python. Yeah, I think that's a really interesting point. And adders is changing fast. It's still getting a lot done to it, and you don't want to hamper it and cover it in quicksand or some sort of tar, right? You want to slow it down by sticking it in the standard library and going, well, you can only change very slowly now and only every year. Yeah. That's some of the reason why requests isn't in the standard library, right? Exactly. Yeah. Same reason. All right. So for our final
Starting point is 00:15:13 thing, I wanted to start with our first thing. Flask. Flask. My version of Flask I want to talk about is three times faster than your version of Flask. So how does it do that? So there's a thing called Quart, which I haven't done much much with Cort, but it's kind of like a wrapper around some of the AsyncIO stuff, but also an API that can run Flask apps. Like I said, I haven't done a ton with it, but Cort is this thing that you can use that has the same API as Flask, but is AsyncIO friendly. So you can plug it into the super, super fast things
Starting point is 00:15:46 like UV loop, or async PG for asynchronous Postgres, which is pretty awesome. And there's some really amazing benchmarks there. So Flask, along with Django, and along with Pyramid, and all the others, they don't support any async and IO stuff. And they can't take advantage of basically releasing the thread to go do other work when it's, say, waiting on a database or on a call over request or something like that. Just because they're all using WSGI, that's not how WSGI works. So you can plug in a quart, which basically has the same API as Flask, and you just have to make a few minor changes to get your code to go much faster. So here's an article with a demo application. They've got benchmarks and stuff saying we're getting roughly three times the speed by just switching a few things around in the app.
Starting point is 00:16:40 Yeah, I think that's cool. I definitely need to try this. Yeah, so the things you have to do, obviously, if you want to take advantage of async IO is you have to make your functions async, right? Otherwise, they're just regular functions, they go just the same speed. So you would put async in front of your view methods. And then when you call into things like databases, or web services via request, say you have to await those to basically tell Python, give up my thread i'm waiting on this and then pick it up when it gets back right put me back somewhere farther down in
Starting point is 00:17:11 the loop when this returns so that's all cool but your database access has to have some sort of asynchronous component so when you do a query you can wait on it otherwise it's kind of useless again so that's why it's both the court but also async pg right which is pretty cool so it's not entirely easy to switch over depending on what you're doing like if you're using sql alchemy sql alchemy i don't believe supports anything with async so you're kind of out of luck it depends on what you depend upon actually okay it's easy to switch if it's going to work at all. How's that? Yeah, and one of the things I think is neat about this, and it's a clever idea, is instead of inventing a completely new framework, it is a completely new framework, but they wanted to, like, I think it's a good idea to slow down the learning curve. You've got to figure out the async stuff, but you don't really have to reigure out how the framework works because they've said,
Starting point is 00:18:09 yeah, that's cool. The framework's just like flask. That is so, that is such a good observation. And it's really right. There's HTTP, AIO, HTTP. I don't remember the order, sorry, but there there's that there's Gepronto, there's Sanic. There's all these, these other frameworks trying to take advantage of things like UV loop loop and async and await but they're like and you start from scratch you know and you learn a totally new framework with this like you could probably go take miguel's tutorial thing and then go make it faster it's kind of cool and that's what i plan on doing yeah perfect all right well that's our news for this week brian anything uh you got going on over there? No, I'm just trying to learn Flask, man. Awesome. That sounds really fun. So are you familiar with the Pythonic staff of
Starting point is 00:18:51 Enlightenment? Yes, I carried it around for a while at PyCon. Yes, so did I. So a lot of people probably don't know about this. There's a picture of me with Anthony Schaaf, who I mentioned in the mocking bit, and me walking around with this giant, I don't know, it's probably four feet tall, this big heavy staff. At the end, it has like a massive Python logo. And so one of the guys that was involved in creating that thing originally actually decided, so many people asked for it, he's creating a store where you can buy your very own Pythonic staff of enlightenment. So he's like, Hey, would you mind letting people know about the staff? I'm like, yeah, this is pretty cool.
Starting point is 00:19:29 I'll let people know. So yeah, I haven't checked it out yet. Any idea how much it is? I think it's like a hundred bucks US. Okay. I may need one anyway. I know. Well, Christmas is coming.
Starting point is 00:19:39 Everyone needs a cool Python stuff for Christmas. Yeah. Anyway, I thought that was fun. So I thought I'd throw that in there at the end for you guys. That's nice. Cool. Anyway, I thought that was fun, so I thought I'd throw that in there at the end for you guys. That's nice. Cool. Yeah, indeed.
Starting point is 00:19:48 All right, well, Brian, great to chat with you as always, and thanks everyone for listening. Thank you. 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:20:00 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.