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.
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.
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 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.
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.
Speaking of new releases.
New releases and the web.
Amazing stuff.
Django 2.0 is released.
And this is a huge, huge change.
It has 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 a lot of cool new features.
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.
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 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 3.1, then a 3.2 LTS.
So anytime you go into new territory past the LTS version, it's 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 switched their tutorials by default to use Python 3 versus Python 2,
that dramatically changed the usage by numbers on PyPI.
And so now 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.
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 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'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.
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 PEP 8 was a tool called PEP 8.
That's now been changed.
The name has changed to PICodeStyle.
But now I usually use Flake8 for my Linter.
And there's a...
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.
So the idea is how many different decision paths are possible through that code.
All right.
So if you had a method of cyclomatic complexity 5, there's five separate execution paths that could go through there.
There could be one if case that does an early return, another that's an if, elif, elif.
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.
You could probably get a plugin that would turn that into a hyperlink that shows the details from this list.
And that would be awesome.
Oh, yeah.
That'd be good.
Yeah.
I'm using, so PyCharm does this, checks for a lot of this stuff.
And yeah, and I usually turn it on for pytest too.
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.
One of them is time.
The other one is the network and external services.
Some of that being requests type things, 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 requests 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, 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.
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.
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 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 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 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 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 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 thing, I want to start with our first thing.
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 UV loop 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.
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 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 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.
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 refigure out how the framework works because they've said.
Yeah, that's cool.
The framework's just like Flask.
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's that.
There's Jepronto.
There's Sanic.
There's all these other frameworks trying to take advantage of things like UV loop and Async and Await.
But they're like, and you start from scratch.
And you learn a totally new framework.
With this, 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.
Be cool.
All right.
Well, that's our news for this week, Brian.
Anything 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 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.
So he's like, hey, would you mind letting people know about the staff?
I'm like, yeah, this is pretty cool.
I'll let people know.
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.
Everyone needs a cool Python staff 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.
Yeah, indeed.
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.
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 Okken, this is Michael Kennedy.
Thank you for listening and sharing this podcast with your friends and colleagues.