Python Bytes - #247 Do you dare to press "."?
Episode Date: August 26, 2021Topics covered in this episode: Keep your computer awake during long processing How to write a great Stack Overflow question Github.dev - press ‘.’ to edit code in any GitHub repo Log analyzer ...(minus google analytics) KMK: Clackety Keyboards Powered by Python SQLModel - use the same models for SQL and FastAPI Extras Joke See the full show notes for this episode on the website at pythonbytes.fm/247
Transcript
Discussion (0)
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 TalkPython 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 TalkPython Training.
Visit them via pythonbytes.fm slash courses.
And if you're looking to do testing
and get better with PyTest,
check out Brian's book at pythonbytes. 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.
And 2021. I'm Brian Ocken.
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 TalkPython
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, Atom 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. 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 a totally different thing. So I recently was working on this project around some of the courses over at TalkPython training, and I needed to do a bunch
of video processing and re-encoding 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 re-encode 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 Mac OS because there's a bug, but it's supposed to work on Mac OS,
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 3, 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 go and set its current thread
to be 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 subpros 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
in 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. 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 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.
How did I miss that?
Anyway.
So I, you know, I don't remember it's a company i hadn't heard of how did i miss that anyway um 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 send any sort of expert that you know, like maybe if you want to ask a Pyramid
question to Michael or a PyTest question for me.
These sorts of things are, this is the 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 PyCons 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 somebody was asking him.
And 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 a couple of things that I really love
is converting that he doesn't really talk about, but convert 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 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 because it's like some architecture problem, but then he turns it into colors of toys.
And it's an easier problem to see.
The other thing is making sure the toy example is runnable with all the import statements and everything.
So somebody can just plop it into their own editor and run it to see 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 moderators and also other people reading it is this is probably 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. And 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. 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 really good questions so you get the best
answers 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 they're 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 ProSus, ProSus, we've all heard of them,
for $1.8 billion. So that is not messing around. That forum site did all right.
Jill Spolsky and Jeff Atwood did okay.
And then the other one is,
unfortunately, Dan Heifert 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 should just go to TikTok.
What do you think?
Yeah, totally.
So have you ever really wanted to make a quick edit
to something you've got in a GitHub repo,
but you're kind of stuck with 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.
Feels like you're a Notepad.
Well, GitHub announced GitHub Codespaces recently, which is cloud-hosted development environments with powerful 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 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 frontend running in your web browser.
But what's really cool is that you can come in here and you can edit,
you can add new files.
Like I can go in here and add 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 there's support for
different extensions that are web enabled that know how to work in this new mode. So we've
actually added the Python extension recently in here. So I can actually get some autocomplete
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 classical 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 more real editing experience with the productivity, things like that, that I'm used to when I'm working with text editor.
But this is all using the storage file system from the web browser's local storage,
as well as the files from the GitHub repo.
So for example, if I go in here and just change
hello world to hello Python bytes 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 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 uh so
so that doesn't uh that doesn't work yet does it yeah so that's 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 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 type hints that we have.
I see, the TypeShed type hints.
Yeah, TypeShed type hints.
It's still okay, though.
It's still better than what you get with a normal editor.
Yeah, it's pretty cool. One kind of bonus thing I want to throw in here is that
if you go to the extensions tab, 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 run a little bit of Python code from
these cells here using PyIodide.
So that's pretty neat.
Oh, wow.
So this is...
Web assembly for the win.
Yeah.
So that's github.dev.
I think it's really cool how much more you can do right from the browser within GitHub.
I'm excited to see where this goes.
Brian, did you know about this?
I didn't.
And I was just playing with it in the background on my own repo.
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, you'll see which ones are.
So if they're highlighted here and not grayed out.
So this V this extension works
um 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 them 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 What's that? How do you use Google Docs or some other editor?
I paste into that after I've written the stuff
in somewhere else.
Nice.
Dan, when I first saw this, 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 became 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 nice.
Like this is a really cool.
Yeah, I'm excited about it.
Let me throw one more bonus in here.
So if I change this to say,
I edit this 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 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 diff side by side
with those cells.
So 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, right with red, green,
add, remove, yeah.
And the 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 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 let me use it indeed um so one of the things i don't really edit on
and get very much uh what happens like once you're done does it create a merge request or just edit it in place? It makes a commit directly to the repository.
So I can just throw that right in there.
So you have to have, obviously, you have to have permissions.
You have to be one of the people that can commit directly to that branch, right?
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 uh 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 don't even why did i type com i've never hold on python
bytes dot 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 wanna go to a news 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 to get us away from retargeting and tracking.
And we have no known trackers. And I'm running a VPN, which probably blocked a bunch of the ones off of work. Get us away from retargeting and 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 TalkPython training or TalkPython or whatever. 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 goaccess.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, 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 they 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 you the features real quick. Then open up the demo. write 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 you the features real quick.
Then open up the demo. It says fast real-time millisecond-based latency updates written in C.
Only uses incursors as dependencies.
Works on almost all the formats, Nginx 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?
These little, 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,
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 pretty neat.
And so 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 is 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. is 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 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 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 the podcast, which ones, which,
which ones are resonating with people and maybe do more of that.
Yep.
So you can see like there's a little bunch of options here.
Burnside's host requested files, files they call it but it's
really just urls uh so you go over here and this would tell you sort of the traffic you know across
your various um pages right so this would be like your list this uh 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.
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 to uh on a cron job
yeah exactly you could push the logs out instead of uh 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 uh this this
looks like a cool thing so thanks uh 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 package called KMK. It's actually a collection of pieces of software,
but 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 uh the gist of it is is
you've got um if there's if you've got a keyboard that that runs a run circuit python and it's
powerful enough to to run uh to handle your keyboard and stuff.
So there's a couple of tips in here.
There's a couple available through Adafruit of different boards that would work.
We'll get into the details a little bit more,
but basically you can configure a keyboard with a single file,
single Python file.
You can, it controls both split keyboards, two pieces of keyboards and single keyboards.
You can hook up macros, things like chainable macros to, you can have key sequences built into 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.
We're linking to the
project
and there's some information here. There's some guides on how to get started. And then we're linking to 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 found somebody that ran this,
and 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 Adafruit boards
and then reprogram it.
So if you're the kind of person
that's okay with ripping the 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 anybody working on CircuitPython
or the KMK 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 said, okay, 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 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 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 light travel keys,
but I got the ergonomic keyboard uh
so thin ergonomic logitech not into the i've got the uh i've got the uh microsoft sculpt ergonomic
which i absolutely love um i know brian's got even more exotic stuff that he's running over
there if i can get mine up here i'll i'll do the. It's like, it's mounted to the, no. Oh,
my junk too.
I've got a Kinesis.
Yeah.
The Kinesis inverted one.
So 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 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.
Dan, you may have heard of this guy uh pablo pablo uh salgado has programmed his
keyboard rgb lights to color code the build bot status for c python 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 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 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
because this thing has been blowing up on Twitter
all of a day old with like 2,000 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 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 then returns 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 Alchemy, 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 your APIs. But then now we had this thing where you have
SQL alchemy models and Pydantic models, and then you're translating back and forth between them.
And so Sebastian Ramirez, who 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 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 got some of the SQL alchemy type things like it's your primary key is the ID is optional,
but it's defaults to its primary key. So what's really cool about it, this class is both a SQL alchemy model
and a Bidantic 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've got this hero class.
You can create objects in the database
by going hero1, hero2, hero3, 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 fast API.
So you can accept hero objects.
You can return hero objects. You can return hero objects.
And so you write that one schema and you use it in multiple places.
Yeah, what you have on the screen here for FastAPI is response model equals the database entity,
which that then drives the OpenAPI 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,
with FastAPI, if you go to slash docs,
you get 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 OpenAPI.json. And so you can use tools like open API generator to even generate client code
in any different language that you're using, like Python, JavaScript, Lua. And 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.
Right, including the consuming clients
because they could regen off of the JSON schema, right?
Yeah, yeah.
So I think that's really cool.
And one other really nice thing 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 SQL Alchemy 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,
once you return that object from the database,
you actually get autocomplete on it.
I can show that here.
Yeah, that's really cool
because a lot of times you just get star, star, KWR,
and you're 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, I was working on it.
Put it at the very end of the line, maybe.
Yeah.
Or in the next line.
Yeah.
Yeah.
Yeah, so I can access all the properties on the hero object.
One cool thing about how he did this,
he used a proposal from Eric Trout, the author of
PyWrite 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's kind of the magic
that allows some of this dynamic behavior to show up in type checkers.
I love it because so often you'll do a SQL alchemy query or something and what you get back, it's so many layers.
It's lost what that type is.
It's like, 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 one uh i swear sebastian spends as much time on his docs as he does his code amazing talent um i got it and
we were talking about type hints and the editor experience as well um sebastian does a great job
with the onboarding experience as well as the uh youth 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.
I mean, look at the docs here.
This looks like a professional team wrote all this stuff, right?
This is incredible.
Instead, one very productive guy.
And then, Brett, good question.
I want to ask the master managers 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 it's just one person.
Yeah, you know, 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 SQL Alchemy and makes it better.
FastAPI takes Starlet 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 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 SQL Alchemy supports,
Alembic migrations as well.
And in theory, it should be possible to integrate
with postGIS and TS vectors for doing geo stuff and full-text search 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 SQL Alchemy 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 was thinking about that as well.
One of the challenges that you can run into
when you just sort of say, well, 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 to dict but you explicitly opt
in or opt out certain fields so you could do things like that about uh bringing those in um
so pretty neat but yeah uh you got to be a little bit careful about that 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.
You're not hashing it. But that's a bigger problem. But still, I wanted to bring up one
more thing is his choice of heroes in his example love these uh we've got dead pond
spider boy and rusty man hilarious love it yeah those are great well so those are our six items
um do we have uh any extras got anything to share right uh sometimes i have extra extra
nine times here all about it i got none this time. How about you?
Really?
I've got like two.
So I bring one.
So first one I wanted to bring up was a really fun episode.
People know I also do a podcast called testing code episode.
One hundred and sixty three does.
It's a talk with Stefan Bedoul about pip install of local directory. And so 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 there's a, chapter 12 is up.
So chapter 12, one of the things I got questions about
for the first edition was,
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 PIP. 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 going? Yeah, it looks like chapter 12 is coming along. Well done.
You're basically done, right? You're on vacation 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 um it's gonna come out in hard copy
early next year so super i have one extra you dan yep. So one cool library that I wanted to share is Type for Py.
So we've been talking about types a lot lately.
Seems like it's a regular topic of conversation.
And they can provide a lot of benefits to your code base and to text editors like VS Code or PyCharm.
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 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 attempts
from different researchers to create a good machine learning
model for inferring types.
What I like about it is not only is it sort of best in class in terms of its accuracy,
is that the author of this machine learning model open sourced the training data set with 4.3 million type annotations in it, the training code. So you can go in and actually train this on your
own code following some of these very simple commands to train the model and pre-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
actually something that you can use. 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. Just one sort of quick caveat that the sample VS Code extension uses the
machine learning model that's hosted on type4py.com. And so if you're not working on an
open source project, you probably want to fork the extension and stand up the machine learning model using the web server that he links to.
Yeah, that's a good bit of warning.
But this looks super cool.
It seems like one of those things you could do a first pass, retrofit it, and then go back and check it out, run MyPy against it.
You could even go to Pydantic and use the validate decorator and put that on all the things and then run
your tests. The validate actually verifies the type annotations really match the
runtime behavior. So yeah, a lot of neat things. Excellent.
Brian, did you think that was funny? No, not funny
at all. This might be funny. We need something funny.
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, to 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 and woman developer pair
and they're just looking like perplexed at each other.
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 um but i thought it was funny anyway um
it was uh 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?
Yeah, I love it.
Definitely passed by reference.
Definitely. Well, this has been fun. Definitely pass 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.
Bye.
Thanks 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.
Get the full show notes over at pythonbytes.fm.
If you have a news item we should cover, as in B-Y-T-E-S. Get the full show notes over at pythonbytes.fm.
If you have a news item we should cover,
just visit pythonbytes.fm 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 Ocken, this is Michael Kennedy.
Thank you for listening and sharing this podcast with your friends and colleagues.