- Podcasts
- Python Bytes
- #55 Flask, Flask, Flask, 3x Flask
About this episodeTopics covered in this episode: Django 2.0 Released The Big Ol' List of Rules requests-staticmock PEP 557 -- Data Classes have been…
- 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
Transcript
From the publisherThis transcript comes from the podcast’s own feed, shown as the publisher wrote it.
#55 Flask, Flask, Flask, 3x Flask
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 Okken. 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. Yeah. 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.
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 goals to do additional sections. He has an ebook version and a video version coming out of it, 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 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.
One of the 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. 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.
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. 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.
So you've got a bunch of rules for us or something, huh? I do. What's up with that? 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 PEP 8.
So Flake8 covers PICodeStyle, which is PEP 8. 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, cyclomatic complexity is a pretty interesting metric for code maintainability.
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 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. 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 of plugins, whether you're using Sublime, Visual Studio Code, or PyCharm, or whatever.
I have my pytest plugin to check Flake 8. Once you find an error trying to fix it, it's good to know 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, just want to let everyone know that this podcast 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, you know, a huge mess of a thousand features like you might get somewhere like AWS or Azure, you just want to have a server and work with it in a really nice way. Check them out at DigitalOcean.com and let them know that Python Bytes 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.
And the idea is you can create a request session and then mock that out like, 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 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 that you create one of these sessions and it keeps a persistent connection. It handles the cookies per, you know, across all the requests and stuff like that. So that's really handy. And what Anthony's 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 the data that's coming back is just in like a, you can just set it up as like a tree structure in your file system. It's kind of like your old school HTML directory. That's right. With some index.html and all that kind of stuff.
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 PEP557. And these are kind of a different form of regular old classes. 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 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 definers part of the class structure instead of the self dot attribute equals value through the dunder in it.
But like you said, it still generates that dunder in it 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 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 attrs.
And attrs 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 and talking about this data class.
But I'm not sure. But anyway, there's a few. Adders is still great. And these data classes don't do everything that attrs 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 attrs. 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 the core of Python. Yeah. I think that's a really interesting point. And attrs 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 thing, I want to start with our first thing. Flask. Flask. Ah. 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 this thing called Court, which I haven't done much with Court.
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 Court 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 like uvloop or AsyncPG 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 Whiskey, that's not how Whiskey works. So you can plug in a Court, 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.
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 AsyncIO, 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.
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 Quart but also AsyncPG. 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 SQLAlchemy. SQLAlchemy, 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.
No, I'm just trying to learn Flask, man. Awesome. That sounds really fun. So are you familiar with the Pythonic staff of 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 Shaw, 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.
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. 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.
