- Podcasts
- Python Bytes
- #385 RESTing on Postgres
About this episodeTopics covered in this episode: PostgresREST How Python Asyncio Works: Recreating it from Scratch Bend The Smartest Way to Learn Python…
- PostgresREST
- How Python Asyncio Works: Recreating it from Scratch
- Bend
- The Smartest Way to Learn Python Regular Expressions
- Extras
- Joke
Transcript
From the publisherThis transcript comes from the podcast’s own feed, shown as the publisher wrote it.
#385 RESTing on Postgres
If you want to connect with us, we're all on Fosstodon, the show, and Michael and I. We're Fosstodonians. Fosstodonians, yeah. So those links are on the show notes. Also, in the show notes or at pythonbytes.fm, you can go to live if you want to join us. Join the audience. Usually, it's Tuesdays at 10 a.m. Pacific time.
You can also use that link to get older videos, too, if you want to watch what happens live. And finally, if you'd like to receive an email a day or two after the show to find out all of the links and to have a link to the show. But make sure if you listen to this and you want to make sure that you don't miss any links and be able to go check those out later, you can become a friend of our show.
This is the perfect time. So there's always a perfect time somewhere in the world. But you've got this thing going on tomorrow. And then for the rest of the week, I'm going to this thing called the Giant Loop Ride. This off-road rally that's at a hot springs and maybe 300 other off-road riders out in the southeast desert of Oregon.
That's all I'm saying. But also why we're doing this in the middle of the night for some places. So I would say if you have a podcast that kind of runs late, sometimes it'll make you tired, wouldn't you think? Would you have to rest a little? Yeah, yeah. Let's talk about Postgres, PostG, rest, as in Postgres, rest API.
So this one comes to us from Mark Little. So thanks, Mark, for sending us over. It's a good one. This is basically a web server, web front end for your database. So you've got Postgres. And you might think, all right, well, I would just like to expose the tables as endpoints with, you know, ability to do queries over HTTP endpoints.
You somehow pass the query over. You want to have security, things like that. So this is an app that you can run and it automatically, with zero API work for you, turns your Postgres instance into a somewhat restricted API, basically. Cool, right? Yeah. Yeah. Now, obviously, these things, I don't know, I always have mixed feelings when I talk about these things.
On one hand, if you just say, I would like, say, my view front end or something, or even my Python code to just have access to the database as an API, just read, you know, the crud things of all the tables, you're good to go. This is what you want, right? On the other hand, sometimes I want to have more specific things.
Instead of just, I want to go through the user, the create new user API endpoint that uses, I don't know, argon2 for memory hard, password hashing and other checks. And maybe, you know, like every time I come up against these things, I'm like, oh, they're really cool. But at the same time, I kind of want a little more specific control over how it works.
So if you're just like, I just have a database, I just want to be able to expose it real quick, let's go. Postgres rest sort of thing. Cool, right? So the way it works is it's written in Haskell, which you may, and it's 4% Python, so I guess there's a little Python in there. But, you know, it doesn't really matter what it's written in.
It's just a thing you run, and then you talk to it from your Python or from your JavaScript or whatever it is, okay? So there's a couple of things you care about. One, you probably care about performance. I'm not super familiar with the performance of Haskell, but I believe it runs on top of the JVM.
So serializing JSON responds directly in SQL. It does the data validation, the authorization, combining row count and retrieval, data post in a single command. All those different things are happening in the database, not in this thing, right? And finally, it uses the Haskell, instead of SQL, it's the Haskell library, which manages database connections, a binary protocol for talking to it, and is stateless for horizontal scaling.
Also, very important for security, you don't want to just go, and the whole thing is read-write on the internet. How could you get, you know, a little Bobby Tables doesn't find it? So it uses JSON web tokens, and basically what you do is you create a user in the database, and then you set authentication, like, this user can only read, and they can only read these tables.
As the database changes, you could keep it stable, it seems like, and it documents itself through OpenAPI. Well, this thing's pretty neat. So if you have a Postgres database, and you're like, look, we really just need read, write, update, you know, maybe even an internal app, and you just want to have it real simple, check this out.
Yeah, I think that's just whatever the wimpy free tier was, that's giving you a sense of what you can do. Okay, cool. Neat. I want to talk about AsyncIO a little bit. So there's an article called from Jacob Padilla, how Python AsyncIO works, recreating it from scratch. And I kind of like this idea, because I mean, I love AsyncIO, but sometimes it just feels magical.
And to sort of walk through some of the, how it might be working in the background is kind of cool. So it goes through generators review, talks about the event loop, talks about sleeping, then yielding to await and await with AsyncIO. And I just kind of like the format that this is written in to just sort of get your, get a good mental model of how AsyncIO works.
And then you just run it in, in steps, each, each fragment of the generator and off you go. But yeah, it seems pretty wild. Yeah. So it kind of walks through all the little steps and then it shows what the, so it has like the final bit. And then it rewrites the whole thing in actual AsyncIO so that we can see what it looks like.
Good stuff. Indeed. What, what else is good is our sponsor Mailtrap. All right. Let's talk about them. This episode is sponsored by Mailtrap, an email delivery platform that developers love. An email sending solution with industry best analytics, SMTP and email API, as well as SDKs for major programming languages and 24 seven human support.
It's basically scales the weighting. Now this, this is going to be a dated statement, I believe, but at the moment, Python is not super good at doing computational parallelism. Right. But you know what is GPUs. GPUs are awesome at doing parallel programming. So, you know, your typical Python program has one thread, or if you don't do anything in most languages, they have one thread.
So it says, with Bend, you can write parallel code for multi-core CPUs and GPUs without being a C or CUDA, respectively, expert with 10 years experience. And it feels just like Python. So they give us this example. Here's a function called sum. Not the best example because it shadows a built-in, but whatever.
Let's get rolling with it, right? And so it says, def sum, take a depth. And there's some value that it starts with and then adds them all up, right? So it says sum all the numbers from 0 till 2 to the end and 2 to the depth, right? It has a switch statement that once it gets to the end, it stops and then sort of recurses down as it would.
And there's nothing that looks parallel about this whatsoever. But what happens is this code actually runs in parallel potentially on those thousands of GPU cores. Oh, wow. Wild, right? Yeah. So the way it does that is, there's a picture here, is it breaks these things up into what are called computational graphs instead of work.
And then it just, it goes and processes them to see if that part of the graph can be parallelized, which is kind of nuts. So you don't even write the parallel code. It just looks at your code and goes, oh, that loop that we just saw or that recursion type of thing we just saw, that actually could be done.
Recursively by just changing the parameters or something, right? So pretty wild. And so, yeah, they have this HVM, highly massively parallel runtime that achieves near ideal speedups as a function across the available cores. And then Ben is the programming language that runs in parallel, powered by this thing.
Looks like Python. I don't know how compatible, say, its standard library is or things like that. Yeah. And then they have this thing called Kind, which is a parallel proof checker. Have no idea about that. I just said it's coming soon. So this is available on GitHub. And you come over and you can find their language.
So it looks like the real implementation for it is here, although it looks pretty short. I don't know. Updated a couple of days ago. But here it says, yeah. I don't know. We'll have to see how large of a thing is it. I guess that's just some pretty big scroll bars. So maybe the whole runtime is here.
You could just take the pieces and break them up and run them on cores. Yeah. If it just finds those automatically and does that, which is what it claims to do. Yeah, that's great. That's really great. Yeah. That's really great. All right. Next up, I've got regular expressions. So we're getting all sorts of deep topics today.
We are. We sure are. So I ran across, I was looking at Reddit. And so this book is an older book, about a year, I think it's a year old. It's called The Smartest Way to Learn Python Regular Expressions. And I don't normally pitch other people's books or anything on the show, but I thought it was kind of nice that it's a LeanPub thing.
Anyway, ran across it on Reddit. And the topics sound neat. So they go through an introduction of regular expressions. They do an application. I talk about puzzle learning and stuff. And then I skimmed the rest. But it looks like great stuff. And some Python related thing to teach regular expressions would be good.
And then I went and watched their video. And now I am even more intrigued. So the idea around this book is that modern ways to learn are different than possibly people my age might be used to, just reading a book and trying things out. But this is set up. There's a little video introduction to the book.
And I can't click on anything with the Kindle, but at least the one I'm using. But anyway, it might be fun to go through. Yeah. Looks excellent. Give us a report when you get back. Sure. Yeah. See how it went. Yeah. And, you know, not enough regular expression material out there for just deep, diving deep and really getting your head around it.
And sorry about forgetting who to credit here. On Wikipedia, we have the Python Conference, aka PyCon. And what does it say about it? It says, the Python Conference, also called PyCon, is the largest annual convention for the discussion and promotion of the Python language. Scroll down a little bit, Brian.
And then COVID hit, knocked it down, and it's slowly building back. Like in Pittsburgh, it was up to 2,500 again. So what is that? It's still got about 50% more to add or a third looking backwards. It's down. That's not what I want to talk about. I want to show you what is in this Wikipedia table right below it.
Well, it's kind of nice to see it on the West Coast. I mean, nothing against Pittsburgh, but it's only two hours away from, where was it? Cleveland. Yeah. Yeah. Yeah. So we did have Salt Lake City in the intermission times, but Salt Lake City was still under a lot of funk. It was the first one that came back in person after COVID.
So it was kind of funky. I went to the 2023 one. I know that, and I guess I'm going to go on the record of saying, I think that there's a lot more cool destination things, destination places on the coasts. Yeah. And Midwest, obviously. But I think doing it kind of in the middle of the country makes sense so that people can get easy flights from anywhere in the country.
Just saying. Yeah. And what are you optimizing for, right? Are you trying to move it around? Are you trying to make it as centrally located? Are you trying to make it as cheap as possible? Are you trying to make it a destination? All these different things play into this. And this is the biggest funding for the PSF.
So it's not just like a curiosity, right? This is the funding for the PSF to a significant degree. It's interesting, though, that Portland was way back in 2017. And this in 2026 is going to be the next time that it's going to be on the West Coast. I don't think that I know that everybody on the East Coast thinks that Salt Lake is in the West, but it's not really.
And he wrote a book called Replay. It's a memoir of an uprooted family. And apparently it goes back to 1938 when somebody in his family, his parents fled Austria to escape the Nazi persecution. And then, you know, they traveled to the U.S. Eventually they went to France. Then they went to eventually, I think, actually, I'm just making this up.
I don't know if we ever made it to the U.S. I think so. But then he goes back eventually. Yeah, because he goes back eventually and works in France for a little while. But then did this book about his life, his family and his life story and about the Prince of Persia. So I'm kind of excited to read this.
I really like these looks inside the storytelling of the history of some of these famous games and famous platforms. Yeah, they're very interesting. Yeah. And apparently his dad wrote the music for the original video game. Okay. It's kind of cool. On a, I think, anyway, one of the older computers in the 80s.
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 here, pretty happy. And this boss comes in, manager says, I don't like this bash script you created. Write it in Python instead, commands the boss.
Import subprocess. Subprocess call work.sh. Well, that's a day's work done. Nice. How about that? Just if you want me to write in Python, I'll just call the script from Python. Let's go. Yeah. Well, one of the times where I'm like, where early on, I guess not early on. Anyway, 20 years ago or so, I find myself in a new job.
And they've got all these builds, make scripts for C++ code. And I can't remember. They were in batch files and bash and all sorts of stuff. And I said, can you rewrite this? Because it's kind of a mess. So I rewrote it in a combination of, I had a combination of bash, Perl, Python, said, a whole bunch of things, whatever I wanted to, to what I thought was an elegant solution.
