Hello and welcome to Python Bytes, where we deliver Python news and headlines directly to your earbuds.
This is episode 157, recorded November 14, 2019.
I'm Brian Okken.
And I'm Michael Kennedy.
And this episode is brought to you by DigitalOcean.
So, Michael, we're going to cover a topic that we've covered a little bit before, I think.
We covered Cerebrus, right? Or Cerberus?
Cerberus. Yeah, we covered Cerberus, which is like a validation layer for unstructured data.
So, this is as built as part of the Eve framework by Nicola, who runs both of those projects.
And it's really nice, right?
So, I get like some JSON posts back to my REST.
It's a REST framework.
I get a JSON post for some data.
And I have some models to find.
It can tell you whether they're a fit or not.
It can tell you what's required.
I do think the way you set it up is a little bit of out of band.
So, Colin Sullivan shot us a note after that.
Said, hey, that's really cool.
You should also talk about Pydantic.
Had you heard of Pydantic?
I think so.
But, yeah.
Tell me more.
And it's got a great name.
Yeah, it definitely has it.
Yeah, yeah.
It's got a super name.
I believe I've heard of it, but I didn't do anything with it.
So, on Colin's suggestion, I checked it out.
And, yeah, this is a sweet, simple framework that solves some really nice problems.
And a lot of times with these frameworks, I'm like, yeah, I would love to use this.
But at the same time, like, it's not that helpful.
And so, I'm not sure I'm actually going to use it.
I could just put a little test in my class to make sure this file, like, this thing parses an int or this name is here or whatever.
But this one, like, might convince me to do it because, yeah, this is super, super cool.
All right.
Let me tell you what it is.
So, it's data validations and settings managements for using Python type annotations.
And it's the type annotations that make me really extra happy.
Oh, really?
Okay.
Yeah.
So, you know how we've got data classes and you can have, like, annotated values there and you get a little validation and whatnot.
But this is super cool.
So, I can just take a create class and say it has things like an ID, which is integer, a name, which equals a default string, a date time, which has a default of none, things like that.
Right.
So, you basically, either you have type annotations or the thing has a default value, which implies the type.
Okay.
Yeah.
And this probably represents some data that's exchanged over REST or something like that.
Right.
Some sort of dictionary.
So, if I get a dictionary back, then what I can do is I can just star, star, unpack that dictionary into the object, the class that I've defined.
Right.
So, basically, keyword arguments, ID equals whatever the value is, name equals whatever the name and the dictionary is and so on.
And it will validate all that using some really simple rules that you follow along.
So, we've got a class and it has an ID, which is an integer, but has no default value.
It has no none.
That means the ID has to obviously be an integer, but it's also required.
If it's not there, an error will be raised.
The name is a string, so it has to be a string.
But because it has a default value, it's optional to pass it.
Oh, okay.
That's cool, right?
The date time, which is a date time field, is not required because it has none as a value.
But if nothing's passed, it's just going to be none.
So, it's an optional date time.
That's pretty cool.
So, some of the reasons that I think this is cool and they call out in their webpage is that it works automatically with all the IDEs that you already have.
Right?
There's no, like, special, oh, yeah, there's a YAML file that tells me what this schema looks like for this.
Or there's a JSON schema that comes back and, like, no, it's standard Python with type annotations.
So, your IDE knows already what all those things are and you don't have to, like, backfill that, right?
So, the validation also works for just working with the classes.
That's pretty cool, right?
Yeah, that's cool.
Yeah, it's supposed to be faster than all the other libraries they tested and they have a link to the ones that they did.
It also supports really rich recursive validation.
So, if you've got, like, a list or a tuple and maybe, like, stuff is inside there, right?
Or something like that, right?
You've got some nested types.
So, it'll actually recursively traverse the stuff that you're looking for, right?
So, it doesn't just test the top-level things.
It tests, like, the entire object graph.
And by default, the way it works is you derive from the Pydanic base model, which is cool.
But you can also use a decorator on a data class, which we talked about.
It's very similar because of the type annotations.
And it'll actually put parsing and validation on there for you.
Oh, that's neat.
Yeah.
So, if you really want to use data classes, you can make them better with Pydanic as well.
Okay.
Yeah, simple, right?
Yeah.
So, where do you put it in your...
So, do you get it, like, when you get data in, you validate the data with Pydanic then?
That's the thing is you don't even validate it.
That's what's so sweet about it.
There's not even a validation step.
You have the class.
And in the notes, I'm putting a user class.
So, I'll maybe reference that.
And then you get some external data, which is a dictionary.
And then you just create the user object.
It's a user of star star dictionary.
And it'll unpack it keyword-wise.
The validation happens in the fact that you don't have a dunder init on your class.
And it derives from the Pydanic base model.
So, it uses its dunder init, which theoretically does the validation.
Oh, okay.
So, you can't not validate then.
Yes.
Exactly.
Yeah.
You just basically...
I tried to create the class.
And either that worked really well or not so much.
Oh, that's actually pretty cool.
And you get like a JSON response of all the things that were wrong with the validation as part of the exception, I believe.
So, you can actually go, no, there's actually three things wrong here.
Not just, well, the first thing it hit made it crash.
So, this is obviously useful for like REST APIs and stuff like that or grabbing external data.
But there's a lot of times where we're passing dictionaries around between components.
And it'd be good to have some, if there's less trusted components, to have some sort of validation.
So, this is pretty cool.
Yeah.
Even web forms that get posted back, a lot of times those come back in Pyramid or Flask as dictionaries.
Right?
If you wanted to map that to a class, right, you could get validation.
There's a lot of places.
Yeah.
Even settings files, right?
Yeah.
There's a lot of people that just throw stuff that gets adjacent or something that gets thrown in a file.
And it's user editable also.
Yeah.
So, you have to validate it because who knows what somebody edited it to.
Yeah.
Absolutely.
All right.
What you got next for us?
I am doing, hopefully doing a favor, adding work to Ned Batchelder.
So, he posted on Twitter recently that there is changes afoot in coverage.py.
So, coverage is, hopefully everybody knows, coverage is great for using to tell you how much of your code base, your test suites are covering.
I mean, that's how it's usually used.
So, you could potentially do anything to try to measure coverage, but usually it's around a test suite or something.
Anyway, so, the change is they've added measurement contexts.
So, allowing you, while it's collecting data for coverage, it collects what was the context of what it was doing while it was covering certain bits of code.
Now, that seems a little, the obvious use model of that, there's lots of use models.
The obvious use model is which test covered which line of code.
And to have that, and that's a lot of data.
So, he's changed the way the data for coverage is being stored.
And it's pretty cool.
So, I'm going to jump to the conclusion.
There's this cool feature.
The context feature is very cool.
I want to talk about that.
But first of all, it is a little bit of a break in the coverage, use of coverage.
I think the reason is just because there's a, the way the data is stored, there's a little local database stored.
So, there's another dependency that isn't an external dependency.
It's a built-in dependency.
But it's something that some versions of Python don't always have, I guess.
So, for that reason, he's asking everybody, please try out the beta 1 coverage 5, 5.0 beta 1, and try it out and let them know if there's any issues.
Right.
So, basically, the idea is go try it and see if what you're doing before still works.
If not, let them know real quick before it becomes permanent, right?
Right.
Exactly.
And I really want this to become permanent because measurement context is so cool.
I tried it out this morning.
I'm going to put in show notes.
I wasn't really clear on how to download, how to install a beta version of something.
So, you just do the, like for this, it's pip install coverage double equal 5.0b1.
Okay.
So, we'll put that in the show notes.
It's not too bad to install it.
And then also, I didn't put this in the show notes, but one of the other tricks I found out is if you want to know what versions are available to pip install,
you can just do the coverage equal equal and then don't list a version.
And you'll get an error message that says, I don't know what you're talking about, but here's all of the versions that are available.
That's pretty awesome.
I didn't know that.
Yeah, that's pretty cool.
So, I tried it out, a few lines of code to, or a few lines of command line stuff to run coverage on a little dummy file.
And sure enough, if I generate the HTML report, on the right-hand side of the window or the screen, I've got little drop-downs on every line of code to tell me which test covered which line of code.
I like that a lot.
That's cool.
Yeah.
Very neat.
Yeah, super nice.
I look forward to it.
Okay.
I don't know why I think this is funny.
My brain's just not working, man.
Will you do the ad read?
Got it.
Now, this episode is brought to us by DigitalOcean.
And I just want to tell you about something brand new that's gone from beta to general availability, memory-optimized droplets.
Droplets are DigitalOcean's words for virtual machines, right?
Goes to the cloud, clouds full of rain, rain droplets, that sort of thing.
And if you have some sort of workload that requires a lot of memory, well, then these things are like super optimized that.
So it has eight gigs of RAM for each dedicated virtual CPU.
You can get them with two or many, many more, right?
Multi-core systems.
So basically, you can go all the way from 16 gigs to 256 gigs of RAM, which is like a ridiculous amount of RAM.
One thing you can do to make your app run faster is to make sure it never touches the disk, right?
So if it could just cache everything, that would be great.
So they're really good for things like high-performance SQL or NoSQL databases, large memory caches and indices, indexes, things like that.
And just lots of big data and stuff running with large runtime requirements.
So if you need between 16 to 256 gigs of RAM and you want to just pay mostly for the memory, right?
The pricing is optimized around that use case.
Then check them out at pythonbytes.fm/DigitalOcean.
They're a big supporter of the show.
Speaking of cool stuff, the PSF and the Python Software Foundation Packaging Working Group, actually, that group of the PSF,
they're looking to hire some folks.
They're looking for, I think, three developers and maybe a project manager.
I can't remember exactly all the details, but quite a few number of people to make pip better.
Like you just said, if you said, you know, pip install coverage double equals, it'll help you, right?
So this is supposed to be a much better setup.
So the idea is that one of the things that could be improved in pip is its dependency resolver, right?
So it's, you know, this package depends on this thing, but other package also maybe depends on that, but a different version.
Or, you know, I don't know how often it's happened to you, but I've had the order in which I list stuff in the requirements causing issues.
Because one requires, I don't know, doc opt of this version.
The other one requires doc opt of another version.
And how can you possibly install them both at the same time, right?
Weird stuff like that.
Poetry has noticed this problem and it has a solution to it, but it's around poetry.
And it'd be really cool if that sort of dependency resolution was built in to pip.
That'd be great.
Yeah.
The underlying idea is to make distributing and installing Python software just more reliable and easier.
So funding has been allocated to two contractors, a senior developer, an intermediate and an intermediate developer.
That's what it is.
To work on developing, testing and building this feature, the test infrastructure, code review, bug triage, all that kind of stuff.
And this is a non-trivial offering.
So I believe the senior developer will end up getting $116,000 out of this based on the time they're estimating and the rate.
And then the either senior developer or the contractors, I can't remember, get $103,000 each.
This is quite significant.
Not too shabby.
Yeah.
That's like a, not just a, hey, I need somebody to work on this for a couple of weeks.
That's like a legit thing.
So if you'd like to contribute to Python, work on pip, things like that, just, you know, go check out this link.
It shows you how to apply.
Very cool.
Yeah.
So when I work on Pandas, Brian, I kind of feel a little bit lost.
There's all these operations and I don't use Pandas enough to kind of actually know what I should be doing.
Often it's in the context of Jupyter Notebooks where the autocomplete's slightly less good than PyCharm or VS Code.
I could always use some help when I'm working on Pandas.
How about you?
Yeah, I could.
And I know people that, there's a lot of people that work in it all the time,
but I usually just jumping in for some particular use.
And I know I don't know the best way to do things.
There's a thing called DovePanda.
I think I'm saying that right.
D-O-V Panda.
And this was submitted by Dean Langstrom, Langsom, sorry.
I think that it's his project, but essentially it's an overlay on, I'm just going to read his thing.
He says directions.
So DovePanda has directions and are hints and tips for using Pandas in an analysis environment.
DovePanda is an overlay for working with Pandas.
So the idea is, like, if you have this installed also, you're working in a Jupyter Notebook and you start typing stuff, you start doing Pandas operations.
It looks at what you did and provides hints and it pops up in little windows in your notebook to give you hints on, I think you're doing this, but there's a better way to do it or giving you tips.
So it's like Clippy for Pandas in Jupyter.
Yeah, but it's definitely, sort of.
But instead of having just one Clippy that pops down, they're in your notebook so you don't have to deal with them right away, but you can go back and improve your use of Pandas within the notebook.
It's pretty cool.
Yeah, it actually looks really helpful.
So the example they have, they've got a bunch of pictures on the GitHub repo you all can check out.
But, like, for example, there's one where someone's calling pd.concat and taking two data frames and specifying the axes equals one.
And then the little panda pops up and says, all data frames have the same columns, which hints for concat on axis zero.
You specified axis one, which may result in unwanted behavior, and it'll show you the code.
Or after concatenation, you're going to have duplicate column names, pay attention, and things like that.
It's got a bunch of great little tricks.
And then, you know how you mentioned Kevin Markham from Dataschool.io and his tips?
You can type DovePanda.tip, and it'll pull up a Kevin Markham tweet.
That's pretty cool.
Like, inside your notebook, it'll pull up, like, some random tip.
Yeah, that's pretty cool.
A whole circle there.
And, like, you can use it, apparently you can use it, not even just in notebooks.
So there's a command line mode where you can set the output to be, you know, there's no inline output to go to.
So you can tell it to print the output to just standard out or to a display or to somewhere else.
That's nice.
So if you're using, you want to have these sort of tips, but you're not using notebooks, you can still get them.
Yeah.
Very cool.
This next one is really simple, but I think some folks will find it super useful.
You know, maybe you picked up that project from someone else at work, and they're not following all the best Python practices.
You see a bunch of import stars all over the place.
And you're like, man, didn't somebody tell these people that import star is not worth it, right?
That there's all these potential drawbacks.
So enter remove star.
Remove star is a command line app you can run or command you can run.
And you point it at either a module, a file, a directory, something like that.
It will go through, and by default, it'll just find the issues where import star is done.
And then it will look at the actual files and say, well, you said import star, but you're actually just, you know, like from collections import star.
Maybe you're actually just using named collections encounter or something like that.
Yeah.
Maybe that's it or tools.
Anyway, you're just using one or two things.
And it'll say, you know what?
You could replace that line with from collections import named tuple, right?
And it could suggest that, or you could actually give it a command to say, no, just change all my files.
Fix it.
Yeah.
This is very cool.
Yeah, it's great.
So it's not that it just says import star is bad.
It actually figures out what of that star is being used and what you should actually write, and then it will write it for you.
Yeah, so my normal operation when I see something like this is just to comment out the import statement and see what breaks.
And that's not the best way to do things.
So this is way better.
I like it.
Yeah, yeah.
It reminds me a little bit of Flint, F-L-Y-N-T, which will take all your strings and rewrite them as f-strings.
This will take all your import stars and rewrite them as proper specific imports.
OMG, I totally forgot about Flint.
We've got a whole bunch of code that we wrote for 3.5 that still has all the old stuff in it.
So yeah, I got to use that.
Well, it's about to get a whole lot better.
Hit it with Flint.
It's so good.
Yeah, definitely.
Awesome.
All right.
Well, that's it.
Remove stars.
There's not a whole lot to it.
It's just a great little command line tool you can use to make your Python code better.
Yeah.
So the last thing I want to talk about today, actually, oddly enough, we didn't plan this, is another.
It came from Brian Rutledge, too.
So the PSF thing that we talked about, the hiring developers came from him, too.
So we've got two stories from Brian.
So thanks, Brian, for helping us out.
Yeah, absolutely.
Thanks, Brian.
Double thanks.
Well, so one of the things that Brian's working on is a pytest plugin called pytest Quarantine.
This is so cool.
Hopefully all your tests pass.
But let's say you've got a, you just implemented, you got really fantastic, you got into testing, and you started writing a bunch of tests, and you put it on a code base, and you got a bunch of failures.
You know you're going to fix them, but you're not going to fix them right away.
So what do you do?
And the idea with pytest Quarantine is it saves a list.
So you run it once, and you tell it to save a list of all the failing tests.
And it saves it somewhere, and you can throw it in Git or something, store it.
And then you run it again with that list, and it automatically marks all of the tests that have failed in the past as X fails.
Now this is something you can do manually to say, I know this is going to fail, just run it as an X fail instead of, it separates it from a failure.
You know, there's arguments of whether that's good or bad, but it's very useful so that you can still use your suite to find new failures while you're working on the old ones.
Anyway, this is a nice little extra tool.
I think it's super cool.
I also wanted to bring this up because he sent me this really nice email.
So apparently I met Brian a couple times at PyCon in Cleveland, and he said he started out as a complete pytest newbie and bought my book, started working through it, loved pytest, and then helped his company to adopt pytest, and then wrote this plugin.
And he wrote it at work and convinced his company to be able to release it as open source.
So that's super cool.
Yeah, that's really great.
Yeah, good work, Brian.
This sounds like super useful.
You know, you've got to make some huge change.
If it breaks 50 tests, you can't start solving all 50 at once, right?
You've got to like chop your way out of them.
So yeah.
Chip away at it.
Yeah, exactly.
Quarantine them and then just, you know, take them one at a time.
So yeah, I like it.
I mean, there are ways in which you can deal with this.
Like in PyCharm, you can say run only this test or run certain ones.
But you know, like it doesn't help you on continuous integration or something like that, right?
So yeah, I think this is great.
And one of the things I wanted to bring up also is I've dealt with this in the past on a temporary basis, of course, where you've got for some reason a breaking change that fails some things.
You're working through them.
And we have occasionally, if there's like a known failure, that the fix is scheduled, right?
It's a, we know about it.
We're going to fix it, but it's not going to be fixed for like three weeks.
You can add X fail to the test itself.
But one of the issues with that is you're to add the X fail mark, you edit the test file.
So one of the benefits of this is you're not actually editing the test file.
You're editing a different file that marks those.
So that's kind of right.
You don't want those changes to show up and get saying, well, we've made all these changes to these tests, but actually, no, we're just trying to fix something else and get them out of our way.
Yeah, I like it.
Yeah.
All right.
Well, that's it for all of our main items.
Brian, you got anything extra you want to throw out there?
I do not.
How about you?
I've got some pretty cool news.
So I recently decided to go through the effort of figuring out how much energy all of our services and servers use, right?
So for like delivering Python bytes and talk Python and Talk Python Training courses and all that stuff.
And I figured out how much that was and went out and bought renewable energy credits to offset all the carbon from all of our infrastructure.
Wow.
That's neat.
Yeah.
So I'm going to keep doing that going forward.
So not a huge, huge amount, but it's, you know, I think a good signal for all the other companies out there as well to say, look, if this podcast or these podcasts can be carbon neutral for their server structure, why can't we?
Right?
Yeah.
Cool.
So anyway, small, but hopefully can trigger some good change.
All right.
Ready for a joke?
I am so ready for a joke.
I need it this week.
Well, it's more science than it is programming.
But I think our audience will generally, generally like it.
So I'm going to tell the joke and then explain the joke because I'm not sure everyone will know, but I think a lot of us will get it.
And jokes are so much more funny if you explain them.
I know.
Absolutely.
They are.
So imagine a time not too long ago.
Dr. Heisenberg from quantum mechanics fame.
He's driving down the highway and he gets pulled over for speeding.
The policeman comes over.
The officer says, excuse me, sir.
Do you know how fast you were going?
Heisenberg pauses for a moment and then answers, no, but I do know where I am.
I love that.
That's so funny.
Yeah.
Thanks.
Yeah.
So the Heisenberg uncertainty principle basically says that the position and velocity of an object cannot both be measured exactly at the same time.
Not even theoretically.
You can know one or the other, but not both.
So, yeah.
He knows where he is.
Yeah.
Funny.
Pretty good.
All right.
Well, thanks for being here.
Good to be back together.
After taking off and hiding in Florida for a while.
Now we're back on the usual track.
Yeah.
All right.
Have a good one.
You too.
Bye.
Thank you for listening to Python Bytes.
Follow the show on Twitter 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.
This is Brian Okken, and on behalf of myself and Michael Kennedy, thank you for listening
and sharing this podcast with your friends and colleagues.
Thanks.