Hello and welcome to Python Bytes, where we deliver Python news and headlines directly to your earbuds.
This is episode 86, recorded July 11th, 2018. I'm Michael Kennedy.
And I'm Brian Okken.
And Brian, we have a new special guest, Bob Belderbos.
Yeah, pretty excited.
Yeah, welcome, Bob.
Hey, nice to be here. Thanks.
Yeah, so people may recognize Bob from his PyBytes with an I, code challenge stuff that he does,
as well as the 100 Days of Code course that we did together, right?
Yeah, we did a major course on 100 Days of Code in Python. It was very fun.
Yeah, it was only, what, 20 hours of video and 80 hours of exercise?
Yeah.
That was awesome. So welcome to the show. We're super excited to have you here.
Before we get into everything, let's just say thank you to DigitalOcean, our customer,
and their sponsor of our show as well. Let's check them out at pythonbytes.fm/DigitalOcean.
You'll get $100 free credit if you're a new user. That's pretty awesome.
Brian, if I had to guess, if I were just going to pick out of thin air what kind of topic you might have,
it might have something to do with testing.
Yeah, I do like testing quite a bit.
So what do you got for us?
Well, we have responses, and there's a funny story around it, but this is,
responses is a utility library for mocking out requests, Python library.
And it's from GitSentry.
So it's under the GitSentry GitHub account.
And it looks pretty cool.
It's actually, we'll include a small snippet in our show notes, but there's,
it's a decorator approach to, and other ways to be able to test your,
pretty much mock out.
If you have an application that uses requests to access any, really any end point on the internet,
you can use this to mock out those.
And it has some dynamic, dynamicness to it.
And it's, it looks actually pretty complete and pretty nice.
That's pretty sweet.
Yeah.
You put an attribute on your, your function.
And during that one, any call to requests like request.get or the response.json or whatever
is going to be basically overridden as if you had done that with patch,
but it's, it's a little more focused on just requests, right?
Yeah.
It's, it's focused on requests and it's also, it's designed to go well with the pytest framework.
So it cleans up after itself really easily.
Nice.
How does it integrate with pytest?
Like what, what's special about that?
You have the decorator you put on your test is like responses activate.
So it'll, and then you're, you're going to have it, you register a, an end point to
mark and what response it should give.
And then all of that gets cleaned up at the end of the test.
You don't have to undo it or anything like that.
Yeah.
That's awesome.
Very nice.
The funny thing is, is this is exactly what Anthony and I were, we're considering building
together.
We just didn't know this one existed.
And so I wanted to point it out because, your responses is a clever name to go with
requests.
I get it.
But if I'm trying to find something that goes well with pytest and goes well with, requests,
I might not know to search for responses.
So that's why I'm violating it out.
So I think anybody that's trying to do something like this should check it out.
It looks pretty complete.
Cool.
It looks very useful.
So it's not making any call over the network, right?
That's the whole point of, of patching it out.
Right?
Well, it's patching.
It'll patch out the, the end points that you specify.
I don't really know.
I don't know what the behavior is for other ones.
It probably lets everything else go through.
Yeah.
That's the one of the things I like about it.
As you say this, if I request this HTTP endpoint, then do this rather than I'm just replacing,
you know, request.get with everything.
Right.
Oh yeah.
Pretty nice.
I do see your point about SEO and discoverability for responses because like response and responses
is just such a common word on so many API libraries.
Like how are you going to find the testing one?
Yeah, I was looking.
It says in its description that it's Python 2.7 or newer.
Well, so it isn't obvious to me that it was 3x, but it looks like in the investigate the
toxiny file, it's tested up through 3.5 at least.
So it's probably 3.6 and 3.7 compatible too.
Haven't tried it, but.
Yeah, probably.
Probably.
Yeah.
Very nice.
All right, Bob, it's time for yours.
What one did you bring?
Yeah, I found an interesting decision tree article, 29 common beginner Python errors on one page.
And I found this interesting because these kind of decision trees or graphics, you get
a lot of information in one page you can print out or study in detail.
And it's like a flow.
It's like an infographic for like getting my code to work.
It's amazing.
Yeah, it didn't land directly on the article.
Actually, it was Microsoft devs that tweeted it out.
And that's how I found it on the Python hashtag.
And it goes through this whole flow, like what kind of error do you get?
And then it goes into these common Python errors, like attribute errors, syntax error, type errors.
And for each one, it gives like a couple of plausible causes.
And yeah, I mean, if you have more experience, some might be obvious, but some are very subtle.
And that was like a great reminder, like comparing three int with a three string that returns false.
And for example, when I was going through it, I also could relate to similar things.
Like when you exhaust the file, that also can happen with a generator, right?
In generator, you go through it once.
And then if you do that again, then, hey, it's like an empty list because a generator only, you can only consume once, right?
So it was a nice, I guess, exercise to take a lot of info in from one infograph.
And I think for a beginner, it's very useful to go through that at least once.
And I think the author made the infograph after having teached Python for a long time in the biology space.
So that was also inspiring for me, like teaching others.
Yeah.
How you could summarize that information at some point.
I'll try to give a quick summary just so people can get the visual.
So it's like an infographing.
It says, start here.
Do you get an error when you run your code?
Yes.
What type of error do you get?
And there's a whole branch there.
No.
Does your code use loops or if statements?
If you use an if statement, go check this.
It just like walks you through some pretty basic stuff.
I kind of like it.
Brian, what do you think?
Yeah.
Actually, this is one that I've run across before.
And when our team started using Python for testing, one of the other engineers actually printed this out and posted it on the side of their cubicle so that people that came and asked them questions could just look at that first.
Oh, that's pretty cool.
And I like the fact that it's coming from Python for biologists.com.
So we're seeing Python being taught in a lot of different domains, which is awesome.
Yeah.
That actually is a really good point.
It's pretty awesome.
So the next one that I have for us is this thing called MicroMongo, which somehow I have not heard of previously.
But Brian, you know, I go on and on about async programming stuff and parallelism and things like that, right?
Yes.
And I do know that you kind of like MongoDB also.
This is like I get to put them together.
It's amazing.
So one of the main challenges of this async await stuff actually becoming super useful and super powerful.
There's two fundamental problems limiting like the serious adoption of this.
One is the web frameworks don't deeply support it.
So I can't have an async flask method.
I have to switch to something like Cort or use Fibora or some other framework like the main Django pyramid flask.
They don't support the async.
So that's thing one.
But we're seeing some movement there.
But even if you could get that to work, a lot of the ORMs don't have great ways to take advantage of their parallelism or their potential parallelism while they're waiting on the network talking to the database.
Because usually that's what your web app is doing and that's why you would want the async.
It's like you could let it do other processing while you're waiting on a database response.
So what you need is if you're using an ORM or something equivalent called an ODM for Mongo is the ability to interact with those databases in an async way.
And so this micro Mongo is a very lightweight and small, as you might imagine from the name, object document mapper for MongoDB and Python that brings this ability to do async operations against MongoDB.
Right.
So if you wanted to use, say, Cort or Fibora and you want to use MongoDB as the backend, you have to use something like this, which is pretty sweet.
Very interesting.
I need to go on the async train myself yet.
So maybe this is a good opportunity to learn it.
So definitely.
It's not that, I mean, Mongo itself, you don't need, that's on the other end.
So that's already asynchronous or can be, but it's the document mapper that's the problem.
You need the library that talks to the database to have an async option.
So for example, like let's take something that people know more about probably SQLAlchemy, right?
If you go to SQLAlchemy and you create a query and you say like order by or filter or whatever, you need SQLAlchemy to be rewritten.
So that filter is async is an async method or it's useless in terms that, right?
So it's like the place you want to get to is interacting with your database asynchronously.
But if the ODM or ORM that you're working with in the middle doesn't support that, there's zero workaround.
You're done, right?
You cannot do that.
Whereas most of the things you're doing is waiting on either web services or you're waiting on databases.
And that's where you would get like the massive scalability.
Another thing that's really cool here is there is something called motor, which is another async way of programming against MongoDB.
But this one will let you choose the foundational bits.
So you can say, I don't care about asynchronous programming.
So I just want the base to be PyMongo.
Or you can have TXMongo.
Or you can use motor async, which is something from Jesse Davis, I believe, at MongoDB.
Or you'll like this, Brian.
You can even say the driver, the foundational database access part of this ODM is MongoMock.
So if you want, you can just say actually replace the internals with this mocking database layer for testing.
Oh, actually, that's pretty cool.
Okay.
Neat.
Yeah, so this is, I haven't used it.
I generally use Mongo Engine.
But this is really looking quite promising and powerful.
So definitely want to give a shout out to that.
Speaking of shout outs, let's give a shout out to DigitalOcean.
So DigitalOcean is sponsoring this episode.
And they have a great, very affordable, very reliable service over at DigitalOcean.com.
So our stuff runs on DigitalOcean.
We have a couple of servers doing all the magic, including one doing MongoDB.
Back there somewhere in New York City, I think it is.
Anyway, they're super great.
Go over there and create a virtual machine.
Get an awesome machine for five bucks a month.
Get it set up in 30 seconds.
And off you go.
If you want to create something more pre-configured, they have a bunch of one-click apps to say set
up like a ghost blog for five bucks a month for one click.
Things like that.
So check them out at pythonbytes.fm/DigitalOcean.
You get $100 credit for a new user.
And you'll let them know you're supporting the show.
And thanks for them supporting the show.
So, Brian, what's up with this statistics stuff here?
I've not done statistics for a really long time.
I'm one for saying that I've been known to oppose the stringent math requirements in a lot of computer science and other science fields.
But regardless, there's a lot of people that end up having to learn statistics.
And I think statistics is a good thing to learn, at least some of the basics.
Yeah, I think we often come into, like, even going through computer science, you might not even take statistics.
You probably took calculus and differential equations and other stuff that you never, ever use again.
Right.
Yeah, exactly.
Yeah.
Let's not go down that tangent too far.
We'll all start crying about remembering how much work we put into learning that stuff, right?
I know I have never needed to factor a polynomial, ever, in real life.
How about computing the inverse of a non-segular matrix?
Sorry, let's keep going on statistics.
However, sometimes I do need to know, like, averages and medians and stuff like that.
So this is, I'm highlighting this article.
It's called the Basic Statistics in Python, Descriptive Statistics.
And I like it, it's just using some simple Python to demonstrate how you would build up some simple statistics concepts to teach the concepts, like min, max, mean, median, mode.
Some of the others, like standard deviation.
It's just a handful of them.
But those are kind of hard things to get your head around when you're first looking at it.
So being able to play with the numbers with a computer, I think, is a neat thing.
So they give some descriptions on how you would implement some of these functions in Python.
Oh, yeah, that's nice.
It does a nice job.
Like, for instance, if you wanted to figure out the average, you can take the sum of all of everything and then take how many scores.
And it's like the sum divided by how many.
Those are good concepts to look at.
The thing that was missing out of it is the little bit at the end, I think, that says, by the way, don't ever do this if you're actually writing Python code.
Because all this stuff's already built in.
Exactly.
Yeah, you've got the various numerical libraries for it, right?
Isn't this stuff already built in?
And, you know, min and max are already built in just normally and some of the others.
But I actually went and looked and found I didn't know about the statistics module.
That's built in, I think, as of 3.4 or 3.5 or something.
That's right.
It's quite new.
I don't remember.
But I think it might be 3.5, actually.
3.4.
Thank you, Bob.
Okay.
But I threw in some examples.
But, you know, if you're just using that, it doesn't help you learn what statistics is.
So that's why I think the article is still good.
I stuck up.
I was looking at the statistics and I noticed that it had both standard deviation variance and population standard deviation and population variance.
I don't know enough statistics to know what the difference in those are.
I know P goes in front of the method name.
No, that's awesome that that's in there.
Yeah, I think it's interesting that they added that module into the standard library in 3.4.
It's cool.
Yeah, I guess maybe they were tired of people implementing them in crazy hand-coded ways.
Yeah, and it's got to be faster written in C to compute the variance than to do it in, like, a Python loop.
That's a good point.
Yeah, and it, again, goes to say that always keep up with the standard library because there's so much stuff in there that can save you writing code and probably gain you performance.
Yeah, absolutely.
All right, Bob, what's the next one you got here?
This is a big piece.
Strings and character data in Python by real Python.
I don't have the...
If it's by real Python, you know it's kind of like a novella.
Yeah, exactly, because I want to compare it with that AetherTools article you featured a couple of weeks ago.
That was, like, this long piece that took me an hour to read, but I got so much out of that.
And it's the same here.
I didn't even, honestly, didn't finish it yet.
I'm, like, 60, 70% in and already got so much value out of it.
Because it does this great detailed tour of strings in Python and all the methods you can use on them.
And I put some snippets in the show notes because, I mean, for a beginner, it's great because you will be working with strings from day one.
But even if you know Python, there's, like, a lot of these little tricks in there that can just make you faster in Python.
If a string is a digit, you can use isDigit on the string, which returns a Boolean.
Rather than try to convert it to an int, catch the exception, go, looks like it's not an int.
Yeah, that kind of thing.
Yeah, so you can use that method.
Or, for example, you want to look for white space, a space, a tab, or a new line.
There's also an isSpace method that does that for you.
Or commonly with terminal apps, you want to have this banner with the text centered.
Well, you can just do string, center, and then the width, and then the filler character.
So there's a lot of good stuff in there.
And I think it's worth to go and spend that hour and learn all that stuff.
Because, I mean, it will just shave off time when you're actually coding.
And another thing I also found, kind of the linking you do when you read such an article.
For example, polymorphism, right?
If you have the index method, it works on a string, but it also works on a list.
So I put a snippet in the show notes that I use index on a string and a list.
And you see that they behave in a similar way.
Same goes for count.
So I found that also an interesting point to highlight.
Oh, yeah.
This is really interesting.
I just threw that into Instapaper.
And Instapaper says, reading time, 33 minutes.
Yeah, this is definitely, it's like a book chapter almost on strings, character strings.
I might be a slow reader, but.
Well, I'm sure I would, too.
And, you know, that's not counting that it's like code.
Code you got to analyze, right?
Yeah.
You want to try some things.
Yeah.
Exactly.
That's a great find.
It's really cool.
And people coming from different languages, I know I was like this.
I did not expect that strings would have all of these cool operations on them.
Yeah.
Especially if you're coming from C, where strings are really just, you know, character, pointer, array type things.
They're basically just memory, right?
Yeah.
But imagine you have to do that, leaving zeros to a number.
And that would take you a couple lines of code.
In Python, it's just a number, string, z fill, and then the number of zeros you want to be penned.
And it just makes for shorter code.
Yeah, it's wonderful.
Z fill?
Who's he?
Yeah.
So, you want to have 42, for example, and it needs to be a width of five.
You can just set fill it with zeros.
Nice.
So, I'm going to close this out with something that is surprisingly controversial.
I don't know.
I'll see what your thoughts are.
So, there's a new PEP in town.
Pep 572.
And whenever I think about these things, I always think, you know, someone has worked super hard on this.
And that's really awesome.
They're making a contribution.
And then, you know, to see people react in, I don't know, not totally excited ways.
I don't know.
It's sort of, I don't know how to feel about it, let's say.
But there's a new piece of Python syntax that will allow you to, in a single expression, create a variable and assign to it.
So, so often you have to do something to the effect of, like, I'm going to create a variable, set a value, and then if that value is something, I'm going to do a thing or I'm going to do something else.
So, the new syntax lets you put a colon equals to define that and put it all in one line.
So, instead of saying, like, match equals pattern.search, if match is not none, et cetera, et cetera, you can just say, if match colon equals pattern.search is not none, all in one.
I'm actually even confused about your example.
I thought that the point to that new operator was explicitly to check against none.
The point of the thing is to allow you to both create the variable and set its initial value and check it at the same time, whatever you're checking it for.
Oh, right.
And you don't even have to check it.
For example, one of the really common use cases for this is to use it in, like, a list comprehension or something like that.
And any time you need more than one line, that cannot be put into a list comprehension or a lambda expression, right?
But now, with this one line variable creation assignment potential test behavior, you can put these into inline expressions like list comprehensions or lambdas or so on.
I think that's it.
I've only skimmed some of the use cases, but that's what I'm thinking it's for.
Okay.
So this is interesting.
The syntax is not terrible.
I think it's kind of okay.
There's already lots of examples of this in other languages that had pretty decent implementations.
So one of my thoughts was like, well, why didn't that get adopted?
So both in like C# link expressions as well as JavaScript expressions, you have let something equals a value in terms of like a, like in terms of a for loop or something like that.
That could have also been an interesting option.
But I don't know.
To me, it's okay.
But I don't know that it's necessarily needed for the language.
Like it's not solving a problem that I think very many people actually have.
I wouldn't have to use it and see it in other people's code because from the snippet you posted, I find it a bit confusing.
It might just be me having to get used to it.
But it's not the kind of syntax I'm used to from Python, right?
Which is more readable.
What do you guys think?
To me, it's like one step down the slippery slope.
You know, I mentioned C# and the let stuff.
And I feel like the C# language used to be pretty nice.
And it's just a complete train wreck.
Now, there's all these little three or four ways of doing the same thing.
And they're all getting like a few characters shorter.
But now there's five of them.
They're all more or less equivalent.
It's just like, whoa, why do we keep adding these things to this language?
They're just unneeded.
And I feel like this is sort of in that category of stuff.
Python's nowhere near as bad.
Might not be the PEP itself, but more what we start to introduce and what other syntax changes
that might be coming, right?
If you open it up for this.
Yeah.
So what I want to sort of point people at for this is the tweet by Raymond Hedner.
And then the Twitter conversation that follows it, which is really interesting.
Like you don't normally see this much conversation
in a thread on Twitter.
But there's like 44 messages onto this one tweet.
And it's pretty interesting.
Some people like it.
Some people don't.
But I think it's worth reading through.
Nice.
So anyway, new PEP in town.
And it's approved, right?
This is not proposed.
My understanding is this is approved.
So it's now going to be part of maybe Python 3.8.
Wow.
Yeah.
I'll totally use it.
But it is.
It did one.
It was one thing that made Python different was that like in C, if you tried to do check
for equality and you accidentally did assignment instead, it's wrong.
And I guess that's why the syntax is specific so that it's not going to be an accident.
Right.
In C, you could say something to the effect of like, if match equals pattern.match, do
such and such.
Right.
And you actually meant if those two things were actually equal.
Right.
So there's a way.
The syntax does make that sort of fall through error case not actually.
You can't omit an equal on accident and actually get assignment.
Yeah.
You have to explicitly put the colon in there.
It's colon equal.
So.
Mm-hmm.
Okay.
All right.
Well, there it is.
PEP 572, the most controversial accepted PEP I've heard in a little while.
Pretty interesting.
All right.
Well, that's it for our news items, you guys.
Brian, you got anything else you want to share with the world these days?
What's going on?
Well, actually, kind of nice tie-in with this PEP 572.
I wanted to just talk with somebody about all the new stuff that's actually proposed and
accepted already for 3.8.
And I think just for 3.8.
So that's kind of what Anthony Shaw has been up to lately.
So I'm going to have Anthony on testing code and we're going to talk about that.
Oh, that's going to be great.
I've also realized that doing this podcast with you, it's pretty easy to do it once a
week because you're waiting for me to do it once a week.
So I'm trying to rope in some other people and Anthony's one of the people that agreed
to try to do testing code more regularly to try to get more of those out.
Oh, yeah.
That's awesome.
Yeah.
That's really cool.
How about you, Michael?
Well, if we want to give shout outs to Anthony, I just had him on Talk Python as well about
his security article.
He's really killing it.
Him and the real Python guys, they're definitely cranking out the content.
That's great.
But the thing that I guess is the biggest news that has me getting up early and staying up
late is I'm working on a new about 10 hour course for data driven apps in Pyramid with
SQLAlchemy and like production migrations and all sorts of stuff.
That is almost done.
It should be out next week.
I'm just finalizing a lot of the videos this week and it'll be rolling.
Nice.
So that's what I'm up to.
Bob, how about you?
You want to tell people about PyBytes real quick?
What that is?
Yeah.
PyBytes started out as a blog with articles and we quickly moved into a blog code challenges
and that got quite some traction.
So we built out a code challenge platform, which you can find on codechallenge.es.
So code challenges spelled as code challenges together, but ending in .es.
And you can log in with GitHub and code in the browser.
So there are exercises with automated pytest.
Yes, Brian, it's pytest.
And you can code in the browser and verify those exercises.
And yeah, the feedback is great.
I mean, people are learning a lot of Python that way because it's so practical.
And so we're growing that.
We have a Slack community behind it where a lot of people are joining and we have very good
Pythonic discussions.
So let's, yeah, I'm excited about this.
Yeah, right on.
Yeah, it's a cool platform.
Happy to see you doing it.
And thank you for being on the show, Brian.
Thank you.
Thank you, as always.
I'm happy to hear you're doing more testing code as well.
That's pretty sweet.
Yeah.
All right, catch you later.
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.
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.