Latest / Talk Python To Me / #80: TinyDB: A tiny document db written in Python
Transcript
- 0:00NoSQL and document databases like MongoDB have made building fast, scalable software that is easy to evolve and maintain much easier for a broad class of applications.
- 0:09Embeddable, file-based databases like SQLite have made shipping an application that requires a database a no-brainer.
- 0:16The database just runs in process, so there's no setup or maintenance.
- 0:20Yet, when you try to intersect these two excellent capabilities, you'll find the options are very limited.
- 0:25There just aren't many embeddable document databases.
- 0:28If you're a Python developer and you want a native Python solution, the options are much slimmer still.
- 0:33That's why I'm excited to introduce you to Marcus Siemens and TinyDB.
- 0:37TinyDB is a 100% pure Python, embeddable, pip-installable document DB for Python.
- 0:43This is Talk Python To Me, episode 80, recorded October 13, 2016.
- 0:56Welcome to Talk Python To Me, a weekly podcast on Python.
- 1:18Thanks for listening to my podcast.
- 1:42I've heard from so many of you that the insight into the industry you gain from each week is very important to you.
- 1:47I want to take this moment and tell you about my online courses for those of you who want to go deeper and convert your enthusiasm to working knowledge.
- 1:55At Talk Python, we currently have three courses available.
- 1:58Python Jump Start by Building 10 Apps, for those of you who are just getting into Python.
- 2:04Write Pythonic Code Like a Seasoned Developer, covering over 50 hard-won coding tips.
- 2:08And Python for Entrepreneurs, available for early access already, covering web development, design, and everything else you need for an online Python-based business.
- 2:18See these courses and more at training.talkpython.fm to hone your Python skills, no matter your experience level.
- 2:25Now, I hope you enjoy this week's interview.
- 2:27Marcus, welcome to Talk Python.
- 2:30Thanks for having me.
- 2:31Yeah, it's really great.
- 2:32I was talking to Austin, I think it was show 63, possibly, about mutation testing.
- 2:39And he said, I'm doing this really cool work with mutation testing.
- 2:42Oh, and for the database, I'm using this cool, embedded, document-oriented database called TinyDB.
- 2:49And since then, I was like, wow, an embedded document database?
- 2:52How awesome.
- 2:53And it's 100% in Python.
- 2:54So even cooler.
- 2:56And since then, I've wanted to have you on the show and talk about it.
- 2:59So welcome.
- 2:59Yeah, thank you.
- 3:00Yeah, it's going to be fun to talk about this database.
- 3:03It's tiny, from what I can tell.
- 3:05It's great.
- 3:06Yeah, it is.
- 3:07But before we get into that, of course, let's talk about your story.
- 3:11How did you get into programming in Python?
- 3:12When I was about 10 years old, I kind of got into programming because my dad bought me a book called C++ for Kids.
- 3:21I was 10 years old.
- 3:23I didn't really understand much.
- 3:25I just copied the examples and made sure it compiled.
- 3:29And that was basically it.
- 3:31So later when I had a book, Python for Kids and Java for Kids,
- 3:37then with around 14 or 15 years, I started really doing some programming in Java on my own.
- 3:44So that's when I actually got really started with programming.
- 3:49Cool.
- 3:49And what were some of the first apps you built with Java?
- 3:52What kind of work was that?
- 3:53My first application was a Hangman implementation in Java.
- 3:58So I had some problems with how I structured the code because I had to make everything public static in Java.
- 4:05So it compiled.
- 4:06And then something didn't work and I asked my dad for help.
- 4:11And he was horrified when he saw the code because everything was public static.
- 4:16And he then helped me out with sorting out where to pass an object and so on.
- 4:23So, yeah, I got some help from him.
- 4:26And, well, that's how I first got into working with Java.
- 4:31And a couple of years later, I applied to university to study electrical engineering.
- 4:38And before that, you had to do an eight-week internship.
- 4:41So six of these weeks at a small company, which mainly sells servers, which they sell servers with support contracts for enterprises.
- 4:52And they asked me to create an application so they can configure the servers via the web.
- 4:58And the project was they asked me to do it in Python.
- 5:01So that's how I got started with Python and wrote my first serious Python code.
- 5:08That's cool.
- 5:08And what was your impression?
- 5:09Were you like, oh, yeah, Python's awesome.
- 5:11Or were you like, whoa, where'd public static void go?
- 5:13Where'd public static go?
- 5:15Yeah.
- 5:16It was, I think it was way easier than I expected.
- 5:20Because after like one or two weeks, I was already quite confident that I could do the whole project.
- 5:26So I didn't really have any experience beforehand with Python.
- 5:30And with that project, after one or two weeks, I was really confident that I could do the whole project without big problems.
- 5:39Yeah, that's great.
- 5:40Very nice.
- 5:41I find a number of people get into Python that way.
- 5:44They're like working on some project and people come along, their co-workers or boss or somebody.
- 5:50will say, hey, can you do this thing in Python?
- 5:53They're like, Python?
- 5:54Well, I don't know anything about it, but let me give it a try.
- 5:55It turns out to be really easy.
- 5:57So that's really great.
- 5:58Yeah, it is.
- 5:59Definitely.
- 5:59It's very easy to start.
- 6:01And I would say it's one of the best programming languages to learn programming.
- 6:07Yeah, I certainly think it is as well.
- 6:10In the United States, they used to teach Java as the primary computer science 101.
- 6:16First course, you take programming languages.
- 6:19And in the recent years, it's switched to be primarily Python.
- 6:22So that's great.
- 6:23At my university, people have to start with C and C++.
- 6:27And most people have quite a lot of problems with that.
- 6:31And I guess it would be kind of an easier route to go with Python first.
- 6:36But I don't know.
- 6:37It's their choice.
- 6:38Yeah, of course.
- 6:39I think there's something to understanding a C-oriented language where you actually work
- 6:45with pointers and directly with the memory and stuff.
- 6:48But I'm not sure you should start there.
- 6:49You know what I mean?
- 6:50Yeah, it's your very first exposure.
- 6:52Just for comparison, my first computer science 101 course in college was Lisp.
- 6:59So that was really different.
- 7:02All right.
- 7:03So that's how you got started.
- 7:04What do you do day to day?
- 7:05University, I'm still studying.
- 7:07And next week, my lectures start again.
- 7:11Till then, I do some freelancing work, mainly web development.
- 7:15Nice.
- 7:16And is that web development in Python?
- 7:17No, it isn't.
- 7:18It's mainly JavaScript.
- 7:20Right now, I'm doing some front-end development, which is mainly JavaScript.
- 7:24And sometimes I wish it were Python, but I can't always choose.
- 7:29That's right.
- 7:30Sometimes if there's good work to be done, you just got to go do it.
- 7:33All right.
- 7:34So the database that you created called TinyDB is a document-oriented database.
- 7:40And that falls into the realm of NoSQL databases.
- 7:42And so just for everyone out there listening, I realize many of you know, but maybe some
- 7:46of you don't really totally know the difference between, say, relational databases and NoSQL
- 7:51databases.
- 7:51So relational ones, I think we're all pretty familiar with.
- 7:55There's a bunch of tables.
- 7:55There's foreign key relationships between them.
- 7:57We try to normalize our data, minimize duplication, and sort of set it up for querying from any
- 8:03particular angle, right?
- 8:04You have a wide range of queries you can write.
- 8:07But in the NoSQL world, there's a different set of trade-offs that those databases are making,
- 8:12right?
- 8:12Like the document databases don't have as much of a flexible set of queries you can write.
- 8:20But if you build the documents right, you can really optimize them for exactly the use case
- 8:24that you're looking for, right?
- 8:26So your data is more hierarchical and more directly models like how you might actually want to work
- 8:32with objects and relationships in your code rather than in storage.
- 8:35So there's also other ones like key value stores and even graph databases, although in my mind,
- 8:41those are not NoSQL databases.
- 8:43That's a different debate for a different time.
- 8:46Yeah, it is.
- 8:47Yeah, but so yours, TinyDB, falls into this document database.
- 8:51So why don't you tell us, what is TinyDB?
- 8:53Yeah, TinyDB is, like you said, a NoSQL, basically, a database.
- 8:59And actually, I didn't plan to do a NoSQL database.
- 9:03I just plan to do something that is easily usable.
- 9:09I happened to write TinyDB for another project, which was in total, I think, like 300 lines.
- 9:16And I didn't really feel like doing SQLite or SQLite database for it, because if I had to change
- 9:24anything about the data structure, I would have to write migrations and so on.
- 9:30So I just wrote a really simple database with a bunch of dictionary objects, which contain the data,
- 9:40and then added some query language for it.
- 9:43And it happened to become quite popular.
- 9:47Yeah, that's really cool.
- 9:48And it is quite popular.
- 9:49It's got over a thousand stars on GitHub, which is very cool.
- 9:52Yeah.
- 9:54You must be proud of that.
- 9:55That's great.
- 9:55Yeah.
- 9:56Yeah.
- 9:56What was interesting is that I didn't really do any marketing or promotion at all.
- 10:02I just put it on GitHub, uploaded some documentation, and just left it as it was.
- 10:07And after a couple of months, I got the first issues.
- 10:12And like a year later, I got the first pull requests without any involvement on my side,
- 10:18which was really a real great example of open source, how it works.
- 10:22Yeah, that's a great validation that people see what you've made, and they actually want to contribute back.
- 10:27You're like, wait, what?
- 10:28Yeah.
- 10:28Where did this person come from?
- 10:30They are working on my project.
- 10:31How great.
- 10:32Yeah, that's nice.
- 10:33You made a really interesting comment when you described why you didn't want to use MySQL or SQLite,
- 10:38whichever one you were referring to.
- 10:40And that's, you don't want to deal with migrations and all the headaches of evolving your schema.
- 10:45And I think one of the really, really powerful things about document databases that you don't really appreciate until you try it
- 10:54and you've sort of lived with an application over time is the ability to easily evolve the schema, right?
- 11:01Like if you want to add a field, you add a field to either your dictionary or class in your application,
- 11:08and that just becomes part of your schema.
- 11:10It's usually much easier, I think, to maintain these applications that are based on document databases.
- 11:17That's my experience anyway.
- 11:18Yeah.
- 11:18It was my experience too with the project I was working on originally because I didn't really know how the data would look like.
- 11:26So I needed something that would allow me to change the data as I need or as I find out I need another field so it wouldn't be hard to edit.
- 11:37Yeah, absolutely.
- 11:38And I think it's really cool that you have this as an embedded database because I would say probably the most popular choice for these types of databases would be MongoDB.
- 11:48But then you've got to deal with another server and, you know, configuring that thing and the connections.
- 11:54And just, you know, sometimes you just want a small embedded thing that's just part of your app.
- 11:59Yeah.
- 11:59Yeah.
- 11:59What I really like about SQLite is that it's embedded so you don't need any server, you don't need any maintenance or anything.
- 12:07So basically Python, basically TinyDB combines the best of SQLite being embedded and MongoDB being a document database.
- 12:19Yeah, that's really cool.
- 12:20So you said it's written 100% in Python.
- 12:23How many lines of code is that?
- 12:25Yeah, it's about 1,200 lines of code of which 40% approximately are documentation.
- 12:32And in addition to that, like 1,000 lines of test code.
- 12:37That is awesome.
- 12:38Yeah, and you have 100% test coverage as well on your project, which is really nice when you've got something that's actually managing your data.
- 12:45You want to be really pretty sure that it's working right, yeah?
- 12:48Definitely.
- 12:48Definitely.
- 12:49Okay, so if it's 100% in Python, how do I get it into my application?
- 12:56Like, can I pip install it?
- 12:57Yeah, definitely.
- 12:58Installing should be very easy.
- 13:01Just run pip install TinyDB, and then you just have to open a Python repl to get started, import TinyDB, and basically you are ready to go.
- 13:14Yeah, that's really nice.
- 13:15And so there's no server to start, like if your app is running, the database is running, right?
- 13:20Yeah.
- 13:21Yeah, yeah, very cool.
- 13:22So you had some reasons why you should use TinyDB and some reasons maybe why you might not.
- 13:30So what use cases are really good for considering TinyDB?
- 13:35I think it's just like the project I was working on originally.
- 13:39I think it works best for small projects where you don't really have or you don't really want to use a bigger database.
- 13:48So a small web application, I think some guy used it for a password manager or something.
- 13:56So really projects where having MySQL database or something like that would just add too much complexity.
- 14:04Yeah, certainly if you're going to deploy an application or library and you don't want to have the getting started steps, say,
- 14:13now you need to set up this server and you need to run this script to build this schema,
- 14:18and then you need to set the connection string here, and then you can get going, right?
- 14:24So to me it feels pretty similar, not an exact fit, but similar to, say, SQLite, when you might use SQLite.
- 14:31Yeah.
- 14:32It's similar in that you can really easily get started and you don't have to manage a server or something like that.
- 14:40Sure.
- 14:41So because it is a pretty small database, it doesn't try to do everything, right?
- 14:45It's not like MongoDB rewritten in an embedded Python variation.
- 14:51It really does do less.
- 14:52So there are some things it doesn't do, and maybe if you really depend upon them, you should not focus.
- 14:59You should pick something else.
- 15:00What are those cases?
- 15:01Just so we know people can decide if it fits for them.
- 15:04I think the main disadvantage of TinyDB is when you're using multiple threads.
- 15:11In that case, there is an extension for TinyDB you can use.
- 15:15If you have multiple processes, then you are really out of luck because there is no type of locking the file you're writing to.
- 15:25So you would probably run into issues with that.
- 15:28And also, it might turn out to be a bottleneck in performance.
- 15:33If you have hundreds of thousands of entries in your database and you search them, because TinyDB doesn't have any kind of index, it will have to scan the entire list every time you do a search.
- 15:45So you probably shouldn't do really involved data processing with it.
- 15:51Okay.
- 15:51Yeah.
- 15:51And that definitely, some of those are serious.
- 15:54Some of them might not be.
- 15:55It depends on your use case, right?
- 15:57Now, I don't want to talk about it yet.
- 15:59We'll get to it.
- 15:59But there's a bunch of extensions.
- 16:01And I think there are some that will come and solve some of those performance problems, which are interesting to some degree.
- 16:10And also, they open the possibility for a listener, an enterprising listener out there, to come and add something like multi-process or multi-thread support, things like that, right?
- 16:22So we'll get to the extensibility.
- 16:24There are a number of extensions now.
- 16:26I hope it's quite easy to add a new one.
- 16:30So if there are a couple of things where TinyDB doesn't support them out of the box, but if you want to, you can write an extension, you can write your own storage mechanism, and so on.
- 16:42Yeah.
- 16:42Yeah.
- 16:43Great.
- 16:43Yeah.
- 16:43We'll talk about that in a little bit.
- 16:44Awesome.
- 16:45So when I think of competitors, you have a few listed, and we've talked about a couple of them.
- 16:52Competitors, like we said, is not exactly the right word because it's all open source, but choices you might choose from.
- 16:59On one hand, like MongoDB, if you're doing document databases and you're doing real large data production apps, maybe this is, you want to scale out, maybe this is the right choice.
- 17:10But there's also some other pure Python databases that I had not heard of that I thought were pretty interesting.
- 17:15So do you want to tell us about the two that you listed on your site?
- 17:19What are their names?
- 17:20Before starting with TinyDB, I first did some research if there already is some kind of embedded Python database I can use, and that fits my use case.
- 17:32And there wasn't really, I found two projects which are somewhat similar, but not really.
- 17:38For example, there's a project called CodernityDB, which is also pure Python and NoSQL.
- 17:46But as far as I know, it's much more complex.
- 17:49You have the ability to use indexes.
- 17:52You have HTTP support.
- 17:54So it might be a great fit for a project.
- 17:57But in my case, it was just too much code.
- 18:01I think it's like 7,000 lines of code, which would be seven times the size of my project.
- 18:07So, yeah.
- 18:10And I think that's without unit tests, the size of the code.
- 18:13So it's even bigger, yeah?
- 18:14Yeah.
- 18:14And the other one was BuzzHug.
- 18:16BuzzHug is also quite interesting because it's optimized for speed.
- 18:21But what I didn't really, the reason why I didn't use BuzzHug was that it didn't have the kind of query language I wanted to use.
- 18:29So while it is really fast and it's also pure Python, it just didn't have API in the way I wanted to have or the kind of API I imagined to use.
- 18:43So I chose to build my own project.
- 18:47And also, it's like twice the size of TinyDB without tests.
- 18:53Yeah, definitely.
- 18:54So the CoderityDB, that one is a key value store.
- 18:59So in some ways, maybe not quite as capable as the document databases.
- 19:04It depends.
- 19:05They do have some additional indexes and queries you can write.
- 19:08But maybe not quite as flexible.
- 19:10And then BuzzHug is relational.
- 19:12So you have, again, all the trade-offs with managing the schema and evolving it in migrations.
- 19:17So, yeah, to me, it feels like SQLite is kind of the biggest competitor to you, you guys.
- 19:24Yeah.
- 19:24Yeah, cool.
- 19:26So can you maybe, I don't necessarily want to talk about code exactly in audio format because that doesn't usually go so well.
- 19:34But just give us a sense of, like, what is the API like?
- 19:37Like, if I wanted to create, you know, get started with TinyDB, I wanted to create a database and insert some records.
- 19:44Like, what does that look like?
- 19:46Yeah.
- 19:46So you start with importing TinyDB.
- 19:50And there are two main classes you will probably use, which is the TinyDB class and the query class.
- 19:59So you basically create a new instance of TinyDB.
- 20:03And you already have your database.
- 20:06You don't need any for the setup code or anything.
- 20:10And then you can just call DB.insert or the instance and call the insert function, pass some Python dictionary, and you already inserted your first object into the database.
- 20:23Yeah, very nice.
- 20:24And so it just basically, you either can directly or into a table insert more or less Python dictionaries, right?
- 20:31Yeah.
- 20:32And basically, you have all the basic functions, create, read, update, and delete.
- 20:36You can do some interesting searching.
- 20:39Search for objects with specific parameters.
- 20:43You can do some quite involved searching with the API I provided.
- 20:48But yeah, I think besides the database itself, the query language is like the second biggest or has the second most code in the whole project.
- 21:01If you have a list, you can search for an item in that list with specified properties and so on.
- 21:09So if you want to, you can really go crazy with searching.
- 21:12But still, the basic API is insert, delete, and then, of course, querying and update.
- 21:21Yeah, nice.
- 21:21And to me, the query language looks a little bit like SQLAlchemy in that you do it sort of in a Python language and it's translated to the query that goes down to the system, right?
- 21:32I actually found out about the SQLAlchemy project.
- 21:36I think I just discovered it.
- 21:39After I wrote TinyDB, so it happened to have a very similar query language.
- 21:45Yeah, you didn't take it straight from there.
- 21:47But it is a very natural way of doing it.
- 21:49So basically, the fact that you use the native Python sort of equality and things like that and then it translates it over is really nice.
- 21:57The exchange of data, if you just go against your API directly, is really in dictionaries.
- 22:04And sort of extended a little bit with your primary key being on there as a separate type of attribute and so on.
- 22:11But more or less as dictionaries.
- 22:12Is there an object data mapper layer, kind of like Mongo, has MongoEngine or Mongo Kit, where you work in higher level stuff?
- 22:21Like I would write, say, like, here you have, in your example, you had a person.
- 22:25Like, could I create a person class and maybe put rules or columns or structure on that and then use that to query with?
- 22:32Yeah, in a way, you can already use the query language of TinyDB as an ORM.
- 22:39So you can create a new instance of the query object and call it user.
- 22:45And then you can call something like user.name equals to the name you're looking for.
- 22:52And you already have a query, which you can use to search the database.
- 22:58So you can do that or use that as some kind of ad hoc modeling.
- 23:04Okay. Yeah, nice.
- 23:05And do you support Python 3?
- 23:08Python 2?
- 23:10What versions of Pythons do you support?
- 23:11I tried or I still try to support as many versions of Python as possible.
- 23:16I'm not sure about Python 2.6.
- 23:18But upwards from Python 2.7, I think I support every version.
- 23:23Also because TinyDB doesn't have any external dependencies even on other Python packages.
- 23:30So it's quite easy or it was quite easy for me to write code which works in both Python 2 and 3.
- 23:37So if you're using Python 3 or Python 2, either way, you're good to go.
- 23:43Yeah, that's really excellent.
- 23:44So I'm very happy to see the increased Python 3 support for your project, other projects.
- 23:50I had the Bware guys on last time and they're like, it's all Python 3.
- 23:55So that's great.
- 23:56The other thing, if it's pure Python, the very next question I had was, well, if it supports Python 3, what's the story with PyPy, the JIT compiled version of the interpreter?
- 24:07I didn't myself really use it with PyPy, but I still run every commit I do with continuous integration, which tests all the Python 2 and 3 versions and also tests that it works with PyPy.
- 24:21But myself, I didn't really have a use case where I would need to use PyPy.
- 24:27So I don't know if it's faster or slower than you would expect.
- 24:31Okay.
- 24:32But it definitely works.
- 24:34It definitely works.
- 24:35Okay.
- 24:35That's really cool.
- 24:36And so maybe it's a lot faster.
- 24:38Actually, we'll get to some stuff that talks a little bit about speedups that you can do.
- 24:42And maybe that just takes PyPy out of the loop in terms of being necessarily important.
- 24:48But anytime you have something like this that's pretty central to how your application performs and you can do it on PyPy, that's pretty interesting to see.
- 25:10This portion of Talk Python To Me is brought to you by Hired.
- 25:12Hired is the platform for top Python developer jobs.
- 25:15Create your profile and instantly get access to 3,500 companies who will work to compete with you.
- 25:20Take it from one of Hired's users who recently got a job and said,
- 25:23I had my first offer on Thursday after going live on Monday, and I ended up getting eight offers in total.
- 25:28I've worked with recruiters in the past, but they've always been pretty hit and miss.
- 25:32I tried LinkedIn, but I found Hired to be the best.
- 25:35I really liked knowing the salary up front.
- 25:37Privacy was also a huge seller for me.
- 25:40Sounds awesome, doesn't it?
- 25:41Well, wait until you hear about the sign-in bonus.
- 25:43Everyone who accepts a job from Hired gets $1,000 sign-in bonus.
- 25:46And as Talk Python listeners, it gets way sweeter.
- 25:49Use the link Hired.com slash Talk Python To Me and Hired will double the sign-in bonus to $2,000.
- 25:54Opportunity is knocking.
- 25:56Visit Hired.com slash Talk Python To Me and answer the door.
- 26:07Speaking of that, one thing that I saw that was cool, and this is something I learned today,
- 26:11I hadn't really paid any attention to it before, is you said one of the speed-ups you can do is,
- 26:16if you install a package called uJSON, you can get TinyDB to run a lot faster.
- 26:23Because as you can imagine, it's all about JSON parsing, multi-directional JSON parsing, right?
- 26:28And so what's uJSON and why is it faster?
- 26:32uJSON is a really cool project.
- 26:34I think the main or the reason why JSON parsing matters so much is that basically due to the way TinyDB works,
- 26:43every time you do, every time you interact with the database, you have to read the database file from the file system.
- 26:54So you really need a fast JSON parser.
- 26:57And the JSON parser that comes with Python is really powerful.
- 27:02You can extend it in a few different ways.
- 27:05But if you really need performance, this kind of extension really only gets in the way,
- 27:13because probably to implement it in Python, the whole parsing,
- 27:18and then you lose a lot of speed.
- 27:21So uJSON is one project I found, which also implements Python,
- 27:27I mean, which also implements JSON parsing,
- 27:29but it does it all in C.
- 27:32So it's really fast.
- 27:34You do have a trade-off that it's not as extensible.
- 27:37But if you don't need to overwrite some internals of the JSON parser,
- 27:45then it's really good to use uJSON to get more performance out of it.
- 27:50Yeah, that's really cool.
- 27:52And you can just pip install that.
- 27:53And you said that TinyDB will look for its existence and prefer it if it's there.
- 27:59But if it's not there, it's fine.
- 28:00It'll just use the standard library JSON, right?
- 28:03Yeah, so if you install it, it works like a drop-in.
- 28:08You just install it, and Python and TinyDB auto-detects it,
- 28:12and you get the speedup instantly.
- 28:16Yeah, nice.
- 28:17Let's talk about extensibility a little bit.
- 28:19So there was a couple of things I asked about,
- 28:23and you're like, well, I'm not sure it's really in there, maybe.
- 28:25But you've written a pretty cool framework for extending TinyDB.
- 28:31And so some of the things you can extend are like you could write a custom storage engine.
- 28:36You can write custom middleware, custom table classes.
- 28:40And then there's a bunch of projects that people have written that then you can plug into TinyDB to make it nicer, faster, etc.
- 28:49Yeah, extensibility was something I first started with having support for multiple storages.
- 28:58So you could swap out the JSON storage and use memory-only storage.
- 29:03I decided to provide even more extensibility so you can write a middleware,
- 29:09which modifies the way TinyDB works with any kind of storage.
- 29:15And also there are quite a bit of extensions out there you can use to modify TinyDB works in total.
- 29:24So there's a bunch of extensions, and one of them is TinyRecord.
- 29:28What's the story of TinyRecord?
- 29:30TinyRecord was basically written because TinyDB itself didn't have any real support for multi-threading.
- 29:39So with TinyRecord, you basically have some sort of transactions and also uses locking.
- 29:46So you can use TinyDB from multiple threads.
- 29:49And then you have an object where you can add a bunch of modifications you want to use.
- 29:57And yeah, basically it's transaction support for TinyDB, which is quite handy if you need multi-threading.
- 30:03It depends on your use case, of course.
- 30:05Yeah, of course.
- 30:05But if you need it, that is really cool.
- 30:07So another one that someone wrote, these are all separate projects on GitHub, by the way.
- 30:13Another one that somebody wrote is called TinyMongo.
- 30:16Yeah, TinyMongo.
- 30:17So does that like match the MongoDB API or something, but then actually store it as through TinyDB?
- 30:25Or what's the story with that?
- 30:26Yeah, TinyMongo, as far as I know, allows you to use an interface or an API which matches the MongoDB,
- 30:35API, but instead uses TinyDB.
- 30:38I think one day I just got an issue on GitHub where someone said, hey, I have this extension
- 30:45for TinyDB so it can replace MongoDB.
- 30:48And I included it into the list of extensions.
- 30:52Yeah, that's really cool.
- 30:54So it does look like it more or less just replaces the PyMongo API.
- 30:59So you've got like insert and find one and it even has like the dollar operators.
- 31:05So like dollar set and so on.
- 31:07Yeah.
- 31:08So one of the things that comes to mind for this for me is because TinyDB stores its files,
- 31:15at least by default, in a simple JSON document and there's no setup for it.
- 31:21If you were doing unit testing and you needed something better than just simple mocking
- 31:26and studying out your test data against MongoDB, it might be cool to switch it to this and use
- 31:33this as a way to kind of have a slightly better test backend for a real Mongo system or something
- 31:39like that.
- 31:40If you don't want to start up a separate server just for testing, that might be a really cool
- 31:46solution for that problem.
- 31:48Yeah, nice.
- 31:49So then there's a couple of others.
- 31:50One is TinyDB serialization.
- 31:53And I noticed that when I was playing around with TinyDB that I, for example, put like a
- 31:59date time in as part of my record.
- 32:02And of course, you can't, people may or may not know, you can't just go to the JSON module
- 32:07and go save this thing if it has a raw date time in it.
- 32:10It just, it doesn't support it.
- 32:11And that's what happened when I tried to save my thing in TinyDB.
- 32:16So does this address that kind of problem and other stuff?
- 32:18Yeah.
- 32:19I even think that touring date time objects was the reason why the extension came into
- 32:25existence.
- 32:26Because for TinyDB to tour other objects than dicts or for the JSON module really to support
- 32:35it, you have to modify it yourself.
- 32:40So the extension allows you to register your own serialization code.
- 32:45So you can store all kinds of objects that you want to store.
- 32:50And it allows you to specify how exactly it will be stored.
- 32:54I see.
- 32:54So I can, I can basically tell it like, hey, if you see a date time, serialize it like this.
- 32:58And if you see some other type that it doesn't necessarily know about, store it like that.
- 33:03Okay.
- 33:04Yeah.
- 33:04Yeah.
- 33:05Very nice.
- 33:05What about TinyDB SmartCache?
- 33:08It's also a really interesting project.
- 33:10I think it was created after a pull request or actually, yeah.
- 33:16So there was an addition to TinyDB because as I said before, searching can get really slow
- 33:24if you have a lot of objects.
- 33:27So what TinyDB does is that as long as you don't modify the database, it stores the results
- 33:34of your search queries.
- 33:35So it doesn't have to redo the work if you didn't change anything at all.
- 33:40So really handy in some cases.
- 33:43So if you do like a query and you give it some parameters and it comes back with 20 records,
- 33:49the SmartCache will say, if I see this query again, just give them those 20 records and more
- 33:54or less.
- 33:55Actually, TinyDB itself will already do that caching for you.
- 33:58So what SmartCache does is that if you basically takes it one step farther, if you search query,
- 34:06it stores the result.
- 34:09And then if you do some updates on the database, it doesn't throw away the search results, but
- 34:15instead replaces them or updates the cache with the new results.
- 34:21So if you have a query which matches some elements and if you insert a new element which also
- 34:27matches that query, it will go directly to the cache.
- 34:30So it doesn't have to redo the host searching, which TinyDB itself would have to do.
- 34:38But of course, it's also a trade-off because it uses more memory, because it has to show more results and also do more.
- 34:47There is some overhead on every insert and update because it has to check every query if it matches and update the cache in place.
- 34:57Yeah, but in some cases it can really be a really handy thing to use if you have a lot of updates and deletes in your code and you don't want to redo or to process all the entries in your database for every search.
- 35:13That might be an extension you want to use.
- 35:16Okay, yeah, that sounds really cool.
- 35:18Very nice.
- 35:20So let's also talk a little bit about the extensibility.
- 35:23So you said there's three basic places that we could extend TinyDB, and one of them is writing a custom storage engine.
- 35:33So by default, it's just a single, your data is more or less stored in a single JSON file.
- 35:40And that's more or less the connection string when you create the database, right?
- 35:44You say, here's the file.
- 35:45Yeah, you just pass the file name and it uses it.
- 35:49Yeah, nice.
- 35:50Again, very SQLite-like.
- 35:53But then you said you can create alternate storage engines.
- 35:58And this got me thinking about some ideas.
- 35:59But there are some that you talked about already, right?
- 36:02Yeah, for example, there is a storage which uses or just stores the data in memory.
- 36:10The first storage I wrote even was for YAML.
- 36:14So you have these YAML files later switched to JSON because it was faster, because YAML can get really complex in some cases.
- 36:23If you get really fancy, you could even do some kind of HTTP stuff.
- 36:29But I don't think that there is an extension for that yet.
- 36:33But that would be something really interesting to try.
- 36:36Yeah, absolutely.
- 36:37And so when I saw this, I was thinking, well, okay, maybe there are certain things you can do to leverage some of the shortcomings that you had talked about when you're like, maybe not so much from multiple process, stuff like that.
- 36:52So, for example, MongoDB recently switched from storing binary JSON in a variety of files to something called WiredTiger, which is apparently much faster.
- 37:03And they support multiple processes from there through certain types of locking and so on.
- 37:09I was thinking, hmm, could you take one of these storage engines and somehow, you know, with some work, plug it into TinyDB?
- 37:15Yeah, that should be possible.
- 37:16Yeah, that should be possible.
- 37:17Writing a custom storage is really easy because you just need to create a class with a constructor, which takes the parameters you need to create the storage.
- 37:29And then method for reading and one method for writing the data to the storage.
- 37:35So you could do some logging there.
- 37:38I think that would be an interesting thing to do, logging on storage level.
- 37:44So basically, then it has to wait until the database file itself is locked or unlocked to return the data.
- 37:53That would be interesting.
- 37:55It would be interesting, right?
- 37:56And so, I mean, it definitely seems like you could unlock some potential there as well.
- 38:01Okay, cool.
- 38:02So custom storage engines.
- 38:04And you could even do crazy stuff, right?
- 38:06Like you could say, I'd like to use an S3 bucket as my storage location or those types of things, right?
- 38:12Whatever you're looking at.
- 38:13As long as you can read it and write it, it should be possible to use it as a storage.
- 38:19Interesting.
- 38:20Okay.
- 38:20The other thing you talked about was custom table classes.
- 38:23What's that?
- 38:24Yeah, custom table classes mainly came into existence to modify the way the database implementation works.
- 38:32So it's basically in other cases you would have to over or to use a subclass.
- 38:39And so with custom table classes, you basically what you do is have a subclass of TinyDB.
- 38:47And then you can really do a lot of modifications.
- 38:51For example, the smart cache extension uses a custom table class to provide or to intercept every insert, update, and delete, and so on to add its own logic behind it.
- 39:04Okay.
- 39:05Could you use something like a custom table class to say, like, this field is required?
- 39:10Or this one must match like some kind of constraint, like it must be an email address or something like that?
- 39:16And then like not let it insert if you tried to save the wrong one?
- 39:20Yeah, that's definitely an interesting idea.
- 39:22Yeah.
- 39:22That would be a really interesting extension for our TinyDB validation because you can intercept all the in-source and updates.
- 39:33And then you could check if it matches and raise an exception if it's not valid.
- 39:38Yeah.
- 39:38Yeah, that's really nice.
- 39:40The other way, I guess, that we haven't really spoken about yet is middleware.
- 39:44So what kind of things can you do with regard to extensibility and middleware?
- 39:49Yeah.
- 39:49Middlewares are the third way to do modifications or to modify the way TinyDB works.
- 39:55Basically, it acts as a layer between TinyDB and the storage you pass to it.
- 40:00So you can do some interesting things over there.
- 40:04For example, in TinyDB, there is a caching middleware, which provides a way so it doesn't have to hit the file system on every read and write.
- 40:14Only every couple of reads and writes.
- 40:17So you don't have that overhead.
- 40:20And yeah, so you can do some modifications there, different ones from a custom storage.
- 40:27I think the main way in which middlewares are interesting, because you can use multiple of them at the same time.
- 40:34You can use it with any kind of storage behind it and do some modifications over there.
- 40:40Yeah.
- 40:41For example.
- 40:41Yeah.
- 40:42Okay.
- 40:43Very cool.
- 40:44So what's the future of TinyDB?
- 40:46Have you got anything you're adding or anything like that?
- 40:49I don't really have any big plans for TinyDB.
- 40:52I think right now I'm quite happy with how the project works and how the API is looking like.
- 40:59There might be some renaming of methods.
- 41:03So the intent of the method becomes clearer.
- 41:07But apart from that, right now I don't have really any big plans to change how it works.
- 41:13Because, I mean, the project itself is quite small.
- 41:16So there's not much I would want to add into the TinyDB core itself.
- 41:23But of course, there's still room for a lot of extensions to write.
- 41:28Yeah, that sounds great.
- 41:30Are you looking for people to create extensions or suggest changes or basically open source contributors?
- 41:36Yeah, I think writing extensions for TinyDB can lead to some really interesting projects.
- 41:43As we already talked about a couple of extensions.
- 41:46And apart from that, I'm always glad if people have improved the documentation.
- 41:51Because I'm not a native English speaker.
- 41:55So there might be a few rough places in the documentation.
- 41:59So if people think that the wording might need some improvement, I'm definitely happy to accept that pull request.
- 42:07And also, if you write an extension, just open an issue on the project on GitHub.
- 42:12And I will add it to the list.
- 42:14Okay, excellent.
- 42:15Well, I think it's a very cool project.
- 42:17I would love to see a nice, robust, embedded document database.
- 42:21So thanks for taking us a little closer to that world.
- 42:25Yeah.
- 42:26Awesome.
- 42:26All right, so before I let you go, I have two quick questions for you, as always.
- 42:30So first of all, you're welcome to name your own stuff if you want.
- 42:34What's your favorite PyPI package?
- 42:36I just noticed we now have over 90,000 PyPI packages.
- 42:40And so you must have some exposure to a few that are pretty interesting.
- 42:44Yeah, I think the package I would recommend is the one we already talked about, which is uJSON.
- 42:52Because in many cases, it can improve performance by a lot.
- 42:56Without, if you don't need to customize the way the JSON parser of Python works, then using uJSON can be really cool.
- 43:06Yeah, that's great.
- 43:07And so many people are processing JSON in one way or another these days.
- 43:11So it's very broadly applicable.
- 43:13A good choice.
- 43:14I'll throw in TinyDB for you as well.
- 43:17So pip install TinyDB.
- 43:18And how about your editor?
- 43:20If you're going to write some Python code, what do you open up?
- 43:22It depends on the project, really.
- 43:24For small projects, I probably use sublime text.
- 43:27But if it's more than two or three files, I probably fire up PyCharm and use it.
- 43:35Okay.
- 43:35Also because, yeah, that's an interesting development in Python with gradual typing.
- 43:40It's high-pins.
- 43:41Yeah.
- 43:41And PyCharm, as far as I know, already supports them to some extent.
- 43:49So it's really handy for that as well.
- 43:52Yeah, that's really cool.
- 43:53If I'm trying to understand some code and I'm like, oh, these three things that are coming in are these types.
- 43:59If you tell it, it'll give you a lot more assistance trying to understand something.
- 44:04If you're jumping into something you don't totally know the API for, that's cool.
- 44:07All right.
- 44:08Yeah.
- 44:08Awesome.
- 44:09PyCharm is a good one.
- 44:10So sublime text.
- 44:11All right.
- 44:11Final call to action.
- 44:12People should get out there.
- 44:14Write some extensions.
- 44:15What do you think?
- 44:15Yeah, definitely.
- 44:17Write some extensions.
- 44:18Write some extensions.
- 44:19Yeah.
- 44:20And if you're looking at the API, discuss the documentation.
- 44:21I'm always happy to hear feedback.
- 44:33And also if you find some difficulties or even just difficulties to get how to get started with TinyDB, I think that's worth of an issue on GitHub.
- 44:45So other people can improve if you or other people can have an easier way to get started if you run into problems.
- 44:53Yeah, just open an issue on GitHub and we can discuss things over there.
- 44:58All right.
- 44:59That sounds great.
- 45:00So, Marcus, thanks so much for being on the show.
- 45:03Yeah.
- 45:04Thank you for having me.
- 45:05Yeah.
- 45:05Bye.
- 45:06This has been another episode of Talk Python To Me.
- 45:10Today's guest has been Marcus Siemens.
- 45:12And this episode has been sponsored by Hired.
- 45:14Thank you for supporting the show.
- 45:16Hired wants to help you find your next big thing.
- 45:19Visit Hired.com slash Talk Python To Me to get five or more offers with salary and equity presented right up front and a special listener signing bonus of $2,000.
- 45:28Are you or a colleague trying to learn Python?
- 45:30Have you tried books and videos that just left you bored by covering topics point by point?
- 45:35Well, check out my online course, Python Jumpstart, by building 10 apps at talkpython.fm/course to experience a more engaging way to learn Python.
- 45:44And if you're looking for something a little more advanced, try my Write Pythonic code course at talkpython.fm/pythonic.
- 45:51You can find the links from this episode at talkpython.fm/episodes slash show slash 80.
- 45:57Be sure to subscribe to the show.
- 46:00Open your favorite podcatcher and search for Python.
- 46:02We should be right at the top.
- 46:03You can also find the iTunes feed at /itunes, Google Play feed at /play, and direct RSS feed at /rss on talkpython.fm.
- 46:13Our theme music is Developers, Developers, Developers by Corey Smith, who goes by Smix.
- 46:17Corey just recently started selling his tracks on iTunes, so I recommend you check it out at talkpython.fm/music.
- 46:24You can browse his tracks he has for sale on iTunes and listen to the full-length version of the theme song.
- 46:29This is your host, Michael Kennedy.
- 46:32Thanks so much for listening.
- 46:33I really appreciate it.
- 46:34Smix, let's get out of here.
- 46:41I'll see you next time.
- 46:58Open.
- 46:58you