Python Bytes - #364 Holy Match-Cases Batman!
Episode Date: December 12, 2023Topics covered in this episode: A Python/Django Advent calendar Dropbase helps you build internal web apps with Python Real-world match/case Extra, extra, extra, so many extras! Extras Joke See ...the full show notes for this episode on the website at pythonbytes.fm/364
Transcript
Discussion (0)
Hello and welcome to Python Bytes, where we deliver Python news and headlines directly to your earbuds.
This is episode 364, recorded December 12th, 2021. I'm Michael Kennedy.
2023, and I'm Brian Ocken.
2023, and you are Brian Ocken.
And this episode is brought to you by all the things that we're doing that many of you support,
and we really appreciate that. Courses over at TalkPython, mine, Brian's, other folks, the complete PyTest
course over at Brian's site, Patreon supporters. Connect with us on Fostedon. We're all over there,
including the podcast. And many of you who are listening at this very moment know, but many who
are listening later may not. If you hear us say, hey, so-and-so said this in the audience, if you would like to be in the audience, check out pythonbytes.fm slash live.
Usually Tuesdays at 11 a.m. Pacific time, just as it is now.
We typically link from each show notes on the website, not as easily in the podcast player show notes, but on the website, you can get to the video version if you ever want to. Although certainly not intended to be required. Brian, let's kick it off.
Yeah. Let's talk about one of the things that is often around is, this was going to be one of my
extras, but I'll just bring it up anyway, is the advent of code. So we've covered this in years past,
but at adventofcode.com,
there are a bunch of things you can do to a programming exercise
and you can pick any language you want.
And a lot of people do this
and it's so much fun.
But there's other ways to do Advent of Code.
And I really liked,
I saw James Bennett is doing a Python Django advent
calendar. And this is super cool. He's just decided to do a short blog post, possibly short,
some not so short blog post about Python or Django every day during the 2023 advent. And it's,
it's pretty nice. The first one is talking about enums
and just a little,
I mean, it's a good reminder
that Python has enums,
but also you can make them nicer
by making sure that you assign values
and it's easier,
instead of just using enum
to derive a class from enum enum.
And so that they're like, you know the they're printable
and stuff like that easier um the values are good uh so that's good what i really loved seeing a
couple of his posts were around testing so there is this is pretty great this is a don't mock
python's httpx um and i don't i have a plans to use httpx on the project but i i'm not using it right now but i
really thought this was a pretty cool little write-up the the the there is a discussion
really about different ways you might be able to mock um mock uh using your httpx part but there's
the obvious ways are probably not the the best. His recommendation is a
couple of things, refactoring your code a little bit so that you can pass in a HTTPX client,
but then also using HTTPX's built-in mock transport. I didn't know HTTPX had a mock
transport in there. You're grinning. Did you already know this or do you have an opinion?
I did not. I'm just thinking that Kim in the audience has identified the fact that
HTTPS is very sensitive and we should not mock or tease it.
That's why I'm laughing. Thank you for the joke.
That concludes the joke segment of the podcast.
No, I didn't know this actually. This is really cool. I'm all about HTTPS. It is my
default go-to htp client
these days when you do it request like it's like requests but it also has async and it's it's very
fine and um yeah this this mock transport is pretty neat um anyway it's a little over my head
right now because i don't do this yet but um i think it's a cool thing to point out and also i
really think i think it's a good idea a cool cool idea. I mean, a lot of people do,
like writing a novel in November,
but writing a little blog post
during Advent,
that's a pretty neat idea also.
So good job.
Yeah, that's a very interesting idea.
I also think it's an interesting idea
that library creators
may decide to provide
their own mock stub API
rather than just forcing you
to guess
what the internals might need.
That's true, yeah.
Right?
Because as a provider of the library,
like, well, really, this is the thing you need.
And really, there's these three things you should change,
but if you call this function, we'll do that.
Whatever, right?
Like, they just know the internals better
and let them do it.
Yeah, and there's an example of using,
of having a mock transport
that just returns a not found
and being able to do that without going outside of the library.
I think that's a really cool idea.
I mean, and we are seeing that more, more frameworks deciding,
well, people are going to have to test this.
How do they do that?
And I think it's a good trend.
Nice.
Tony Sherman points out, saw a similar thing in BOTA 3 for AWS.
So very cool.
Right. Okay.
There's the other one that I thought is a short one is test your documentation.
Talking about doc test.
And I love throwing stuff like this in.
You have a little doc string in your code to show how it's used,
why not test those?
So you can use either doc test or you,
PyTest can run your doc test tests as well.
Because of course it can.
Yeah.
It does all the things.
Awesome.
What you got?
I'm going to drop into this next one here.
This next one is drop base.
Now this, from what I can tell, right at the top, it has a pricing. So it's not a free
type of thing for you. However, I think it's interesting enough to talk about it anyway,
not a sponsorship, just something Michael thinks cool. So it says build internal tools with just
Python. So you can build, it says full stack. Let's call them forms over data web apps
for your internal teams.
Import existing Python scripts
and quickly build layered UIs
with granular permissions.
So when you look at the thing itself,
you're like, huh,
I don't really know what this means
or if this is interesting.
Like they don't have a bunch of great screenshots
or whatever on their homepage.
So what you'll see is like,
you can go write a query.
It's all sort of starts from a database, which is why I'm calling it forms over data. Okay. And that is
like a user interface that lets you basically access the database with a little bit of rules
and such. So the idea, if I recall correctly, I looked at this last week is what you do is you
write a SQL query against a database like Postgres or something, okay?
And that will give you a result that looks kind of like Excel-ish, static, doesn't do anything.
But then you can click and say, make this a smart table type thing, okay?
So then it becomes sortable, editable, and so on, right?
So all the results, you can just turn that into a grid that people can edit and it goes back to the database.
You can create a so-called master detail type of view.
So in the example on the homepage, they have a workspaces and then each user belongs to a particular, there's a bunch of users that belong to each workspace.
So you can do a relationship where you say, I'm going to, if you click on one part of the screen, fill the other part of the screen with say the users of that workspace. So if you like select the workspace in the top grid,
it'll like fill it out all the users at the bottom. You turn that into a smart thing that
you can edit it and you can do things like click a button to whatever. In their example, it says,
if you assign somebody to a workspace or change their role, you can click a button to send them an email, like their new status in that workspace.
That code is written in Python, right?
And you can plug in functions behind all this stuff.
And all that code there is written in Python.
So you want kind of a real quick way to, if you've got a little SQL skill, a little Python skill, build this cascading UI type thing.
There you go.
It's pretty cool. That's pretty neat. Yeah, pretty neat. And, you know, they, they also have some
widgets and stuff, I believe, like sidebars, search bars, et cetera. Yeah. Then you write a bunch of
that in Python and it's, it's super cool. So I think that could be quite useful for folks if
they want to go and check that out. And, you know, you're not trying to build some new super fancy looking app.
You just like, I just need to provide kind of this relationship stuff and a little bit
of Python to do a little bit of work on my UI.
So people can check that out.
I think there's a free tier.
Let me look.
Now there's a free tier that has up to three users.
And then for teams, like if you want teams, you can pay more.
So again, not an advertisement, but UIs built with Python, even though we go on and on about them, they're often a rarity.
So when we get a chance to talk about them, it's kind of cool.
Yeah, that's neat.
Yeah.
And semi-related, sort of.
I'm going to have Samuel.
Remember last week we talked about Fast UI?
Yeah.
Samuel Cullen from Pydantic reached out and said,
hey, we should talk more about this.
It's a brand new thing we're trying to do interesting things with.
So I'm going to have him on TalkPython technically next week,
but it's going to be recorded next week and then released later.
Like in April or something like that?
Yeah, something like that, right?
Something.
Probably January.
Okay.
Anyway, more on that over there.
But drop base people and check that out.
And if people do, it'd be cool if they could, like, shoot us a note and see if it's, like, turns out to be cool or not.
Yeah, I would love to hear back.
Yeah.
So maybe Kim could save some time because Kim says the description of this tool pretty much describes my job.
But yeah, cool.
Anyway, I'd like to talk about the structural pattern matching a little bit.
So we have covered structural pattern matching and talked about it before this came out when in camera, which 310 brought us structural pattern matching. Way back in 3.10.
How young were we then, Brian?
So young.
It's a different world.
Anyway, the tutorial is pretty good.
So this is from Ned Batchelder, Real World Match Case.
So he comments that the tutorial is pretty good, and it is.
However, the example is a little bit
of a toy example and um and it's nice to see see this really in action so here's a real world use
case um ned's got a github bot installed as a webhook and it returns back so every time something
happens on a repo it sends back a a payload of JSON data to the bot.
And the bot's got to figure out what to do with it.
And so it turns that JSON into a nested set of dictionaries.
And anybody that's dealt with JSON return data, that's what you get.
You get a bunch of nested dictionaries.
So how do you deal with that? Well, uh, there, there was, um, his,
his, this example shows that the match case, um, or the structural pattern matching match case
works really great to match this. So, uh, this example looks, uh, for, for events that happen
and for each event, they matches it just to look, to see if there's an issue and a comment. And,
and so one of the cases is
looking at the the content of that so you've got these nested dictionaries you can and you can deep
dig deep into the nested here it's just one layer down a couple layers of nested dictionaries but
still you could go deeper if you wanted to to match that and then not only matching the dictionary
and pulling out bits of the data like he's looking at closed ad and created at and comparing those,
but using a comparison.
So you grab the stuff and then say,
if some,
like if two things are equal and in other,
in another example of if the,
who,
if the login is the bot name,
that's the,
that's different.
I don't want to do events that are based on my own actions um uh
do something else but then proceed with it so kind of some a couple cases where you doesn't
want to do anything but other cases where some action is required so the these are um is a pretty
cool use case of uh i just wanted to highlight that it's a really cool use case of using structural pattern matching
to make some code that might be complicated other ways a little simpler.
So this isn't too bad to read.
This is so much more interesting than the normal switch type statements
I write with match these days.
Yeah.
It's like we're looking for multiple things within the document
and then applying a conditional on part of the matched things.
And, oh, it's pretty wild.
Yeah.
I'm actually thinking about this in a, I've got sets of data where for each element I'm
looking, yeah, it's a bunch of nested if clauses to say if, if this is true, if that's true
and something like this might work really pretty good.
So.
Indeed. true and something like this might work really pretty good so indeed i'm going to try to make
that uh the conditions here like uh the chapter art for this this chapter we have chapter items
for all their topics right people can use that throughout the show so yeah very neat look nice
so look down at your podcast player before now i love that we we don't mention that a lot but a lot
of people uh do do appreciate the chapters.
So we jump around.
We do multiple topics and you can use the chapters to get to them.
Absolutely.
Or use them as reference.
All right.
Speaking of reference, Brian, I have extra, extra, extra, extra, extra times 10.
Okay.
And I know you've got a lot of things as well.
So I have so many extras that my next topic is the extra, extra, extra, rather than having some follow-up stuff.
Ready for it? Yeah. All right. Follow-up number one. We talked about switching DNS and how much
of a pain that was last time and all the hassles that we could run into. And it occurred to me,
and there's some people out there like, Michael, obviously we do this all the time. I suspect many
people don't though. It occurred to me that there is your domain registrar, like GoDaddy, Hover, whatever, Namecheap.
And they often have all the DNS settings for you.
But you can create, you can use, there are services that are way better, it turns out, that host your actual DNS settings that have nothing to do with your registrar.
All you got to do is just go to your register and say, use this. So I use bunny.net for delivering the podcasts,
for the courses, all sorts of things. And they have a DNS section that is super cool. So check
this out, Brian. We've done in the last week, I set this up. We've done over half a million DNS
queries, not requests to the page, but like, hey, tell me who this is. And we'll remember that for
a while to your, for your ISP. That's a lot of DNS queries. A lot of people want to know what
pythonbytes.fm is. That's exciting. But why is this so, why am I so excited? Like, look how
nice and readable this is for people who are watching that if you're not, it's like, it'll
create, if you have a TXT record, it'll like create nested sections, go all of these records apply to this domain or that domain and so on.
And then look over here at you can edit all these here, but check this out.
If I want to say like this, the CDN setting, maybe I'm working on it.
And you know, what's a super hassle with DNS?
Like I make a change.
Please wait a couple hours and you can try it.
If it doesn't work, it will be broken for a few hours and then you can fix it.
Right.
What a hassle.
Because I don't I migrated 25 domains.
I know I messed some of them up.
Not badly, but a little.
So check this out.
If you click here, you've got your one day, one hour, typical time to live.
It goes down to every 15 seconds.
So if you're like working on something and you want to be able to like try, oh no, go
ahead to quick, switch it back.
Just set it to that for a while.
Isn't that awesome?
Yeah.
The other thing you can do with this
is you can write, unfortunately, JavaScript,
but you can write JavaScript that runs on every DNS request.
So you can say things like,
I know you asked what this is, but you're in Asia.
So we're going to tell you that it's this server
rather than that server.
So you get a faster local machine
and all sorts of interesting stuff here.
This is free.
What?
This is free for the first 20 million DNS lookups. And then it's like 10 cents per million. So for
us, right, we're going to do, it looks like 2 million free, right? We could be 10 times more
popular before we pay a few cents. So the reason this is cool is you can set this up and then
switch your domain. And then as
soon as you just change the name, you can get it all dialed in and you don't have to have this kind
of like awkward downtime in the middle. So people can check this out. There's other ones. Cloudflare
has something similar. DigitalOcean has something similar, but this one is super, super neat.
That's pretty cool.
Yeah. And Tony says similar, what you can do in Cloudflare. Yeah. They're both CDN providers.
They both have like similar stories. Okay. I'm going to go faster now because I have more.
I wrote a cool, say, blog post type thing
that shows you how to use Warp
to hide all your terminal secrets.
Look at this, Brian.
So here, welcome to Ubuntu.
I'm not going to say what version.
And here's your IP addresses.
We're not going to say what those are.
So if you're doing presentations
that somehow involve working with terminal stuff that might accidentally reveal
something like what is an IP address that you shouldn't have, or what is an API key or something,
if you use warp, all you got to do is check a box and say, obscure all my secrets. And now your
presentations are safer. Isn't that cool? Yeah, that's pretty neat. Yeah. So talked about warp
before, but I, that is new. I. So talked about warp before, but I,
that is new. I think it's a new feature, but anyway, it's cool. We also have a supporting
developer in residence. Lukas Lenga says, welcome. We have Peter Victorian now as a
supporting developer in residence working with Lukas. So we now have two times the help over
there because Lukas has been so successful keeping Python moving forward.
They're now getting more folks. So that's pretty awesome, right?
Yeah. They successfully cloned him.
Yeah, they did. He's now an AI. No, just kidding. Also, that was sponsored by Bloomberg. So
shout out to Bloomberg for being excellent. Joe sent us two things that are pretty funny.
They're almost worthy of like a joke type of thing,
but I thought I would just share them.
So he decided to embody various songs through Python.
So we have the Bohemian Rhapsody
and we have Money for Nothing by Dire Straits.
So remember, it's like, I just want my MTV, right?
Money for nothing, all that.
So they got the class MTV. I'm linking to the
show notes. Of course you can pull up, you know, starts out with self.want equals nothing,
print money for nothing. And then it goes, all I want is, all I want is my MTV. And it sort of
cycles through that. You know, look at those yo-yos. That ain't working. Maybe get a blister
on your little finger, right right it's got like working
falls do true money cost of zero that's pretty awesome guys dumb little blister you know guys
dumb as falls right so if you kind of want to enjoy this uh musical melody here does it run
i'll have to try it out i'm sure that it does run yeah yeah. Yeah, interesting. Well, OS.system, install microwave-ovens.
Oh.
Echo.
I mean, custom kitchen deliveries, refrigerators,
moving color TVs.
I don't know if I'd run it, actually.
But there's a similar one for Bohemian Rhapsody.
It's pretty funny.
So people can check that out.
Don't you regularly just download random code and run it?
Well, when I see OS move, maybe not.
PyCascades is coming to Seattle in 2024.
Their call for proposals is out.
So people can check that out.
It's a fun conference.
If you're in the Pacific Northwest or want to go to the Pacific Northwest,
you can see the little banner wrapper thing.
CFP is open.
Okay.
So some interesting nomenclature coming for us in the world is
free threaded Python. Have you heard this free threaded Python? I didn't know we were charging
for threads. I know this one is like, it's a total freemium model. So with the no-gill work,
with the sub interpreter work, all these different ways in which Python runs more concurrently,
especially the no-gill stuff
there's um a plan to basically call it free-threaded python not no-gill because no-gill is how it's
free-threaded but free-threaded is the point you know it's like okay yeah i have a a a gas exploding
mobile like i have a thing that drives i don't care about the driving not the gas exploding right
similar type thing here so um seth michael larson points out that this has been blocked on PyPI.
To make way for, this is interesting, redistributions of the Python runtime compiled with the free-threaded mode, i.e. no-gill.
So one, we're calling it free-threaded Python.
Two, you might be able to pip install Python.
I don't know.
Okay.
So that's that one next
there's a very interesting letter open letter to the python south for a foundation from python
python africa and apparently there's been a lot of i don't know what the right way to point it
but it is basically not that great a support for python in africa i just want to sort of put this
out there they wrote it people can check it out and read it but would like just would like to see
better support for reaching out to folks in africa and have the psf maybe be a little bit better
supporter for it there's been a lot of contention around that so check that out people it's worth knowing okay and then we have oh yeah
it's it's not super congruent with the mission of the psf on a couple of levels one like psf is
there to promote the python language and this is a lot of like we need you to make this social
change or you're not going to have a conference in africa seems a little incongruent with the mission about supporting Python. I don't
know. Python, moving on. Python 3.12.1, the first major update of 3.12, is out. So people can check
that out. It contains many new features and optimizations with over more than 400 bug fixes.
So we don't like bugs in our code and 400 of them. We definitely don't want that many. Yeah. Along with that came the three 11 seven release. Okay. All right. Update your Pythons.
Update your Pythons. Everybody gets their new Pythons. Final one. And this one could be a joke,
but it's just too visual. It's really awesome. Um, let me make sure I credit this one correctly.
So Johannes Lippmann, when he heard you talk about the Obfuscated Python Code Contest,
this is the first international one, by the way.
Okay.
Okay.
The Obfuscated Python Code Contest.
He's like, I got to be part of this. So not only was he part of it, but he won.
He won the most introspective of them all.
It's beautiful too.
Check this out. So if we go here, it's literally the letters P Y and a bunch of insane symbols in the most wild, wild way possible. Like this, if you open it up, it's just like ASCII art P Y.
Yeah. What this does when you run it, is it annotates. Oops, go back.
When it annotates, there's always, like, so you don't have to run it.
You can just click the image.
It basically annotates and colorizes the Xenopython.
Oh, that's so cool.
Yeah, and so you can check out other ones, too, right?
I haven't even looked at the most roundabout one.
He's also highlighting which portions of the Zen of Python his code is completely ignoring.
Yes, exactly.
Yeah.
So these are fun and people can check them out.
You've got the image.
Well, this one has a movie even.
So instead of just a picture, the first, the winner, I guess it's not the number one winner.
There's just different categories.
The most puzzling is by Moshe.
Oh, wow.
And it's like a maze, which is insane.
That is amazing. Oh, that that was well done very well done okay so that's it for all my my extras okay over to you that's my extra extra
extra segment which was very extra i just have a couple extras um we've covered this before uh
microsoft uh python for vs code has a December 23 release.
Actually, we haven't covered that.
But looking in the, that came out on the 7th.
And the, within this December release has an announcement that, oh, there's a, oh, yeah, I had it already highlighted.
The Python test adapter rewrite experiment if you don't remember this uh but it was back in it started in uh june um they they did a change where uh they were working on um you had
to you had to opt in to the the change of how vs codes test how test discovery was working anyway
i reckon i always we covered it because i
recommended everybody turn it on um you now that is being rolled out to 100 of the users so now
you have to opt out of it if you want to if you don't want it but don't opt out of it the test
discovery just update your vs code and you'll have better test discovery now super cool the test
discovery is so much better now than it was before um thank you
everybody that worked on python for vs code or worked on this part of it so that's pretty cool
the other extra i wanted to um uh to mention was um was daniel roy greenfield wrote a little tip
and i i always i kept meaning to do this and i've always forgotten. So now this I'm going to do it this time is set in my shell script RC files.
Set pip require virtual env to true.
And that will make it so that if you try to pip install something and you're not in a virtual environment, it will it will not do it and it will just complain.
So I never mean to pip install anything unless I'm in a virtual environment.
So this is good.
I like this.
Yeah, that's cool.
There's a lot of these sort of environmental settings
that can control things
that are not entirely obvious, right?
Yeah.
Be a good blog post
to write up some of the ones you should really know about.
Absolutely.
Well, there you go.
Fire up your Hugo.
Let's go.
Yeah, so those are my extras. Would you say you go. Fire up your Hugo. Let's go. Yeah.
So those are my extras.
So would you say you got to do a lot of research to maybe find all those particular environment
variables?
Stuff might really be overwhelming.
So check out this joke, Brian.
Okay.
It comes to us from Ray on Macedon.
It's a sticker.
And I kind of want this sticker.
It's got a bunch of tabs at the top.
It says, my brain has too many tabs open.
Four of them are frozen and I have no idea where the music is coming from oh my gosh it's amazing yeah
i have no idea where the music is coming from that that happens to be so i guess it must happen to
everybody so often it's like there's music playing and i have no idea which tab is playing music
um i know it it has like a little speaker icon
that you can see if it's going.
I feel like this little sticker
describes both my Monday mornings and
my Friday evenings or afternoons.
It's like, oh, I can't deal.
What is going on here? Too much in
life. Yeah, that's funny.
I need this sticker. I do too.
I do too. All right. Well,
thank you for the episode
as always and thank you everyone for listening.
Yes, thank you.