Latest / Talk Python To Me / #328: Piccolo: A fast, async ORM for Python (updated)
Transcript
- 0:00ORMs are one of the main tools to put first-class data access in the hands of non-SQL-loving
- 0:05developers.
- 0:06And even for those who do love SQL, making them way more productive.
- 0:10When you hear about ORMs in Python, we often hear about either SQLAlchemy or Django ORM.
- 0:15And we should, they're great.
- 0:17But there are newer ORMs that take better advantage of modern Python.
- 0:20On this episode, you'll meet Daniel Townsend.
- 0:23He's the creator of Piccolo ORM.
- 0:25A great ORM that is async first, but also has a synchronous API.
- 0:30It has a super clean query syntax, and it's easy to learn.
- 0:33This is Talk Python To Me, episode 328, recorded July 22nd, 2021.
- 0:39Welcome to Talk Python To Me, a weekly podcast on Python, the language, the libraries, the
- 0:57ecosystem, and the personalities.
- 0:59This is your host, Michael Kennedy.
- 1:00Follow me on Twitter, where I'm @mkennedy.
- 1:02And keep up with the show and listen to past episodes at talkpython.fm.
- 1:06And follow the show on Twitter via at Talk Python.
- 1:09This episode is brought to you by Linode, us over at Talk Python Training, and the transcripts
- 1:15are brought to you by Assembly AI.
- 1:16Please check out what we're all offering during our segments.
- 1:19It really helps support the show.
- 1:21Do you want to learn Python, but you can't bear to subscribe to yet another service?
- 1:25At Talk Python Training, we hate subscriptions too.
- 1:28That's why our course bundle gives you full access to the entire library of courses for
- 1:33one fair price.
- 1:34That's right.
- 1:35With the course bundle, you save 70% off the full price of our courses, and you own
- 1:40them all forever.
- 1:41That includes courses published at the time of the purchase, as well as courses released
- 1:46within about a year of the bundle.
- 1:47So stop subscribing and start learning at talkpython.fm/everything.
- 1:54Dan, welcome to Talk Python To Me.
- 1:55Yeah, thanks for having me.
- 1:56I'm a big fan of the show, so it's kind of like a little dream to be on the show.
- 1:59Oh, how nice.
- 2:00That means a lot.
- 2:01Thank you.
- 2:01It's great to have you here, and you've built some really neat software that I'm looking forward
- 2:06to diving into.
- 2:07It's interesting because we're going to talk about your ORM, but it's also, there's so many
- 2:12different areas at which now they're kind of past Python 2, and there's a lot of folks
- 2:16who are saying, you know what, we can really put Python 2 behind us, and let's just build
- 2:20for the future.
- 2:21That just opens up so many doors, right?
- 2:23Like, oh, well, type-ins, of course, type-ins.
- 2:25Why not type-ins?
- 2:25Well, Python 2 is why not type-ins, but not anymore, right?
- 2:29And async and all these other things, and just, it's really great to have these frameworks
- 2:35like yours coming along that just go, modern Python, what can we build now?
- 2:39Yeah, I totally agree.
- 2:40It felt like Python 3.6 kind of closed the door on the, should we still be using Python
- 2:442 conversation?
- 2:45Because by that point, you had async, you had type annotations, data classes, I think
- 2:50were Python 3.7, enums.
- 2:52It just goes on and on.
- 2:53Like, it just felt like the Python community knocked out of the park, Python 3.6 and on.
- 2:58Yeah, I have a theory that is completely unsubstantiated, that actually a lot of the progress has to
- 3:05do with f-strings.
- 3:06So many people are like, ah, I just want to easily format this code.
- 3:09What do I need?
- 3:10I need Python 3.6.
- 3:11Fine.
- 3:11That's true, actually.
- 3:12We're going to switch.
- 3:13And it's such a minor feature.
- 3:15Still minor.
- 3:16The pain of going back would be horrible.
- 3:18I couldn't imagine using Python 2 now, actually.
- 3:20I know.
- 3:21Yeah.
- 3:21Same.
- 3:22Quick welcome to some folks on Livestream.
- 3:24Chris May.
- 3:24Hey.
- 3:24Happy to see you here.
- 3:25Paul Everitt.
- 3:26So glad you can stop by.
- 3:27And Teddy as well.
- 3:28We'll get some questions in from you all, I'm sure.
- 3:30Now, before we get into Piccolo and all the cool stuff that I was alluding to there, let's
- 3:36just start with your story, Dan.
- 3:37How do you get into programming in Python?
- 3:38Yeah, so I've been programming for quite a long time now.
- 3:40I might look quite youthful on this camera, but that's just because my webcam's got a filter
- 3:44on it.
- 3:44But I've actually been through.
- 3:46The touch up my appearance is checked.
- 3:47Yeah.
- 3:48I've actually been.
- 3:48I'm just kidding.
- 3:49Yeah.
- 3:49Programming with Python for about 14 years.
- 3:52So Django was kind of one of my first frameworks and just really fell in love with it because
- 3:57in university I learned C. And then it's like, oh, this world of Python exists and it's
- 4:01a bit more easier than C. And so I've used it extensively since then.
- 4:05I've mostly been working for startups and design agencies and then really fell down the
- 4:11async rabbit hole over the last few years.
- 4:14So I really enjoy working with WebSockets and building kind of interactive front ends and
- 4:20also really kind of excited the way that Postgres continues to grow and have new features.
- 4:25They're the things that really excite me in the Python world.
- 4:27Yeah.
- 4:27Those things are super exciting and the web frameworks are coming along to embrace them.
- 4:32The language features, you know, async and await.
- 4:35We had asyncio in Python 3.4, but it didn't really gain a ton of adoption because there
- 4:40was, it sort of had this callback style of working, which is not the same as, you know,
- 4:45await a thing and just keep rocking.
- 4:46Totally agree.
- 4:47Yeah.
- 4:47So I think they pretty much took what Twisted had created and ported it over.
- 4:51And like I say, it's all callbacks and it was still better than, it was still good
- 4:55to have that option in the standard library.
- 4:57But I totally agree.
- 4:57Async await really kind of set it on fire because it was quite hard to approach asyncio at
- 5:02first.
- 5:02It was, you don't have to think about how you write code differently.
- 5:05You just put the awaits in the spots and then, you know, everything kind of flows.
- 5:08It's, I know it's not the same, but from how you think about structuring your code is real
- 5:13similar.
- 5:14Yeah.
- 5:14And I think the maturity is there now with the libraries and the standard library and it
- 5:18feels like you can just dive in and it feels a bit like a no brainer now to use async.
- 5:22Yeah.
- 5:22I think it is a no brainer for sure.
- 5:24So it's interesting that you got into Django in the early days because Django is the only
- 5:31major web framework that says, here's your ORM.
- 5:34Here's how you use it.
- 5:35As opposed to Flask that says, do what you want.
- 5:38Or Pyramid that says, do what you want.
- 5:40You know, you could use an ORM, but you don't have to.
- 5:42But Django is, it's part of the culture and the zen of it.
- 5:45Right?
- 5:46Yeah, I'm a big fan of Django.
- 5:47I think it's kind of a masterpiece because it stood the test of time so remarkably well
- 5:52and how you could have written a app 12 years ago and then in an afternoon, you could have
- 5:58upgraded it to the latest version.
- 5:59So there's a lot to be said for Django and I really like the tight integration.
- 6:03So people tend to prefer one or the other, the kind of the Flask route where you can make
- 6:08all of your choices.
- 6:09But I like really tightly integrated solutions where you come to a problem and then you pretty
- 6:16much know what to pick.
- 6:17Yeah.
- 6:18So that's definitely inspired Piccolo to some extent.
- 6:20Yeah.
- 6:21I love the diversity of technology and libraries and ways of doing things in the Python space.
- 6:26But when people come from other technologies, it's a challenge to go, well, here's 20 different
- 6:33ORMs you could use and seven different web frameworks.
- 6:35And they're like, I don't want seven.
- 6:36I want to know what I should be doing.
- 6:38What should I do?
- 6:39Right.
- 6:39And so it's a mixed bag.
- 6:41I really like that.
- 6:42But the thing is, if you come from the JavaScript world, then it's a relief because you've only
- 6:45got 20, not 2000.
- 6:46Oh my goodness.
- 6:48This one's seven months old.
- 6:50It's like ancient.
- 6:50Can we still use it?
- 6:51Yeah.
- 6:52Yeah.
- 6:53No, no.
- 6:53That's a whole different discussion.
- 6:55The JavaScript churn over there.
- 6:57Yeah.
- 6:57But yeah, let's focus in on what you built here.
- 7:00So let's talk about Piccolo.
- 7:01You just sell it.
- 7:02You describe it as a fast async ORM for Python.
- 7:06That's easy to learn.
- 7:07And I think it seems like it really hits on all those points.
- 7:10Tell us about it.
- 7:10Yes.
- 7:11So all those terms are like fairly aspirational.
- 7:13So the fast is because it's built on asyncpg.
- 7:16So this is a really fast Postgres adapter that the Magic Stack guys built.
- 7:20Part of the reason it's so fast is it's written in Syphon.
- 7:23So it's compiled.
- 7:24And then there's also a few other features in Piccolo, which kind of help it speed wise, like
- 7:28frozen queries where it doesn't have to generate the SQL each time.
- 7:32It kind of caches some of it.
- 7:33So that's kind of where the fast comes from.
- 7:35Async.
- 7:36Oh, nice.
- 7:37Async.
- 7:37Async.
- 7:38Async.
- 7:38Async.io.
- 7:38That was really one of the core reasons for building it is because I was a Django channels
- 7:43power user and I had to build a lot of chat applications in my day job.
- 7:47So for me, async was kind of essential.
- 7:49And then the easy to learn is called also aspirational.
- 7:52I put like as much effort as I possibly can into the documentation and stuff like that.
- 7:56But I'll let the readers be the judge of that.
- 7:59Yeah.
- 7:59That's in the eye of the beholder.
- 8:00Right.
- 8:00But at the same time, you have done a lot of things that I don't see others doing.
- 8:05Like the documentation is good.
- 8:06Obviously, that's pretty standard on a lot of the different frameworks, although they can
- 8:11be super intense.
- 8:12Right.
- 8:12Like I love SQLAlchemy.
- 8:13But when I go and read there, I'm like, OK, here's a 20 page doc.
- 8:16I better pay attention and turn off my music and just like not miss something on the other
- 8:21hand.
- 8:21But what you are doing that's pretty unique is you've got like a playground.
- 8:24You've got a playground for the admin backend.
- 8:26Oh, by the way, there's an admin backend.
- 8:27And you've got a playground for trying out queries and stuff like that.
- 8:32Right.
- 8:32That's pretty unique.
- 8:33Yeah.
- 8:33So I was really excited by the playground concept.
- 8:36So it's something I borrowed from the Swift world.
- 8:38And because Apple are trying to make Swift accessible to newcomers, they have this concept
- 8:43of a playground, which is basically a pre-populated chunk of code.
- 8:47And then you can just play around with it as kind of the name implies.
- 8:51So I use IPython to achieve it.
- 8:53So a lot of people know IPython, just running it on the command line like a terminal.
- 8:57But you can actually embed it within your Python code.
- 9:00So the way the playgrounds work is it creates a SQLite database.
- 9:04It loads an example schema, populates it with data, and then basically launches the IPython
- 9:10shell.
- 9:11So you basically have all of that set up.
- 9:13Because one of the problems with learning an ORM is the barrier is huge when you think
- 9:17about it for a newcomer.
- 9:18You have to set up a database.
- 9:21You have to understand schemas and migrations and running them and populating data.
- 9:26So I just wanted to say, look, I'm going to shortcut all of that.
- 9:28You install Piccolo.
- 9:29You do Piccolo Playground Run.
- 9:31And then you've got an example schema.
- 9:33And then you can actually follow along with the documentation.
- 9:35So whenever there's an example query in the Piccolo docs, you can actually run it in the
- 9:40playground.
- 9:40And you should see the results.
- 9:42So yeah, I was quite excited about that.
- 9:43I like it a lot.
- 9:44And you're right that it is challenging.
- 9:45You know, as we build up experience and we get used to it, it's like, oh, yeah, you just
- 9:49fire up the database server and then you connect to it.
- 9:52Then you do this thing.
- 9:53But how do you get the database server installed?
- 9:56What if you have the wrong version?
- 9:57What if it requires authentication?
- 9:58What if there's a firewall?
- 10:00Just like there's layers and layers of places where people get sheared off.
- 10:03Like, ah, this doesn't work for me.
- 10:05All right.
- 10:05And so keeping that simple, it's great.
- 10:07And I think I just want to give a shout out to SQLite.
- 10:10Because what you just said is such a good example of you're probably going to run this
- 10:14against Postgres or something like that.
- 10:16Yeah.
- 10:16But for the little example, you don't need that.
- 10:18You can just use SQLite.
- 10:19And it's so nice that that's a serverless in the sense that it's just embedded.
- 10:24Doesn't require a separate process to run or connect to.
- 10:26It's already comes with Python, generally speaking.
- 10:29It's really nice.
- 10:30Yeah, I totally agree.
- 10:31And that's the reason for SQLite support within Piccolo.
- 10:33Because as you said, no one's going to run it in production.
- 10:35But then it's just that frictionless setup, which is so great.
- 10:39Like, using SQLite asynchronously doesn't really make a lot of sense.
- 10:43Because you don't really have any network lag talking to the database.
- 10:46But it's great to have it in there for convenience.
- 10:48Yeah, it is.
- 10:49You know, I suspect there may be some people who run it in production in like an early, early
- 10:53prototype stage.
- 10:54Yeah.
- 10:55They just want to get something up.
- 10:56And here, let's just put it up on, you know, on some hosting place and just check it out
- 11:00real quick.
- 11:00And then we'll go from there.
- 11:01Yeah.
- 11:01One thing that's quite nice is I used to do this in the design agency.
- 11:05We'd use SQLite for the prototype.
- 11:06And then you could R-sync it down to your local machine.
- 11:09So you didn't have to do a database dump and reload it.
- 11:12You could literally just R-sync the file.
- 11:13Yeah.
- 11:14So there are a bunch of conveniences with SQLite for sure.
- 11:17Yeah.
- 11:17So it gets to be too much data.
- 11:18Or really, you want sort of too much concurrency or multi-machines scale out and all that sort
- 11:24of stuff.
- 11:24So it's not the final destination.
- 11:25But I do think it's a really interesting and important starting ground there.
- 11:28Yeah, for sure.
- 11:29Yeah.
- 11:30So I gave a shout out to two ORMs already, the Django one and SQLAlchemy.
- 11:36And I feel like those are probably the two big hitters of the Python space, although there
- 11:41are many, many more.
- 11:42So why not just use those?
- 11:44Why go and create Piccolo?
- 11:46Yeah.
- 11:46So when I started it, there was no async options for ORMs at all.
- 11:50It was all very new.
- 11:52And my day job involved async programming all of the time.
- 11:56Because of chat apps, online games, that kind of stuff.
- 11:58So that was really why I was like, well, nothing really served my needs at the time.
- 12:02Right.
- 12:02Because until recently, SQLAlchemy had no async capabilities.
- 12:07Yeah.
- 12:08And Django ORM more or less still doesn't, right?
- 12:10That's the final part that they're trying to move to async.
- 12:13Yeah.
- 12:13So the workaround is to run it in a thread, but it's just not as performant as running
- 12:17with asyncio directly.
- 12:18So there's kind of this vacuum for a time with async ORMs.
- 12:21But then it was also about just starting through a blank sheet of paper and thinking, look,
- 12:26like we've got all these great tools to work with now in Python 3.6 and onwards.
- 12:30How can I absolutely push them to the limit?
- 12:33So the syntax, it uses a lot of direct references to objects rather than strings.
- 12:39So for example, in Django, you do my table to objects and then dot filter and you name equals
- 12:46Dan.
- 12:47The problem with that is you can quite easily make a mistake as the code changes.
- 12:52So you might end up having a column which no longer exists or so strings can be a little
- 12:58bit fragile.
- 12:59So with Piccolo, it's just object references everywhere.
- 13:02So rather than name, it's in this example here, it's band dot name where band is the
- 13:07name of the table.
- 13:08Right.
- 13:08So it's like many of the ORMs, it's sort of class OOP based, right?
- 13:13So you create a class that maps to a table and database, right?
- 13:16Yeah.
- 13:17So there's one distinction I'd like to make quickly as well.
- 13:19So there's ORMs and then there's query builders.
- 13:21And in Python, there's not really too much of a distinction, but in other languages like
- 13:25JavaScript, query builders are really popular as like a separate idea to ORMs.
- 13:30And query builders, they kind of, you create a SQL query, you execute it on the database,
- 13:35and then you tend to work more with dictionaries and lists rather than objects.
- 13:39So I'd say that 90% of Piccolo is a query builder, but people are used to ORMs and Python and it
- 13:46can actually lead you to some quite poor patterns, ORMs, because what the first thing people learn
- 13:51with Django is they'll go my table dot objects dot get.
- 13:55So it returns an object and then they'll change the attributes of the object and then they'll
- 13:59call save.
- 14:00But then that's actually two database queries because you have to get it and then you have
- 14:03to update it.
- 14:04Well, and it's not just two database queries.
- 14:05It can be a lot of serialization.
- 14:07Yeah.
- 14:08Like the slowest part of all of my data stack is if I were to try to pull back 10,000 records
- 14:15at a time, it's the deserialization of those 10,000 things that is actually the slowest part
- 14:19of the entire process.
- 14:21And here, if what you're talking about, like you're talking about the band, if the band
- 14:25has tons of information and you just want to change the name, right?
- 14:29You're pulling all that data back, converting it or whatever, then changing the thing and
- 14:33then pushing it back down.
- 14:34Yeah.
- 14:34It's a lot of work.
- 14:35And a lot of the time what will happen is you'll serialize it into an object and then
- 14:38you'll deserialize it out into JSON anyway.
- 14:41And it's kind of pointless when you think about it.
- 14:44And there's also the problem with objects getting stale.
- 14:46So you might pull an object into memory, but then some other user might manipulate all those
- 14:51objects.
- 14:51But then when you save it, are you going to overwrite those fields that have been updated
- 14:54in the database?
- 14:55So it's problematic in a way.
- 14:57So it is an ORM, but then I'd really encourage people to look at it as a query builder too,
- 15:02because in my own apps, I use the select method a lot.
- 15:06So rather than returning the objects, it returns dictionaries.
- 15:09And then it has this option, an output method.
- 15:12So you can just literally deserialize it straight into JSON.
- 15:15So this is what makes it fast because the query is going through asyncpg, which is super
- 15:20fast.
- 15:21And then it's coming back as a dictionary straight from asyncpg.
- 15:24And then it's using all JSON to stick it straight into a string.
- 15:28So you're kind of missing, you're skipping all this deserialization nonsense.
- 15:32Yeah, yeah.
- 15:32This is super interesting.
- 15:34Now that you pointed out, I'm not used to seeing projections in ORMs.
- 15:39What I'm used to seeing is give me back the classes that, in this case, a band, right?
- 15:44Do a query like where the popularity is greater than 100 or whatever, and give me a whole bunch
- 15:47of bands, band objects back, and then I'm going to work with them.
- 15:51But sometimes like the one I work with most commonly is MongoEngine.
- 15:55I know it's not an ORM, it's an ODM, but close enough.
- 15:58Does the serialization bit.
- 15:59And you can say, I don't really want the other parts.
- 16:02Just give me the name, like the website or something.
- 16:04And it won't ship or deserialize those things.
- 16:07But you just end up with the same objects that just have none everywhere else, right?
- 16:11Where in your ORM, you can actually say band.select, band.name, band.url, or something like that.
- 16:19And that would return the dictionary with those two things, right?
- 16:21A list of dictionaries, probably?
- 16:23A list of dictionaries, yeah.
- 16:24And then it also has a feature, like Django has this feature, which is invaluable.
- 16:28It's called values list.
- 16:30And so if you just want one value back, it'll then condense it down to just a list of values.
- 16:35Right.
- 16:35Like I want all the IDs of the bands that play pop music or something.
- 16:39Yeah, and it's so much more efficient than doing like in Django, you know, band.orl,
- 16:44objects.all, and then looping through to get the IDs.
- 16:47It's just these little tricks you learn.
- 16:49Like if you use Django for ages, you just learn these little tricks.
- 16:53Right.
- 16:53Another one that stood out to me is the ability to do set-based operations.
- 16:58Because when I think of ORMs, I just, for everyone listening, I adore ORMs.
- 17:02I think they're really empowering for people.
- 17:04I think they take a lot of the modern tooling that we love, like refactoring, and allow you
- 17:10to apply that like over to your query.
- 17:12Because if you wanted to like change the casing of band.name, you could refactor, rename that,
- 17:17and it would affect your queries because that's still Python code, right?
- 17:20That said, there are places where people either abuse it or it's just inappropriate.
- 17:24So the places where it gets abused a lot would be the N plus one problem, right?
- 17:29Where you've got a lazy reference to something else.
- 17:32And you don't know that that's going to be a separate query for every time you touch one
- 17:36of those.
- 17:36And you get a list of objects back and you loop over them and you access like, in your example,
- 17:40you've got band.manager.
- 17:42For B and band, band.manager, right?
- 17:44That could be 101 queries for what should have been one, right?
- 17:48Yeah, that's a really good point.
- 17:49And even experienced developers get this wrong because they might use serializers, which are
- 17:53calling properties under the hood on the table, which are triggering SQL queries.
- 17:56So this is another design intent behind Piccolo is whenever a query is run, it's very explicit.
- 18:02You are literally calling .run or .runsync.
- 18:05So there's none of this magic.
- 18:07You can't accidentally create an N plus one query.
- 18:09You might accidentally end up with a coroutine or something.
- 18:11Yeah.
- 18:12So yeah, it's a really good point because I think N plus one is kind of like the scourge of
- 18:16developers with performance.
- 18:17And also coming back to your point about ORMs, as a backend developer, we can spend hours a day
- 18:22using ORMs.
- 18:23It's kind of like one of the main tools in our tool belt.
- 18:26So it's kind of quite nice to start from a blank sheet of paper and think, how can I make
- 18:30that experience maybe like slightly better if I can?
- 18:32This portion of Talk Python To Me is sponsored by Linode.
- 18:37Visit talkpython.fm/Linode to see why Linode has been voted the top infrastructure
- 18:42as a service provider by both G2 and TrustRadius.
- 18:46From their award-winning support, which is offered 24, 7, 365 to every level of user,
- 18:51to the ease of use and setup, it's clear why developers have been trusting Linode for projects
- 18:56both big and small since 2003.
- 18:58Deploy your entire application stack with Linode's one-click app marketplace, or build it all from
- 19:04scratch and manage everything yourself with supported centralized tools like Terraform.
- 19:08Linode offers the best price-to-performance value for all compute instances, including GPUs,
- 19:14as well as block storage, Kubernetes, and their upcoming bare-metal release.
- 19:19Linode makes cloud computing fast, simple, and affordable, allowing you to focus on your
- 19:24projects, not your infrastructure.
- 19:26Visit talkpython.fm/Linode and sign up with your Google account, your GitHub account,
- 19:32or your email address, and you'll get $100 in credit.
- 19:35That's talkpython.fm/Linode, or just click the link in your podcast player's show notes.
- 19:40And thank them for supporting Talk Python.
- 19:42The N plus one problem, I believe, is either there's some tool doing something behind the
- 19:49scenes you don't know, but often it's just a lack of understanding.
- 19:51Oh, that actually is a lazily loaded property, which is going to trigger a query, so I should
- 19:57have put a join, and then I'd be in a better place.
- 20:00That's a programmer pattern thing that you should pay attention to and work with.
- 20:04The one where I don't know how to fix it is more like the serialization thing.
- 20:08Like, what if I want to go through my database and go to 10,000 records and make some changes
- 20:14to them?
- 20:14So often it's do the query, loop over the 10,000 things, make a change, call save.
- 20:20Maybe it's in one giant transaction that you finally push the changes back, but you're
- 20:25pulling all the data back.
- 20:27And one of the things I really like about your, or I'm here, is like this update section here,
- 20:33where you can do set-based operations without pulling the records back.
- 20:37Yeah.
- 20:37So you can do stuff like, so this example here, wait, band.update, band.popularity, 10,000.
- 20:42But then you can also do band.popularity is band.popularity plus 10.
- 20:47And then in the database, it will then just add 10 to all of the numbers.
- 20:50Oh, really?
- 20:51That's awesome.
- 20:52And then it's all just magic around, you know, Python magic methods.
- 20:56It's just, as a library author, it gives you so much power.
- 20:59It's one of the things I love about Python.
- 21:01Yeah.
- 21:01Because when you're building like query languages, like ORMs, I think very few languages can
- 21:06really rival Python with its flexibility.
- 21:08Yeah.
- 21:09So that's really why a lot of this stuff's possible.
- 21:11It's really neat.
- 21:11And I think the ability to push these changes without actually, you're still programming
- 21:17in this, the ORM classes and the models, but you're not actually pushing a whole bunch of
- 21:21them back and forth to make the changes, but to do these set-based operations, like delete
- 21:25them where, or make this update to this value where this is true, and then just push that,
- 21:30make that happen in the best way you can with SQL, right?
- 21:33Yeah, exactly.
- 21:34I think coming back to your point around the M plus one, I think properties are something
- 21:39that can be a little bit evil.
- 21:40And I've really shy away from them in the Piccolo code because it can, you call a property
- 21:46and you think you're getting a value back, but it could be doing any kind of magic.
- 21:49And then once you've defined something as a property, you then can't add arguments to
- 21:55it without breaking your API.
- 21:56So yeah, I think that that's something I've tried to steer away from in general with Piccolo,
- 22:00like properties.
- 22:01Yeah.
- 22:01A lot of hidden stuff happening there, right?
- 22:04Yeah.
- 22:05It's not entirely clear.
- 22:06I think they're super useful, but certainly in something where I thought I was accessing
- 22:10a field of the class, but what I actually did is make a network call.
- 22:13Like that distinction is possibly too big of a bridge to just make that automatic a lot
- 22:18of the times.
- 22:18Yeah.
- 22:19There's no async properties in Python as well.
- 22:22So that's kind of one of the reasons why it doesn't use any async properties.
- 22:25Maybe they'll add it.
- 22:27If they add it, maybe I'll put a comment on the PEP saying, don't do it.
- 22:30Yeah, exactly.
- 22:32Another thing that's interesting here is on all this code, every code you've written
- 22:36is await band.select or await band.delete and so on or update.
- 22:41And then at the end, you say run.
- 22:43This is the explicit part that you were talking about in your API.
- 22:46Like I know here's where it's happening.
- 22:48And it probably makes a lot of sense to do that as well, because on the flip side of it,
- 22:52that's where you have to await it anyway, right?
- 22:54Yeah.
- 22:54So what happens is you build up this query, you just chain methods to it.
- 22:58And then at any point you can print out that object and I'll give you the SQL.
- 23:02And then until you actually await it, there's something under the hood that I don't really
- 23:07publicize.
- 23:07You don't need the dot run.
- 23:09If you await it, it will run as a convenience because people forget.
- 23:12But then it just makes it easier from a documentation perspective to say when it's async, use run.
- 23:17And when it's synchronous, use run sync.
- 23:19Right.
- 23:20And then if you do run sync, then I've got like a bit of magic in there where it tries to
- 23:26create an event loop to run it, or it tries to figure out if there's already an event
- 23:29loop.
- 23:30If there is, run it in there.
- 23:31So you can use Piccolo in an old school WSGI app if you wanted to just synchronously.
- 23:37Yeah.
- 23:38Well, let's dive into that because that's one of the things that really stood out to me.
- 23:41Many frameworks or APIs packages tell you, you're going to either have to go, you know,
- 23:47you're going to take a fork in the road.
- 23:48You're going to go down the async fork and you're going to use the async library like
- 23:51HTTPX or you're going to go on another fork and you're going to use the request library
- 23:55that has no async.
- 23:56And you're going to go down that path and you choose and then you just go.
- 24:00And with Piccolo, you can actually run, I guess the default behavior would be to be async and
- 24:07await.
- 24:07But it has this dot run sync, which will kind of cap where the asynchronous behaves and goes.
- 24:13And it'll just that you could run it in a regular Flask app or Django app or whatever
- 24:18and not worry about it being async at all.
- 24:20Right.
- 24:21Yeah, that's right.
- 24:21And it's actually one of the design challenges with Piccolo is how do you create an API which
- 24:25is synchronous and asynchronous?
- 24:27And there's only really two ways of achieving it is with a method like run or run sync or
- 24:33with context managers.
- 24:34So some of them you'll create either an async context manager or synchronous, and then that
- 24:40will then impact whether the underlying query is synchronous or not.
- 24:43But then it adds a little bit more boilerplate if every time you're in a query, you need
- 24:47a context manager to tell it to be async.
- 24:49So this is kind of the best outcome I could think of was just have dot run or dot run sync.
- 24:55I think this is great, especially since even if you forget the run, it'll still run async,
- 24:59but there's a way to kind of cap it.
- 25:01So something I wanted to talk about, it's driven me crazy ever since async and await were introduced
- 25:07because I don't find it to be true, but I hear it all the time spoken about in the community
- 25:12async and await, they're super neat, but they're like viruses.
- 25:16And in the sense that soon as like one of your functions way, way at the bottom has to do
- 25:21something async, well, then the thing that calls it has to be async and await it.
- 25:24And the things that call that function now all have to await it.
- 25:27And that percolates all the way to the top of your app.
- 25:30And so now you've, by using any async library, you've turned your entire thing into this like
- 25:34async vertical call stack.
- 25:36Your example here shows that that's not, it doesn't have to be that way, right?
- 25:40That's sort of the naive, I'm just going to like write the code without thinking about it.
- 25:43But if you wanted to say, have your data access layer do three things, because it's got to pull
- 25:49some stuff from different places.
- 25:51You want that to be async.
- 25:52It doesn't mean that function has to be async.
- 25:54It could just start its own event loop, do the three things faster than without it,
- 25:57and then return the answers, right?
- 25:59You can kind of cap it at any level that you want.
- 26:01And your run sync is kind of an example of that.
- 26:04Like you can choose to not have it, just turn your entire app async.
- 26:08You can jump between them.
- 26:09So typically if people use an async, then it's like, the argument is, if you need async,
- 26:13your whole app probably should be async because otherwise why use an async?
- 26:18But then you can flip between them quite easily.
- 26:20So if you've got a synchronous app and you want to call some async code,
- 26:23there's asyncio.run.
- 26:24And you can also do stuff like spin up an event loop in a different thread and then send
- 26:29work to that.
- 26:30Yeah, absolutely.
- 26:31So it is quite fluid.
- 26:32You can flip between them quite easily.
- 26:34Yeah, I mean, just one example that comes to mind is what if I wanted to go web scrape
- 26:39every page at a certain domain?
- 26:41So I've got a function that gives me a domain.
- 26:43I give it a domain and then I want it to return or store into the database all the pages, right?
- 26:49That would be perfectly reasonable to have that thing go, okay, well, let's do a,
- 26:53a request, figure out what all the pages are, and then just, you know, recursively sort of grab them
- 26:57all asynchronously.
- 26:58You would get a huge boost on getting every page off a site, even if that function blocked.
- 27:03You know what I mean?
- 27:04Because it itself could just go crazy against the server.
- 27:06Maybe it shouldn't, but it could.
- 27:08Yeah.
- 27:08I'm a huge fan of asyncio.gather as well.
- 27:11So that's a really beautiful API just for saying, do these 50 things now, please.
- 27:15And let me know when you're done.
- 27:17Yeah.
- 27:17And block, right?
- 27:18When you're done, give me a list of answers or errors.
- 27:21Yeah.
- 27:22Also out in the live stream, Chris May.
- 27:23Hey, Chris.
- 27:24It says, I'm so excited to use Piccolo with unsync.
- 27:27I have a workflow that'd be nice to parallelize.
- 27:29And yeah, so I think unsync is another really interesting library that wraps up asyncio plus
- 27:36threading plus multi-processing.
- 27:38But then gives you a nice way to cap it as well because you can go, like, be given a task that
- 27:43comes back from there.
- 27:44If you just ask for the result and it's not done, it'll just block like a regular thing.
- 27:48And it does kind of what you're talking about.
- 27:50It'll have a, it has a background thread with its own event loop and it just pushes all the
- 27:54work over to there.
- 27:55Yeah.
- 27:56So, yeah.
- 27:56That's a cool library.
- 27:58Yeah, it is.
- 27:59At least for the size, right?
- 28:00Like 126 lines and it unifies those three APIs and adds some more stuff.
- 28:05That's pretty big bang for the Python byte.
- 28:08Yeah, that's impressive.
- 28:09I wish Piccolo was that terse.
- 28:11I think thousands, tens of thousands of lines by now.
- 28:14Yeah.
- 28:15Yeah.
- 28:15Yeah.
- 28:16So another one of the new Python 3, 6 onward type of things that's really cool is the type
- 28:22annotations.
- 28:23Yeah.
- 28:23I love type annotations.
- 28:24So part of my day job in the past was using Swift and Swift is almost like hyphens, like
- 28:30brother or sister.
- 28:31It was very heavily inspired by Python.
- 28:33Right.
- 28:34It's like if Python all of a sudden decided it was incredibly strict about typing,
- 28:37and type definitions.
- 28:39Yeah.
- 28:40That would be a lot like Swift, right?
- 28:41Inward compiled.
- 28:42Yeah.
- 28:42It's a combination of type annotations and the tooling to support it.
- 28:45So in Python's case, it's VS Code.
- 28:47And in Swift's case, it's X code.
- 28:48And it just means that when you're refactoring, it just makes it so much more confident about
- 28:53what's going on.
- 28:54Yeah.
- 28:54It provides documentation because previously people were putting in a doc string anyway.
- 28:58So why not put it in your function definition and then you can introspect it.
- 29:02And my Pi is incredibly powerful.
- 29:04I honestly don't think I could have built Piccler without type annotations because it makes your
- 29:09code so much more maintainable.
- 29:10The click to go to VS Code as well is just a beautiful usability improvement.
- 29:16And then kind of one of the hidden benefits is it makes tab completion so good.
- 29:20So a lot of Python auto completions, they use a library called Jedi under the hood.
- 29:26So when I was building Piccolo, I had to look at the source code to try and figure out how
- 29:30it does its magic.
- 29:31And if you give a type annotation to something like this is what this function returns, it's
- 29:35a really strong indicator to Jedi that this is what's going to get returned.
- 29:40I don't need to do any magic anymore.
- 29:41Yeah.
- 29:41I would say that half the time, that's why I do it is to make the editor better.
- 29:45So both VS Code and PyCharm, like take a good look at what the type annotations are.
- 29:50Yeah.
- 29:50Right.
- 29:51And you just say, oh, you're trying to pass a string and that's really supposed to be
- 29:54an integer.
- 29:54But then also, like you say, tab completion or autocomplete all over the place is fantastic.
- 29:59Yeah.
- 29:59I think there's a distinction as well, where I think if you're building an application,
- 30:02let's say you're building a Django app or a Flask app, you don't need to care quite as
- 30:05much.
- 30:06Like I personally would still add type annotations, but for libraries, I think it's just absolutely
- 30:10essential.
- 30:11Like I don't think any new Python library should be written without it because you kind of,
- 30:15shortchanging your users in a way.
- 30:18Is there?
- 30:18I totally agree.
- 30:19Yeah.
- 30:20Yeah.
- 30:20In the Flask app, for example, that you've mentioned, you know, I would say on the boundaries,
- 30:25right?
- 30:25Like here's a data access layer, like put type annotations on those little bits there.
- 30:29And then the rest of the app, usually the editors and the tools will just pick it up
- 30:33and continue to tell you what you're working with.
- 30:35Yeah.
- 30:36But if you're doing a library, right?
- 30:37You want every function or every class to be kind of standalone and know everything it
- 30:42can.
- 30:42Yeah, definitely.
- 30:43And one more thing about type annotations is it's probably the greatest source of interview
- 30:47questions ever made because you can ask people in an interview, what's the difference between
- 30:51like a sequence and an iterable?
- 30:52And like when you use type annotations, you really start to think about what's going on.
- 30:57And it's a great learning experience too.
- 30:59Yeah.
- 30:59I want to pass a generator here, but it takes a list of things and it says that won't work.
- 31:03And maybe you just need to relax your type annotations to an iterator.
- 31:07Yeah.
- 31:08A thing.
- 31:09A quick question from Roller out there in the live stream.
- 31:11Hey, Roller.
- 31:12Can I just pick a little and play some Mongo engine or is it just for a relational stuff?
- 31:16Yeah.
- 31:17It's just relational and it's, you can use SQLite locally, but it's mostly Postgres.
- 31:21It was really built to take advantage of Postgres because Postgres is, it's like the fastest
- 31:27growing SQL database in the world, which is remarkable to think it's how old it is.
- 31:32And I think I'll, you know, I think adding other SQL databases would be quite easy, but
- 31:37adding something like Mongo would be a bit trickier.
- 31:40I wouldn't say it was impossible, but a bit more work.
- 31:42Yeah.
- 31:42I would think so.
- 31:43Yeah.
- 31:43It's not, certainly not impossible, but like joins and stuff would get tricky.
- 31:47What about SQL injection?
- 31:49I mean, many of us have heard about the little Bobby tables, XKCD, which is delightful, you
- 31:55know, you know, sort of schadenfreude sort of way.
- 31:58We all kind of want to relish in somebody else suffering this, but I find that this is actually
- 32:04one of the really nice things about ORMs most of the time is that they scrape off the ability
- 32:10to do SQL injection because you're not building the SQL.
- 32:13Yeah, definitely.
- 32:14So your database adapter.
- 32:16So something like asyncpg or psycopg in the synchronous world, what you want to do is you
- 32:21want to pass it the query string with placeholders for any user submitted values.
- 32:25And then you submit the values that separately, like in a list.
- 32:29Right.
- 32:29Like a parameterized query, basically.
- 32:32Yeah.
- 32:32And as long as you do that, you're safe.
- 32:34But then for a library, when people are programmatically creating very complex SQL queries, and then you
- 32:40need to try and make sure that you've got the right values that match the right placeholders
- 32:45to pass to the adapter.
- 32:46It is quite challenging.
- 32:48There's some like recursive code where it has to.
- 32:51So we use something called query strings internally within Piccolo.
- 32:53So it never concatenates strings for SQL statements.
- 32:57It just, it uses query strings and then it compiles them before sending it to the database adapter.
- 33:02And it basically looks through all of the sub query strings it might have if it's a really
- 33:06complex query.
- 33:07And then it kind of passes it to the adapter.
- 33:10But yeah, it's just one of the complex things about building ORMs for sure.
- 33:14And also one of the most dangerous to get wrong.
- 33:16Yeah.
- 33:16Yeah, it absolutely is.
- 33:17There's untold number of bad things that can happen with SQL injection.
- 33:22And it's so easy.
- 33:23All you have to do is put a single little tick to comment out stuff, semicolon to finish that
- 33:30statement.
- 33:30And then you can run arbitrary code.
- 33:32And a lot of times you can even, some database engines will let you run sub process type things.
- 33:38Which is even worse.
- 33:39But yeah, it's not good.
- 33:41So you definitely want to avoid it.
- 33:42Yeah, it's a good argument for using ORMs and query builders because it'll make it less
- 33:46likely.
- 33:46I think for sure.
- 33:47You know, another thing that I wanted to touch on a little bit here is the actual filtering
- 33:53or projection statement type bits.
- 33:56So I mentioned using MongoEngine before, which I'm a big fan of.
- 33:59And it's basically a Mongo equivalent of the Django ORM.
- 34:03So in that regard, they're real similar.
- 34:05And you do things like, if I wanted to say where the band popularity, or let's say the
- 34:10band name is Pythonistas, right?
- 34:12You would just say name equals Pythonistas as part of the filter.
- 34:16And there's two things that are crummy about that.
- 34:19One is you get no autocomplete that there's a column called name because it doesn't really
- 34:24know what class, even though you started out like band.objects.
- 34:27It doesn't, in the filter part, it no longer knows that the name came from the band, right?
- 34:31That's not part of the language.
- 34:33And then the other one is you're doing an equals or you've got like weird operators like in
- 34:38the name, like name underscore underscore GT for greater than and stuff like that.
- 34:43Whereas yours, you just write what you would put into an if statement or a loop or something
- 34:48like that.
- 34:49So you would say like band.popularity less than a thousand.
- 34:51That's the thing you send in there.
- 34:53Yeah, that's right.
- 34:53So I've been caught up, tripped up so many times in the past with Django where I've had
- 34:58something like name double underscore something else.
- 35:01And then it can't really understand that's wrong, like a linter or when you, while you're
- 35:07coding, it only really knows at a runtime and then you've got a 500 error.
- 35:09So the idea here is a linter would be able to pick up these problems.
- 35:14Exactly.
- 35:14Because so much of the pieces are, they're just using star, star, kwrgs and then they
- 35:19figure out how to generate a query out of like looking for special keywords in the key names
- 35:25and then turning those to columns.
- 35:27Also the refactoring thing, right?
- 35:28The linters.
- 35:29And if I want to do a refactoring to rename popularity, it's not going to check popularity
- 35:34underscore GT as a keyword argument.
- 35:37It has no idea those are related.
- 35:38Yeah.
- 35:38Yeah, definitely.
- 35:40So the way it's implemented this, the double equals and all these operators is the amazing
- 35:45things about Python is how you can just overload like fundamental things about the language.
- 35:50So you can overload what addition means.
- 35:52And when someone first tells you that, it sounds like the most mental thing in the world.
- 35:55Cause why would you want one plus one to equal five?
- 35:57But then it turns out when you're building an ORM, it's golden.
- 36:01And this is one reason why I find Python just so compelling over and over again is because
- 36:06as a library author, you can do this stuff that you can get closer to more of like a DSL than
- 36:12like a normal programming language.
- 36:14Specific language.
- 36:14Yeah, absolutely.
- 36:15Yeah.
- 36:16So is this a be done with descriptors or what's the magic?
- 36:19For the less than there's like done like double underscore LT and you can override that.
- 36:25And then what happens is when you call that method, it returns a where object.
- 36:28And then you can also, you could do in brackets, band dot popularity, less than a thousand and
- 36:34then double and sign as well.
- 36:35Band dot popularity greater than 500.
- 36:38So you can combine them with and and all statements.
- 36:41So the where statements in Piccolo can get like really powerful.
- 36:44So you just have to teach the where clauses how to and and then structure it in a way that
- 36:49Python will let it kind of go through, right?
- 36:51Yeah.
- 36:52Or you can do dot where, put some stuff and then another dot where statement.
- 36:55And if you've got multiple where statements, it becomes like it's an and.
- 36:58Yeah.
- 36:58But yeah, it's just, it's just all Python magic, which is one of the reasons I love Python.
- 37:02Yeah.
- 37:03Speaking of overridden things, the thing that I think is the most insane, but I'm starting
- 37:08to love, but took a while to get used to is the way the path object works for defining
- 37:13paths.
- 37:14Yeah.
- 37:14So, you know, the forward slash often means like drive separation on the POSIX systems.
- 37:21And, you know, it's close enough.
- 37:22You could actually put forward slash in your strings and Python on Windows and it'll
- 37:25still like, okay, fine.
- 37:26Backslash is what you meant.
- 37:27So they overrode the divide operator in code to allow you to concatenate strings and paths
- 37:34together.
- 37:35And that's just crazy.
- 37:36Yeah.
- 37:37The first time I saw it, I was very confused, but when you understand that it's okay, but
- 37:40yeah, totally.
- 37:41Yeah.
- 37:42I'm, I've gotten okay with it as well.
- 37:45And I start to use it and I really like it now, but I was like, I don't know if I can
- 37:47get behind.
- 37:48This is, this is a bridge too far.
- 37:49This is, that's division.
- 37:51What are we doing here?
- 37:51No, it's cool.
- 37:52It's cool.
- 37:53Talking of magic to Piccolo, like all around, it uses meta classes a lot.
- 37:57And there's something that got added in, I don't know if it's Python 3.7 or 3.6, but they
- 38:02actually changed meta classes slightly.
- 38:04So there's now like a Dunder magic method called init subclass.
- 38:07And Piccolo uses this a lot.
- 38:08And it's, it's actually an amazing hidden feature of Python where you can now add keyword
- 38:14arguments to a class definition.
- 38:15So if you had class foo, open brackets, inherits from bar, comma, and then you can start adding
- 38:21keyword arguments to the class to customize its creation.
- 38:24And that's kind of like a new layer of magic that's been added to Python recently.
- 38:29And Piccolo uses it extensively, but I don't see many other libraries using it yet because
- 38:33it's probably not so well known, but yeah, just kind of spread that bit of magic.
- 38:37So hopefully people can use it too.
- 38:39Nice.
- 38:39Yeah.
- 38:39That sounds awesome.
- 38:40I can certainly see how I'm like trying to create the classes, like the band class or
- 38:44whatever you say that it's going to be, would definitely use that.
- 38:47So one of the things that you say is awesome about Piccolo is the batteries included.
- 38:52Yeah.
- 38:52So let's talk about some of the batteries.
- 38:54So yeah.
- 38:56So the main battery by far is the admin, because when I started it, I was working for a design
- 39:00agency and admins are incredibly important for design agencies because you want to put
- 39:05something in front of a customer that they like the look of and they're comfortable using.
- 39:08So this is a huge part of the effort that's gone into Piccolo.
- 39:12And so this is a, it's sister project called Piccolo admin.
- 39:15And what happens is it's, it's an ASGI app.
- 39:17So I can maybe go into more detail about ASCII later on, but all you do is you give it a
- 39:21list of Piccolo tables and then it uses Pydantic.
- 39:25So Pydantic is a serialization library and it basically creates a Pydantic model from the
- 39:30Piccolo table.
- 39:31And then Pydantic models have this really useful method where it's JSON schema and it creates
- 39:38a JSON schema for the Pydantic model.
- 39:39Right.
- 39:40Because the Pydantic classes know this field is an int, this one is an optional date time
- 39:45and so on.
- 39:45Yeah.
- 39:46So Piccolo has really good Pydantic support, but it's in a sister repo called Piccolo API.
- 39:50And then that, so that creates the Pydantic model and it also has something called Piccolo
- 39:55CRUD.
- 39:56So you give it a Piccolo table and it creates another ASGI app, which has got all of the CRUD
- 40:02operations for your database.
- 40:03So you can programmatically create this huge API just by giving it a few tables.
- 40:07And then the front end is written in Vue.js and it's completely decoupled from the back
- 40:12end.
- 40:12It's just all by API.
- 40:13So yeah, I'm a huge fan of Vue.js because it's, it's very natural for Python developers
- 40:18who are used to the template syntax in Django and Flask.
- 40:21If you looked at the Vue templates, you'd be like, this looks very familiar.
- 40:24So I'm a big fan of that.
- 40:26Yeah, it's nice.
- 40:26Yeah.
- 40:27I think it's super close to Chameleon because of the attribute driven behavior as well.
- 40:31Yeah.
- 40:31Yeah.
- 40:32But then like to make a working admin requires so much work because you've also got the security
- 40:36side.
- 40:36So Piccolo API has a bunch of really useful ASCII middleware and it has like session authentication,
- 40:42CSRF protection and rate limiting as well because you don't want people to spam the login.
- 40:47So like just to get a fairly simple admin is in, it's like a iceberg to do it properly.
- 40:54So yeah, a lot of efforts gone into the admin, but I'm really proud of it.
- 40:58And like, this is really what excites me more than anything about the future because as
- 41:02we add support for post GIS and stuff like that, being able to create really interesting
- 41:06widgets around data.
- 41:08So, you know, how can I design a rectangle field for post GIS or a location field or.
- 41:14I could see some really cool stuff that are sort of template extensions, you know, like
- 41:19let's just pick Jinja, for example.
- 41:20Like if you had one of these objects, you could pass it over and it knew, for example, here's
- 41:26a daytime, you could just say, make a calendar picker here.
- 41:29And it just, you know, as long as you have the JavaScript included and instead of just
- 41:33putting the text, it gives you like a nice little Ajax-y widget or these, this list goes
- 41:39on a map, drop in the map widget and off it goes.
- 41:42Yeah.
- 41:42So basically Piccolo admin, it's just, it's just turns Pydantic models into UI and it's
- 41:48actually quite, it's actually quite interesting.
- 41:50And I'd love to get it to the point where for a business app, you just use Piccolo admin,
- 41:53you don't even have to build UI.
- 41:55You just say, here are my tables.
- 41:56Yeah.
- 41:57And then, yeah.
- 41:58Well, the truth is so often, like there's a lot of these little internal apps that people
- 42:02build that are just like forms over data.
- 42:04I just need to see the details, click on one, edit it, create a new one and delete one.
- 42:08And like, that's the app I need for this table.
- 42:10Could you build that?
- 42:11Right?
- 42:12Yeah, for sure.
- 42:13It is a lot of it.
- 42:14Cause I think with the approach I talked to Piccolo is you have a lot of Python libraries
- 42:18and they kind of start from the outside in.
- 42:20So they start from the URL layer and then the views and like middleware.
- 42:24And then over time, they then add the data layer and the security.
- 42:27But then with Piccolo, it's kind of from the inside out.
- 42:30Like I started from the data layer and then have the admin and some middleware.
- 42:34So it's quite a nice companion to have a ASGI app.
- 42:36So just kind of pick the framework you like and then Piccolo kind of gives you the data
- 42:40layer and the admin.
- 42:41That's kind of.
- 42:41Yeah.
- 42:42Yeah.
- 42:42Yeah.
- 42:43Well, let's talk about the ASCII stuff a little bit.
- 42:44Cause you did mention that there's some interesting support for those things.
- 42:49And it's got like, to some degree, native FastAPI, Flask, and even Blacksheep, which is
- 42:56an interesting one, support for those frameworks, right?
- 42:58Yeah.
- 42:59So I'm a huge fan of ASCII because I was a Django Channels power user and Andrew Godwin
- 43:04created ASCII out of Django Channels.
- 43:06It's really like a beautiful thing.
- 43:09If you look, so Starlette was the one that really built the foundation.
- 43:12So this is an async library, but also kind of like a framework.
- 43:17Like you can build an app with Starlette or you can use it to build up frameworks.
- 43:20What's amazing about ASCII is like every component in an ASCII framework is ASCII.
- 43:25So ASCII is basically the spec where it's a function that accepts a scope, a send and
- 43:30a receive.
- 43:31And then if you look at the internals of Starlet, everything's ASCII, like the middleware's ASCII,
- 43:35the endpoints are ASCII, like the whole thing.
- 43:37And it's like super composable.
- 43:39So you can say, I've got an ASGI app and you can mount other ASGI apps within it.
- 43:44So this is what I love about ASCII as a spec.
- 43:48So you can say, take a FastAPI app or a Starlette app and include Piccolo admin.
- 43:52Same with Blacksheep.
- 43:54Right. You could say slash catalog is actually handled by this other app written in FastAPI
- 44:00where the main thing is written in Flask or something like that.
- 44:02And you just kind of click them together in that cool way.
- 44:04Maybe Core would have to be the one, but still.
- 44:06Yeah. And I really love that.
- 44:07It's quite exciting that you could kind of build an app with multiple frameworks and be like,
- 44:11well, this part of the app will be better served by FastAPI.
- 44:14But this bit, I just need Starlette or I want Blacksheep or whichever ASCII framework people
- 44:19can dream up.
- 44:20So I think it's really exciting for the Python community, the ASCII spec.
- 44:24Yeah. And we did talk a little bit about the challenges and the cascading effect of async and await.
- 44:30But if you're already running a framework that has async view methods, there's just nothing to it, right?
- 44:35You just write your code and you just await the bits you got to await in the view method.
- 44:39And it just like the server and the framework handled the vent loops and all that business.
- 44:44Yeah. I think as well, what kind of happened is asyncio came out and it doesn't directly affect speed.
- 44:49It's more about throughput, but it's like the Python community took it as a challenge to build faster frameworks.
- 44:55And so there's a lot of them have really quite fast internals and they do feel quite cutting edge.
- 45:01Yeah. Like uvloop and stuff like that.
- 45:03They're like, how can we do this, but have the minimal overhead of adding this?
- 45:07I mean, people do talk about, okay, async and await won't make your code go faster.
- 45:10Well, it won't make CPU code go faster.
- 45:12But so often what we're doing, especially in web apps is waiting.
- 45:15I want to wait on a database and then wait on the network.
- 45:18And then I want to wait on the database again.
- 45:19And then I'm going to wait on an API and then I'm going to send back three lines of JSON, right?
- 45:24Like 99.9% of that is just waiting on something else.
- 45:27And when you're using async and await, like you can just do other things while you're doing that 99% of waiting.
- 45:32Yeah. So usually if you do a database query, it takes a few milliseconds.
- 45:36But then if you use the time it module on Python and you see how long basic operations take, they're more like microseconds.
- 45:42So there's like orders of magnitude difference in how long a database query takes to basic Python stuff.
- 45:47But this is why having stuff like uvloops are really important because if you had a really slow event loop, it kind of wouldn't make much difference.
- 45:53But because the event loops fast as well.
- 45:55And like a lot of the projects I did in the past, the throughput is really important because some apps, you won't have a lot of traffic.
- 46:01And then all of a sudden you'll get a thousand users.
- 46:04So I was doing like live events and you'd get a thousand people at once.
- 46:08And in that situation, throughput is incredibly important.
- 46:11Right. I mean, look at how the whole healthcare rollout in 2008 went.
- 46:15Right. I just can't help but think there must have been more awaits available to those, that those frameworks and those web apps.
- 46:22Because it just kept crashing and stuff was timing out.
- 46:24And I'm sure it's just like, well, we're just going to wait on this other slow government API.
- 46:30And we're going to do it for a lot of people.
- 46:31And it's just going to overwhelm it.
- 46:32Right. And it's just crazy.
- 46:34It just feels natural because like in the web server world, you had Apache and a lot of people moved to Nginx.
- 46:39That's very similar.
- 46:39It's like event loop driven.
- 46:41And we've kind of seen how beneficial Nginx was.
- 46:43So it just makes perfect sense to build your backend in the same way.
- 46:47Yeah, absolutely. Absolutely.
- 46:48All right. A couple more things we got some time to talk about.
- 46:51So over here, you've got a Django comparison page, which I guess also could be slightly a Mongo engine comparison page.
- 46:59Because like I said, they're basically the same thing without the nesting.
- 47:01So if somebody is familiar with Django and they're like, I would like to consider using this for my framework or for part of my code or whatever, just like want to use it.
- 47:10But they already know how to do stuff in Django.
- 47:12You have like, well, here's how you would create an object and save it in the different frameworks.
- 47:16Here's how you would update an object and make changes and so on.
- 47:20And you can just go through one at a time and just sort of compare the different pieces, right?
- 47:24Yeah.
- 47:25So it's quite heavily inspired by Django.
- 47:27But then I think Django in its ORM, it's more Pythonic.
- 47:30So rather than using where it uses filter.
- 47:33But then with Piccolo, it's meant to be very, very close to SQL.
- 47:36Because the theory is, if you know SQL, it'll be super easy to learn.
- 47:40And when you do need to drop down to a SQL query, there's no like mismatch.
- 47:44You're just like, well, I'm always sort of working in a SQL mindset.
- 47:47But there are a lot of similarities with Django still.
- 47:50I think people could pick it up quite quickly.
- 47:52Yeah, I agree.
- 47:53Like instead of object that values list, you have a select projection.
- 47:57Or instead of filter, you have a where.
- 47:59But it's honestly not a huge mental jump to make.
- 48:02Yeah.
- 48:02And Piccolo also takes like huge inspiration from Django migrations,
- 48:06which I think is kind of like the gold standard of migrations in any language.
- 48:10So a lot of effort's gone into that.
- 48:13Yeah.
- 48:13That's another one of the batteries that you were kind of touching on, right?
- 48:16Is the migrations bit.
- 48:18Yeah.
- 48:18But it's migrations are incredibly hard to do right.
- 48:21I think the Django way is.
- 48:23I can only imagine.
- 48:24I don't even want to try to imagine writing that because it seems really hard.
- 48:28Yeah.
- 48:28So the way Django does it is it looks at your tables and then it creates a migration file.
- 48:33It then adds up the migration files to build a picture of what the schema looks like.
- 48:38And then that's how it then creates subsequent migrations by doing a diff.
- 48:41Right.
- 48:42That's what Piccolo does.
- 48:42I've got to go from this level to that level.
- 48:44So here's the five migrations to basically use in order.
- 48:47Yeah.
- 48:48And then you've got to do code generation as well.
- 48:50So like with Piccolo, it has to actually create a Python file.
- 48:53And that's harder than what it seems actually writing a Python file.
- 48:56But if you look at the Piccolo migrations, they're actually really, they look like quite beautiful
- 49:00Python code.
- 49:01And there's a little trick I use internally.
- 49:03I use like the black formatter on it before I write out the auto-generated code.
- 49:07Oh, that's cool.
- 49:08So your generated code is pep8 and all the goodness.
- 49:11Yeah.
- 49:11So if you look at it, you're like, oh, that's quite nice.
- 49:14That's clever, actually.
- 49:16Yeah, that's really clever.
- 49:17Yeah, it's cool.
- 49:18Black.
- 49:18Yeah.
- 49:19Yeah.
- 49:19I feel like other frameworks, like for example, the cookie cutter stuff, you know, you're just
- 49:24generating code files like crazy.
- 49:26I feel like you could apply that same technique as after we inject all the user entered values,
- 49:31let's just do a quick format on them and then drop them.
- 49:33Yeah.
- 49:33It makes sense because otherwise you'll run your linters on your project and they'll fail because
- 49:38your migrations aren't correctly formatted.
- 49:40Yeah.
- 49:41Yeah.
- 49:41Yeah.
- 49:42No, that's cool.
- 49:42A quick question from Teddy out in the live stream.
- 49:45It says, I don't use ORMs much in my day-to-day.
- 49:48What are good use cases besides web apps for them and where does Piccolo perform better?
- 49:52So two questions.
- 49:53So I think that data science is obviously a big bit.
- 49:57So another reason for building Piccolo is data science is so much on the ascendancy in the
- 50:02Python world and people are just, you know, still dealing with databases on a day-to-day
- 50:06basis.
- 50:06So you can use it in a script if you like, but there's maybe a couple of examples in the docs
- 50:11where, you know, you might be scraping some data from a website and then you just need
- 50:15to stick into Postgres.
- 50:16So that would be another good example for using Piccolo.
- 50:19Yeah.
- 50:19That is a good example.
- 50:20And then where it performs better.
- 50:22It's really where you need the async or you might still want a web app component, even if
- 50:28it's just like a data science script, you still might want an admin screen to view that data.
- 50:32Yeah.
- 50:32I think that performs better.
- 50:34You could maybe break into two pieces.
- 50:36Where does Piccolo perform better?
- 50:37And I think the async stuff is really important there, like you say.
- 50:40And then where does an ORM perform better?
- 50:43You know, when you talk about performance and getting stuff done, like sometimes it's how
- 50:47fast is the code run, but sometimes it's how fast do I get the final thing built.
- 50:51Right.
- 50:52And I think ORMs, even if they're not always the most efficient way, sometimes they're really
- 50:56efficient, but not always.
- 50:57But, you know, they could help you safely get to the other side, especially if you don't
- 51:02know SQL super well.
- 51:03Yeah.
- 51:03They hold your hand a little bit.
- 51:04Yeah.
- 51:05I created this while working at a design agency and speed's really important at a design
- 51:09agency, not really execution speed in terms of SQL queries, but in terms of scaffolding
- 51:13an app and being productive.
- 51:15So there's, so Piccolo has something called Piccolo asking you, and then to use that command,
- 51:19it'll basically scaffold you a web app.
- 51:21So it supports FastAPI, Starlet, Blacksheep.
- 51:24Yeah, that's really cool.
- 51:24So yeah, it's, you just kind of create your starter code and your starter structure from that,
- 51:28right?
- 51:29Yeah.
- 51:29So a bit like with Django where you, you know, you create a project on the command line,
- 51:33but with Piccolo, you get an option of different ASCII frameworks.
- 51:36And over time, I'd like to add, you know, way more because there's, there's, there's
- 51:40many more exciting ASCII frameworks like Quart, Sanic, Django itself is actually an ASCII
- 51:45app, so it could support Django.
- 51:46Yeah.
- 51:47Django has come along there.
- 51:48So if people were out there listening and their favorite framework wasn't listed in one
- 51:54of those three or wasn't one of those three, PRs are accepted, I guess, and they could
- 51:59integrate, you know, their, their favorite Sanic or whatever they're after.
- 52:03Yeah, definitely.
- 52:03Like any feedback's really appreciated.
- 52:05So like the community has definitely helped me a lot with Piccolo, just, just as much as
- 52:09trying it and giving feedback or, you know, obviously pull requests are also like really
- 52:13valued.
- 52:13Even if you just want to raise an issue to say, well, how did you do this?
- 52:16Like, you know, that's still welcome.
- 52:19Yeah.
- 52:19Awesome.
- 52:20Well, well, maybe that's a good place to talk about where things are going in the future
- 52:23and kind of wrap up our conversation a bit.
- 52:26Yeah.
- 52:26So I feel like I'll never be bored with Piccolo because Postgres is continuously developing
- 52:31and adding new features.
- 52:32And I almost feel like Postgres is almost like an operating system in a way.
- 52:36Like the amount it does is kind of insane.
- 52:38So it even has like a PubSub built in.
- 52:41You can do that.
- 52:41Listen, notify.
- 52:42Oh, wow.
- 52:43I'd like to do post GIS support.
- 52:44Timescale DBs are really up and coming extension as well for Postgres for time series data.
- 52:49But then a lot of the stuff I'm excited about is like on the admin side.
- 52:54So as I mentioned before, Piccolo admin effectively turns Pydantic models into UI.
- 53:00So the next thing I want to add is you can basically give it arbitrary Pydantic models and it will
- 53:06render them as forms in the admin.
- 53:08So if you have, for example, you want to send an email, you'll just create send email model,
- 53:13give it to Piccolo admin and it'll generate a form.
- 53:15That's stuff I'm really excited about as well, just to increase the utility of Piccolo admin.
- 53:19So a backend developer could build a functional app for a business without actually writing any code.
- 53:24That's kind of the dream is to build like a really, really great admin.
- 53:27Yeah.
- 53:28Yeah.
- 53:28These self-serve, like once you create the app and hand it off,
- 53:32how far can people go before they have to hire your design agency again or something like that?
- 53:38The more that they can just run with it, the better, I suspect.
- 53:40Yeah.
- 53:41It's such a huge benefit from Django, like having that admin.
- 53:44So I just want to kind of see what I can do with the latest technologies to build a really great one.
- 53:49Yeah.
- 53:49What's the story with Django and Piccolo?
- 53:52Is there a reasonable way to click them together or is it really not so much so far?
- 53:57I think you can.
- 53:58I haven't really tried it much, but it's very configurable, Piccolo.
- 54:01So, you know, and none of the names, they deliberately don't clash with Django.
- 54:04So Django has a settings.py, Piccolo has a piccolo underscore conf.py.
- 54:09And then Django has a migrations folder, but Piccolo has piccolo underscore migrations.
- 54:14So there's no clash there.
- 54:16So in theory, they would work together.
- 54:17There's no like compatibility layer between the admins.
- 54:20So you'd have like two separate admins or, but I'd like to add support for Django as it is an ASGI app
- 54:25and it's the originator of the ASGI standard.
- 54:27And I still think Django is one of the great kind of masterpieces in the framework world.
- 54:32It's lasted so long and it's still such a rock solid choice.
- 54:35I would like to see what I can do there.
- 54:38Yeah.
- 54:38The closer those could be, I think, right?
- 54:41Like this having the genesis and so many similar ideas to Django,
- 54:44it seems like they should be somehow working together, which is great.
- 54:47Yeah, that'd be cool.
- 54:48All right.
- 54:48Well, Dan, I think that might be about it for time that we got.
- 54:51Let me ask you the final two questions that I always ask.
- 54:55If you're going to write some code, you can work on Piccolo.
- 54:56What editor to use?
- 54:58I think I caught a hint of it earlier, but go ahead.
- 55:00Yeah.
- 55:01VS Code all the way.
- 55:02I was a huge Sublime Text and TextMate user.
- 55:04And I was like, I'll try out this VS Code, see what all the hype's about.
- 55:07And after five minutes, I was never going back.
- 55:10I just think it's such a great gift to the world from Microsoft.
- 55:13It just gets better and better as well.
- 55:16Yeah.
- 55:16Love VS Code.
- 55:17Right on.
- 55:17And then notable PyPI package you want to give a shout out to?
- 55:21So I'm going to cheat and pick two.
- 55:22So Pydantic, because I think it's such a nice serialization library.
- 55:26And I think it could almost be in the standard library.
- 55:28It feels so Pyphonic and natural.
- 55:30And then Starlet, because I think it's just a beautiful foundation for the ASCII world.
- 55:37And I'd really encourage people to look at the code to see the power of ASCII, how it
- 55:41is this like turtles all the way down.
- 55:43Everything's ASCII.
- 55:44It is quite interesting.
- 55:45So those would be my two shout outs.
- 55:48Yeah.
- 55:48Very cool.
- 55:48You know, FastAPI is so popular now, but FastAPI is kind of like an opinionated view on top of
- 55:53Starlette to a large degree.
- 55:55Yeah.
- 55:55FastAPI is great as well.
- 55:57Yeah.
- 55:57Well, I mean, it takes the two things you said, Pydantic and Starlet, and puts them together
- 56:01in like a nice way, which I think is pretty neat.
- 56:03Yeah.
- 56:03It's got great taste.
- 56:05Yeah, for sure.
- 56:06I'd just like to say one thing really quick, and just thanks to everyone who's contributed
- 56:09to Piccolo, because there's been people who've been contributing for several years by this
- 56:13point and have put a lot of work in.
- 56:15So yeah, just a shout out to anyone in the Piccolo community.
- 56:17Yeah.
- 56:18And, you know, Final Call to Action, people are interested in using this.
- 56:22It's good to go.
- 56:23It's ready for production, web apps, and all that kind of stuff.
- 56:26I didn't really want to promote it before it was ready.
- 56:28And I use it in production.
- 56:29I have done for years.
- 56:29And I am quite conservative about pushing stuff out there, unless I think it's, you know, solid.
- 56:34It's got 100 unit tests.
- 56:35You know, it's solid.
- 56:37I'm not saying there's not some edge case I haven't discovered yet in some version of,
- 56:42you know, Postgres or something.
- 56:43But I use it in production every single day.
- 56:45Well, congrats on building a really cool Pythonic ORM.
- 56:49I really like the way that you put things together.
- 56:51And yeah, it looks great.
- 56:53It's got a lot of nice modern Python features.
- 56:56And people should definitely check it out.
- 56:57Cool.
- 56:58Yeah.
- 56:58Thanks a lot, Michael.
- 56:59Yeah.
- 56:59You bet.
- 56:59See you later.
- 57:00Yes.
- 57:00Bye.
- 57:02This has been another episode of Talk Python To Me.
- 57:04Our guest on this episode was Daniel Townsend.
- 57:07And it's been brought to you by Linode and us over at Talk Python Training.
- 57:10And the transcripts are brought to you by Assembly AI.
- 57:13Simplify your infrastructure and cut your cloud bills in half with Linode's Linux virtual machines.
- 57:18Develop, deploy, and scale your modern applications faster and easier.
- 57:22Visit talkpython.fm/Linode.
- 57:24And click the create free account button to get started.
- 57:28Transcripts for this and all of our episodes are brought to you by Assembly AI.
- 57:31Do you need a great automatic speech-to-text API?
- 57:34Get human-level accuracy in just a few lines of code.
- 57:36Visit talkpython.fm/assembly AI.
- 57:39Want to level up your Python?
- 57:41We have one of the largest catalogs of Python video courses over at Talk Python.
- 57:45Our content ranges from true beginners to deeply advanced topics like memory and async.
- 57:50And best of all, there's not a subscription in sight.
- 57:53Check it out for yourself at training.talkpython.fm.
- 57:56Be sure to subscribe to the show.
- 57:58Open your favorite podcast app and search for Python.
- 58:01We should be right at the top.
- 58:02You can also find the iTunes feed at /itunes, the Google Play feed at /play,
- 58:07and the direct RSS feed at /rss on talkpython.fm.
- 58:11We're live streaming most of our recordings these days.
- 58:15If you want to be part of the show and have your comments featured on the air,
- 58:18be sure to subscribe to our YouTube channel at talkpython.fm/youtube.
- 58:23This is your host, Michael Kennedy.
- 58:25Thanks so much for listening.
- 58:26I really appreciate it.
- 58:27Now get out there and write some Python code.
- 58:29Thank you.
- 58:29Bye.
- 58:30Bye.
- 58:31Bye.
- 58:32Bye.
- 58:33Bye.
- 58:34Bye.
- 58:34Bye.
- 58:35Bye.
- 58:36Bye.
- 58:36Bye.
- 58:36Bye.
- 58:36Bye.
- 58:37Bye.
- 58:37Bye.
- 58:38Bye.
- 58:38Bye.
- 58:38Bye.
- 58:38Bye.
- 58:39Bye.
- 58:39Bye.
- 58:39Bye.
- 58:40Bye.
- 58:40Bye.
- 58:41Bye.
- 58:42Bye.
- 58:42Bye.
- 58:42Bye.
- 58:43Bye.
- 58:44Bye.
- 58:44Bye.
- 58:45Bye.
- 58:46Bye.
- 58:46Bye.
- 58:47Thank you.
- 58:49Thank you.