Hey there, thanks for listening. Before we jump into this episode, I just want to remind you
that this episode is brought to you by us over at Talk Python Training and Brian through his pytest
book. So if you want to get hands-on and learn something with Python, be sure to consider our
courses over at Talk Python Training. Visit them via pythonbytes.fm/courses. And if you're
looking to do testing and get better with pytest, check out Brian's book at pythonbytes.fm slash
pytest. Enjoy the episode. Hello and welcome to Python Bytes, where we deliver Python news and
headlines directly to your earbuds. This is episode 247, recorded August 25th. Really? Oh,
we're almost done. In 2021, I'm Brian Okken. I'm Michael Kennedy. And I'm Dan Taylor. So Dan,
before we jump into things, welcome to the show. But can you let people know kind of who you are?
Yeah, thank you. It's great to be here. A big fan of the show. So I'm Dan Taylor. I'm a program
manager manager on our Python team. So I manage a team of program managers that work on our Python
developer tools. For example, our Python support and Visual Studio Code and Visual Studio IDE. And
we also manage some of our engagements with the Python community. For example, our sponsorships of
PyCon and the Python Software Foundation. That's awesome. You know something, Dan, I thought was
pretty interesting. I was just watching a talk from Brett Cannon, getting ready for an interview I did
on Talk Python with Lenga. A lot of stuff going on there. But the talk was only from 2018. And Brett
went into detail describing what VS Code was. I was like, Adam or Sublime and all these things.
It needs no introduction these days, does it? No, no. It's just been amazing watching just how much
it's grown over the past year and become a household name for people, especially in the Python community.
Cool. Well, Michael, how do I keep my computer awake?
Well, first, you make sure it gets eight hours of sleep a day. You don't overwork it. I know it's
work from home. So you might be working on it, playing on it and watching TV. Oh, wait, this is not this is a
totally different thing. So I recently was working on this project around some of the courses over at
Talk Python training. And I needed to do a bunch of video processing and reencoding for like, take the same
videos, but make them smaller. So I wrote some really cool Python code to go through and take
the source videos and do all this analysis and reencode it into a bunch of formats. You know,
that takes like five hours for one of our courses. And if my computer goes to sleep, it's going to take
more than five hours. I got to go and like keep waking the thing up again. Right. Well, what can I do?
I could go into the settings and say, you know what? Don't do that. Just don't go to sleep right
now. But then maybe I'll forget. Who knows? It would be cool if just while my Python code was running,
it would stay awake and then it would potentially not stay awake when it was done. Wouldn't that be nice?
Yeah. Yeah. So I found this cool library called WakePy and the implementation of using this is
ridiculously easy. You just say from WakePy import, keep awake. And then you create a context manager
with keep awake, do the stuff you want it to do while the computer doesn't go to sleep. Done.
That's cool. Yeah. It's super easy. It works cross-platform. It works on windows. It works
on Linux. It doesn't really work on macOS because there's a bug, but it's supposed to work on macOS,
which is where I'm running this right now. So that's kind of unfortunate. Wouldn't you say?
It turns out it's one of these strings versus bytes weirdness. So I actually decided this is kind
of cool. So what I'm going to do is I'm going to create a PR. There's a PR that says it doesn't
work on Python three, but you know what? And here's like the error. If anyone cares, you get this,
a bytes like object is required, not string. So I submitted a PR. The PR is
not yet addressed or responded to or anything. So if you want to use this for the time being,
I can really only on macOS, to be clear, I can only recommend it off of my branch until you see
that PR merge because it won't work at all. But if you're using windows or Linux, I think it'll just
work straight away. And it's interesting if you actually dig into it, I kind of imagined it would
just be doing the same thing. But if you dig into it, like in the Mac version, there's literally a
command you can type on. This is awesome. You can type on the terminal. You can type
caffeinate. That will keep your OS from going to sleep. If you just run that on the terminal.
If you type a dash D, it'll keep the display on, things like that. It won't let the screen go to
sleep. But over on say Windows, it does a bunch of tweaking with threads. So what it'll do is like
go and set its current thread to be like continuous, system required, display required as a bunch of
bitwise flags. And then over on Linux, it uses what else? It uses something completely different.
It runs mask and unmask, which I don't actually know what that does. I suspect it keeps it awake.
So anyway, it's a pretty cool little project. It's very simple, but it also solves the problem that I
can imagine a ton of people doing data science-y like things would run into because they want to do a
bunch of processing and not have their system go to sleep. What do you all think?
I think I'm just glad that I know about caffeinate now because that's cool.
Yeah. If you want to keep your Mac awake, you literally just have to go over and type caffeinate
on the terminal. You don't have to have Python or any of those kinds of things. But if you want your
Python code to control it, this thing is basically subprost juggling that command.
Yeah. I can imagine if you're writing code that's scraping websites, looking for updates and things like
that, you really wouldn't want your computer to go to sleep. And I always find myself messing around
and all the power settings and maxing things out. Exactly. So this is nice.
Yeah. Yeah. Very cool. Very cool. Awesome. Well, that's it for that one, Brian.
Cool. Off to you.
What do we got next? We have, oh, how to create a great stack overflow question. Actually, this is,
this is great. Yeah. Stack overflow just sold for like $1.4 billion. It's quite the site.
Yeah. Who did it sell to? Do you know?
No, I don't remember. It's a company I hadn't heard of.
I didn't miss that. Anyway. So I, you know, I don't really, I actually, of course, when I Google
stuff, I get a bunch of stack overflow answers and I've utilized it. And I had, I like tried to play
the whole game for a little while of like trying to get stack overflow points and stuff. And then I
realized, yeah, this just isn't my thing, but it's a thing for a lot of people of like answering this.
But the reason why I brought this up isn't just to get great answers on stack overflow, but there is a
good trick to that and it's good. But it's also, if you're asking questions really from anybody, if you,
if you send a, any sort of expert that, you know, like maybe if you want to ask a pyramid question to
Michael or a pie test question for me, these sorts of things are, this, this is a, this, the same,
same topics apply. So let's just jump in. This is from Kevin Markham at data school. Kevin's a great
guy. We've kind of hang out with him sometimes at picons and stuff when we had those, but the punchline
of his article, which we'll link to is you need to write a brief introduction and you want to have
self-contained code examples and then detail the expected results and why you want those results.
And then add any other important notes linked to relevant questions. And then also write a title
that summarizes the question. These all seem sort of obvious. So I'm really glad that Kevin went through
kind of an example. So he started with an example that somebody, one of his students asked him about
pandas. And the question really was about data frames and filling in missing values. And it had
a domain specific thing that is somebody was asking him. And then, then Kevin goes through and
rewrites the question as a good stack overflow question. And it's night and day. It's like amazing
to see this. So I really encourage people to read the article, but what a couple of things that I really
love is converting that he doesn't really talk about, but convert the, the example from any domain
specific stuff to a toy example. And that skill of really, and that's a good skill to have anyway,
is to, to say this problem that I'm having in my code, it's really a generic problem. How do I make
that using a toy example to describe the problem? And oftentimes actually you can answer your own
question. Once you get it into a toy example and you realize, Oh, I'm just, I'm not, I'm overthinking it.
So this is a good first step. and so this is, that's a great thing to see, but, this is a great
example. Cause it's a, it's like some architecture problem, but then he turns it into colors of toys.
Um, and it's a, it's an easier problem to see. The other thing is making sure that two examples runnable
with all the import statements and everything. So somebody can just flop it into, their own editor
and run it to see, you know, if, if, if, if they can see the same problem and see if they can figure
out. That's good advice. If it's too specific or you don't have the data or it's not complete,
you have to speculate rather than actually fix, you know, verify you can fix the problem or not. Right.
Yeah. And the part that I, one of the things that I wouldn't have thought of is linking to other
questions because there's a fear, I think of moderators and also other people reading it is,
this is probably our, it's, if it's simple, it's probably already been answered. What did you
already look or whatever? And the, the act of linking to other answers to say this question over
here doesn't really, it kind of helps me, but it's missing because of this or something, some reason
why the other answers, the, and that that's an interesting take on it just to, so that people
don't dismiss you right away as somebody that didn't do any research to begin with. So.
Yeah, for sure.
Yeah. It's cool. Usually by the time I get to stack overflow, I'm pretty desperate, for help.
And so forming a really good question. So you get the best answers and, is, is really important.
I really like that narrowing it down to a reproducible example.
Yeah. And people can be mean on stack overflow and on Reddit and on other, and on YouTube and other
places on the internet. But if you show that you've tried, I think that will disarm them somewhat.
Totally.
Right. I mean, I've seen questions like I have this homework question and I can't do
my homework. Could anyone help? And I get that email too. And like, no, no one can help. No
one wants to do your homework for you. Yeah. So no one should, but if you have a legitimate
problem or you think you found it a legitimate bug or there's just no documentation and it's
clear you've tried, I suspect that'll disarm people. They may still not be able to help you,
but at least they won't be mean. Hey, Brian, before we move on to the next one, I want to
do two quick real-time follow-ups. So one stack overflow sold to tech giant process process.
We've all heard of them for $1.8 billion. So that is not messing around. they, that's
that forum site did all right. Jill Spolsky and Jeff Atwood did okay. And then the other
one is, unfortunately Dan Heford out there in the live stream said he just ran caffeinate
three times on his Mac and he can't get it to sleep and it's acting jittery and anxious.
So, I don't really know what we can do about that, but maybe we just go to TikTok. What do you
think? Yeah, totally. So have you ever, have you ever really wanted to make a quick edit to
something you've gotten a GitHub repo? but you're kind of stuck with that, that rudimentary
feels like you're working with notepad maybe a little. Yeah. So you click the edit, there's a little
edit thing. You find the file, you edit it, and then you go in there and there's like no help. And
you just type away. Yeah, I do that. I don't love it, but I do that. It feels like
your notepad. Well, GitHub announced, GitHub code spaces, recently, which is a cloud
hosted development environments with powerful machines and you have to pay for them. But one
of the fun bonus features that they launched at the same time that we only really announced on TikTok
and other social media, is, is called github.dev. So you can actually go to any, github repo
and say, I just want to edit this github repo that I have here. And I can just press the dot key
and that will reopen this, this github repository in something called github.dev. So this is an
entirely, web browser based version of VS Code that's built right into github. It works on any
github repo. and there's no server behind it. It's serverless. So there's nothing that you need
to pay for here. and so it also has some limited functionality because there's no backend. This is all
just, front end running in your web browser. but, what's really cool is that, you can
come in here and, and you can edit, you can add new files. Like I can go in here and add, you know,
a new Python file, in the file explorer on the left, I can just click and add a new file and I can
work with this more like I'm used to working with a text editor. and as I can just say imports,
flask, for example, and because we've, there's support for different extensions that are
web enabled that, that know how to work in this new mode. So we've, we've actually added the Python
extension recently in here. So I can actually, get some autocomplete from, from, some of the
modules. So if I say from flask import, flask, I get the autocomplete for that flask object.
And I can do the typical thing where I can say flask equals name and I can type app dot,
and I get, all of this, you know, add template filter route, all those in the autocomplete. So I
can actually get a, a more real editing experience with that productivity, things like that,
that I'm used to when I'm working with text editor, but this is all using the, the storage file system
from the web browsers, local storage, as well as the files from the GitHub repo.
so if, for example, if I go in here and just, change hello world, hello Python bytes,
um, in this code here, and I hit save, on a piece of code, and then I see I've got source control
changes here and it's telling me that I can click on that file and I can actually see a diff of that
file, side by side. And I can just, commit that using the source control panel. And if I,
if I add this commit, it'll go directly into my GitHub repo. so that's pretty cool.
So a lot of the, that's amazing. I do see right below the source control thing, a, a play debug
thing. so, so that doesn't, that doesn't work yet. Does it? Yeah. So that, that's what there's
like continue working on code spaces at that point. You got to get a real machine for, if you want to
do things like the hosted, the real hosted version. Yeah. Yeah. But, but there is some, we're actually
pushing the boundaries of what you can do in the, in these web versions in some interesting ways. So you
can get basic syntax highlighting as, as you saw autocomplete. but there's no Python interpreter.
So when I go to definition, say, I want to go to definition on this flask module, it just brings up
type hints, the pipe, the type hints that we have. I see the type shed type hints. Okay. It's still okay
though. It's still better than like what you get with the normal editor. Yeah. It's pretty cool. One,
one kind of bonus thing I want to throw in here is that, so if you go to the extensions tab,
you'll, you'll be able to search and you'll see which extensions are web enabled. And one of the fun
ones that, somebody on our team put out is this VS Code pyiodide one. so if you actually open a
Jupyter notebook, you can, you can run a little bit of Python code from these cells, here, using
pyiodide. So that's pretty neat. Oh, wow. So this is WebAssembly for the win. Yeah. So that's
github.dev. I think it's a really cool how much more you, you can do, right from the browser
within, within github. I'm excited to see where this goes. Ryan, did you know about this? I didn't.
And I was just playing with it in the background on my own, repo. I was, I haven't, I was
looking through the extensions and maybe, you know, off the top of your head, Dan, can I get the Vim
mode in this extension? well, if you, you'll see which ones are, so if they're, if they're
highlighted here and not grayed out, so the, this Vim extension works, so you can, you
can see which ones are available. It says available in VS Code Web. You just made Brian's day. There's
a couple of Vim options. Yeah. Yeah. I can't, I just can't use any editor without Vim mode anymore.
Yeah. My brain just doesn't work without. How do you use Google Docs or like other editors or
anything like that? What's that? How do you use Google Docs or some other editor? I paste into
that after I've written the stuff in some, in somewhere else. Nice. Dan, when I first saw
this, I was, I was on my GitHub repo and I'm like, Oh, let me just hit dot because it seems
like that's too simple, but I'll try it. And then boom, it came an editor. I'm like, Oh my
God, this is awesome. And then I went into the settings and I put it into dark mode and I
changed the font size and all the font family. And I'm like, Oh, this is, this is nice. Like
this is a really cool. Yeah. I'm, I'm excited about it. Let me throw one more bonus in here.
So if I change this to say, edit this, a Jupyter notebook cell here. And if I go into the diff of,
if I go into the source control panel, after editing this Jupyter notebook cell, I can actually see a
real, a real diff side-by-side with those, with those cells. So that's, that's something cool you can
do without having to install a full editor and things locally.
Very cool. Yeah. And it's a nice diff of the cell contents, not like the JSON crazy diff.
Yeah. You get the left, red, right, left, right with red, green, add, remove. Yeah. And outputs are
squashed. So you're not seeing a bunch of, if you diff a notebook, you'll see a bunch of XML and JSON
being removed and added. This is, this is more of that rich.
Take all of the various encoding language storage formats and then jam them all together and then
diff that. That's fun. Yeah. No, this is awesome. This is a great view.
Click it. I'm going to use it.
Indeed.
So one of the things that I don't really edit on and get very much, what happens like once you're
done, does it create a merge request or just edit it in place or?
It makes a commit directly to the repository. So I can just throw that right in there.
And so you have to have, you obviously have to have permissions. You have to be one of the people
that can commit directly to that branch. Right. Yeah. Yeah. This is great. I was so delighted when
I saw it. So I'm going to definitely use this. Nice. All right, Brian, am I up on the next one?
You are.
All right on. Let's move on to it. Okay. I'm resisting the temptation to hit dot and play around with the
wake pie. Instead, let me take you over here. So we go to Python. That's, that's not Python bytes.
Close, but not exactly. Gosh. And they don't even, why did I type com? I've never, hold on.
Python bytes.fm. That's our domain. So we're live streaming right now, by the way, if you didn't
notice. If you go over here, notice I've got Firefox that I'm using and it has this little shield and
almost anything you go to, man, I want to go to a new site, but let's go to CNN and you'll come over
here and you'll see a bunch of stuff up here getting blocked, like social media trackers,
tracking content. Like, look at that. That's a ridiculous amount of gross Google analytics,
Google tag services, Rubicon project, US East, apparently like that's bad. Right. So I've done
a lot of work. Get us away from retargeting tracking. And we have no known trackers.
And I'm running a VPN, which probably blocked a bunch of the ones off of CNN. So that was the
ones that got through the blocking and stuff. So it's really nice to not have all that stuff and
let people come visit the site and know that we're not retargeting them. We're not tracking them. We're
not doing stuff. We're just giving them a podcast or same thing over on talk by then training or talk
Python or whatever. Right. You go there, you get the content. We offer things like a podcast with ads
that people can take. And that's enough. We don't need to like follow you around for all sorts of
creepy reasons. Right. The drawback is we don't have a lot of analytics. Right. We can get a sense
about like download numbers and things like that, but we can't report like, well, in the last hour,
since we talked about this thing, here's the traffic. Can we, or would we? So, junction apps
over on Twitter said, Hey, you know what? I heard you going on and on about, the retargeting of all
these places and how you dropped it. Have you heard about go access.io? Have either of you heard
about this thing? No, no, I hadn't either, but check it out is super cool. So what it is it is a
Google analytics like service that you install on your server. And what it does, if I put this image
in a tab, so it doesn't go away. What it does is it looks and tails your logs and gives you real-time
analytics on your logs rather than by looking at, going through JavaScript and hooking into
people's browsers. So no tracking, none of that stuff, but you can come over here and in your
terminal, run it and get a real-time view of your traffic, your visitors, all the kind of stuff you
would expect from Google analytics visually as a graph-like thing in your terminal. Interesting,
huh? It's really cool.
Yes. What kind of information are you getting?
Well, probably the easiest way to find out is they have a web view and have the terminal view
and the web view is really generate a HTML file and then just request it. So there's not like a web
server type thing, but you go over here and click, it says here, I'll give the, give you the features
real quick. Then open up the demo. It says fast real-time millisecond, based, you know,
latency updates written in C only uses in cursors of the dependencies works on almost all the formats
engine X amongst the others. just set the log format and run it on your log, got the terminal and
the UI, the web UI bit. So if you go over here, you can see like, look at this off of just the log
files. So you can see things like here, I'll pull them up. So you can go over and say, well, what are the
referring URLs? let me make that small. So you can kind of see in here, unfortunately,
here's the referring URLs. So you can see in this example, they were, you know, shop internet,
whatever, right? At least it'll, it's a fake site, but you can come over here and like actually see
graphs as well as like grids and pages. So like think Google analytics, but if you run a site and you
can point this against your logs and get real time information in the terminal or as in this web view.
And yeah, it's, it's pretty neat. And so do you, you don't, do you have to install anything or do
you just run this and it finds the logs on your system and pulls it all together?
Yeah, you do have to install. Let's see here. so there's a getting started and it says the way to
get started probably to use a package manager for your Linux machine. If you're running on Linux,
or you can get a Docker image and you run it. And then the, what you do is you just simply run the
command against your log file and, and tell it what format and boom, off it goes. So you could
set it up as a, like a cron job or something that will then generate the HTML file. And you could just
refresh that, you know, however often you want to, or run the terminal one.
You could run it once a day too.
And yeah, you don't need, save the report.
Exactly. Yeah. You don't need real time, real time. You need just sort of how's the day going
or something like that. How was yesterday?
Right. So like one of the things that I use information like this about is I don't really
want to track users. I don't care about individual users, but I do want to know,
like I've got putting out different content on my blog or on a podcast, which ones, which,
which ones are resonating with people and maybe do more of that. yep. So,
so you can see like, there's a little bunch of options here for insights, hosts, requested
files, they call it, but it's really just URLs. so you go over here and this would tell
you sort of the traffic, you know, across your various, pages, right? So this would be
like your list, this, requested files, URLs are the one you'd want. Yeah. Yeah. So anyway,
I haven't set this up yet, but it looks pretty neat and it could be worth checking out.
Yeah. Neat. And I guess even you could download your logs.
And not install it on your server. If you really wanted, right? You want this view,
but you don't actually want to put it on your server because putting stuff on the server.
Well, you know, so you could download it and then just run against your logs locally.
And you might be uploading your logs to a storage account somewhere too, on a cron job.
Yeah, exactly. You could push the logs out instead of, running it there. So
a bunch of cool ways to use this, but if people are running web apps and they want more visibility
into it, this, this looks like a cool thing. So thanks, junction for sending that over.
All right. Off to you, Brian.
I want to talk about keyboards a little bit. So this was a topic and talk a topic recommended by
somebody named blaze blaze, I think. so there's a, a package called KMK. It's actually a collection of,
uh, collection of pieces of software, but it's, the, we're linking to the KMK firmware.
So this is firmware for computer keyboards written and configured in circuit Python.
So got super excited about this. I'm still kind of excited about it. the gist of it is,
is, you've got, if there's, if you've got a keyboard that, that runs a, run circuit Python
and it's powerful enough to, to run, to handle your keyboard and stuff. So there's,
there's a couple of tips in here. There's a couple available through Adafruit, of different,
um, different boards that would work. We'll get into the details a little bit more, but, I,
basically you can configure a keyboard with a single file, single Python file. You can, it controls
both, split keyboards, two pieces, keyboards, and single keyboards. You can, hook up macros,
things like, chainable macros, to, so you can, have key sequences built into one,
one key, or you can hook it up so that a key hitting multiple times, you can hit like a particular key
three times in a row and you'll have one action versus something else. So, even controlling
under glow and LEDs and backlights and all that sort of stuff totally would be fun. I want to use this.
I don't know how it seems like a lot of work though. So, so we're linking to the, the,
the, the project and there's some information here. There's some guides on how to get started.
And then we're also going to run, I, I found somebody that ran this, and, and the, he's
got a video that walks through, doing it. But the gist of it is, is there aren't any keyboards
that do this right away. So you've got to take an existing keyboard, rip out the existing circuit
board and replace it with one of the 80 fruit board, 80 fruit boards, and then, and then reprogram
it. So if you're the kind of person that's okay with ripping the circuit, circuit board out of
your keyboard and replacing it with something else, that'd be great. And I actually wouldn't mind doing
this. I think that'd be fun, but I'd kind of like to know which keyboards I can do this with and which
ones I can't. I know there's a huge list of keyboards out there, but even just a couple
like starter kits would be great to hear.
It really works well with this one. And here's where the circuit Python board fits in there easily
rather than, yeah, it doesn't really go back in.
Yeah. So one of the reasons why I'm bringing this up is I'd love somebody from Adafruit or from,
work, anybody working on circuit Python or the came came K team or somebody to, maybe put a couple more
tutorials out to just say, Hey, this is how you do it for like, I'd love it to have like an off the
shelf full keyboard, even if it's small or whatever that it said, okay, here's, here's,
here's how you do it. You pull this circuit board out, put this other one in, it's like $40 on Adafruit.
And there you go. You can, you can try it. And here's a sample of file that you would run it,
run just a normal keyboard, usage. And now, now you can customize it because the, just getting back
to a workable keyboard seems already like a lot of work, but it still looks fun. I'd love to be able
to hack a keyboard with Python. Very cool. Dan, what kind of keyboard do you use? well, I use,
I'm a big fan of the thin keys, right? The very, the very light travel keys, but I got the ergonomic
keyboard. so thin ergonomic Logitech, not into the, I've got the, I've got the, Microsoft
sculpt ergonomic, which I absolutely love. I know Brian's got even more exotic stuff that he's
running over there. I don't know if I can get mine up here. I'll, I'll, I'll do the camera.
It's like, it's mounted to the, no. Oh, my junk too. I've got a Kinesis.
Yeah. The Kinesis inverted ones. Those are super interesting. What I would like to see would be
some keyboard manufacturer saying we're shipping a keyboard to you. Oh, and you can program it with
Python and basically do this for you as a package thing and just, you know, build it out of these
components. That'd be fantastic. Yeah. That'd be great. Well, I think it'd be fun with, with everyone
decking out their home offices these days. If you could do a little bit of magic light show while
you're presenting or talking on a call, that would be really cool. That'd be fantastic. Yeah. Hey,
Brett Cannon's out there in the live stream. Danny may have heard of this guy, Pablo,
Pablo, Delgado has programmed his keyboard RGB lights to color code the build bot status for
CPython. I don't know which library uses. That's awesome. So like it's green if everything's good,
but it turns red if CI fails or something like that. Oh, that's awesome, Brett. Thanks for sharing.
That'd be cool. You know, I never really, I mean, when I first saw the, the LEDs under the keyboards,
I thought, you know, yeah, gamers like it, but I don't think I would probably have a use for it,
but now I'm jealous. I want, I want, I want to light up things on my keyboard. It'd be fun.
Yeah. Especially if you could program them, right? Like this, this example here, my mouse
sits here and pulsates and changes color, but because it's on a Mac, the software doesn't work
and I can't do anything with it. So it's, it just annoys me there. It's a good mouse otherwise,
but anyway. So is it a windows only thing or what? No, the mouse is perfect on Mac,
but the software that lets you customize it and do interesting things to the colors doesn't work
on the Mac. Lame. Super lame. Super lame. All right. What is not lame is this last thing that Dan has
got here. Cause this thing has been blowing up on Twitter. It's all of a day old with like 2000
GitHub stars. And I was like, Oh, this has got to be covered. And then Dan, you were on it.
Yeah. I just saw it yesterday and I was like, I have to talk about this.
So I, I know that when I've built web apps in the past, one of the big frustrations I have is
that when you want to add something new to your database, you need to add it to the SQL queries.
You need to add something to the backend code. You need to add a piece to the API that, that then
returns that, that new object to users. And then in your client code, you need to consume it. And so
there's all this repetition that you're doing across your web app every time you need to
make a change to your data or add a new object. And that's very error prone as well.
And so things like SQL, SQL, SQLAlchemy, you can use to write code that interacts with your
database. And then things like FastAPI, you can use Pydantic to return models to your users,
to the, to your APIs. But then that now, now we had this thing where, where you have SQLAlchemy
models and Pydantic models, where, and then you're translating back and forth between them.
And so, Sebastian Ramirez, who just, just released yesterday, this new SQL model library, which looks
just awesome because it actually combines the schema for talking to the database and speaking to your API
into one, one schema object that you write. So, what's really cool about this is that, so the way you work
with this is that you define your objects. For example, he's got in the GitHub repo, this hero class
with an ID name, secret name, age. And it's, it's got some of the SQLAlchemy type things like it's your
primary key is the, the ID is optional, but it's defaults to not its primary key. So what's really cool about it,
this class is both a SQLAlchemy model and a Pydantic model. I don't know how he did that. He said there's
some magic that's going on behind the scenes. And so what's really cool is that you can use this to
instantiate your database. So you can, you can, so you've got this hero class, you can create objects in
the database by going hero one, hero two, hero three equals hero bracket and pass the name, secret name,
all the fields to the object. And, you can also return those same objects. You can use that same hero
class and use it in your FastAPI. So you can, accept hero objects. you can, return hero objects. And so that's,
that's one, you write that one schema and you, and you use it in multiple places. Yeah. What you have on the screen here for a FastAPI is
response model equals the database entity, which that then drives the open API documentation
in addition to just driving. Yeah, exactly. You got it right there. So you can go there and
it's sort of end to end the same model, which I think is really great. Yeah. And so you can look at your
in, with FastAPI, if you go to slash docs, you get the, the swagger, UI. And so you can see that same
schema with the ID name, secret name age. And, what's really cool about that is that it also has,
this generates the open API.json. And so you can use tools like open API generator to even generate
client code and all the, any different language that you're using like Python, JavaScript, Lua. And,
so, so you can, if you just went back to that original hero class and added a new field, you could
potentially have it in all different layers of your application, with one, including the consuming
clients. Cause they could regen off of the, the JSON schema. Yeah. Yeah. So I think that's really cool.
And, and one other really nice thing that, that Sebastian did here is that he really emphasized
making sure that in the editors that you have a good experience with autocomplete because,
in the past stuff like the Django ORM and the SQLAlchemy ORM, the editors have had to write
custom code to be able to have a good autocomplete experience for that. and so Sebastian used,
type hints, really well throughout the design of this so that as you're, returning objects from
the database, you actually get, you can, once you return that object from the database, you actually get,
um, autocomplete on it. I can show that here.
Yeah. That's really cool because a lot of times you just get star, star KWR, you know, like, oh, thanks.
Yeah. So I got this statement, select hero, where hero name equals hero name, and then the object
that returns, I can go hero dot, and then, sorry, that was working.
Put it at the very end of the line, maybe. Yeah.
Or in the next line. Yeah. Yeah. yeah, I can, so I can access all the, the properties on,
on the hero object. one, one cool thing about, how he did this. He used
a, a proposal from, Eric Trout, the author of PyRite called the data class transform
that, it's this proposal that basically you can annotate various objects to say that they behave
like data classes. And so that that's kind of the magic that allows some of this dynamic behavior to
show up in, in type checkers.
I love it because so often you'll do a SQLAlchemy query or something and what you get back, it's
so many layers. It's lost what that type is.
Yeah.
It's like, ah, now I got to like re-annotate it again.
I got to search for everything. Look at all the different source code in my code base and stuff
like that. So this is really cool.
We've got some great comments in the chat. here's, here's one. I swear Sebastian spends as
much time on his docs as he does his code. Amazing talent. I got it. And we were talking about
type hints and the editor experience as well. Sebastian does a great job with the onboarding
experience as well as the, youth, the developer experience. And a lot of large companies could pay
attention to what this one person is doing, making it easy for people to pick up a new tool. It's amazing.
Yeah. I mean, look at the docs here. These are, this looks like a professional team wrote all this
stuff, right? This is, this is incredible. Instead of one, a very productive guy.
And then, Brett, good question. I want to know how Sebastian manages to release a new,
very cool project every single year. No kidding, man. He has a powerhouse. He's like changing how we
do web development. and, and it's just one person. But yeah, you know, I, I think I can,
speak to that just a little bit, by the way, if you look at the magic of FastAPI, you look at
what's happening here. These are really nice things to work with, but they're not from the ground
recreations of what they are. This takes Pydantic plus SQLAlchemy and makes it better. FastAPI
takes Starlette and Pydantic and makes them better, right? It's like the recombining of things that are
already really good into something better. Yeah. And some cool things that, that come as a result of
that repackaging. So according to Sebastian, this should support async database sessions out of the
box, because that's something that SQLAlchemy supports, Alembic migrations as well.
and in theory it should be possible to integrate with post gists and TS vectors, for doing
geo stuff and full text search with, with, with Postgres, but he says he hasn't tested it yet.
Yeah. I have no idea, but potentially async and await as well. Now that SQLAlchemy supports that.
Yep.
But again, this is a day old, so we haven't really had a chance to dig in it too much. I want to highlight
just a couple more comments. Savannah on the live stream says, Sebastian's attention to developer
experience is so awesome. Yeah, I agree. Keep it up, Sebastian. And Rehan says, it looks great for a
toy case, but people make professional interior systems. do they really want to share the same
types between their database and their rest API? That's a good question. I, I was thinking about
that as well. One of the challenges that you can run into when you just sort of say, oh, here's my
data model out onto the internet as JSON. And then take, especially taking it back as you can run into
like situations where you overexpose information or you run and run into a mass assignment type of bugs
where values get set or you didn't expect them to be set. So with Pydantic, you can do things like
call, you have to call to JSON, but you can also, or to dict, I can't remember. I think the dict,
but you explicitly opt in or opt out certain fields. So you could do things like that about
bringing those in. so pretty neat, but yeah, you gotta be a little bit careful about that.
Uh, but yeah, very cool stuff. You don't want your password hashes on your user object.
Exactly. If you're going to return your user, you don't necessarily want to exactly return,
say the password hash or God forbid, not just the straight password.
If you're not hashing it, but that's a bigger problem, but still.
Oh, I wanted to bring up one more thing is, his, his choice of heroes in his example.
Love these. we've got dead pond, spider boy, and rusty man. hilarious.
Love it. Yeah. Those are great.
Well, so those are our six items. do we have, any extras? Do you got anything to share?
Right. sometimes I have extra, extra nine times here all about it. I got none this time. How about you?
Um, really? I've got like two.
So I bring one.
Um, so, first one I wanted to bring up was, really fun episode. people know I also do a podcast called testing code episode 163. does, it's a talk with, Stefan Bedul about, pip install of local directory.
And, so if you're, if you're doing anything where you, do the pip install of a local directory, definitely check that out. Or especially if you never knew that you could do that. it's a good episode. the other thing I wanted to bring up, is, it's been like five episodes since I plugged my book, but no, I, there's a chapter 12 is up.
So chapter 12, the first, one of the things I got, questions about for the first edition was it's the whole book is talking about testing packages and there's a lot more in the Python world than just packages.
So chapter 12 is a reaction to that. It's testing scripts and applications. So it's not a pivot. If you don't have a pip installable package, you can still use pytest to test it. And chapter 12 directly addresses that. So I wanted to bring that up.
Awesome. Alvaro out in the live stream says, how is the second edition testing mode going?
Yeah. It looks like chapter 12 is coming along. Well done. You're basically done, Brian.
You're on vacation.
Well, there were less than 10 chapters in the first book, but now I'm, I'm working on chapter 13, planning on 17 altogether. it's going to come out in hard copy early next year.
So super.
I have one extra.
How about you, Dan?
Yep. So one, one cool library that I wanted to share is a type for pie. So we've been talking about types a lot lately. Seems like it's a, it's a regular topic of conversation.
And, they, they can provide a lot of benefits to your code base and to text editors, like VS Code or, or PyCharm. And, but what happens if you want to retrofit or add a bunch of types to a code base that's not typed already? That's, that's a big daunting task.
And so I, I came across this library, you know, shared amongst our team. And what I thought was really cool about it is that, it's a state of the art machine learning model for inferring types.
So we've, we've talked about before, Hey, wouldn't it be cool if we could just go generate all the types for all the packages out there so that we could have great autocomplete on everything.
But it turns out you can't because you can't do that until you actually run the code and have the objects to bind it to.
And so, this machine learning model is sort of the latest in a line of, of attempts, from different researchers to create a good machine learning model for inferring types.
Um, what, what I like about it is not only is it, it sort of best in class in terms of its, accuracy is that the, the author of this machine learning model,
open-sourced the training data set with, with 4.3 million type annotations in it, the training code.
So you can go in and actually, use, train this on your own code, following some of these very simple commands to, to train the model and pre-processed, process the data with your own code base, open source to a VS Code extension.
So you can actually have developers using it.
It's not just some, Hey, this is our results.
QAD, go read the paper.
It's, it's actually something that you can use.
Um, and so if you're looking at trying to retrofit a bunch of types into your code base, this is something you might want to try out.
Uh, just one sort of quick caveat that, that the sample VS Code extension uses the machine learning model that's hosted on type4py.com.
And so if, if you're not working on an open source project, you, you probably want to, fork the extension and, stand up the machine learning model, using the web server that, that he links to.
Yeah, that's a good bit of warning.
But this looks super cool.
Uh, it seems like one of those things you could do a first pass, retrofit it, and then go back and check it out, run my pie against it.
Um, you could even go to Pydantic and use the validate decorator and put that on all the things and make it, then run your tests.
The validate actually verifies the type annotations really match the runtime behavior.
Um, so yeah, a lot, a lot of neat things.
Excellent.
Brian, did you think that was funny?
No, no, not funny at all.
This might be funny.
We, we need something funny.
Yeah.
So we've been doing a lot of geek and poke lately and, yeah, this is a really good one.
So, you know, we talk about testing and we talk about continuous integration.
If you want to take that, like you go from testing to CI to the next level, we're talking continuous delivery, right?
I'm going to get pushed to the production branch.
That's going to kick off the CI, which is going to kick off a deployment.
Glorious, right?
Yeah.
Well, we've got a cartoon around it.
So there's this, man, a woman developer pair, and they're just looking like perplexed at each other.
It says for five days, I've been doing nothing else than trying to deploy this blinking application on our production environment.
Title continuous deployment.
That's one way to interpret that.
Literal.
Very literal.
Yeah.
All right.
Well, hopefully that was more funny.
Yeah.
So I've got a not very good joke, but I thought it was funny anyway.
Um, it was, shared to us.
Um, it was first from, Carla, not a robot.
It's a great handle and, told to us by blue fiddle guy also.
So, joke is if a programmer gets an interview because of a recommendation from a friend, are they being passed by reference instead of value?
I love it.
Definitely passed by reference.
Definitely.
Well, this has been fun.
So thanks everybody.
Thanks Dan for showing up.
Thank you for having me.
Yeah.
Thanks Brian.
Thanks guys.
Thanks everyone out there.
Bye.
Thanks for listening to Python bytes.
Follow the show.
Follow the show on Twitter via at Python bytes.
That's Python bytes.
That's Python bytes as in B Y T E S.
Get the full show notes over at Python bytes.
If you have a news item we should cover, just visit by the bytes.
And click submit in the nav bar.
We're always on the lookout for sharing something cool.
If you want to join us for the live recording, just visit the website and click live stream to get notified of when our next episode goes live.
That's usually happening at noon Pacific on Wednesdays over at YouTube.
On behalf of myself and Brian Okken, this is Michael Kennedy.
Thank you for listening and sharing this podcast with your friends and colleagues.