Latest / Talk Python To Me / #197: Modern Python Standard Library Cookbook
Transcript
- 0:00A recent Twitter poll went around the web, and it asked,
- 0:02what percentage of the Python standard library do you think you know?
- 0:06Someone copied me on it, maybe expecting some really high percentage answer like 80-90%.
- 0:11In reality, what I did answer, and my rough estimate still is, that's probably around 50%.
- 0:16This episode with Alessandro Molina definitely helped confirm that estimate for me.
- 0:21He just published a book entitled Modern Python Standard Library Cookbook,
- 0:26and it's full of these great little corners of the standard library that you might not have bumped into,
- 0:30but you'll be super glad to hear about them on this episode.
- 0:33It's Talk Python To Me, episode 197, recorded January 10th, 2019.
- 0:38Welcome to Talk Python To Me, a weekly podcast on Python,
- 0:55the language, the libraries, the ecosystem, and the personalities.
- 0:58This is your host, Michael Kennedy.
- 1:00Follow me on Twitter, where I'm @mkennedy.
- 1:02Keep 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 is sponsored by Linode and Rollbar.
- 1:12Please check out what they're offering during their segments.
- 1:14It really helps support the show.
- 1:15Alessandro, welcome back to Talk Python.
- 1:18Hi, Michael. Thank you.
- 1:20Yeah, it's great to have you on the show again.
- 1:23It's been a long time since back on episode 35 when we talked about Turbogears,
- 1:28and you're back with a new project that I think is really, really cool,
- 1:32a look at the standard library through a modern Python lens,
- 1:37which I'm excited to dig into with you.
- 1:40But first, maybe just tell us, what have you been up to the last couple of years
- 1:44since we heard from you on the show?
- 1:45Yeah, actually, I've been mostly doing Python development as usual.
- 1:51That's my most prominent interest in open source development.
- 1:58I started a bunch of open source projects aside of Turbogears,
- 2:01but I'm still moving Turbogears forward,
- 2:05and we actually just released version 2.4,
- 2:08which had a major rewrite of the framework,
- 2:11which is great because we are trying to keep it modern.
- 2:14The standard library is doing a great job at that.
- 2:17Yeah, that's really cool.
- 2:18What are some of the other projects that you've been working on,
- 2:21the other open source ones you've released?
- 2:23One of the most interesting projects, I think, was DuckPy,
- 2:27which is, I will say, JavaScript execution environment for Python,
- 2:32because one of the problems that I faced
- 2:35was integrating the assets pipeline in web projects,
- 2:40and that always requires you to have Node.js involved
- 2:45and Webpack or tools like that.
- 2:48And I really wanted to have a solution
- 2:50that worked in the Python environment.
- 2:53Like you did pip install,
- 2:56and you got not only all the Python dependencies,
- 2:59but also the JavaScript dependencies
- 3:01and your ECMAScript compiled and things like that.
- 3:06That's pretty cool.
- 3:07So it's kind of like what people are using npm for
- 3:10on Python web apps to manage Bootstrap
- 3:13and AngularJS and stuff along those lines?
- 3:16Yeah, it's a suite of various tools, I would say,
- 3:19because it provides a JavaScript interpreter,
- 3:21which is used to run things like the TypeScript compiler
- 3:26or the Babel compiler.
- 3:29So to translate your JavaScript from more recent versions
- 3:34to the oldest ones,
- 3:36you don't need to install Node.js anymore.
- 3:38You can use DuckPy and have Python do that for you.
- 3:42And of course, it provides a command
- 3:46which is compatible with npm.
- 3:49So you can install npm packages from Python
- 3:53without having the need to have npm or Node.js.
- 3:56It's not super easy to explain
- 4:00because I think that people frequently get confused
- 4:03about its purpose when I try to explain it.
- 4:05But the one idea is that you can have,
- 4:08like, run just run your pip install
- 4:11and you can have your whole application compiled,
- 4:15JavaScript files compiled back to plain old JavaScript
- 4:19and all your dependencies installed,
- 4:22even if they are from JavaScript
- 4:24instead of being from Python and things like that.
- 4:26So it makes your life a lot easier
- 4:28because you don't have to care
- 4:30about maintaining two different packaging environments
- 4:33and more to different interpreters and things like that.
- 4:36I really like it.
- 4:37That sounds awesome.
- 4:38Cool.
- 4:38So let's maybe talk about this idea
- 4:41of the modern standard library.
- 4:43So, you know, there's a whole range of people listening
- 4:47who have different levels of experience with Python.
- 4:49Let's just start with what is the standard library?
- 4:52Standard library is practically everything
- 4:55that ships with Python itself.
- 4:57I mean, it's known that Python ships,
- 5:00but it is included.
- 5:02And the standard library have those batteries.
- 5:04Yeah, exactly.
- 5:06They're the batteries that come in the box
- 5:11when you get Python.
- 5:12You can get other batteries,
- 5:14but these are the built-in batteries
- 5:15or the ones that come included, right?
- 5:17Yeah, I would say that they have far more than batteries
- 5:19because there is tons of things that it can do for you.
- 5:23There is a lot inside the standard library.
- 5:26And I think it's also something that is not super frequently covered around
- 5:32in blog posts or things like that.
- 5:34We always tend to have, like, blog posts on the most recent new cool project
- 5:39or things like that that is outside of the standard library, of course,
- 5:43because things get included in the standard library after years they are around.
- 5:48But that's actually good because it means it's something you can really rely on.
- 5:53Absolutely.
- 5:53It's important that that has to stay there.
- 5:55And there's actually a huge bar for bringing things into the standard library.
- 5:59I recall a year or two ago,
- 6:01there was a debate about whether requests should be brought into the standard library
- 6:05to more or less supersede the built-in HTTP client capabilities.
- 6:11And they decided no, not because they felt requests wasn't good enough,
- 6:15but because requests was changing more quickly than the standard library could really facilitate, right?
- 6:21Because it's released with new features really every 18 months and things like that.
- 6:25Yeah, absolutely.
- 6:26That's one of the problems of the standard library,
- 6:29but also one of the reasons why it's very good.
- 6:31Because the things you have there, you can rely on them for the years to come.
- 6:37It was especially clear in the recent, in the last years,
- 6:43where Python changed very quick.
- 6:45We released many versions of Python in a very quick time.
- 6:49And it was clear that even if you are on Python 3.2,
- 6:54and you upgrade to Python 3.7,
- 6:5799% of what you use from the standard library is still there,
- 7:02works exactly like before, and you can be fine.
- 7:05Of course, between Python 2 and Python 3, there were major changes,
- 7:09but that was expected due to the B.
- 7:11That was intentional, right?
- 7:12That was like, okay, we finally have to just bite the bullet and make these changes.
- 7:16But other than that, you're right, it's really stable.
- 7:18There was some blog post or something about somebody wrote how they hated Python,
- 7:23and one of their reasons they claimed was,
- 7:25well, if you have 3.5 and you upgrade to 3.6, it might not work.
- 7:30I'm like, no, that's actually exactly how it works.
- 7:34And I'm really impressed with the stability of Python as it changes.
- 7:38I haven't seen any problems.
- 7:39The only problems I've encountered is I've used features too new on my dev machine,
- 7:44and then I pushed it to production where I didn't realize,
- 7:47oh, yeah, that feature is not yet on my server.
- 7:50So that's my own fault.
- 7:52Yeah.
- 7:52That happened to me from time to time too, of course.
- 7:56Yeah.
- 7:57Not often, but unfortunate when it does.
- 8:00So I think actually when people talk about how amazing Python is,
- 8:06or when they judge any programming language and they compare it against another programming language,
- 8:11they might compare the syntax.
- 8:13They might say, well, look how much easier like a foreign loop is in this language versus that language,
- 8:19or this exception handling block is cleaner than that exception handling block.
- 8:24But while that matters, I think actually what most people have in mind when they think about how they feel about a language
- 8:31is the standard library of the two languages and maybe the broader ecosystem as well.
- 8:35When I think of like, why is Python awesome?
- 8:38I don't think, well, because the way the language works with numbers is great.
- 8:42I think, well, I can import all these things and solve all these problems right away.
- 8:47And that's really the standard library, not the language, right?
- 8:49There are many things that are built in Python.
- 8:51They're like whenever you use the dictionary or list or things like that.
- 8:56They are built into the language itself.
- 8:59But every time you do an import, it means you are going to the standard library.
- 9:05Unless it's a package you installed explicitly, of course.
- 9:08Of course.
- 9:09So let's talk about your book.
- 9:10And your book is called the Modern Standard Library, Python Modern Standard Library Cookbook, right?
- 9:18And I like these cookbook ideas because you're like, well, I'm trying to solve some networking problem.
- 9:23Oh, here's two little recipes that I can use to solve that problem.
- 9:27So let's just start with what you mean by modern in the standard library cookbook here.
- 9:34And then we've selected a handful of specific recipes that we'll talk about that are pretty fun.
- 9:39Yeah, that's the modern part was the hardest part for me to find out to best decline in the implementation of the book, I would say.
- 9:50Because it's, of course, modern because it covers Python 3.
- 9:55And most of the things that are there are Python 3 specific.
- 10:00But I didn't want to go for, like, the only latest Python version.
- 10:04At the time where I started writing the book, 3.6 was just released.
- 10:09So we are now at 3.7.
- 10:12And it's probably going to happen pretty soon, 3.8.
- 10:16So I didn't want to follow the most recent thing because I know that in the real world, on your job, you are probably not going to be allowed to upgrade Python every single time when your release happens.
- 10:30So you probably are going to need the recipes that you can apply on your daily life on version of Python, which are modern, but not the most recent one.
- 10:43So I will say that I tried to think to things that work on Python 3.5.
- 10:48And I think that that's actually clear on some recipes because there are different ways to do those things in, like, more recent Python 3.6 or 3.7.
- 11:00But I still went for the way that you can do that in the works with Python 3.5 and the subsequent version.
- 11:11So I tried to balance between being modern and covering as many users as possible.
- 11:17You know, that's always...
- 11:19Yeah, it's a tough balance to strike.
- 11:21But I do think the choice of targeting Python 3.5 is pretty good.
- 11:24Like, really, if you're using Python 3, most people are on 3.5 or higher at this point.
- 11:30And so, I mean, you do give up a few cool things.
- 11:33You give up data classes and you give up f-strings and a couple of other things that would be really nice to mix in here.
- 11:39But at the same time, it's a little more timeless, a little more broad this way.
- 11:44All right, well, let's just talk about some of the recipes you have in here.
- 11:47So we pulled out some of the more interesting ones.
- 11:50You know, what I liked about going through these in your book was a lot of times I'm like,
- 11:55oh, I didn't know that class or that function existed and did this.
- 12:00And whenever I'm surprised like that, I'm like, oh, you can send emails out of the logging framework?
- 12:05That's pretty awesome.
- 12:07I didn't know that, right?
- 12:08So I think these will be pretty interesting to folks.
- 12:12And let's just start with that, like reporting errors in production.
- 12:14So I guess we should maybe frame this a little bit.
- 12:18There's lots of ways to report errors in production or do the other things we're going to talk about
- 12:22if you depend on some external library, right?
- 12:25But the goal of your book is how much awesome stuff can you do without installing or depending on other libraries,
- 12:31unless you absolutely have to, right?
- 12:33Yeah, absolutely.
- 12:34I think that you say that the right thing.
- 12:37One of the reasons why I wanted to start this book is exactly because there are a lot of things that people don't know that are available in the standard library,
- 12:47or they don't know that they can easily be changed to work in a different way or things like that.
- 12:54So like, for example, you mentioned the logging module.
- 12:57And we probably all know that it exists.
- 13:01And many of us are using it on the daily world to log messages on things like that.
- 13:07But the interesting thing is that it has many different handlers.
- 13:12So you can send the output of what you are logging in many different places.
- 13:16And one of those handlers actually sends the output to an SMTP server, so by mail.
- 13:23So you could send your logging messages by mail.
- 13:28Right.
- 13:28So you've got a logging.handlers.smtphandler class, right?
- 13:32Yeah.
- 13:32You just plug that in.
- 13:33Yeah, it's built in.
- 13:34Exactly.
- 13:35And, of course, sending all your logged messages as in an email doesn't make too much sense because it will make your life very hard.
- 13:44But for some specific messages, it might make sense.
- 13:48And as you can configure the logging method to filter only some messages, like in this case, we are talking about logging errors, so logging exceptions.
- 13:57You can use the logger to only report exception by mail.
- 14:02So every time an exception happens in your code, you get notified and you are aware.
- 14:07You don't have to wait for the user to come to you and complain that the software is not working.
- 14:11You can answer, I already know.
- 14:13I've already fixed it.
- 14:14Yeah, and that's really cool.
- 14:15And in your example, you do this a lot, actually, and I like this, is you create decorators that you can use to, say, decorate a method and say if there's an error here, you know, exception here, email it to us and things like that, right?
- 14:31Yes, exactly.
- 14:32In the recipe, it explains how to achieve that with decorator, which usually you will throw the decorator at the main function of your program so that every exception that happens is reported to your user.
- 14:46But if you are, for example, writing a web application, you might want to decorate the whiskey main callable because the main application is actually the application server, not your own code.
- 14:58But the core idea is that if you apply that decorator to the beginning of your code base, then everything that crashes within your code base will be reported to you and you'll know when the program fails.
- 15:15This portion of Talk Python To Me is brought to you by Linode.
- 15:18Are you looking for hosting that's fast, simple, and incredibly affordable?
- 15:22Well, look past that bookstore and check out Linode at talkpython.fm/Linode.
- 15:27That's L-I-N-O-D-E.
- 15:29Plans start at just $5 a month for a dedicated server with a gig of RAM.
- 15:34They have 10 data centers across the globe, so no matter where you are or where your users are, there's a data center for you.
- 15:39Whether you want to run a Python web app, host a private Git server, or just a file server, you'll get native SSDs on all the machines, a newly upgraded 200 gigabit network, 24-7 friendly support, even on holidays, and a seven-day money-back guarantee.
- 15:54Need a little help with your infrastructure?
- 15:55They even offer professional services to help you with architecture, migrations, and more.
- 16:00Do you want a dedicated server for free for the next four months?
- 16:03Just visit talkpython.fm/Linode.
- 16:06It looks like you could probably take this idea and extend it.
- 16:11Like, if you wrote a web service of some sort that you could call and report your errors, maybe log it in your own database, you could create your own custom handler, stick it into the login framework, and do the same thing.
- 16:23Instead of sending mail, it logs it remotely to your service, which goes in your database for reporting and whatnot, right?
- 16:28Yeah, absolutely.
- 16:29You can actually write your own custom handler and log the messages and the exceptions wherever you prefer.
- 16:36But I think that, as usual, most of the modules available in the standard library are very good when you have an average, medium level of complexity of your needs.
- 16:51Because when you're using the standard library, it's a great way to report your errors.
- 17:11So, you might want to switch to a national external service that is dedicated to that.
- 17:32Sure, but maybe you have some level of data protection or you don't want to share your tracebacks and all that kind of stuff, right?
- 17:40So, you might want to keep it custom for privacy or IP reasons.
- 17:45All right.
- 17:45So, that's really cool.
- 17:46And just to give people a sense, this is like three or four pages in the book.
- 17:49It's not a huge, long chapter on it, right?
- 17:51These are really quick little things out of many, many recipes.
- 17:55So, that's very cool.
- 17:56Another one has to do with temporary objects.
- 18:01So, what people often do is if they've got to load up a bunch of arbitrary things, they'll stuff them in a dictionary, put the values in there, and pass them around.
- 18:10But those don't behave like objects.
- 18:13I can't say container.value.
- 18:15I have to say container.bracket value or container.get quote value, things like that, right?
- 18:21So, it kind of breaks this idea of these truly flexible objects that you can just pass around in Python, right?
- 18:28Yeah.
- 18:29That's surprisingly one of the most frequent complaints that I hear from, like, new Python users.
- 18:37They're still not well-versed in the language.
- 18:40Maybe they are approaching Python for the first time.
- 18:43And they frequently came to me saying, hey, but I could do these in maybe JavaScript when I can just declare an object on the fly and assign attributes.
- 18:52Yeah, probably a C++ and a C# and a Java developer wouldn't have this complaint.
- 18:57No, yeah.
- 18:58Right?
- 18:58Yeah, absolutely.
- 19:00And actually, in Python, that's not hard at all.
- 19:05You can do the same exact thing in Python.
- 19:07And there is a fast way to implement an object that allows you to do the same exact thing.
- 19:15You can implement your own bunch objects, which are able to store anything in a dictionary.
- 19:21Because in the end, it is nearly the same as a standard Python object works.
- 19:27Because we store attributes in a dictionary.
- 19:31But you can access them to the dot notation.
- 19:34So by saying object dot whatever attribute you want.
- 19:38One of the really interesting things is that if you don't have too many, too much problems of using a model that is made for doing totally different things,
- 19:48you can even use the arc parse dot namespace class, which does exactly that.
- 19:54But it won't be immediately obvious to other people that read your code what are you doing and why you import a namespace for parsing arguments.
- 20:02But it allows you to accept any property to the dot notation.
- 20:07Yeah.
- 20:08So in here, you define a class.
- 20:09You call it a bunch, which is like just an object that can be extended with arbitrary values.
- 20:15And the trick is to have it derived from dictionary, have it implement dunder get attribute and dunder set adder.
- 20:22And then it just reads from and writes to the internal dictionary and converts key errors to attribute errors.
- 20:29And you have these little anonymous objects you can just create.
- 20:32Yes, exactly.
- 20:33And one interesting benefit is also that it inherits from a dictionary.
- 20:38You can set all the attributes at the beginning in a single step if you want, because you can just provide them as arguments to the dictionary.
- 20:45Right.
- 20:46It basically has like a built-in keyword argument initializer you could just use, right?
- 20:52Yeah.
- 20:52Which is cool.
- 20:53The one trick that I thought was nice, I had seen the get attribute and set adder before.
- 20:57But one of the problems is if you go and ask what type it is, you know, type of or type, parentheses, the thing.
- 21:05Yeah.
- 21:05It'll always say bunch, right?
- 21:07But you showed a way to like extend this just a little bit so it'll actually report whatever name you want it to like in a traceback or in a, you know, rep or things like that.
- 21:17Yes, you can extend the bunch to report the name you need so that when you receive a traceback or something like that, as you pointed out, you'll read that it's actually a user, for example, and not just a bunch that you never know what's contained.
- 21:32And you can actually extend that even further to pass type checks.
- 21:36If you are, for example, trying to use them to emulate some other object or things like that, you can actually extend it to pass the type checks for the other objects.
- 21:46So it can be pretty flexible and convenient.
- 21:49Yeah, I really like it.
- 21:50So what are some of the use cases where you might choose to use that over a dictionary or over a custom, more fixed structure class or something like that?
- 21:59Well, for example, the bunch is something that I use frequently when experimenting, when writing prototypes or things like that.
- 22:06I don't want to go and declare the whole hierarchy of classes before I even have clear the idea of what I'm trying to do.
- 22:12So I frequently end up relying on bunch for that purpose.
- 22:18And also, for example, in TurboGear itself, they are used to keep around some user-provided values that you don't know what the user is going to store there.
- 22:28You don't know what the user is going to need to keep around and things like that.
- 22:32And you want to allow the user a quick access to those attributes without having to look up in addition or things like that.
- 22:40So it's generally very convenient when you really don't know yet what you are going to store in that object.
- 22:47Right.
- 22:47Or it's determined at runtime in bizarre ways, right?
- 22:50Like, you know, I think of like a CSV file.
- 22:53And maybe that's some common stuff.
- 22:57But other things you don't know if they're there and you just want to put it all, you know, load it all up, right?
- 23:00You just gave me a pretty interesting idea, actually.
- 23:03It would be easy to extend anything that loads additionally, like a JSON parser, for example, to output bunches.
- 23:12So you can access the properties in a normal way instead of having to look up in addition.
- 23:17I like it.
- 23:18And, you know, we'll talk, you know, at least in your book, you cover like default values and avoiding all the tests, you know, is this key in this dictionary?
- 23:27And you could probably combine those to come up with a pretty clean API for interacting with data exchange of all sorts.
- 23:34Yeah.
- 23:34Cool.
- 23:35So one of the next ones that you talked about is templating.
- 23:40Now, Python has lots of templating outside the standard library.
- 23:44We've got Jinja 2.
- 23:45We've got Chameleon templates.
- 23:47You can, like, write your own if you really want to go crazy.
- 23:51But all those are both external packages with many dependencies and often just found in the web and things like that.
- 23:57But what I was surprised to see is you can actually extend what string.format means with your own implementation and sort of create your own little mini templating language like Jinja 2 or something.
- 24:10Yes, absolutely.
- 24:11That's actually one of the recipes that I used for many years.
- 24:16And now I was not sure about which way to propose the best implementation because there are new ways you can achieve the same result in Python 3.6.
- 24:28For example, to the f-strings, you can actually write code in an f-string itself and it would be evaluated.
- 24:35But for the previous version of Python, that was not available.
- 24:41And also the kind of code that you can write in f-string is limited to expression so that you can evaluate.
- 24:47While it's not really suggested, but by subclassing the string formatter and specifying it, you can actually make it able to run any kind of code that you provided in the template.
- 25:00Like even define a function and call it from your template and things like that.
- 25:05The trick is interesting because I didn't know myself up to like recent years that the string formatter can actually, whenever you write something between the brackets, it calls the method to look up that value.
- 25:20So you can actually write anything within the brackets and it will always ask to that method, what's the value of this thing that the user brought within the brackets.
- 25:31And at that point, by subclassing it and driving that method, you can make anything happen.
- 25:37Like you can even take code that was written within the brackets and run it and put back in place of the brackets the result of the code execution and things like that.
- 25:48Yeah, that's a really great trick.
- 25:50So you basically have a little miniature template language just in a string and you just say format, here are the values and boom, it comes out just like you would.
- 26:00And you even have what is effectively like a loop, right?
- 26:04You give it a list of messages and it can basically iterate over them using string.join.
- 26:10Yeah, so in the specific example that I did in the book for the recipe, it tries to showcase that you can even achieve loops because you actually have list compressions or you can have join or depends on the kind of output that you need to do.
- 26:27But you can run a list compression within the template or you can run a join within the template and so output a result for multiple entries that depend on the container that you provided.
- 26:43It's really cool.
- 26:43I think this is a great example.
- 26:44And so often you see people sort of imperatively building up strings in code and it's just, it doesn't seem like a great way to do it.
- 26:53It can't be great for performance.
- 26:54It's not clear really what the output is supposed to be sometimes.
- 26:58And this way I think is a lot nicer.
- 27:00So quite cool.
- 27:01Yeah, of course, because you can have the wall, you can have a better idea of the wall output that you are going to generate because you see it on the fly instead of having to go through tons of if cases and string concatenations and things like that.
- 27:15You could have basically a, you know, a multi-line string with more or less the exact shape of what's going to be outputted.
- 27:21Yeah.
- 27:21It's just the place is going to be filled in.
- 27:23It's really nice.
- 27:24Much like a chameleon template or something like that, right?
- 27:27A simplified version.
- 27:28It's not as powerful.
- 27:30Yeah.
- 27:30But it's already can satisfy most of the needs that you might have.
- 27:34Yeah.
- 27:34And, you know, this is one of those cases where I'm like, I had no idea you could do this.
- 27:38This is quite cool.
- 27:39There was a conversation on Twitter recently where somebody sent out a poll and said, what percentage of the standard library do you think you know?
- 27:48And it was, you know, zero to 20, 20 to 40%, 40 to 60, and so on.
- 27:54And they copied me thinking, I don't know, maybe they're thinking I would check like, oh, yeah, like 90%.
- 27:58But it's because of this stuff.
- 28:00I'm thinking like 40% is the right answer.
- 28:03Because I know how to do the really common stuff super well.
- 28:05But there's all these little extra amazing things that it's like, I didn't even know that existed.
- 28:10And so, for example.
- 28:12Yeah, I totally agree with you.
- 28:13Maybe I'll ask you, what do you think?
- 28:14What percentage of the standard library do you think you know?
- 28:16And after writing this book, maybe that number went up a lot.
- 28:18Oh, yeah, absolutely.
- 28:20I think that that's a really hard question because I believe I probably don't go over the 70%, you know?
- 28:28And I wrote a book about it because there are so many things that are hidden inside.
- 28:33There's probably not even people that wrote the standard library know every way you can use it, you know?
- 28:40Yeah.
- 28:40It's pretty incredible.
- 28:41Anyway, back to the topics.
- 28:43This next one also falls into that, oh, that is so cool.
- 28:47I didn't realize this was around for us.
- 28:50So I know about working with memory.
- 28:52I know about working with files.
- 28:54But you have this cool example of I would like to basically cache something in memory or load it into memory.
- 29:01But if it gets too big, I need to switch what I'm doing, maybe save it to a file and start reading it from there.
- 29:07Because, well, if it's 20 gigs, that's probably not going to work out super well most of the time, right?
- 29:12So tell us about this.
- 29:13So that's one of my favorite tools in the standard library.
- 29:17When I need to, like, keep around some data, temporary data, maybe a file that the user uploaded to me or something,
- 29:26or something I'm generating, like think of an image you are reciting or things like that.
- 29:31It makes sense to do that in memory because it's the fastest option, so it's usually the best way to go.
- 29:39But at a certain point, if you really don't know the size of the input that you are going to receive,
- 29:45if you are reciting, like, 300 pixels image, it can fit in the memory of every computer.
- 29:52But if you are reciting 5 gigabytes of JPEG, it can start to be a big problem.
- 29:58And that's the point where having something, a data structure that automatically switches from memory to disk
- 30:06on a specified threshold can be very convenient.
- 30:09And that's exactly what the temporary file spooled temporary file class does.
- 30:15You create one, and at the beginning, everything is in memory up to a point where it grows so big
- 30:22that it goes over a threshold that you choose.
- 30:25And when the threshold is surpassed, everything switches to disk, and you don't consume memory anymore.
- 30:30That is so cool.
- 30:31So this is the spooled temporary file, and you just give it a maximum size, and you work with it like a regular file.
- 30:39You can write from it, seek on it, read from it.
- 30:42And either it stays in memory, or if it turns out that it was got to be too big,
- 30:47then it just, you know, writes itself to disk and streams off the disk.
- 30:50That's cool.
- 30:51Yeah, absolutely.
- 30:52Because I frequently saw this pattern implemented by many projects, like use byte.io if the size is small,
- 31:00and switch to temporary file if it's big.
- 31:03But it can actually be done for you without having to write any code if you use a spooled temporary file.
- 31:08This is cool.
- 31:09To me, it feels like that's something you see a lot in Python when people come from another language
- 31:15or some other technology where they're like, oh, I need to do this thing, so I'm going to implement it from scratch
- 31:21when it could just be temp file dots, well, temporary file.
- 31:25You don't need to implement it.
- 31:26It's done, and it's already tested, and it's fast, right?
- 31:29I think that's part of knowing the standard library well, right?
- 31:33Yeah, and it cannot only make your life easier because it's, of course, probably more tested
- 31:39and been around for years and more robust and things like that.
- 31:43But it can usually be actually faster because many parts of the standard library are implemented in C,
- 31:50so they can be far faster than the code that you've wrote yourself in Python.
- 31:55So it's usually a very good idea to look up into the standard library before trying to write something from scratch.
- 32:03That touches on this idea of is Python fast or slow?
- 32:07And I feel often that the answer is both or either.
- 32:10Something like this, right?
- 32:12Like you can write code that just runs in pure Python, and it can be very inefficient.
- 32:17But as soon as you work with something that just hands off something down to a C layer,
- 32:22either that's in the standard library in CPython or if it's in, say, NumPy or something like that,
- 32:28like all of a sudden that whole conversation changes.
- 32:31And this is a little bit of like you may pick up that advantage automatically
- 32:35by just using the standard library stuff better.
- 32:38Yeah, I think that it's the only, if not one of the few languages,
- 32:42where the pattern on performance is reversed.
- 32:46The farther away you stay from the machine, the faster it goes, you know?
- 32:51Yes.
- 32:52The more you try to work with low-level data, the slower it will be.
- 32:56The more you try to work with higher-level data and functions, the faster it will go.
- 33:01That's a really good perspective.
- 33:02I like it.
- 33:05This portion of Talk Python To Me is brought to you by Rollbar.
- 33:08Got a question for you.
- 33:09Have you been outsourcing your bug discovery to your users?
- 33:12Have you been making them send you bug reports?
- 33:15You know there's two problems with that.
- 33:16You can't discover all the bugs this way.
- 33:18And some users don't bother reporting bugs at all.
- 33:21They just leave, sometimes forever.
- 33:23The best software teams practice proactive error monitoring.
- 33:27They detect all the errors in their production apps and services in real time and debug important
- 33:32errors in minutes or hours, sometimes before users even notice.
- 33:35Teams from companies like Twilio, Instacart, and CircleCI use Rollbar to do this.
- 33:40With Rollbar, you get a real-time feed of all the errors so you know exactly what's broken
- 33:46in production.
- 33:46And Rollbar automatically collects all the relevant data and metadata you need to debug
- 33:52the errors so you don't have to sift through logs.
- 33:53If you aren't using Rollbar yet, they have a special offer for you, and it's really awesome.
- 33:57Sign up and install Rollbar at talkpython.fm/Rollbar, and Rollbar will send you a $100
- 34:04gift card to use at the Open Collective, where you can donate to any of the 900-plus projects
- 34:10listed under the Open Source Collective or to the Women Who Code organization.
- 34:14Get notified of errors in real time and make a difference in open source.
- 34:18Visit talkpython.fm/Rollbar today.
- 34:20The next pattern that you talk about is with displaying progress bars.
- 34:27And for all the Python developers, you know, think when you type pip install a thing and
- 34:31you see the little downloading progress bar going across in the terminal or command prompt,
- 34:35like those kinds of progress bars, right, on the terminal.
- 34:38That's a very common need.
- 34:40And actually, I came up with this recipe because I wanted to showcase a more general need that
- 34:47frequently you have when writing terminal text-based software, which is you really don't know the
- 34:53environment where you are going to run.
- 34:55You know, maybe my user has a terminal which is very small or maybe a full screen or things
- 35:02like that.
- 35:02So when you try to provide your output, it's always hard to find the right balance of how
- 35:08much I should write, how much I should write on a single line, when should I go to a new line,
- 35:13and things like that.
- 35:14And the progress bar is a perfect example for that because it should always consume as much
- 35:20space as available and write the progress on top of that, you know.
- 35:26So it was a perfect case to showcase that in the final library, you have tools that allow
- 35:33you to inspect the terminal, understand what's the size.
- 35:36And we very, like, I don't remember the specific length of the recipe, but I think we are talking
- 35:42about, like, less than 10 lines of code.
- 35:45You can write a fully functional progress bar that fits and adapts to the size of the screen
- 35:50and things like that.
- 35:51Yeah, it's a great little example.
- 35:53And of course, there are things like TQDM if you want to go outside, right, which is a
- 35:58cool progress bar.
- 35:59But that's not the same as I have no dependencies and I still have a progress bar, which is pretty
- 36:04cool.
- 36:04Yeah, exactly.
- 36:05Yeah, if you want to cover all the cases, like things like TQDM are very cool when I use
- 36:11it in some projects, I think it works great.
- 36:14But, you know, one thing is using five lines of code function and the other is bringing
- 36:20in a whole library.
- 36:22For sure.
- 36:22One of the things I liked about this recipe is you used a lot of cool parts of Python.
- 36:28So you have a nice decorator that you put onto a function.
- 36:34And that function long as it is a generator that returns numbers that can drive the progress
- 36:41bar.
- 36:41So basically, you do your work and you're looping through it and the function can just periodically
- 36:47yield out a number, which is one to 100, which is the progress, right?
- 36:51That's a great pattern.
- 36:53Yeah, absolutely.
- 36:53I think it's very convenient compared to, like, the most common solution that I saw around
- 36:58when implementing progress bar is actually to go for a territory that you, like, throw up
- 37:05around the original data source.
- 37:07And at that point, it computes the site itself, but that requires that you already have all the
- 37:14data available.
- 37:15Like, I can apply that pattern to a list because I know that a list has five elements, so I can
- 37:21compute how much progress I should do for each element.
- 37:24But I cannot apply that to a generator because I might not know how much data is going to
- 37:29generate a generator.
- 37:30And with this pattern is the best balance, in my opinion, because you just yield the progress
- 37:37yourself.
- 37:38It doesn't matter what you are iterating.
- 37:39It can be in the network input.
- 37:42It can be on a generator.
- 37:43It can be on a list.
- 37:44It doesn't really matter.
- 37:45It's just the great you're financial with the progress bar decorator and yield the progress
- 37:50that you want to report.
- 37:51I really like it.
- 37:52And it's quite cool.
- 37:53So the next one has to do with the overall safety or consistency of a block of code.
- 38:03Now, a lot of times people think if they have a try and they have an accept catching block,
- 38:10they've handled the exception correctly and everything is fine.
- 38:15But that's sometimes true.
- 38:17But a lot of times there's an iterative or multi-step process where a bunch of changes
- 38:23have to be made and either all the changes should be applied or none of the changes should
- 38:27be applied.
- 38:27You know, think of a database transaction, right?
- 38:29That's very common.
- 38:30Either you get to the end or you commit it or roll it back.
- 38:32But that same thing applies in other persistent things.
- 38:36Even in memory, actually, if you're changing different parts in the memory of your app, you should
- 38:40consider this.
- 38:41But this next recipe has to deal with files.
- 38:44So if I'm going to be making multiple changes to a file or I want to make sure I write all
- 38:49the file or none of the file, how do I do that?
- 38:52That's actually a very good question because whenever you handle exceptions, it's obvious that you are
- 38:58handling the exception in your own code.
- 39:01But it's less obvious that there might be side effects from that exception.
- 39:05Like in the case you were writing a file and you fail at a certain point, maybe it's a deep space or something
- 39:12like that.
- 39:13What you've wrote so far, it's already there, was already written up to the point where the
- 39:20candle flashed the bright and so on.
- 39:23So you are not really handling the exception.
- 39:25Yeah.
- 39:26And even if it's not like you run out of disk space, it could be I would need to write 20
- 39:30things to a file in the 10th wall.
- 39:32Yeah.
- 39:33Something is none and I didn't expect it to be and it crashed.
- 39:36Right.
- 39:36So it sort of just bailed out halfway through.
- 39:39Right.
- 39:39Which is how do you know that's going to happen?
- 39:41How do you recover from it?
- 39:42The way you recover is actually not super complex and it can be made a very small tool.
- 39:49And in the book, there is this recipe which is called Safe Open.
- 39:54So that allows you to open a file in a safe way for writing.
- 39:59And whenever you Safe Open, all the writes that you do to the file happen all or nothing.
- 40:06So if everything succeeded, all the writes happen.
- 40:11If something fails, like you have an exception in the middle between three lines of code that
- 40:17draw two pieces, then nothing was written because everything is recovered to the previous
- 40:23phase.
- 40:23And that's actually done based on...
- 40:26I was just going to say, I love it.
- 40:27That is such a great thing.
- 40:28Like you write, you just write exactly the same code to write to a file, use Safe Open
- 40:34instead of Open.
- 40:35And then either the file doesn't even exist if there's an error or it's completely consistent
- 40:40at the end.
- 40:40And this, just to be clear, the Safe Open is a thing that you created in your recipe, but
- 40:45it's pretty straightforward, right?
- 40:46Yeah.
- 40:47It's like four lines of code or something like that.
- 40:50And it actually was like having a database transaction.
- 40:54You start the transaction and you can roll back on committees and the middle nothing happened.
- 40:59It's super cool.
- 41:00So basically the way it works is it writes to a temporary file and then it uses...
- 41:06It is itself a context manager.
- 41:08So you put it in a with block.
- 41:09If you exit the with block without error, then it will just rename the file to the real one,
- 41:15what you're targeting.
- 41:17If it exits with an error, it'll just remove the temporary file, which is perfect.
- 41:22It's so clean and nice.
- 41:22And that works because the rename operation is guaranteed to be atomic.
- 41:27So it's all or nothing.
- 41:29You replace the old file with the new one or you didn't.
- 41:32It's not like when writing that you could have written only part of the output.
- 41:37Yeah, it's super.
- 41:38Now, this other one is an interesting problem in a more complicated area, but still really good.
- 41:47So in Python, we have now, in modern Python, you have basically three ways to do concurrency, right?
- 41:54You've got asyncio, which actually uses only one thread and basically releases that thread to do other work
- 42:04while you're waiting on like a network or a database or something.
- 42:06So that's the asyncio and async and wait keywords.
- 42:09Then we have threads and we have multiprocessing.
- 42:13And the multiprocessing really exists to get around the limitation of the GIL, the global interpreter lock,
- 42:18for computational type stuff, right?
- 42:21Mostly.
- 42:21Yeah, absolutely.
- 42:22That's the only way you can currently work around the GIL and Python
- 42:26because all the other solutions that you mentioned suffer from that problem that you can actually run only one operation at a time,
- 42:36which is obvious in the case of async because that's the inner behavior of the feature itself.
- 42:43But it's less obvious for new users for threads because they expect to be really parallel.
- 42:48And that's the way frequently when talking about parallel operations in Python,
- 42:55multiprocessing is suggested because that way you can actually go concurrent for real.
- 43:02So the way you get around the GIL, the way multiprocessing works is in your code,
- 43:06you say, I want to run these functions with these 10 different values in parallel on different,
- 43:11you know, on different basically processes.
- 43:14And Python will create 10 sub processes or it can pull it up or whatever.
- 43:18But the point is there are multiple other Python sub processes doing the work.
- 43:23And then you get the answer back.
- 43:26But the challenge can be if I'm doing threading or asyncio,
- 43:29I can just have an object and share it and change it with all the different parts of my code.
- 43:33In multiprocessing, that memory is not shared by default.
- 43:37So then how do you interact with like if different parts of my multiprocessing thing are generating data
- 43:44and they need access to it?
- 43:45Like what do I do?
- 43:46Yeah, there are many tools in Python for doing that.
- 43:48But I think that the most powerful one is the multiprocessing manager.
- 43:52And it's also there are very few people that use it on every day and then know it exists and things like that.
- 43:59And even between those that know it exists,
- 44:02even less people know that you have some very cool features.
- 44:07Like you can actually replace a whole database system with multiprocessing manager
- 44:11because not only it allows you to share the values that you want across the processes that all forked from the same parent.
- 44:20So when like using writing a multiprocess program that forks multiple children to do some job.
- 44:27But it also allows you to share data across processes that have nothing in common.
- 44:33They were started on totally different times, different places, on different machines even.
- 44:40Because the way the multiprocessing manager works is that it actually allows you to listen on a port.
- 44:47So it works on a plain TCPIP network protocol.
- 44:51And you fetch and store values in the multiprocessing manager by picking them to this object and sending them across the network.
- 45:02So if you think about that, it's practically like having something like Redis or any other key value store in Python itself.
- 45:10It's already there.
- 45:11That's a great analogy.
- 45:12That's exactly what I was thinking.
- 45:13I'm like, this is like a little baby Redis that your process creates, right?
- 45:18Yeah, exactly.
- 45:19Yeah.
- 45:20It's pretty interesting.
- 45:21And the big benefit is the shared data can be changed and created over time.
- 45:28So, for example, the other things like queues and whatnot that you can use for multiprocessing, you have to create these values up front.
- 45:34And they can get more tricky, right?
- 45:36So this lets you start and stop processes and the values sort of persist across it and they can be created after by the subprocesses.
- 45:44It's like basically a little sub database or a key value store that is just live, right?
- 45:49It's like having a dictionary that is shared by all your processes and they can look for values there or new values there.
- 45:57And I think that it's very cool that it can be easily adapted to work across the network.
- 46:03So you can actually buy really multiprocessing tools and even distribute it across machine and continue to work using the same multiprocessing manager.
- 46:12Yeah, that's pretty interesting.
- 46:14So the next one that we're going to cover is a little bit in this realm.
- 46:18Like I want this thing that I know I could go create a real server for, but I just want to keep it nice and simple, right?
- 46:24We could go get real Redis, but now you have a big infrastructure thing instead of a single Python file, right?
- 46:30And similarly here, like obviously, you know, you have Turbogears, we've got Flask, Pyramid, Django, all these different frameworks that we could go.
- 46:39And they're big, huge dependencies, right?
- 46:42They have dependencies of dependencies of dependencies to run.
- 46:45But this next example shows you can actually create a non-trivial but somewhat basic WSGI HTTP server, right, in Python.
- 46:54Yes, absolutely.
- 46:55That's actually, in my opinion, one of the reasons why Python has so many web frameworks.
- 47:00You cited like four or five, but there are like, I don't know, a hundred web frameworks in Python or something like that.
- 47:08Yes, there's many more.
- 47:08We could go for a while.
- 47:09Yeah, exactly.
- 47:10And the reason, in my opinion, is that because you have so many building blocks already in the Python library that it takes no more than a weekend to create your own web framework, you know?
- 47:22And so when you start adding more complex features, of course, it gets far more complex than that.
- 47:28But for rolling out a framework framework that it's able to route requests to functions or classes and send them back, it takes no more than a few hours of work.
- 47:39And actually, if you already know how to do that, it's like a very few lines of code, like 10, 20 lines of code.
- 47:46You can achieve everything you need, including routing, requests, handling, serving responses, and everything.
- 47:53Yeah, that's interesting because when I thought of what built-in HTTP servers are there for the standard library, I was thinking, okay, so I can create up and just listen for an HTTP request.
- 48:07That's pretty easy with sockets and stuff.
- 48:09But the whole routing and all that kind of response stuff, I didn't realize was that easy to add on, right?
- 48:16Like, much of it is already built in and things like that.
- 48:20Yeah, absolutely.
- 48:20And not only, you don't even need to go as far as handling the requests yourself because actually there is a fully working application server in the standard library itself that by default works to single-threaders.
- 48:36So you will not be able to serve more than one request at a time.
- 48:40But there is actually another mix-in in the standard library that you can apply to that class to make it multi-threaded.
- 48:46So you get a fully functional multi-threaded application server with just one single line of code and only using what's available in the standard library.
- 48:56That's really cool.
- 48:57At that point, the only part you need to add on top of that is the routing, which is very easy to roll out using regular expressions lookup.
- 49:06Absolutely.
- 49:06And you cover that in this simple little one.
- 49:08I guess one other thing that it doesn't do is it doesn't serve static files necessarily, right, in this first example.
- 49:14But your very next recipe is, and here's how you serve static files.
- 49:17Yeah, absolutely.
- 49:18That's very easy.
- 49:19So you can actually be extended in just a bunch of lines, of course.
- 49:24Quite cool.
- 49:24All right.
- 49:25So another one that we might want to do in the same general area is creating HTML.
- 49:32So if you're doing basic stuff like this and you have any user input or you just are going to stick it into some other template system,
- 49:42basically if you are generating HTML, you need to be really careful about untrusted HTML.
- 49:49Like if you accept user input or the data is based on user input, they could, instead of putting their first name,
- 49:55they could put, you know, script, hackable JavaScript or other horrible stuff.
- 50:00And if you are not careful and escape it, it'll just go in and execute, right?
- 50:04That's one of the most boring parts of writing web application in the past,
- 50:09that you have to like to manually go and escape everything yourself.
- 50:13I saw, like I still remember those huge files of code where like every single string was wrapped in an escape code.
- 50:22And that's horrible because you have to do that for every single thing.
- 50:26And if you forget even one, that's a big security issue in your code, you know?
- 50:32It is.
- 50:32And it's not obvious, right?
- 50:33Because like you could have regular text or you could have like Unicode characters that mean the text,
- 50:40but they don't look like the text.
- 50:41There's just like all sorts of weird ways that people could try to sneak through.
- 50:45So you don't want to try to do that yourself.
- 50:47That's for sure.
- 50:48You don't want to have to care.
- 50:50I mean, that's something that I always thought.
- 50:52There should be some easy way that does that for me always unless I explicitly want to write some HTML in the output.
- 51:01And that's essentially what the recipe can do because, again, the string formatter can be extended in many different ways.
- 51:09And one of the ways you can modify it is actually by doing the escaping of everything you provide to the formatter.
- 51:17So whenever you're bidding in your string, all the variables that you inject in the string can be escaped for you.
- 51:24So you don't have to care.
- 51:25And so when you're writing your webpage output or your email output or whatever you're trying to send out to the whole world,
- 51:33you don't have to care about properly escaping everything yourself.
- 51:37The formatter will do that for you.
- 51:39Yeah, I really like it.
- 51:40You basically just use standard string format and wherever the input, the variable values go, they either get escaped or not escaped based on your pattern.
- 51:51You can say you can either mark them as like safe HTML because you want to dynamically generate them, but you need to stick it in there because it's your code.
- 51:59Or you're taking user input and that needs to definitely be escaped.
- 52:02Yeah.
- 52:03It's a cool pattern.
- 52:04Yeah, cool.
- 52:05All right.
- 52:05The very last one that we're going to talk about is tracing code.
- 52:10So understanding what code is executed.
- 52:13And you had an interesting comment about how it's not just useful for debugging, but it's also really interesting to just understand what a new library does.
- 52:24Like if you want to say, I'm going to run this function, what does it do?
- 52:27If it could actually show you the sequential Python that it executed, that'd be kind of neat, right?
- 52:32Yes, exactly.
- 52:32That's one way I most frequently end up using this recipe, actually.
- 52:37When I want to see what's going on in a new piece of code, the PDB debugger is not always the...
- 52:44It's very good when you have to pinpoint a specific point and understand what's happening right there at that moment.
- 52:52But when you want to get a general overview of what that whole package or library or set of functions is doing and how, where things go when you do something.
- 53:05So you want to follow the flow of the code itself.
- 53:08It's not as easy.
- 53:10You end up like spending hours just writing next into the PDB to show you where it goes next.
- 53:17Yeah.
- 53:18Yeah.
- 53:18And what this recipe can do for you is leverage what's available in the standard library to trace the code that Python executed and tell you the output of exit if each code was run and show you the source code that was executed so you can understand.
- 53:36Hey, I called this method.
- 53:37And by calling that method, I also ended up calling all these other methods and they executed these branches.
- 53:44And so that's why I got that answer.
- 53:47Now I see, now I understand how I ended up having that response.
- 53:51Yeah, I think it's great.
- 53:52And you could even see, oh, this library is actually calling into this other library.
- 53:56Yeah.
- 53:57And then maybe even why.
- 53:59Why is this a dependency?
- 54:00Oh, I see what it's doing.
- 54:01It's using it here.
- 54:02Yes, exactly.
- 54:03You end up discovering a lot.
- 54:05And it's like having something that goes through the source code for you.
- 54:11You know, whenever you want to understand a new library, you usually end up going to GitHub or something like that, open the source code and start reading, you know.
- 54:20And you see that here it calls that, so I go and look into the source code to where that function is implemented and so on.
- 54:27You do all this work yourself.
- 54:29But the tracing module can actually do that for you.
- 54:32And so it generates a single flow of everything that happens.
- 54:36I think that's really cool, a cool way to think of it, because when you open up somebody else's code that you've never seen before, you're like, all right, well, what is important?
- 54:44What isn't?
- 54:45I'm going to have to sort of sift through this and figure out, okay, it looks like this is where the action is, and I'm going to pay attention to this.
- 54:51And it's kind of a little bit of a detective job.
- 54:55Whereas this, it only will show you what executed.
- 54:58So you can kind of ignore all the other stuff and just see the part that it actually used.
- 55:03That's pretty cool.
- 55:03Yes, and you just have like to nearly decorate the function that you want to trace, and you will get the output printed.
- 55:10So it's very easy to apply.
- 55:12That's cool.
- 55:12So definitely, we're going to try playing with this as well.
- 55:15All right, well, those are the 10 recipes that we chose to talk about because we thought they were pretty cool.
- 55:22But there's a bunch of other ones, many, many more.
- 55:24How many are in the book?
- 55:25Do you know?
- 55:25I have to admit that I don't remember the exact number, but there are around like from 10 to 15 recipes for each chapter, and there are 15 chapters.
- 55:35So we are more than 100 for sure.
- 55:38Definitely more than 100.
- 55:39So there's a lot of these types of little things in here.
- 55:42And I think this is a really great book, and I'm happy to highlight it because, like I said, I learned a lot just going through this here.
- 55:49And I'm sure everyone who checks it out will learn even more because they'll go through all of them, not just the 10.
- 55:55Yeah, I really hope that at least a single person by reading the book will say, wow, I didn't know this.
- 56:01That was my whole purpose for the whole time I was writing the book.
- 56:05That's cool.
- 56:05I think after going through all of this book, I'm going to change my answer from 40 to 50% of how much of the standard library I know.
- 56:11It's cool.
- 56:14It's really great.
- 56:14I appreciate the topic.
- 56:15So let's leave it there for that.
- 56:17But I do have the two final questions for you, and I'm going to change it up just a little bit.
- 56:21So if you're going to write some Python code, what editor do you use?
- 56:24I usually use PyCharm for most of the big projects editing.
- 56:30And for small lacking around, I started using Visual Studio Code recently.
- 56:35Yeah, nice.
- 56:36That's exactly what I do.
- 56:38Yeah, that sounds strange when you tell people.
- 56:40They always look at you, Visual Studio.
- 56:42No, it's a pretty good editor.
- 56:44They are doing a really good job, and they're putting so much energy into the Python space these days.
- 56:50So, yeah, I think it's a great answer.
- 56:52Yeah.
- 56:53All right, that's the editor.
- 56:53And like I said, that's basically the same way that I am and using mine.
- 56:57Now, I would normally ask you a notable PyPI package, but let's mix it up and talk about a notable standard library module package that you want to just highlight.
- 57:06Okay, that's an interesting question.
- 57:08There are so many great modules within the standard library that it's really hard to pick one.
- 57:14But if I really have to pick a single module, I would say that the logging module is one of the most fascinating ones.
- 57:22Not because it can be the one that you will use most often or it's more feature-rich, but because there's so many ways it can be set up, configured.
- 57:34There are so many side effects on the things you do that you can go on learning about logging for years, and you will never know everything that the logging module can do, how it interacts, or the configuration format that it supports, and things like that.
- 57:48Yeah, that's a great answer.
- 57:49I totally agree with you on that, by the way, that just like you could learn it forever.
- 57:52You're never done learning it.
- 57:54Yeah.
- 57:55All right.
- 57:55So people are excited about standard library.
- 57:57How do they learn more?
- 57:59Final call action, what should they do?
- 58:00Oh, just go and read the Python source code.
- 58:03That's the way that I learned most things.
- 58:06You know, you just open the Python repository on GitHub and start reading modules whenever you want to see what's going on and things like that.
- 58:14That's really the best way, and it's worked a lot for me, because that's the only way you can actually see the hidden side effects or those functions that you use for years, importing them from the sign library, but you never really know why they're working that way.
- 58:30And that's the way you also discover that something like the formatter can be subclassed with its behavior in different ways or things like that.
- 58:39Because those more advanced views are not really documented because they are more in internal detail, I would say, of the standard library.
- 58:49But once you know that they exist, they've been there for years, and it's pretty safe to leverage them.
- 58:55Yeah, that's great.
- 58:56I definitely feel like there's a whole bunch of stuff to explore, and even more so after talking with you about this.
- 59:02So thanks for being on the show.
- 59:03Absolutely.
- 59:04Thank you for having me.
- 59:05You bet.
- 59:06Bye.
- 59:06Bye.
- 59:08This has been another episode of Talk Python To Me.
- 59:10Our guest on this episode was Alessandro Molina, and it's been brought to you by Linode and Rollbar.
- 59:16Linode is your go-to hosting for whatever you're building with Python.
- 59:20Get four months free at talkpython.fm/linode.
- 59:24That's L-I-N-O-D-E.
- 59:26Rollbar takes the pain out of errors.
- 59:28They give you the context and insight you need to quickly locate and fix errors that might have gone unnoticed, until users complain, of course.
- 59:36Track a ridiculous number of errors for free as Talk Python To Me listeners at talkpython.fm/rollbar.
- 59:42Want to level up your Python?
- 59:44If you're just getting started, try my Python Jumpstart by Building 10 Apps course.
- 59:49Or if you're looking for something more advanced, check out our new async course that digs into all the different types of async programming you can do in Python.
- 59:57And of course, if you're interested in more than one of these, be sure to check out our Everything Bundle.
- 1:00:02It's like a subscription that never expires.
- 1:00:04Be sure to subscribe to the show.
- 1:00:06Open your favorite podcatcher and search for Python.
- 1:00:09We should be right at the top.
- 1:00:10You can also find the iTunes feed at /itunes, the Google Play feed at /play, and the direct RSS feed at /rss on talkpython.fm.
- 1:00:19This is your host, Michael Kennedy.
- 1:00:21Thanks so much for listening.
- 1:00:22I really appreciate it.
- 1:00:23Now get out there and write some Python code.
- 1:00:26I really appreciate it.
- 1:00:46Thank you.