Brought to you by Michael and Brian - take a Talk Python course or get Brian's pytest book


Transcript #364: Holy Match-Cases Batman!

Return to episode page view on github
Recorded on Tuesday, Dec 12, 2023.

00:00 Hello and welcome to Python Bytes, where we deliver Python news and headlines directly to your earbuds.

00:05 This is episode 364, recorded December 12th, 2021. I'm Michael Kennedy.

00:11 2023, and I'm Brian Okken.

00:14 2023, and you are Brian Okken.

00:18 And this episode is brought to you by all the things that we're doing that many of you support,

00:23 and we really appreciate that. Courses over at Talk Python, mine, Brian's, other folks,

00:28 the complete pytest course over at Brian's site. Patreon supporters connect with us on Fostadon.

00:35 We're all over there, including the podcast. And many of you who are listening at this very moment know,

00:42 but many who are listening later may not. If you hear us say, hey, so-and-so said this in the audience.

00:47 If you would like to be in the audience, check out pythonbytes.fm/live, usually Tuesdays at 11 a.m. Pacific time, just as it is now.

00:55 We typically link from each show notes on the website, not as easily in the podcast player show notes,

01:02 but on the website, you can get to the video version if you ever want to.

01:05 Yeah.

01:06 Although, certainly not intended to be required. Brian, let's kick it off.

01:09 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.

01:22 So we've covered this in years past, but at adventofcode.com, there are a bunch of things you can do

01:31 to a programming exercise, and you can pick any language you want. And a lot of people do this,

01:37 and it's so much fun. But there's other ways to do advent of code. And I really liked, I saw James

01:44 Bennett is doing a Python Django advent calendar. And this is super cool. He's just decided to do

01:52 a short blog post, possibly short, some not so short blog post about Python or Django every day during the

02:00 2023 advent. And it's pretty nice. The first one is talking about enums and just a little, I mean,

02:09 it's a good reminder that Python has enums, but also you can make them nicer by making sure that you

02:15 assign values and it's easier even instead of just using enum to derive a class from enum enum.

02:21 And, and so that they're, they're like, you know, the, they're printable and stuff like that easier.

02:28 The values are good. So that's good. What I really loved seeing a couple of his posts were

02:34 around testing. So there is, this is pretty great. This is a don't mock Python's HTTPX. And I don't,

02:43 I have a plans to use HTTPX on the project, but I I'm not using it right now, but I really thought

02:48 this was a pretty cool little writeup. The, the, the, there is a discussion really about different ways

02:55 you might be able to mock mock using your HTTPX part, but there's the obvious ways are probably not

03:03 the, really the best. His recommendation is to is a, a couple of things, refactoring your code a little

03:10 bit so that you can pass in a HTTPX client, but then also using HTTPX is built in mock transport.

03:17 I didn't know HTTPX had a mock transport in there. You're grinning. Did you already know this or do

03:22 you have an opinion? I did not. I'm just thinking that Kim in the audience has identified the fact

03:27 that HTTPX is very sensitive and we should not mock or tease it. Yeah. That's why I'm laughing.

03:33 Okay. Okay. The joke. so that concludes the joke segment of the podcast. No, I didn't know this.

03:40 Actually, this is really cool. I'm all about HTTPX. It is my default go-to HTTP client these days.

03:46 When you do it request like it's like requests, but it also has async and it's, it's very fine.

03:51 And, yeah, this, this mock transport is pretty neat. anyway, it's a little over my head

03:56 right now cause I don't do this yet, but, I think it's a cool thing to point out. And also I really

04:02 think, I think it's a good idea. A cool idea. I mean, a lot of people do like, writing a novel

04:07 in November, but writing a little blog posts, during advent, that's pretty neat idea also.

04:12 So good job. Yeah. That's a very interesting idea. So I also think it's an interesting idea that library

04:18 creators may decide to provide their own mock stub API rather than just forcing you to guess what the

04:25 internals might need. That's true. Yeah. Right. Cause as a provider of the library, like, well,

04:29 really, this is the thing you need. And really there's these three things you should change,

04:32 but if you call this function, we'll do that or whatever. Right. Like they just know the

04:35 internals better and let them do it. Yeah. And there's an example of using, of having a mock

04:41 transport that just returns a, not found, and being able to do that without going out outside

04:47 of the library. I think that's a really cool idea. No, I mean, and I, and we are seeing that more,

04:52 more frameworks deciding, well, people are going to have to test this. How do they do that?

04:57 and I think it's a good trend. So nice. Tony Sherman points out, saw a similar thing in Bota three for

05:03 AWS. So very cool. Right. Okay. they, I, there's a, the other one that I thought is a short

05:10 one is test your documentation talking about, doc test. And, I love throwing stuff like

05:17 this in, you, you have little, doc string in your, in, in your code to, to show how it's used. Why not test those? So you can use either doc test or you,

05:28 py test can run your doc test tests as well. So. Because of course it can. Yeah. It does all the

05:34 things. All right. What you got. I'm going to drop into this next one here. this next one is

05:40 drop base. Now this from what I can tell it right at the top, it has a pricing. So it's not a free

05:48 type of thing for you. However, I think it's interesting enough to talk about it anyway,

05:53 not a sponsorship, just something Michael thinks. Cool. So it says build internal tools with just

05:59 Python. So, so you can build, it says full stack. Let's call them forms over data web apps for your

06:05 internal teams, import existing Python scripts and quickly build layered UIs with gradient

06:09 permissions. So when you look at the thing itself, you're like, huh, I don't really know what this

06:15 means or if this is interesting. Like they don't have a bunch of great, screenshots or whatever

06:20 on their homepage. So what you'll see is like, you can go write a query. It's all sort of starts

06:26 from a database, which is why I'm calling it forms over data. Okay. And that is like a user interface

06:31 that lets you basically access the database with a little bit of rules and such. So the idea, if I

06:37 recall correctly, I looked at this last week is what you do is you write a SQL query against a database

06:43 like Postgres or something. Okay. And that will give you a result that looks kind of like, kind of like

06:49 Excel ish static, doesn't do anything, but then you can click and say, make this a smart table type

06:55 thing. Okay. So then it becomes sortable, editable and so on. Right. So all the results, you can just

07:01 turn that into a grid that people can edit and it goes back to the database. You can create a so-called

07:07 master detail type of view. So in the example, on the homepage, they have a workspaces and then each

07:13 user belongs to a particular, there's a bunch of users that belong to each workspace. So you can do

07:18 a relationship where you say, I'm gonna, if you click on one part of the screen, fill the other part of

07:24 the screen with say the users of that workspace. So if you like select the workspace in the top grid,

07:31 like fill it out, all the users at the bottom, you turn that into a smart thing that you can edit it

07:35 and you can do things like click a button to whatever. In their example, it says, if you assign

07:41 somebody to a workspace or change their role, you can click a button to send them an email,

07:45 like their new status in that workspace, that code is written in Python, right? And you can plug in

07:51 functions behind all this stuff and all that code there is written in Python. So you want kind of a,

07:57 a real quick way to, if you've got a little SQL skill, a little Python skill, build this cascading UI

08:04 type thing. There you go. It's pretty cool. That's pretty neat. Yeah, pretty neat. And you know, they, they also

08:09 have some widgets and stuff, I believe like sidebars, search bars, et cetera. Yeah. Then you write a bunch of

08:16 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

08:23 and check that out. And you know, you're not trying to build some new super fancy looking app. You just

08:28 like, I just need to provide kind of this relationship stuff and a little bit of Python to do a little bit

08:34 of work on my UI. So people can check that out. I think there's a free tier. Let me look now. There's

08:40 a free tier that has up to three users. And then for a team, it's like, if you want teams, you can pay

08:45 more. So again, not an advertisement, but you know, UIs built with Python, even though we go on and on

08:50 about them, they're often a rarity. So when we get a chance to talk about them, it's kind of cool.

08:55 Yeah. That's neat. Yeah. And semi-related sort of, I'm going to have Samuel, remember last week we

09:02 talked about fast UI? Yeah. Samuel Coleman reached from Pydantic reached out and said, Hey, we should

09:07 talk more about this. It's a brand new thing we're trying to do interesting things with. So I'm going to

09:12 have him on talk Python technically next week, but it's going to be recorded next week and then

09:17 released later. So like in April or something like that. Yeah. Something like that. Right.

09:22 Something probably January. Okay. Anyway, well more on that over there, but drop base people and

09:29 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,

09:34 turns out to be cool or not. Yeah. I would love to hear back. yeah. So maybe, maybe Kim could

09:39 save some time because Kim says, the description of this tool pretty much describes my job, but

09:45 yep. Very cool. Anyway. I'd like to talk about, the structural pattern matching a little bit.

09:54 So we have covered structural pattern matching and talked about it before this came out. When in,

10:00 camera, which three, 10 brought us structural pattern matching way back in three, 10,

10:06 how young were we then Brian? So yeah, different world. Anyway, the, the tutorial is pretty

10:13 good as a, so this is from Ned Batchelder real world match case. so, the, he comments that

10:21 the tutorial is pretty good and it is however, the example is a little bit of a toy example

10:27 and, and it's nice to see, see this really in action. So here's a real world, use case.

10:35 Ned's got a GitHub bot installed as a web hook and it returns back. So every time something happens on a

10:42 repo, it sends back a payload of Jason data to the bot and the bot's got to figure out what to do with it.

10:48 And it's, so it turns out Jason into a nested set of dictionaries. and anybody that's,

10:55 that's dealt with, Jason return data. that's what you get, you get a bunch of nested

11:01 dictionaries. So how do you deal with that? Well, there, there was, his, his, this example shows

11:08 that the match case, or the structural pattern matching match case works really great to match this.

11:14 So, this example looks, for, for events that happen and for each event, they matches it just

11:21 to look to see if there's an issue and a comment. And, and so one of the cases is looking at the,

11:26 the content of that. So you've got these nested dictionaries you can, and you can deep, dig deep

11:31 into the nested here. It's just one layer down a couple layers of nested dictionaries, but still,

11:36 you could go deeper if you wanted to, to match that. And then not only matching the dictionary and pulling

11:42 out bits of the data, like he's looking at closed ad and created at and comparing those, but using a

11:48 comparison. So you grab the stuff and then say, if some, like if two things are equal and in other,

11:55 in another example of, if the, who, if the login is the bot name, that's the, that's different.

12:02 I don't want to do events that are based on my own actions. do something else,

12:08 but then proceed with it. So kind of some, a couple of cases where you doesn't want to do anything,

12:13 but other cases where some action is required. So the, these are, is a pretty cool use case of,

12:20 I just wanted to highlight that it's really cool use case of using structural pattern matching to,

12:26 to make some code that might be complicated other ways, a little simpler. So this isn't too bad to read.

12:31 So much more interesting than the normal switch type statements I write with match these days.

12:37 Yeah. It's like, we're looking for multiple things within the document and then applying a conditional

12:44 on part of the matched things. And, it's pretty wild.

12:48 Yeah. I'm actually thinking about this in a, I've got sets of data where for each element I'm, I'm looking,

12:55 yeah, it's a bunch of nested if clauses to say if, if this is true, if that's true and something like

13:01 this might work really pretty good. So.

13:03 Indeed. I'm going to try to make that, the, the, the conditions here, like, the chapter art for

13:09 this, this chapter, chapter items for all their topics, right? People can use that throughout the

13:14 show. So yeah.

13:15 Very neat. Look nice.

13:16 So look down at your podcast player before now.

13:19 I love that. We, we don't mention that a lot, but a lot of people, do, appreciate the

13:25 chapters. So we jump around. We do multiple topics and you can use the chapters to get to them. So

13:29 or use them as reference. All right. Speaking of reference, Brian, I have extra, extra, extra,

13:35 extra, extra times 10. Okay.

13:37 But I know you've got a lot of things as well. So I have so many extras that my next topic is

13:41 the extra, extra, extra, rather than having some follow-up stuff. Ready for it?

13:45 Yeah. All right. Follow-up number one. We talked about switching DNS and how much of a pain that

13:52 was last time and all the hassles that we could run into. And it occurred to me, and there's some

13:58 people out there like Michael, obviously we do this all the time. I suspect many people don't though.

14:03 It occurred to me that there is your domain registrar, like GoDaddy, Hover, whatever, Namecheap.

14:12 And they often have all the DNS settings for you, but you can create, you can use, there are services

14:17 that are way better. It turns out that host your actual DNS settings that have nothing to do with

14:22 your register. What you got to do is just go to your register and say, use this. So I use bunny.net for

14:29 delivering the podcasts or the courses, all sorts of things. And they have a DNS section that is super

14:33 cool. So check this out, Brian. we've done in the last week, I set this up. We've done over half a

14:39 million DNS queries, not requests to the page, but like, Hey, tell me who this is. And we'll remember

14:44 that for a while to your, for your ISP. That's a lot of DNS queries. A lot of people want to know

14:48 what Python bytes.fm is. That's exciting. But here, why is this so, why am I so excited? Like,

14:53 look how nice and readable this is for people who, are watching the, if you're not, it's like,

14:58 it'll create a view of a TXT record. It'll like create, nested sections go, all of these records

15:04 apply to this domain or that domain and so on. And then look over here at, you can edit all these

15:10 here, but check this out. If I want to say like this, the CDN setting, maybe I'm working on it.

15:14 And you know, what's a super hassle with DNS? Like I make a change, please wait a couple hours and you

15:19 can try it. If it doesn't work, it will be broken for a few hours and then you can fix it. Right?

15:24 What a hassle. cause I don't, I migrated 25 domains. I know I messed some of them up,

15:29 not badly, but a little. So check this out. If you click here, you've got your one day,

15:33 one hour, typical time to live. It goes down to every 15 seconds. So if you're like working on

15:39 something and you want to be able to like, I try, Oh no, go ahead to quick switch it back.

15:44 Just set it to that for a while. Isn't that awesome? Yeah. The other thing you can do with this is you

15:48 can write, unfortunately JavaScript, but you can write JavaScript that runs on every DNS request.

15:53 So you can say things like, I know you asked what this is, but you're in Asia. So we're going to tell

15:58 you that it's this server rather than that server. So you get a faster local machine and all sorts of

16:03 interesting stuff here. This is free. What? Or this is free for the first 20 million DNS lookups.

16:11 And then it's like 10 cents per million. So for us, right, we're going to do, it looks like 2 million

16:16 free, right? We could be 10 times more popular before we pay a few cents. So the reason this is cool

16:22 is you can set this up and then switch your domain. And then as soon as you just change the name,

16:27 you can get it all dialed in and you don't have to have this kind of like awkward downtime in the

16:31 middle. So people can check this out. There's other ones. Cloudflare has something similar.

16:34 DigitalOcean has something similar, but this one is super, super neat.

16:38 That's pretty cool.

16:38 Yeah. And Tony says similar to what you can do in Cloudflare. Yeah. They're both CDN providers.

16:42 They both have like similar stories. Okay. I'm going to go faster now because I have more.

16:47 I wrote a cool, say blog post type thing that shows you how to use warp to hide all your terminal

16:56 secrets. Look at this, Brian. So here, welcome to Ubuntu. I'm not going to say what version.

17:00 And here's your IP addresses. We're not going to say what those are, but if, so if you're doing

17:04 presentations that somehow involve working with terminal stuff that might accidentally reveal

17:10 something like, what is an IP address that you shouldn't have? Or what is an API key or something?

17:14 If you use warp, all you got to do is check a box and say, obscure all my secrets. And now

17:19 your presentations are safer. Isn't that cool? Yeah. That's pretty neat.

17:22 Yeah. So talked about warp before, but I, that is new. I think it's a new feature, but anyway,

17:27 it's cool. We also have a supporting developer in residence. Lukash Lenga says, welcome. We have

17:34 Peter Victorin now as a supporting developer in residence working with Lukash. So we now have

17:40 two times the help over there because Lukash has been so successful keeping Python moving

17:46 forward. They're now getting more folks. So that's pretty awesome, right?

17:49 Yeah. And thank you.

17:50 They successfully cloned him.

17:51 Yeah, they did. He's now an AI. No, just kidding. Also that was sponsored by Bloomberg. So shout

17:57 out to Bloomberg for being excellent. Joe sent us two things that are pretty funny. They're almost

18:04 worthy of like a joke type of thing, but I thought I would just share them. So he decided to embody

18:10 various songs through Python. So we have the Bohemian Rhapsody and we have Money for Nothing by Dire

18:17 Straits. So remember, it's like, I just want my MTV, right? Money for Nothing, all that. So they got the

18:24 class MTV. I'm linking to the show notes. Of course you can pull up, you know, starts out with self.

18:29 Want equals nothing. Print Money for Nothing. And then it goes, all I want is my MTV. And it sort

18:38 of cycles through that. You know, look at those yo-yos. That ain't working. Maybe get a blister on

18:43 your little finger, right? It's got like working, false, do, true, money, cost of zero. It's pretty

18:49 awesome. Guy's dumb, little blister, you know, guy's dumb as false, right? So if you kind of want to

18:54 enjoy this musical melody here. Does it run? I'll have to try it out. I'm sure that it does run. Yeah.

19:01 Yeah. So, well, OS.system. Install microwave dash ovens. Oh. Echo. I mean, you know, custom kitchen

19:11 deliveries, refrigerators, moving color TVs. I don't know if I'd run it actually, but there's a

19:16 similar one from Bohemian Rhapsody. It's pretty funny. So people can check that out. Don't you

19:21 regularly just download random code and run it? Well, when I see OS move, maybe not.

19:26 PyCascades is coming to Seattle in 2024. Their call for proposals is out. So people can check that out.

19:34 It's a fun conference. If you're in the Pacific Northwest or want to go to the Pacific Northwest,

19:38 you can see the little banner wrapper thing. CFP is open. Okay. So some interesting nomenclature

19:44 coming for us in the world is free threaded Python. Have you heard this? Free threaded Python?

19:49 I didn't know we were charging for threads. I know this one is like, it's a total freemium model.

19:55 So with the no gil work, with the sub interpreter work, all these different ways in which Python runs

20:02 more concurrently, especially the no gil stuff, there's a plan to basically call it free threaded

20:08 Python, not no gil. Because no gil is how it's free threaded, but free threaded is the point. You know,

20:14 it's like, okay, yeah, I have a gas exploding mobile. Like I have a thing that drives. I don't

20:20 care about the driving, not the gas exploding, right? Similar type thing here. So Seth, Michael

20:26 Larson points out that this has been blocked on PyPI to make way for this is interesting.

20:31 This redistributions of the Python runtime compiled with the free threaded mode, i.e. no gil. So one,

20:39 we're calling it free threaded Python two, you might be able to pip install Python. I don't know.

20:43 Okay. So that's, that's that one. Next. There's a very interesting letter, open letter to the Python

20:51 South for a foundation from Python, Python, Africa. And apparently there's been a lot of, I don't know

20:58 what the right way to point it, but it is basically not that great a support for Python in Africa.

21:05 I just want to sort of put this out there. They wrote it. People can check it out and read it,

21:10 but would like, just would like to see better support for reaching out to folks in Africa and

21:15 have the PSF maybe be a little bit better supporter for it. There's been a lot of contention around it.

21:20 So check that out. It's worth knowing. Okay. And then we have, oh yeah, it's, it's not super congruent

21:27 with the mission of the PSF on a couple of levels. One, like PSF is there to promote the Python

21:34 language. And this is a lot of like, we need you to make this social change or you're not going to

21:39 have a conference in Africa. Seems a little incongruent with the mission about supporting

21:45 Python. I don't know. Python, moving on Python 3.12.1, the first major update of 3.12 is out. So

21:54 people can check that out. It contains many new features and optimizations with over more than

22:00 400 bug fixes. So we don't like bugs in our code and 400 of them. We definitely don't want that many.

22:05 Yeah. Along with that came the 3.11.7 release. Okay. All right. Update your Pythons. Update your

22:13 Pythons. Everybody gets their new Pythons. Final one. And this one could be a joke, but it's just

22:18 too visual. It's really awesome. Let me make sure I credit this one correctly. So Johannes Lippmann,

22:26 when he heard you talk about the Obfuscated Python code contest, this is the first international one,

22:33 by the way. Okay. Okay. The Obfuscated Python code contest. He's like, I got to be part of this. So not

22:39 only was he part of it, but he won. He won the most introspective of them all.

22:45 It's beautiful too. Check this out. So if we go here, it's literally the letters PY and a bunch of

22:52 insane symbols in the most wild, wild way possible. Like this, if you open it up, it's just like

22:59 ASCII art PY. Yeah. What this does when you run it, is it annotates, oops, go back. When it

23:06 annotates, there's always like, so you don't have to run it. You can just click the image. Is it

23:10 basically annotates and colorizes the Xenopython? Oh, that's so cool. Yeah. And so you can check out

23:16 other ones too, right? I haven't even looked at the most roundabout one. He's also highlighting

23:22 which, which portions of the Xenopython his code is completely ignoring. Yes, exactly. Yeah. So these

23:29 are fun and people can check them out. You got the image, but this one has a movie even. So instead of

23:34 just a picture, the first, the winner, I guess it's not the number one winner. There's just different

23:37 categories. The most puzzling is by Moshe. Oh, wow. And it's like a maze, which is,

23:44 which is amazing. That is amazing. Oh, that was well done. Very well done. Okay. So that's it for

23:49 all of my, my extras. Okay. Over to you. That's my extra, extra, extra segment, which was very extra.

23:54 I just have a couple extras. We've covered this before. Microsoft Python for VS Code has a, has a

24:03 December 23 release. Actually, we haven't covered that, but looking in the, that came out on the

24:09 7th. And the, within this December release has an announcement that, oh, there's a, oh yeah,

24:19 I had it already highlighted. The Python test adapter rewrite experiment. If you don't remember this,

24:26 it was back in, it started in June. They, they did a change where they were working on, you had to,

24:35 you had to opt in to the, the change of how VS Codes test, how test discovery was working. Anyway,

24:43 I reckon I always, I, we covered it because I recommended everybody turn it on. You, now that is

24:49 being rolled out to a hundred percent of the users. So now you have to opt out of it. If you want to,

24:54 if you don't want it, but don't opt out of it, the test discovery, just update your VS Code and

24:59 you'll have better test discovery. Now, super cool. The test discovery is so much better now than it

25:03 was before. Thank you everybody that worked on Python for VS Code or worked on this part of it. So

25:09 that's pretty cool. The other extra I wanted to, to mention was, was Daniel Roy Greenfield wrote a

25:17 little tip and I, I always, I kept meaning to do this and I've always forgotten. So now this I'm

25:23 going to do it this time is a set in my shell script RC files, set pip require virtual and to true.

25:32 And that will make it so that if you try to pip install something and you're not in a virtual

25:37 environment, it will, it will not do it. And it will just complain. So I never mean to pip install

25:44 anything unless I'm in a virtual environment. So this is, this is good. I like this.

25:49 Yeah, that's, that's cool. There's a lot of these sort of environmental settings that can control

25:53 things that are not entirely obvious, right?

25:55 Yeah. Big, good, good blog posts. So write up some of the ones you should really know about.

26:00 Absolutely. Well, there you go. Fire up your Hugo. Let's go.

26:05 Yeah. So that's, those are my extras. So would you say you got to do a lot of research to maybe

26:09 find all those particular environment variables stuff might really be overwhelming. So check out

26:14 this joke, Brian. Okay. It comes to us from Ray on Mastodon. It's a sticker and I kind of want this

26:20 sticker. It's got a bunch of tabs at the top. It says my brain has too many tabs open. Four of them

26:25 are frozen and I have no idea where the music is coming from. Oh my gosh. It's amazing. Yeah.

26:30 I have no idea where the music is coming from. That, that happens to be so, I guess it must

26:36 happen to everybody so often. It's like there's music playing and I have no idea which tab is

26:40 playing music. I know it, it, it has like a little like a speaker light icon that you can see

26:47 if it's going, but yeah, I feel like this little sticker describes both my Monday mornings and my

26:52 Friday evenings or afternoons. It's like, Oh, I can't deal. It's like, what is going on here?

26:57 Too much in life. Yeah. That's funny.

26:59 I need this sticker. I do too. So I do too. All right. Well, thank you for the episode as

27:06 always. And thank you everyone for listening. Yes. Thank you.

Back to show page