Transcript #385: RESTing on Postgres
Return to episode page view on github00:00 Hello and welcome to Python Bytes where we deliver Python news and headlines directly to your earbuds.
00:05 This is episode 385, recorded May 27th, 2024. Did I get that right? Yeah. I am Brian Okken.
00:12 And I am Michael Kennedy.
00:14 This week's episode is sponsored by Mailtrap. More about them later.
00:18 If you want to connect with us, we're all on Fosstodon, the show, and Michael and I.
00:22 We're Fosstodonians.
00:24 Fosstodonians, yeah. So those links are on the show notes. Also in the show notes,
00:29 or at pythonbytes.fm, you can go to live if you want to join us, join the audience.
00:35 Usually it's Tuesdays at 10am Pacific time.
00:39 You can also use that link to get older videos too if you want to watch what happens live.
00:45 Then finally, if you'd like to receive an email a day or two after the show to find out all of the
00:54 links and to have a link to the show, but make sure if you listen to this and you want to make
00:59 sure that you don't miss any links and be able to go check those out later, you can become a
01:04 friend of our show and we'll send you those emails. So you had an announcement before we get started.
01:10 I do. Well, I just wanted to say, Brian, the reason that we're not just doing this at a
01:14 normal time, we're just kind of late. And I got some messages from folks in places like New Zealand,
01:18 like, awesome, this is a perfect time. So there's always a perfect time somewhere in the world.
01:22 But you've got this thing going on tomorrow. And then for the rest of the week, I'm going to this
01:27 thing called the giant loop rides, this off-road rally that's at a hot springs and maybe 300 other
01:34 off-road riders out in the Southeast desert of Oregon and riding the mountains and the desert and
01:39 open bar, live music, all sorts of shenanigans going on. Anyway, somebody, yeah, that swimming
01:46 pool is a hot springs, by the way, how crazy is that? If anyone is listening out there and it's
01:50 also going, just come say hi. That's all I'm saying. But also why we're doing this in the
01:54 middle of the night for some places. So I would say if you have a podcast that kind of runs late,
02:02 sometimes it'll make you tired, wouldn't you think? Would you have to rest a little? Yeah.
02:05 Let's talk about Postgres, post G rest as in Postgres rest API. So this one comes to us from
02:15 Mark Little. So thanks, Mark, for sending this over. It's a good one. This is basically a web
02:21 server, web front end for your database. So you've got Postgres and you might think, all right, well,
02:26 I would just like to expose the tables as endpoints with ability to do queries over
02:32 HTTP endpoints, you know, somehow pass the query over. You want to have security,
02:38 things like that. So this is an app that you can run and it automatically with zero API work for
02:44 you, turns your Postgres instance into a somewhat restricted API basically. Cool, right? Yeah.
02:53 Yeah. Now, obviously these things, I don't know. I always have mixed feelings when I talk about
02:57 these things. On one hand, if you just say, I would like, say my view front end or something,
03:03 or even my Python code to just have access to the database as an API, just read, you know,
03:08 the crud things of all the tables, you're good to go. This is what you want, right? On the other
03:13 hand, sometimes I want to have more specific things instead of just, here's the user table.
03:18 I want you to go through the user, the create new user API endpoint that uses, I don't know,
03:24 Argon2 for memory hard, password hashing and other checks. And maybe, you know,
03:29 they're like every time I come up against these things, I'm like, oh, they're really cool. But
03:33 at the same time, I kind of want a little more specific control over how it works. So if you're
03:39 just like, I just have a database, I just want to be able to expose it real quick. Let's go postgres
03:43 rest sort of thing. Cool, right? So the way it works is it's written in Haskell, which you may,
03:50 and it's 4% Python. So I guess there's a little Python in there, but you know, it doesn't really
03:54 matter what it's written in. It's just a thing you run. And then, then you talk to it from your
03:59 Python or from your JavaScript or whatever it is. Okay. So there's a couple of things you care
04:04 about. One, you probably care about performance. I'm not super familiar with the performance of
04:09 Haskell, but I believe it runs on top of the JVM. Sorry if I get that wrong, but I think so,
04:13 which makes it pretty quick. It says it'll do 2000 requests per second on the Heroku free tier,
04:18 which is pretty fast, actually. Right? That's, that's a pretty wimpy server. And 2000 requests
04:23 a second is pretty high end. Three reasons for that Haskell, and it's written used in the warp
04:29 HTTP server, a compile link compiled language with lightweight threads. I also learned about warp. So
04:34 I'm learning about all sorts of things. It delegates as much as it can to the database.
04:38 So it's like a really thin layer over the database as I was describing. So serializing Jspawn,
04:43 JSON responds directly in SQL. It does the data validation, the authorization,
04:49 combining row count and retrieval, data post in a single command. All those different things are
04:54 happening in the database, not in this thing. Right? Yeah. And finally, it uses the Haskell
05:02 instead of SQL, SQL, it's the Haskell library, which manages database connections,
05:10 a binary protocol for talking to it and is stateless for horizontal scaling. Also very
05:15 important for security. You don't want to just go and the whole thing is read right on the internet.
05:20 You know, a little Bobby Tables doesn't find it. So it uses JSON web tokens. And basically what you
05:27 do is you create a user in the database and then you set authentication like this user can only
05:32 read and they can only read these tables, but they can write to that table. I don't know. And then
05:36 that's what you can do when you authenticate over this. Right. So it basically leverages database
05:41 users and their implicit permissions to control what you can do with the API. There's ways to
05:47 version the API, which is kind of interesting as the database changes, you could keep it stable,
05:52 it seems like, and it documents itself through open API. Well, this thing's pretty neat. So if
05:58 you have a Postgres database, you're like, look, we really just need read, write, update, you know,
06:03 it's maybe even an internal app. And you just want to have it real simple. Check this out. It's kind
06:08 of cool. Cool. I didn't know that Heroku had a free tier anymore. But this may let's check
06:15 out. Let's go back and do a little. It may have been from previous times. It Yeah, that statement,
06:21 it must be from previous times, because I this was updated a month ago, but it didn't. Yeah,
06:25 I think those are just whatever the free tier was. That's giving you a sense of what you can do.
06:31 Okay, cool. Yeah. Neat. I want to talk about asyncio a little bit. So there's a there's an article
06:39 called from Jacob Padilla. How Python asyncio, how Python asyncio works recreating it from
06:47 scratch. And I kind of like this, this, this idea, because I mean, I love asyncio, but sometimes it
06:55 just feels magical. And to sort of walk through some of the some how it might might be working
07:00 in the in the background is kind of cool. So it goes through generator generators review talks
07:07 about the event loop talks about sleeping, then yielding to a weight and a weight asyncio. And
07:14 I just kind of like the format that this is written into this sort of get your get a good
07:18 mental model of how asyncio works. So this is, I guess I highly recommend this article.
07:25 Looks really fun. The colors are nice to I, you know, sucker for a good web page that's easy to
07:32 read. But, but this is good. I skimmed it briefly already. And like, yeah, this kind of goes through
07:40 how a weight async works. I think this sort of feels too magical sometimes, but
07:45 it is pretty magical. And at the heart of it is generators, right? So generators themselves feel
07:51 super magical. And once you kind of understand how those work, you understand, well, okay, well,
07:56 that's how you kind of partition the work. And then you just run it in, in steps, each fragment
08:01 of the generator and off you go. But yeah, it seems pretty wild.
08:04 Yeah. So it kind of walks through all the little steps and then it shows what the,
08:07 so it has like the final bit and then it rewrites the whole thing in actual asyncio so that we can
08:13 see what it looks like. So that's great. Along with that, when I noticed the link that some,
08:18 there was a camera where I got it from, there was a comment also about if you want to like learn
08:24 asyncio from trying to recreate it from scratch, there was also a David Beasley talk from the,
08:30 from, I think it's from PyCon. Yeah. PyCon India from 2019. I hadn't watched this. I still haven't
08:37 watched this, but it's on my list now. Build your own async from David Beasley. Awesome instructor.
08:44 So I'm sure this is a good video as well. So we'll link, we'll link to that. So some,
08:48 some learning asyncio. Awesome. Good stuff indeed.
08:51 What else is good is our sponsor Mailtrap. All right. Let's talk about them.
08:57 This episode is sponsored by Mailtrap, an email delivery platform that developers love.
09:02 An email sending solution with industry best analytics, SMTP and email API, as well as SDKs
09:10 for major programming languages and 24/7 human support. Try for free at mailtrap.io.
09:16 Thank you. Thank you Mailtrap. Let's move on to something a little higher order. Shall we,
09:21 Brian? Sure.
09:21 Perfect follow on from what you were talking about. Async IO, parallel programming. It's
09:27 basically scales the waiting. Now this, this is going to be a dated statement, I believe,
09:32 but at the moment, Python is not super good at doing computational parallelism. Right.
09:38 But you know, what is GPUs? GPUs are awesome at doing parallel programming. So, you know,
09:46 your typical Python program has one thread, or if you don't do anything in most languages,
09:51 they have one thread. If you scale out your CPUs, maybe you got 16, my Alienware gaming PC has that,
09:56 my M2 MacBook Pro, I think has 10 threads, you know, but there's way more. It's get a lot more
10:02 work done if you scale those out. Right. But if you scale the GPUs, they have 16,384 threads,
10:07 things like that. Right. Incredible. So I ran across this thing called bend, B E N D. And it
10:14 comes from higher order company, which has a couple of, it's like a runtime for bend and then
10:20 has bend the language. And it's all about taking things and running them in parallel and making
10:25 them basically be Python. So it says with bend, you can write parallel code for multi-core CPUs
10:31 and GPUs without being a C or CUDA, respectively, expert with 10 years experience. And it feels
10:38 just like Python. So they give us this example. Here's a function called sum, not the best
10:42 example because it shadows a built-in, but whatever, let's get wrong with it. Right.
10:46 And so it says def sum, take a depth, and there's some value that it starts with and then adds them
10:53 all up. Right. So, so some, all the numbers from zero till two to the N and two to the depth,
10:59 right. As a switch statement that once it gets to the end, it stops and then sort of recurses down
11:06 as it would. And there's nothing that looks parallel about this whatsoever, but what happens
11:11 is this code actually runs in parallel, potentially on those thousands of GPU cores. Oh, wow. Wild,
11:18 right? Yeah. So the way it does that is, there's a picture here, is it breaks these things up into
11:26 what are called computational graphs instead of work. And then it just, it goes and processes
11:33 them to see if it, if that part of the graph can be parallelized, which is kind of nuts.
11:39 So you don't even write the parallel code. It just looks at your code and goes, oh, you,
11:43 that loop that we just saw, or that recursion type of thing we just saw, that actually could be done
11:49 recursively by just changing the parameters or something. Right. So pretty wild. And so, yeah,
11:55 they have this HVM, highly massively parallel runtime that achieves near ideal speed ups as a
12:00 function across the available cores. And then Ben is the programming language that runs in parallel
12:05 powered by this thing. Looks like Python. I don't know how compatible say it's standard library is
12:12 or things like that. Yeah. And then you have this thing called kind, which is a parallel proof
12:16 checker. Have no idea about that. I just said it's coming soon. So this is available on GitHub
12:21 and you come over and you can find their language. And guess what? It has 15,000 stars. I'd never
12:26 even heard of it until recently. It's pretty popular. Written in Rust, not that that's super
12:30 relevant, but it's like powers a language that looks like Python. That's pretty cool. I'm
12:38 cautiously optimistic about that. It could be good. So it looks like the real implementation
12:44 for it is here, although it looks pretty short. I don't know. Updated a couple of days ago.
12:49 But here it says, I don't know. We'll have to see how large of a thing is it. I guess that's
12:55 some pretty big scroll bars. So maybe the whole runtime is here. We'll see. Maybe the number of
13:00 files might be lower, but could have tons of stuff in there. Yeah, that's true. Anyway, is it going
13:06 to be a thing? I'm not sure. Maybe, maybe not. But it's cool to see people being creative with
13:11 this kind of stuff. Yeah, actually, it sounds pretty cool. And to, I guess, simplify the way
13:18 you get your code ready for massive parallel running, if it can just look at your code and go,
13:24 "Oh yeah, I got it." Yeah, if it can look and say, "This is what's often referred to as
13:29 embarrassingly parallel." There's like no state shared. You could just take the pieces and break
13:34 them up and run them on cores. If it just finds those automatically and does that, which is what
13:39 it claims to do. Yeah, that's great. That's really great. Yeah, really great. All right. Next up,
13:45 I've got regular expressions. So we're getting all sorts of deep topics today. We are. We sure are.
13:52 So I ran across, I was looking at Reddit. And so this is an, this book is an older book,
13:59 about a year, I think it's a year old. It's called the smartest way to learn Python regular
14:05 expressions. And I don't normally pitch other people's books or anything on the show, but I
14:11 thought it was kind of nice that it's a lean pub thing. So it's, they're doing their own thing.
14:15 And I guess I'll get into why I think this is kind of neat, but the minimum price is free. So if,
14:20 and since they're allowing that, I encourage everybody to throw a few bucks their way if they,
14:26 if they can, because why not? It's good to have people teaching things.
14:30 Anyway, ran across it on Reddit and the topics sound neat. So there's a, they go through an
14:38 introduction of radical expressions. They do an application. I talk about puzzle learning and
14:42 stuff, and then I skimmed the rest, but it looks like great stuff. And some Python related thing
14:49 to teach regular expressions is great, would be good. And then I went and watched their video and
14:53 now I am even more intrigued. So the idea around this book is that modern ways to learn are
14:58 different than possibly people my age might be used to just reading a book and trying things out.
15:04 But this is, this is set up. There's a little video introduction to the book. They're using
15:09 the book itself as a, as a long form teaching people, but it's also, there's a video, there's
15:16 a video course attached to it. So if you, if like you, you get the book and you're reading through
15:21 it, there's links to the different course chapters within the book, which is kind of a neat idea.
15:25 And then they've, there's puzzle solvers for each, each thing you're learning. So you can go
15:32 apply it in a puzzle environment and they're using a thing called FinFinster. That's an awful name,
15:41 actually, I think, but F I N X T E R. But but it looks like there's a whole bunch of other people
15:47 using it too. And I've never tried it, but it's this, I don't know, online trying out some code
15:52 platform, I think. So I'm, I'm, I'm going to try this out. I'm curious about the whole idea of
16:00 reading a book and clicking on it and doing that as well. That's, you know, mileage may vary for
16:06 me because I'm probably going to download this onto a Kindle and I can't click on anything with
16:10 the Kindle, but at least the one I'm using, but anyway, it might be fun to go through.
16:15 So yeah, looks excellent. Excellent. Give us a report when you get back.
16:21 Sure. Yeah.
16:21 See how it went.
16:22 Yeah. And you know, not enough regular expression material out there for just deep, diving deep
16:29 and really getting a, getting your head around it. So.
16:32 Yeah. They're very powerful if you're good at them.
16:33 Yeah.
16:34 Big F.
16:34 Those are our items. Do we have any extras?
16:39 I have one that's quite interesting. And I, I don't, somebody gave me a bit of a tip towards
16:45 this. I don't remember who it was. So I think it was a Mastodon, but thank you. And sorry about
16:51 forgetting who to credit here on Wikipedia. We have the Python conference, AKA PyCon.
16:57 And what does it say about it? It says the PyCon, the Python conference, also called PyCon,
17:02 is the largest annual convention for the discussion and promotion of the Python language.
17:06 Scroll down a little bit, Brian, you see things like, where was it in 2003? It was in DC
17:11 at 200 attendees. 2012 was in Santa Clara, 2300. Oh, 2016. Yeah, baby. Portland, 3391. Almost,
17:23 almost the record. Almost 3393. Two more people beat that number. Came over that number in the,
17:35 from Cleveland, Ohio in the before days. And then COVID hit, knocked it down and it's slowly
17:42 building back. Like in Pittsburgh, it was up to 2,500 again. So what was that? It's still got
17:49 about 50% more to add or a third looking backwards. It's down. That's not what I want to talk about.
17:56 I want to show you what is in this Wikipedia table right below it.
17:59 Where we're going next.
18:00 The long beach, baby. Going to California. Let's go. Going to the beach,
18:04 going to some warm weather, going to a vacation spot. I will bet you that this significantly beats
18:10 those numbers because it's just a destination, right? People want to just go there to be there,
18:16 right?
18:16 Well, it's kind of nice to see it on the West Coast. I mean, nothing against Pittsburgh,
18:21 but it's only two hours away from, where was it? Cleveland.
18:24 Cleveland. Yeah. Yeah. So we did have Salt Lake City in the intermission times,
18:30 but Salt Lake City was still under a lot of funk. Like it was the first one that came back
18:36 in person after COVID. So it was kind of funky. I went to the 2023 one.
18:40 I know that, and I guess I'm going to go on the record of saying, I think that like,
18:46 there's a lot more cool destination things, destination places on the coasts and Midwest,
18:52 obviously, but I think doing it kind of in the middle of the country makes sense so that people
18:59 can get easy flights from anywhere in the country. Just saying.
19:02 Yeah. What are you optimizing for? Right? Are you trying to move it around? Are you trying to make
19:08 it as centrally located? Are you trying to make it as cheap as possible? You're trying to make
19:12 it a destination. All these different things play into this, and this is the biggest funding for the
19:18 PSF. So it's not just like a curiosity, right? This is the funding for the PSF to a significant
19:24 degree.
19:24 It's interesting though that Portland was way back in 2017, and this in 2026 is going to be the
19:30 next time that it's going to be on the West Coast. I don't think that, I know that everybody on the
19:35 East Coast thinks that Salt Lake is in the West, but it's not really.
19:39 Not even the same time zone, Brian. Not even the same time zone. Mountain time,
19:44 Pacific time. Let's go. Anyway, this is my extra. Planned tentatively, the PyCon after Pittsburgh.
19:52 So next year is still Pittsburgh. It's round two, but then Long Beach, California. And I'm
19:57 here for it, Brian.
19:58 Also, I've always been curious about, sometimes I'm curious about the numbers of like,
20:02 how many people showed up in Montreal? And it's cool to see. You can just look it up on Wikipedia.
20:07 Yeah, yeah. It's all right here.
20:08 I've got something completely random that I, Prince of Persia was a video game from '89.
20:18 I remember it.
20:20 Oh, not only was it a video game, it was one of the best video games. It was so good.
20:25 Yeah. Well, it was like this, one of these cool, like, you know, racer through all sorts of stuff. We had like the little runner thing and had to jump over
20:32 all sorts of stuff. It was a fun game. And then there was a bunch of, a bunch of different
20:37 permutations of it. And it's still going strong, I think. Still stuff going on. There's a, anyway,
20:45 a cool game. But I ran across this, there's a book about it. So Jordan Mechner is a person that
20:53 wrote it and originally wrote the Prince of Persia game. And he wrote a book called
21:00 Replay. It's a memoir of an uprooted family. And apparently it goes back to 1938,
21:10 when somebody in his family, his parents fled Austria to escape the Nazi persecution.
21:17 And then, you know, he traveled, they traveled to the US. Eventually they went to France. Then
21:23 they went to eventually, I think, actually, I'm just making this up. I don't know if he ever made
21:27 it to the US. I think so. But then there's, then he goes back eventually. Yeah. Because he goes
21:35 back eventually and works in France for a little while. But then did this book about his life,
21:43 his family and his life story and then about the Prince of Persia. So I'm kind of excited to read
21:49 this. - I really like these, like looks inside the storytelling of the history of some of these famous games and famous platforms.
21:56 Yeah. They're very interesting. - Yeah. And apparently his dad wrote the,
22:01 like wrote the music for the original video game. - Okay.
22:04 - It's kind of cool. On a, I think, anyway, one of the, one of the older computers in the 80s.
22:11 So that's it. That's my extra. - Awesome. Let's close it out with a work hack joke. I don't know what you call this, Brian. So we have this woman developer sitting
22:22 here, pretty happy. And this boss comes in, manager says, "Hmm, I don't like this bash
22:27 script you created. Write it in Python instead," commands the boss. "Import subprocess. Subprocess
22:34 call work.sh." Well, that's a day's work done. - Nice.
22:39 - How about that? Just, if you want me to write in Python, I'll just call the script from Python.
22:44 Let's go. - Yeah. Well, what are the, what are the times where I'm like, where I early on, I guess not early on. Anyway, 20 years ago or
22:52 so, I'm, I find myself in a new job and they've got all these builds, make scripts for C++ code.
23:00 And I can't, they were, they were in batch files and, and bash and all sorts of stuff. And, and
23:06 I said, can you rewrite this? Cause it's kind of a mess. So I rewrote it in a combination of,
23:12 I had a combination of bash, Perl, Python, sed, a whole bunch of things, whatever I wanted to,
23:18 to what I thought was an elegant solution. And the team was horrified. They're like,
23:23 we're not learning that many languages. I'm like, okay. So I rewrote it all in Python.
23:30 And that was the first time I, I really grabbed Python for do whatever I want as a shell script
23:36 language. - Yeah. That's very cool. Yeah. I love it. - Subprocess call. Yeah. - Subprocess.call.
23:41 That's right. - Yeah. Nice. - Hey, don't wait for it to complete. Just let it go.
23:46 - Well, yeah. - No error handling needed. Let's, let's just let it run. No, it's a good joke. And
23:53 that comes from your credit where credit's due, that comes from Linux handbook. - Linux handbook.
23:57 Nice. Cool. Well, thanks Michael for another wonderful episode. - As always, thanks everyone for