Latest / Talk Python To Me / #87: PonyORM: The most Pythonic ORM yet?
Transcript
- 0:00If you could have any API you wanted to access data from Python, what would it look like?
- 0:04What would make it Pythonic? This week, you'll hear about Pony ORM. Pony is a Python ORM with
- 0:11beautiful query syntax that lets you write your database queries using Python generators and
- 0:16Lambdas. Join me in a conversation with one of Pony ORM's founders, Alexei Milošković.
- 0:21This is Talk Python To Me, episode 87, recorded November 3rd, 2016.
- 0:26Welcome to Talk Python To Me, a weekly podcast on Python, the language, the libraries, the
- 0:56ecosystem, and the personalities. This is your host, Michael Kennedy. Follow me on Twitter
- 1:00where I'm @mkennedy. Keep up with the show and listen to past episodes at talkpython.fm
- 1:05and follow the show on Twitter via at Talk Python.
- 1:08This episode has been sponsored by Rollbar and GoCD. Thank them both for supporting the podcast
- 1:15by checking out what they're offering during their segments.
- 1:18Alexei, welcome to Talk Python.
- 1:20Hi, Michael. Thank you.
- 1:21Yeah, it's great to have you here. Thanks for coming. I'm super excited to talk about your
- 1:26ORM and that you built this, what I'm saying might be the most Pythonic ORM yet, which because
- 1:33of the really cool query syntax that you use and a couple of other highly Pythonic techniques
- 1:39and I'm very impressed with it. But before we get into the details of Pony ORM, let's talk
- 1:44about your story. How'd you get into programming in Python?
- 1:46Well, I was always interested in programming, actually. When I was a teenager, I had a Sinclair
- 1:51ZX Spectrum computer, played computer games and started to program BASIC. And actually,
- 1:58it was the reason for me to get a master's degree in computer science later. I was programming
- 2:04while studying in the university, but what I really liked was computer networks. So after
- 2:09graduating, I started to work as a network engineer in an IT department of an airline company. And at
- 2:17that time, it was 2001 and 2002, airline agencies had to use dedicated list lines for connecting
- 2:26to airline database system. And I figured that using internet for that would be like 100% cheaper.
- 2:35And then I got that crazy idea of developing a client server solution, which works over internet.
- 2:42So I developed a prototype and it consisted of an app written in Delphi and a Linux server. And
- 2:50then I presented it to the airline management team. And they were impressed and offered me
- 2:55to form a development team. So from that point in time, I continued as a developer.
- 3:02Yeah, that's think of the times back when you had to have a leased line or back when you had
- 3:08BBSs, you had to dial up to get online. Like it's just such a different world, right? But
- 3:13that's really cool to help that company like emerge out of that world.
- 3:17Right. So now you cannot imagine that not using internet, right?
- 3:21Exactly.
- 3:22Yeah. So and then several years later, I started to work for another company. It was a fintech startup.
- 3:32It was a New York based company and they outsourced the development to St. Petersburg, Russia,
- 3:38this city where I'm originally from. And I was developing the backend in C++ and Java. And when
- 3:47the business took off, they decided to form a team, a development team in New York. And they
- 3:53invited me for this role. So I moved to New York and formed a development team there. And it was
- 4:01building software for seven years there. And during that time, actually, my friend, Alexander
- 4:10Kozlovsky, we studied together in the university. And he was really, really interested in databases.
- 4:17And he was actually a senior lecturer. So he taught people databases. And he feared that most
- 4:25people actually don't get relational algebra, you know, and there should be a better way to
- 4:32build software. He suggested an idea to build a web framework. And I love the idea because working
- 4:43for a financial company, I saw that it is a very fast environment. And I actually missed a tool like Delphia,
- 4:53rapid application development tool. And I thought that if I would have one, I would use one. And this is how we started to work on Pony. And it was a weekend project, actually, initially.
- 5:09And it was a template engine. It was routing. It was ORM. And then we found that probably we shouldn't do all those things because it's too much.
- 5:21Yeah.
- 5:22And by that time, by that time, Circle Alchemy appeared. Actually, we started the same year, 2006. It was the first year when we started to work together. And actually, by that time, Alexander already tried to build a mapper. He tried in C++ in Delphi. And Python was a choice which he came to understanding that it should be a dynamic language.
- 5:50Yeah, I'm sure it was much harder in C++ to pull something like this off, especially with the really rich language level query syntax. Like, I'm not really sure how that works in C++.
- 6:01Right. And then we started to work together on this. And he told that it was a SQL object, such a mapper. And in one of email lists, he suggested that why not to use generator syntax for writing queries. And I love the idea, too, because it was natural syntax for queries.
- 6:24It's a completely natural syntax. Like, I think of generator expressions and list comprehensions as kind of like in-memory database operations, just working on your objects, obviously not indexed or anything like that. So if you're going to do that in your Python code, but somehow project that down into the database, that's perfect, I think.
- 6:44Yeah, exactly. And what we liked as well, that it is so natural for someone who just starts working with Python, that you don't need to actually learn another language such as SQL.
- 6:59Yeah, I think you probably could get away with just being able to read but not write SQL with Pony.
- 7:04Yeah.
- 7:04Yeah. Or SQLAlchemy or some of the other ORMs, right? Like, if you can understand the debug output, you may be okay. Who knows? It depends on how complex I suppose it gets. But that's a cool goal anyway.
- 7:18Exactly. And I worked with people who actually can program different languages, but it's hard for them to understand SQL pretty well.
- 7:29And that's why we decided that if we could provide such a tool, it will be very helpful.
- 7:35And I could see how it can speed up making prototypes.
- 7:40And, you know, when you build a prototype, you never know if it will be an application later and how it will evolve.
- 7:48Yeah, absolutely. That means... I wonder how many applications there are in production in the world that were just supposed to be prototypes.
- 7:56I bet it's a really high percentage.
- 7:58Yeah, I agree. You need to have a tool which allows you to start quickly.
- 8:03And then when you actually evolve with this application evolves, you want it to allow you to build this as complex as you want.
- 8:13Well, yeah. And also the ability to recreate the project quickly rather than manage some huge data access layer with some kind of ORM that's really nice and clean.
- 8:24Like Pony ORM is a really good thing to have so you can say, no, no, this is a prototype.
- 8:30We're going to start again. But it only takes like this morning and then we'll have it back to where it is.
- 8:34But proper. Something like that, right?
- 8:36That's cool.
- 8:37An app that created using a simple tool can reach a point where the further development is too difficult or even impossible.
- 8:45And having reached this borderline of complexity, it's often no time actually to start from scratch.
- 8:54Yeah, it's usually you're already down that path and it's too late.
- 8:57How interesting.
- 8:58Okay, so let's talk about Pony ORM a bit.
- 9:01So Pony ORM is a data access layer.
- 9:05It's an ORM, object relational mapper.
- 9:08How would you describe it if somebody asked you what is Pony ORM?
- 9:11Well, it is an advanced object relational mapper and it provides a very easy interface.
- 9:20And even a newbie can start working with this.
- 9:23That is one thing I was struck with when I was working with it is I was almost thinking like, well, how do I connect back this object to the entities?
- 9:34Like, where do I create the entity base class from?
- 9:37And how do I, you know, why don't I have to call add to insert objects?
- 9:42And why don't I have to call commit to commit them?
- 9:44And all of those answers are because you're using some really interesting Pythonic concepts that sort of simplify a couple of the steps that you might have found in other ORMs.
- 9:56Right.
- 9:56And Pony, it's a small horse, right?
- 9:59It's small, smart, and powerful.
- 10:02So we believe that these features we put into our mapper.
- 10:08I think so, too.
- 10:09I think it's really, it is really nice.
- 10:12And I'm looking forward to using it properly on some projects now that I know a little bit more about it.
- 10:16So I want to ask you what's Pythonic about it.
- 10:20But I think maybe the way to uncover the various Pythonic elements is to actually work through an example.
- 10:27But before we get to that, let me ask you this other question.
- 10:30So there's these two styles or two design patterns that typically are in play at ORMs.
- 10:37And that's whether or not the ORM acts as an active record where you sort of work with individual elements.
- 10:46So like Ruby on Rails or I think the Django ORM is like this.
- 10:51Or it's a unit of work, which is like SQLAlchemy, where you create a bunch of objects and make some changes, inserts, updates, deletes, and so on.
- 11:00And when you're finally ready, you commit that unit of work and it applies all of those.
- 11:04Which style is Pony?
- 11:05Yeah, absolutely.
- 11:07There are two basic patterns.
- 11:09And with active record, you have an object which represents a line in the database.
- 11:16And the thing is that if you select the same object twice using active record, you will have two instances of the same object in the memory.
- 11:27And it is a huge problem because now you have two objects which are actually the same.
- 11:35And when you start to change one object.
- 11:37And then change another one, you can have lost updates.
- 11:40And another thing that you need to always remember to call safe on each object.
- 11:47Yeah, exactly.
- 11:47And you don't really do that with unit of work.
- 11:50You just kind of work with your data.
- 11:51And when you're ready, you commit it, right?
- 11:54Every time you read a record from the database, we first check in the identity map if there is such a record already loaded into the memory.
- 12:04And this allows us to return an object from the cache from the memory instead of querying the database.
- 12:15Yeah, so that's cool in that it gives you better performance because you're not round-tripping to the database as often.
- 12:20But it's also maybe even more important that it means if you somehow get the same object twice, it's still the same object in memory, right?
- 12:29Exactly.
- 12:29So it allows us to avoid excessive database calls.
- 12:34And at the same time, we always work with the same copy of the object.
- 12:40And another option that it provides us with is to manage transactions.
- 12:46So we can see when the scope of a function or a context manager was left.
- 12:52And at this point, we can commit all the changes to the database.
- 12:57It is very convenient.
- 12:58Yeah, that's very convenient.
- 12:59I love your use of context managers in this project.
- 13:02So maybe that's a good place to jump into talking about the code.
- 13:06And we have to be a little careful on audio format talking about code.
- 13:10But maybe we could just kind of roughly walk through some of the steps involved.
- 13:15And we could talk about the Pythonic concepts as we get to them.
- 13:19Okay.
- 13:20So obviously, yeah.
- 13:21So obviously, to get started, you have to import Pony, right?
- 13:25So there's a number of things in there that you got to work with.
- 13:29So pip install Pony, import Pony, and so on.
- 13:32And then you create a database.
- 13:35So a database is just an object, right?
- 13:38But this database has a couple of features that go on to be super important.
- 13:43Like on that object, you have a base class.
- 13:48And all of your entities have to derive from it, right?
- 13:50Exactly.
- 13:52So when you create a database, at this point, it is just an object.
- 13:55And later, when you create entities, you inherit it from db.entity, which is an attribute.
- 14:03And it is abstract class, which tells that it is an entity which is connected to this database.
- 14:12And having such pattern, actually, you can create several databases.
- 14:17You can simultaneously work with more than one database.
- 14:20Right.
- 14:21Maybe you have like a core database that has your important stuff and then an analytics database for tracking how people are using your app.
- 14:28Something like that, right?
- 14:29Yeah.
- 14:29It's very often used this way.
- 14:31Yeah.
- 14:31And then you create, like you said, you create this class that derives from db.entity.
- 14:36What does it look like to define the columns?
- 14:38The main difference of Pony from other mappers is that we work in terms of entities.
- 14:45So in SQLAlchemy, you work with relations.
- 14:51And here with Pony, we hire the level of abstraction and work in terms of entities, in terms of objects.
- 15:01And we don't create columns.
- 15:04We create attributes of the object.
- 15:06Because this way, we figure that it is easier for a human being to think in terms of objects, not relations.
- 15:13Yeah.
- 15:13And probably Python as well, right?
- 15:15We're used to working with hierarchical collections of objects anyway.
- 15:18Yeah.
- 15:19Yeah.
- 15:20And then you create attributes.
- 15:21And we provide a very concise way of declaring such entities.
- 15:27Because from the first day of developing Pony, we actually had three goals.
- 15:32We wanted this mapper would be easy to use.
- 15:36It means having PyTonic API.
- 15:38It should be very performant.
- 15:40So high performance is the goal.
- 15:42And the third is safety and reliability.
- 15:47So we would like to avoid SQL injections and make it reliable.
- 15:52Yeah.
- 15:53You do some really nice stuff to avoid SQL injection attacks.
- 15:56And I'll talk about those later as well.
- 15:57Yeah.
- 15:58So when you declare entities, it is as short as possible.
- 16:02You just write the attribute name and then the type of the attribute.
- 16:07So it could be a required attribute or optional.
- 16:11Another two options is you can have a primary key or a relationship to another entity.
- 16:18And then in creating the attribute, you specify the type of the attribute and some additional options like unique or auto generator.
- 16:31When you want to have a sequence creating a new object, sometimes you want ID to be automatically generated.
- 16:41Right.
- 16:41And of course, you have default values as well for like created dates and things like that, right?
- 16:45Right.
- 16:46So we have API reference.
- 16:48And there are about like probably 15 options that you can specify for attribute.
- 16:55And you asked about columns.
- 16:57You can also specify the name of the column.
- 17:00If you would like to give a specific name to the database column or if you have a database already created and you would like to map these entities to the existing tables.
- 17:13So you can specify the table name and the column name declaring the entity.
- 17:19Yeah, that's excellent.
- 17:20So let me try to give people a really quick example here.
- 17:22So you've on your GitHub, you've got a nice example called eStore.
- 17:28And you have relationships between like customers and products and stuff in a shopping cart and so on.
- 17:35So you obviously have a customer class and a product class and so on.
- 17:40But just to like talk about the simplicity.
- 17:42So for a cart item, the whole thing is you have a cart item class that drives from db.entity.
- 17:48It has a quantity, which is a required column, which is an integer.
- 17:52And then you have a customer, which is a required customer object.
- 17:56Just literally parentheses the class name, not even in quotes, just actually the Python class object.
- 18:01And product required of product.
- 18:06So here's an object that has two relationships back to customer and product.
- 18:12And it's like the simplest thing you can imagine, right?
- 18:15Right.
- 18:16In order to make it even simpler, you can take a look at the diagram at our entity relationship diagram editor, which represents this database schema.
- 18:28So you can visually see this.
- 18:30Right.
- 18:30So for people who are listening, if they want to find it and they don't want to go in the show notes, it's editor.ponyorm.com slash user slash pony slash E capital S store.
- 18:41Of course, I'll link to that.
- 18:42Yeah, that's really cool.
- 18:45And you guys have this wonderful online designer that shows you the relationships that lets you model and rearrange them.
- 18:51Even get the what would be the generated SQL, the generated Python classes, and so on.
- 18:56Definitely, I want to come back and talk about this online editor because it looks really special.
- 19:01So, yeah.
- 19:02So very cool.
- 19:02We create these entities and the relationships are really super simple to set up.
- 19:07So that's wonderful.
- 19:08And then another thing that you chose to do in your API is make the ID something that is optional.
- 19:16Right.
- 19:17Oftentimes, you don't have a natural primary key.
- 19:20Yeah.
- 19:20And in this case, you just create attributes that you want to have in the entity.
- 19:29And if you don't declare primary key, pony will add it automatically.
- 19:33And it will be integer type with sequence.
- 19:37Yeah, the auto-incrementing primary key.
- 19:40Exactly.
- 19:41Yeah.
- 19:41Yeah.
- 19:41Yeah, that's perfect.
- 19:43So then you go through the standard stuff.
- 19:44You bind to a database connection and a database type.
- 19:47What types of databases are supported?
- 19:50So currently, we support four major databases, which are SQLite, Postgres, MySQL, and Oracle.
- 19:59And currently, we are working on adding Microsoft SQL Server.
- 20:05So we had users who asked us about it, and we actually added this type of database.
- 20:12But the thing is that recently, we added a JSON type support.
- 20:18So you can work with the database JSON type and make queries to those JSON types.
- 20:26And this part, we didn't add to Microsoft Server yet.
- 20:30But after we will add the JSON support, I think we will include it into the release, and we'll support five databases.
- 20:38Oh, that sounds great.
- 20:39I mean, those are basically the major ones.
- 20:42Are there any that people are asking for that are asking for commonly that are missing?
- 20:48Only a Microsoft SQL Server was something we were asking about.
- 20:52Yeah, it seems to me like those five rounded out.
- 20:54Tell me more about this JSON thing.
- 20:56Is this something that is JSON stored in like Postgres or SQLite, or is it something else?
- 21:04Right.
- 21:05So we used the native database JSON type.
- 21:07So it is JSON B, binary JSON for progress and for MySQL.
- 21:14For Oracle, it is, I believe it is Club.
- 21:17And for SQLite, normally it is stored as text by default.
- 21:23But you always can install a module, which is called JSON 1.
- 21:29And this way, your queries to SQLite will be faster.
- 21:33Okay, so there's a module that I can extend.
- 21:36I see.
- 21:37That I can extend SQLite so that it can understand the internals of that JSON document so I could possibly query by some attribute deep down inside it.
- 21:46Recently, we have added native JSON data type support.
- 21:50And this way, we can actually combine NoSQL database with a relational database.
- 21:56So it's like having best of two worlds.
- 21:59In a relational database, you can declare an attribute of JSON type.
- 22:05And in this attribute, you can keep the data structure, which can vary.
- 22:12Yeah, this is really cool.
- 22:13So basically, you have regular columns, but you can also have hierarchical columns that are stored as JSON, more or less.
- 22:21That's right.
- 22:22Yeah, it does very much seem like here's a MongoDB type thing going on as part of that table.
- 22:30Exactly.
- 22:30And you can actually do queries to the internals of this JSON structure, which is described in our documentation.
- 22:40Yeah, yeah, that's cool.
- 22:41Can you add indexes to the internals of that data structure?
- 22:45I think not yet.
- 22:47It's down to whether the database is supported, I'm sure.
- 22:50Probably if the database supports that, you can...
- 22:54Actually, it is just a native JSON type for the database.
- 22:59If a database supports index, you can add an index.
- 23:02Right.
- 23:03Okay, that makes sense.
- 23:04So it's really a question of, does MySQL support indexes on embedded JSON types, which I actually don't know the answer to.
- 23:10So that's cool, though.
- 23:12We have users who use this feature in production already.
- 23:16Nice.
- 23:16So yeah, speaking of production, can you speak of some of the notable deployments of PonyRM?
- 23:22I mean, I know the vast majority of no visibility into, but...
- 23:25Yeah, some of them asked us not share their details because they have PR departments that they need to approve it with.
- 23:35And for those who we can share, I put testimonials to our site.
- 23:41Oh, yeah, excellent.
- 23:42I see at the bottom, there's a whole bunch of testimonials and people can come check them out.
- 23:45I'm sure not everybody has permission to divulge whether they're, you know, speak on behalf of their company, but that's very cool.
- 23:53So when I was working on the new version of the site, I sent this request to all our users.
- 24:01And actually, those people paid for the license because initially we had Pony released under dual license.
- 24:10So it was AGPL and it was commercial license.
- 24:14And then we figured that AGPL actually doesn't really work with the community.
- 24:19People don't like AGPL at all.
- 24:21And although we were selling licenses, we decided that we probably need to change our model and we just need to release Pony under Apache 2 license.
- 24:34And think about the possibility to monetize our editor because it really can be a tool which allows people to develop applications faster.
- 24:46And we use the same model as GitHub.
- 24:50So if you would like to share your data schema with everybody, make it public, you can use it for free.
- 24:57And if you would like to have a private data schema, then we'll ask to pay some money.
- 25:03Yeah, I think that's a great model.
- 25:04And we'll definitely talk about it.
- 25:06That's at editor.ponyorm.com.
- 25:09Yeah, it's very cool.
- 25:10This portion of Talk Python To Me has been brought to you by Rollbar.
- 25:28One of the frustrating things about being a developer is dealing with errors.
- 25:31Ah, relying on users to report errors, digging through log files, trying to debug issues, or a million alerts just flooding your inbox and ruining your day.
- 25:39With Rollbar's full stack error monitoring, you'll get the context, insights, and control that you need to find and fix bugs faster.
- 25:47It's easy to install.
- 25:48You can start tracking production errors and deployments in eight minutes or even less.
- 25:53Rollbar works with all the major languages and frameworks, including the Python ones, such as Django, Flask, Pyramid, as well as Ruby, JavaScript, Node, iOS, and Android.
- 26:02You can integrate Rollbar into your existing workflow, send error alerts to Slack or HipChat, or even automatically create issues in Jira, Pivotal Tracker, and a whole bunch more.
- 26:11Rollbar has put together a special offer for Talk Python To Me listeners.
- 26:14Visit rollbar.com slash talkpython to me, sign up, and get the bootstrap plan free for 90 days.
- 26:20That's 300,000 errors tracked all for free.
- 26:23But hey, just between you and me, I really hope you don't encounter that many errors.
- 26:27Loved by developers at awesome companies like Heroku, Twilio, Kayak, Instacart, Zendesk, Twitch, and more.
- 26:33Give Rollbar a try today.
- 26:35Go to rollbar.com slash talkpython to me.
- 26:46Let's focus for a moment on what I think are really getting to the Pythonic concepts.
- 26:52What we talked about so far with the way you work with Pony is it's very nice, but it's not super unique.
- 27:00And it's just like a nicer version of what I feel like the other ones are doing.
- 27:03But then you get to things like working with context managers and decorators and all sorts of really nice stuff.
- 27:10Tell us, how do we like insert data?
- 27:13Or how do I get like a database connection and commit it and go through like that whole set of steps there?
- 27:19Well, when you bind entities to the database, the next step, you need to generate mapping.
- 27:24And this way we just connect objects in the program entities to the database tables.
- 27:34And then you can start working with the database.
- 27:38You can select entity objects or create new objects.
- 27:43And all that interconnection should be done within the database session because it is a transaction, right?
- 27:52Right, this whole unit of work thing, right?
- 27:53Yeah, unit of work, exactly.
- 27:55So you need to either create a function and decorate it with DB session.
- 28:00Or you need to use the context manager and work with the database within this context manager.
- 28:09Yeah, so let's focus on that for a sec because I think that's great.
- 28:12So if I want to make a bunch of CRUD operations, inserts, updates, deletes, and so on, one way to do that is write a function that changes the data, creates objects, it updates existing ones.
- 28:25And all I have to do is put a decorator at DB session on that method.
- 28:30And it will handle both the positive outcome and the negative outcome.
- 28:36So if that method runs to completion without errors, without exceptions, it will commit the transaction.
- 28:43But if for some reason there's an exception in there, it will roll back the transaction just by virtue of having the decorator, right?
- 28:49Exactly.
- 28:51So when you wrap your code, which works with entities with DB session, it actually does the following.
- 28:59When you start working with the database, it doesn't start the transaction from the first moment.
- 29:07Because if it is just read, you don't need to open the transaction.
- 29:12You can read from the database without opening it.
- 29:17And when you start to change, the new transaction is created.
- 29:21And then when you leave this scope, if no exception happened, then Pony automatically will commit the changes to the database.
- 29:31If an exception happened, then it will be rolled back.
- 29:36And you can specify parameters to this decorator.
- 29:40For example, if you would like to retry several times, you can do that specifying parameters to the decorator.
- 29:48That might be helpful when you have more than one process working with the same table with the same row.
- 29:55And Pony has a concept of optimistic checking.
- 29:59So this way we avoid a situation when you can lose updates.
- 30:05When Pony saves something in the database, it actually adds checks that the attribute values keep the same.
- 30:15Because when you work with objects, Pony tracks which objects you read and which objects you write.
- 30:23So if you, for example, in your code, read an attribute and then do something and save this object into the database,
- 30:32Pony will make sure that attributes that you read keep the same.
- 30:38Because if it don't, it means that some other process changed the database.
- 30:43And in this case, it cannot be saved in the database.
- 30:46Yeah, this is very common in the sort of disconnected unit of work styles of ORMs.
- 30:52But I really like this model.
- 30:54I mean, the alternative is pessimistic concurrency, which is to say, lock the database.
- 30:58Nobody can do a thing until we're done with this whole part, right?
- 31:02And everybody get in line.
- 31:04And I'm going to do a bunch of changes.
- 31:05And then you can go make your queries or other operations on the database and not see inconsistent data.
- 31:11And that's great.
- 31:13But it also kills concurrency and all sorts of things.
- 31:17With this model, the optimistic concurrency, it's like, well, probably nobody changed this record also.
- 31:23So it'll just do all the work.
- 31:25And then it'll say more or less something to the effect of, update person with ID this where the name is the old name.
- 31:32Set the name to the new name.
- 31:33Something like that, right?
- 31:34So the goal of Pony is provide consistent API and ability to work with the database,
- 31:43even for people who don't know much about transactions and lost updates.
- 31:49So we would like Pony to provide such an easy way to work with the data reliably.
- 31:56Yeah.
- 31:56And I think the retrying thing is really interesting.
- 31:58A really interesting addition there.
- 32:00Sometimes you don't want to make an entire method, basically a transaction in a sense.
- 32:05So you can use another Pythonic concept of context managers, right?
- 32:10So you can just say with DB session, do you work in that little suite?
- 32:13And then when you're out of there, it follows the same rules.
- 32:16No exceptions, commit.
- 32:18Exception, rollback, right?
- 32:19Right.
- 32:19And at the same time, this concept of optimistic checking, it works by default in Pony.
- 32:26But if you need to log the database, do for update query, you can do that too.
- 32:33There's a method for entity has a method for update, and a query has such a method for update.
- 32:40So this way you can log the database if you need.
- 32:44Right.
- 32:45Okay.
- 32:46Very cool.
- 32:46So the next thing, once you have data in the database, of course,
- 32:49you want to write a query.
- 32:50And this is where, this is what got my attention in Pony in the first place, is the way that
- 32:55I would, like, say, do a query on, like, a bunch of people we inserted to the database
- 33:01at the person class would be to write a generator that would do the in-memory query.
- 33:08So you'd say something like, select P for P in person if P dot age is greater than 20.
- 33:12And, like, that's literally what you write.
- 33:13Yeah?
- 33:14Yeah.
- 33:14You write this generator and pass it as an argument to the select function.
- 33:21And what Pony does?
- 33:22And what Pony does, it gets the bytecode of the generator.
- 33:26It decompiles it in order to extract the abstract syntax tree.
- 33:31And then it translates this abstract syntax tree into SQL.
- 33:37And made a presentation on the EuroPython in 2014.
- 33:42And it is available on YouTube where I explain the whole process, how we actually do this translation.
- 33:52Because before, a lot of people actually asking if this translation is fast enough.
- 33:59And I should tell that the whole thing works really fast.
- 34:04And the thing is that you need to translate each generator only once.
- 34:09In the whole program, you need to translate the generator bytecode only once.
- 34:15And then it will be cached.
- 34:16And the result of the translation to SQL will be cached as well.
- 34:22And after that, we just have the SQL query where we just put parameters and send it to the database.
- 34:30That is really awesome.
- 34:31I'm definitely going to link to your 2014 talk.
- 34:34You could also do, like, projections.
- 34:36So, like, in a generator expression, I could say p.name, p.age for da-da-da-da.
- 34:41You support basically doing projections at the database level in the same manner, right?
- 34:46Right.
- 34:47So, basically, you don't even need to think in terms of tables.
- 34:53You just work with objects.
- 34:56And you can select a whole object or you can select a specific attribute.
- 35:02Or you can even do aggregation.
- 35:05So, you select a tuple.
- 35:07For example, you select p.name.
- 35:10And then, for example, if we have some...
- 35:13Let me give you this example.
- 35:15For example, you work with a customer.
- 35:18And this customer has some orders which has the attribute total amount.
- 35:26So, you can do aggregation and return from this query a tuple where you have the customer name and then aggregated amount of his or her orders.
- 35:40That's really cool.
- 35:41Do you have some good examples of that on your docs?
- 35:43Yeah.
- 35:43Okay.
- 35:44I'll be sure to link to them.
- 35:45Yeah.
- 35:45Send me the link.
- 35:46And finally, if for some reason this syntax doesn't work for you, you're just like, you know, I just need to write a SQL query and just have something happen.
- 35:55You can do that as well.
- 35:57And so, you could just go to your entity which represents the table and just say select by SQL and give it some kind of SQL statement.
- 36:05You can write a row SQL, but what is more interesting is that you can insert a row parts of SQL query into a generator.
- 36:16You know, we have such a function, row SQL.
- 36:20And, for example, there is a function in the database which is not supported in Python, for example.
- 36:28Then you can insert this piece of row SQL into a declarative query written in the form of a generator.
- 36:37That is really awesome.
- 36:38And I also like how you automatically capture in your SQL text local variables as names.
- 36:48So, like, if I had X defined locally, in my SQL statement, I could say $X and you would translate that to a parameter, right?
- 36:54Yeah.
- 36:55And one second, let me send you the link for a row SQL.
- 36:59Oh, yeah.
- 37:00That's really amazing.
- 37:00So, you can just say raw SQL as part of either a Lambda or a generator expression and it just, it's going to execute on the database, right?
- 37:11Exactly.
- 37:11So, if you insert this part of a row SQL into the translated query.
- 37:17Now, one thing that's interesting is you have both Lambda expressions and generator expressions.
- 37:22Right.
- 37:23When do I use, and I can pass those as the select clause.
- 37:26So, when do I use which?
- 37:28If you would like to get an object as the result, you can use Lambda or generator.
- 37:35So, they will actually be translated into the same SQL query.
- 37:40But if you would like to aggregate the aggregation or return just a tuple of attribute names, then in this case, you use a generator because it allows you to put any expression in the beginning of the generator.
- 37:57Right.
- 37:58And it will be returned as a list of such tuples, for example.
- 38:03Yeah.
- 38:03I really love that aspect of Pony.
- 38:06That's super cool.
- 38:07So, let's talk about parameters because it's super important for the safety of your app that you don't write just straight up raw SQL with concatenated strings, right?
- 38:17Like, if anybody has a doubt, just Google little Bobby tables.
- 38:20So, the parameters here are really great.
- 38:23Like, tell us about that.
- 38:24Pony allows you to avoid SQL injections absolutely because all the parameters that we pass into the SQL query, they are passed as database parameters.
- 38:36So, this way, there is no way to get a SQL injection.
- 38:40And also, Pony allows you to pass not only parameters but expressions as well.
- 38:47So, you can find that part described in the documentation.
- 38:52When you pass the parameter, you can specify the whole expression inside the generator query.
- 39:00That's really great that you can pass expressions as well.
- 39:02I think there's always tradeoffs when you use something, some kind of ORM versus just writing raw SQL entirely.
- 39:09I would, personally, the safety from SQL injection attacks, the fact that you might forget to use a parameter somewhere, almost alone makes it worth using ORMs.
- 39:20Not to mention all the cool benefits of everything else.
- 39:23So, yeah, I think it's really great that you have all these levels of safety, especially for web apps.
- 39:29Yeah.
- 39:29So, let's talk about the team.
- 39:31You talked about your co-founder just a bit, but maybe mention him again.
- 39:35And how many people are working on the project now?
- 39:37Currently, we have four people.
- 39:39So, Alexander Kozlovsky is my co-founder.
- 39:42And we started to work with him together.
- 39:45And at some point, I decided to leave my job and concentrate on Pony full-time.
- 39:53Congratulations on that.
- 39:54That's awesome.
- 39:55Yeah, thank you.
- 39:55Although, we found another project later.
- 39:59So, currently, we work on Pony.
- 40:02We have four people.
- 40:04We have two more developers.
- 40:06So, one of them works on backend and another on frontend.
- 40:10So, his role is to help us build our editor.
- 40:15We have big plans on that.
- 40:18And currently, we actually, yeah, we work on the editor as well.
- 40:23Nice.
- 40:23Yeah.
- 40:24So, tell us where are you going?
- 40:26What features are you adding?
- 40:27Currently, you can use the editor in order to create entity relationship diagram.
- 40:33And then get either a Python classes or SQL create table statements.
- 40:41And this is actually used a lot by users who actually don't know anything about Pony.
- 40:49And when I write them that, do you use Pony?
- 40:52Why not you just start opening the tab with Python classes?
- 40:57They actually, often, they don't know a thing about Pony.
- 41:01And when they discover this possibility, they love this feature and start using Pony as well.
- 41:08Yeah, that's great.
- 41:09Yeah.
- 41:09So, let's talk about your editor a little bit.
- 41:11So, I really like it.
- 41:12Like I said, editor.ponyorm.com.
- 41:14And the idea is it's basically a database diagram development tool, right?
- 41:20So, you can see your tables.
- 41:21You can see the relationships and so on, which all that is really cool.
- 41:27And I can, like, add a new entity.
- 41:30And it gives you a whole nice designer.
- 41:32It shows you a bunch of stuff that maybe you didn't realize was something that you could model,
- 41:37like the volatility of attributes or things like this.
- 41:40But then once you get your model built together, you can actually go and say, show me the Python code.
- 41:46Yeah, show me the Python code.
- 41:53For the various databases and so on, you know, you can create this.
- 42:00So, you can create a tool that you can create a tool that allows them to do the whole cycle of development.
- 42:12So, when they can create a diagram.
- 42:13So, when they can create a diagram and then get the Python code and work on the project.
- 42:18And when they need to change something, they can just change it in the editor and get their migration applied to the database.
- 42:27And keep those, the log of the changes in the editor.
- 42:31Nice.
- 42:32Yeah, okay.
- 42:32So, that's really neat.
- 42:34That brings us to migrations a little bit.
- 42:36What's the story of migrations there?
- 42:38This point is the feature which is most asked.
- 42:44And at this point, we are working on migrations and we developed the prototype.
- 42:50And it works great.
- 42:53But what we would like to add, an ability to write comments inside the migration file the same way as in Django.
- 43:03So, when you just write operations like add attribute or add relationship, I believe that it will be released within the following month.
- 43:14But it is not that fast.
- 43:17The development of migrations took longer than we initially suggested.
- 43:22Yeah, it seems like it might take a while.
- 43:24It's a non-trivial thing.
- 43:26That's right.
- 43:27But we are very close to the release at this point.
- 43:30Nice.
- 43:30Yeah, what are you doing after you get the migrations done?
- 43:33Some time ago, we started to work on a project which is called Fine Art Biblio.
- 43:37So, it is a Miami-based company.
- 43:40And the goal is to provide Wikipedia for modern art.
- 43:44I formed a team for this project.
- 43:47And from day one, we started to use Pony.
- 43:50It was actually our goal to find a company where we can use Pony in order to build the back end and see how we can make it better.
- 44:01And when we were working on this project, we found that when we need to build a front end, we actually need to have the same models as the front end.
- 44:12And we thought, why not to provide a way to have the same identity map, the same models as the front end.
- 44:23And this way was Pony.js.
- 44:26This way, Pony.js appeared.
- 44:28So, Pony.js is a don to Pony, which is not released yet.
- 44:33Why, I will tell a little bit later.
- 44:37And this way, you can get the objects which are extracted in the back end.
- 44:43You can have the same graph of connected objects at the front end and work with them in JavaScript.
- 44:52Nice.
- 44:52So, I can just basically expose my models from my back end as some kind of service that Pony.js can consume.
- 45:00And then they're like replicated over?
- 45:02Or how does that work?
- 45:03When you extracted objects from the database, then you call the to.json method.
- 45:09And it returns a set of objects which will be sent to the front end.
- 45:15And the front end, we have a library, Pony.js, which puts it into an identity map and allows you to work with objects the same way you do it on the back end.
- 45:29So, you can traverse attributes.
- 45:31You can traverse relationships.
- 45:34You can create new objects and send them back to the back end.
- 45:38That's cool.
- 45:39And for allowing this, for making this work, we created a layer of permissions and security.
- 45:48So, it is also a declarative language which allows you to tell which operations are allowed.
- 45:55Create, delete, update, or modify.
- 45:59But when we were working on this project, we found that Facebook released GraphQL.
- 46:06And we thought that probably it is the same thing that we are working on.
- 46:12And we decided actually to try GraphQL.
- 46:17And by this time, we had one more team member joined to our company.
- 46:22And he developed GraphQL server.
- 46:26And now we have actually three ways to work with the front end.
- 46:31And after we release migrations, we believe that integration with the front end actually will be our major focus.
- 46:41Yeah, you have your back end stuff pretty much dialed in.
- 46:44So, extending that seems like a great idea.
- 47:00This portion of Talk Python To Me is brought to you by GoCD from ThoughtWorks.
- 47:05GoCD is the on-premise, open-source, continuous delivery server.
- 47:10With GoCD's comprehensive pipeline and model, you can model complex workflows for multiple teams with ease.
- 47:16And GoCD's value stream map lets you track changes from commit to deployment at a glance.
- 47:22GoCD's real power is in the visibility it provides over your end-to-end workflow.
- 47:27You get complete control of and visibility into your deployments across multiple teams.
- 47:32Say goodbye to release day panic and hello to consistent, predictable deliveries.
- 47:37Commercial support and enterprise add-ons, including disaster recovery, are available.
- 47:42To learn more about GoCD, visit talkpython.fm/gocd for a free download.
- 47:48That's talkpython.fm/gocd.
- 47:51Check them out.
- 47:52It helps support the show.
- 47:53So, your docs are in Python 2.
- 48:02I want to encourage you to write them in Python 3 and tell a story.
- 48:06So, when people were looking at the adoption of Python 3, maybe a year ago or something like this,
- 48:12and I think around that time, Django switched its documentation to Python 3.
- 48:18They didn't do anything else.
- 48:19They just said, look, Python 3 here.
- 48:21This is the way you do it in Python 3.
- 48:22And, oh, it also supports Python 2.
- 48:24And the usage of Python 3 on PyPI went up significantly, like some number of percent total because of that.
- 48:33But my real question is, does it support Python 3 as well as Python 2?
- 48:39What's the story around on the various versions?
- 48:41Yes, it does.
- 48:42Nice.
- 48:43Yeah.
- 48:43So, all the stuff that I've done with it worked totally fine, and I was playing with it in Python 3.
- 48:48So, that's great.
- 48:49So, I'm really fascinated by people working on successful open source projects that have businesses around them.
- 48:57And so, can you talk just really briefly about your project and the editor and the business model around it?
- 49:05We think that the editor could be helpful for startup companies and for companies which develop new projects.
- 49:15Those people who we were talking to who would like to use Pony in their projects, we found that what they like in Pony is simplicity and that Pony allows them to save resources.
- 49:29So, they can start fast.
- 49:31They don't need dedicated backend engineer sometimes.
- 49:35And they just can easily return to the project which was developed like a year ago and do some changes without actually looking through the manuals.
- 49:48Because the query language is really simple and it allows to maintain projects with less resources.
- 49:59We think that for such people, for such companies, having an editor would help them to build prototypes and to work on applications even faster.
- 50:09Especially if they would be able to work on the same data schema together.
- 50:17And this is one of the features we would like to add to our editor.
- 50:22So, the idea is that it is software as a service.
- 50:26And if you would like to create a diagram and share it with everyone, it is free.
- 50:33And if you would like to keep it as a closed source diagram, then there is some price for this.
- 50:43And it depends on how many diagrams you would like to have.
- 50:47Sure.
- 50:47You know, one thing I think is really interesting about this.
- 50:50You talk about people getting started more quickly and helping new companies or new applications get up to speed.
- 50:57You have an explore section here.
- 50:59So, I can drop in and find things like you've got a corporate directory already modeled.
- 51:05And you've got an e-commerce store modeled and a university modeled and things like that.
- 51:09So, if I know that there's something in your explore area.
- 51:13Some of these are created by you guys, but many of them are by others.
- 51:16And I'm like, well, how exactly would I model this?
- 51:19Let me just look and see what other people have done.
- 51:21Right?
- 51:21I could go and just start from there, right?
- 51:24And copy that.
- 51:25Yeah, exactly.
- 51:25And maybe at some point, we will add an ability to deploy the schema to a cloud database and write logic right there.
- 51:37Yeah, that's pretty interesting.
- 51:38Is there a way to take and reverse the thing?
- 51:43So, I've got the diagram which generates my Python.
- 51:46Could I take my code that I've been working on for six months after starting with one of these and throw the Python in and get the diagram out?
- 51:55Probably we should add that.
- 51:57But at this point, the most people are asking about a reflection tool for the database.
- 52:03So, when you can just get the log of the database, create table statements, and create a diagram based on that.
- 52:12Yeah, that makes sense.
- 52:13Which, I guess, is probably the most important one is you want to be exactly like your database and give me the Python classes to make that happen, right?
- 52:20Right.
- 52:20So, someone would like to work using Pony with a database, and they just don't want to create those classes or a diagram.
- 52:29Yeah, if you've got a thousand tables, you probably don't want to spend time trying to model them again.
- 52:33Just push the button and have that pop out, right?
- 52:36That's cool.
- 52:36All right.
- 52:37Well, we're getting near the end of the show.
- 52:39This has been really interesting.
- 52:40I'm really fascinated, especially with the Pythonic style of Pony.
- 52:44So, thanks for telling us all about it and giving us the backstory.
- 52:48It's great.
- 52:49Thank you, Michael, for having me.
- 52:50Yeah, you bet.
- 52:51So, before I let you go, though, I have a couple of questions for you.
- 52:54There's over 90,000 packages on PyPI these days.
- 52:59And you probably come across some that not everybody knows about.
- 53:01What one would you like to recommend to audience?
- 53:03Well, I love Flask.
- 53:04Flask is definitely nice and great for building apps, especially for building APIs, I think.
- 53:10Right.
- 53:10We use it in our projects, and it's simple, and you can develop very fast using Flask.
- 53:18So, that's why I like it.
- 53:19All right.
- 53:19Flask.
- 53:19Great recommendation.
- 53:20And if you're going to write some Python code, what editor do you open up?
- 53:23PyCharm.
- 53:24Nice.
- 53:26Yeah.
- 53:26A lot of the PyCharm team is based in St. Petersburg, along with you.
- 53:29Yeah.
- 53:29So, do you know some of the guys there?
- 53:31Yeah, I do.
- 53:32Actually, we met with them, and we were asking them to add support for Pony.
- 53:38And we agree that when enough people will be asking about it, they will start working on
- 53:44this.
- 53:44All right.
- 53:45Well, maybe this show will give them a little nudge, huh?
- 53:47That's cool.
- 53:47Any final call to actions for everyone?
- 53:49Like, how do they get started with Pony and your project?
- 53:52Please go to pony.rm.com and read the documentation.
- 53:56Try Pony and give us feedback.
- 53:58We have a Telegram group where people can discuss Pony features and ask questions or stack overflow.
- 54:07And please give us some feedback so that we can make Pony even better.
- 54:13All right, Alexei.
- 54:14Thank you so much for being on the show.
- 54:15It's been fun to talk to you.
- 54:16Thank you, Michael.
- 54:17It was a pleasure.
- 54:17Yeah.
- 54:18Bye.
- 54:18This has been another episode of Talk Python To Me.
- 54:22Today's guest has been Alexei Milashkovic.
- 54:25And this episode has been sponsored by Rollbar and GoCD.
- 54:28Thank you both for supporting the show.
- 54:30Rollbar takes the pain out of errors.
- 54:33They give you the context and insight you need to quickly locate and fix errors that might have
- 54:38gone unnoticed until your users complain, of course.
- 54:41As Talk Python To Me listeners, track a ridiculous number of errors for free at
- 54:45rollbar.com slash Talk Python To Me.
- 54:48GoCD is the on-premise, open-source, continuous delivery server.
- 54:53Want to improve your deployment workflow but keep your code and builds in-house?
- 54:57Check out GoCD at talkpython.fm/gocd and take control over your process.
- 55:03Are you or a colleague trying to learn Python?
- 55:06Have you tried books and videos that just left you bored by covering topics point by point?
- 55:10Well, check out my online course, Python Jumpstart by Building 10 Apps at talkpython.fm/course
- 55:16to experience a more engaging way to learn Python.
- 55:19And if you're looking for something a little more advanced, try my Write Pythonic Code course at
- 55:24talkpython.fm/pythonic.
- 55:27Be sure to subscribe to the show.
- 55:29Open your favorite podcatcher and search for Python.
- 55:31We should be right at the top.
- 55:32You can also find the iTunes feed at /itunes, Google Play feed at /play, and direct
- 55:38RSS feed at /rss on talkpython.fm.
- 55:42Our theme music is Developers, Developers, Developers by Corey Smith, who goes by Smix.
- 55:46Corey just recently started selling his tracks on iTunes, so I recommend you check it out
- 55:51at talkpython.fm/music.
- 55:53You can browse his tracks he has for sale on iTunes and listen to the full-length version
- 55:57of the theme song.
- 55:58This is your host, Michael Kennedy.
- 56:00Thanks so much for listening.
- 56:02I really appreciate it.
- 56:03Smix, let's get out of here.
- 56:07with my voice.
- 56:08There's no norm that I can feel within.
- 56:09Haven't been sleeping.
- 56:11I've been using lots of rest.
- 56:12I'll pass the mic back to who rocked it best.
- 56:15Developers, Developers, Developers, Developers, Developers, Developers, Developers, Developers,
- 56:27.
- 56:27you
- 56:27you