Many of you write to me and tell me how you appreciate the way my guests and I highlight a particular Python package at the end of each episode.
Well, if you enjoy that little segment, you're going to love this episode.
This week, you'll meet Caleb Hadding, who wrote a great book called 20 Python Libraries You Aren't Using But Should.
He and I spent an hour digging into all the very powerful and interesting packages you probably haven't heard of, but will be super excited to use after you learn about them.
This is Talk Python to Me, episode 77, recorded September 20th, 2016.
Welcome to Talk Python to Me, a weekly podcast on Python, the language, the libraries, the ecosystem, and the personalities.
This is your host, Michael Kennedy. Follow me on Twitter where I'm @mkennedy.
Keep up with the show and listen to past episodes at talkpython.fm and follow the show on Twitter via at Talk Python.
This episode is brought to you by Capital One and Intel.
Thank them both for sponsoring the show by checking out what they're offering during their segments.
Hey, everyone. I have a quick message for you before we get to Caleb and his book.
In addition to writing this book for O'Reilly, Caleb also wrote a screencast course on Cython.
And it looks to be one of the better Cython courses out there.
So when he's talking about Cython, if you're really interested in what he's up to, be sure to check out his course, which is linked in the show notes.
And O'Reilly agreed to give away a free copy of his course.
All you have to do to be eligible is be a friend of the show.
So be sure to visit talkpython.fm, click on friends of the show, enter your email address, and you'll be eligible to win.
Now, let's talk to Caleb.
Caleb, welcome to the show.
Hi, Michael. It's great to be here.
Yeah, I'm super excited to talk to you.
We've got some really cool stuff around a book, a free e-book that you did.
And I found it super interesting.
So I think everyone else will.
Basically, we're going to take the last question I always put at the end of my podcast.
What's your favorite PyPI package?
And turn that into an entire episode and just go deep on that idea, right?
Okay.
So before we get to that, though, let's start at the beginning.
Where do you get into programming and Python, that sort of thing?
Yeah, great question.
So one of the other podcasts I listen to is the C++ podcast.
And just about every guest on that show says that they started programming assembler in grade school.
That was not my story.
I didn't get into programming at all in school or high school.
I started really as a hobby in university while I was studying chemical engineering,
which is kind of an odd thing to do as a hobby when you're doing something completely different.
But as the years went on, I kind of got more and more into programming.
And it turned out that I did my master's degree in process control,
which is like a subset of chemical engineering.
And that was all in MATLAB.
So it was pretty much all a programmed course.
And that's really how I got into programming.
And I learned that I really did not like MATLAB very much at all.
Yeah, I spent some time there.
I can sympathize.
I don't love the .m files, no.
Yeah, I got to know it really, really well.
And yeah, I decided that I did not really want to carry that forward.
And it was really when I started working in the first or second year that I started working.
I started learning a couple of languages outside of work.
And the one that I really tried to focus on was Java.
And I signed up for a fairly expensive certification course.
I think Java was around 1.2, version 1.2 or 1.3 or something at the time.
And halfway through that course, I came across Andrew Kuchling's Python tutorial at the time,
which I think was for Python 1.5.
And it just blew my mind.
I kind of had the realization that I couldn't possibly use Java anymore to do the kind of work,
like data analysis work that I was doing, because it was so easy in Python.
It was just a complete waste of time to develop all of these object-oriented structures around
fairly simple data pipeline processing tasks.
Yeah, that makes a lot of sense.
I mean, Java has so much formality.
And maybe, let's say it's maybe good for large applications.
Maybe.
Exactly.
But it certainly doesn't make sense for small ones, right?
Like you're talking about.
Yeah, absolutely.
There's definitely a place for Java for very large programs.
But for the kind of things that I was doing, and especially for shorter programs involving data pipeline processing,
Java is just way more than what you need to get the job done.
And that was pretty much the end of Java for me.
I never finished that course.
I really got stuck into Python.
And this was around 2001.
So quite a few years ago.
And then I watched Python become Python 2.
And 2.4 was a big one for me.
I used that for quite a long time.
And so on.
So yeah, that's pretty much how I got into Python.
But along the way, I did use quite a few other languages fairly heavily.
Fortran I used quite a bit as well because large chunks of the scientific world still use Fortran.
I have written new Fortran 77 code.
I have added that to the world.
Oh my gosh.
That's awesome.
Yeah.
And Delphi I used for quite a few years.
My career has moved into and out of software development and into chemical engineering.
I've kind of straddled both worlds for the past 15 years or so.
And I did a stint as a software engineer in the hospitality industry for writing hotel administration software for about four years.
And that was heavily using Delphi, the IDE from what used to be Borland and then became Embarcadero.
So I got to know that language really, really well as well.
Pretty much as well as I know Python, I would say.
That's very interesting to me.
I kind of regard Delphi and Python as almost polar opposites in many ways.
A GUI is very easy in the one, not quite as easy in the other.
Deployment is extremely easy in the one.
Deployment is kind of difficult in the other.
And so on.
There are many parallels where I kind of see Delphi and Python as direct opposites.
Another good one is the GIL, the global interpreter lock, which I think is really fascinating.
In the Delphi world, for many years, one of the things that developers asked for over and over again was thread safety in the library.
Because it was one of the huge talking points.
They wanted the containers and the structures inside the library to be thread safe because it was too easy to get race conditions in threads because you could just spawn native threads and have them clobber each other's memory layer.
And it's really fascinating to me that the exact inverse argument gets made in the Python world.
That is funny.
The presence of thread safety is the problem because it slows your code down.
So, yeah, it's really interesting to be able to have a depth of knowledge in multiple programming communities because you kind of get a sense of maybe what is really important and what is spurious.
Everything is a set of trade-offs.
Yeah, exactly.
That's the thing.
That's what I was thinking.
Nothing is accidental.
Things are designed the way they are for good reasons.
And they may not always be the best fit for every particular situation.
Right.
You learn a language that maybe it's not obvious initially.
You don't know the history or whatever.
But there was probably some deep thought that went into at least all the popular programming languages.
Those things have evolved with lots of thought over time.
Absolutely.
Yeah.
Nice.
Okay.
Well, so a lot of Delphi, a lot of Python, a lot of scientific programming.
What are you doing with Python and programming today?
Like, what's your day job?
Good point.
So, I can give you a quick two-minute, well, one minute, let's say, run through of the things I've done.
So, I started in chemical engineering with doing a lot of simulation work using off-the-shelf tools for that.
And as time progressed, I started moving more towards the problems where there were no off-the-shelf tools.
And for those, you have to write code.
And I began using a lot of Fortran.
Then I started incorporating Python into that.
And then I took a break from engineering and went into software development where I used Delphi.
But I also used Python as well for web development.
Then I went back into engineering and I started writing simulation software for cold gasification, which is what brought me to Australia.
And what's interesting about that job was for the first time, I really decided to use Python for the entire system, which means all of the number crunching stuff as well.
And that's where Cython really came into the picture for me.
I made the decision to use a full Python stack for that simulation work because it seemed to me that Cython was mature enough, really, to be able to give you the speed that you need to solve these mathematical problems in the background.
And that definitely proved to be the case.
It was a good choice on my part.
Cython is not so much a break from Python, really, as an extension.
But it gives you all of the native speed and control over the memory layout that you need if you want to make fast code.
So I spent a couple of years doing that.
Yeah.
Would you say it's a little analogous to, like, inline assembler and C++ or something like that?
You're like, just if this one loop could be faster.
Let me just do this part fast.
I think I would disagree with that.
Okay.
I began with that in mind.
It's easy to look at it that way, but Cython is so much more than inline assembler.
There's another way to look at it, which is you can write.
You can get all the benefits of C by writing what looks like Python.
And in one or two places, you just add some types onto a couple of variables.
And you can get a hundredfold increase in speed.
So whereas inline assembler is much more of a niche application of a different technology.
Unless you can read assembler really easily, which I can't.
Yeah, neither can I.
Having inline assembler, yeah.
Where Cython is not like that, by and large, Cython is as easy to read as Python.
There are a couple of things that are different in the layout.
But overall, you would find Cython as easy to read as Python.
Okay.
Sounds good.
So back to what you're doing today.
Yeah.
So I've been working for the past couple of months on a contract for GPS tracking, which
has also been using a full Python stack.
And I was very lucky in that they were willing to go straight to Python 3.5.
So I've been writing async I.O. code in Python 3.5 since February, which I feel very fortunate
to have had that opportunity.
And yeah, I'm starting a new job tomorrow from the date of this recording, working for a company
called Console, which was formerly called IIX.
And yeah, I believe my title has something to do with network orchestration, which is going
to be a whole new thing again for me to learn.
That is quite a shift.
But that's really cool.
And Python, of course, plays a super important role in that space.
So it makes a lot of sense.
Excellent.
Yeah, absolutely.
One of the big benefits of Python, just as a technology choice, is that you can use it
just about every way.
Yeah, that actually is really important.
It's really important.
Yeah, so I think that's a wide range of background and experiences.
And it gives you a nice overview of the ecosystem and the standard library and all the different
ways that Python makes you efficient, productive, and so on.
And so you wrote a really cool book.
You wrote it for O'Reilly, right?
Yeah, that's right.
Yeah, as a free e-book, I think.
And it's called 20 Python Libraries You Aren't Using But Should.
And I thought it was a really nice survey.
Very BuzzFeed-y title.
You know what?
Anytime it starts with 10 or 20 or the seven things you should never say, you know it's
a BuzzFeed.
But it really is succinct and it lives up to the name.
It's good.
The idea for the book came from O'Reilly.
Susan Conant at O'Reilly suggested it to me and asked if I would be willing to write
the book.
So the title was established before I got to the project and then I provided the content.
Yeah, but the original working title was 10 Python Libraries You Aren't Using But Should.
And I couldn't stop at 10.
And in fact, if you count the major featured packages, there are 20.
But you'll see throughout the text of the document that I refer to a whole bunch of other libraries
as well in footnotes.
Yeah, I thought that was interesting.
Yeah, we'll talk about the 20 major ones and maybe even touch on some of the ones that you pull in.
Like, for example, one of the web service bits, the implementation of the web service uses a few
other libraries that we can talk about later, but they're not actually part of the 20, right?
So there's, I feel like you get a really good, well-rounded view of what's out there.
I was quite cautious about writing the book because it's a fairly contentious thing.
The choices, the technology choices that people make, many people can be quite passionate about
those things.
And the brief for the book was, we want you to focus on libraries that other people don't know
about yet.
So that means I have to leave things out.
And it also means that I have to leave things out that may be fairly popular, which means
there might be quite a widespread degree of support for libraries that I'm not going to
be mentioning.
So I was somewhat apprehensive about that.
So the libraries that I tried to focus on were things specifically that may not have much
exposure, which is a very interesting idea for the book.
I can't include things that are too niche that they could not be used for much, you know,
very low applicability.
But at the same time, I did not want to include things that were very well known because that
defeats the purpose of the book.
So I found it quite challenging to pitch it.
It's the ones you aren't using, not the ones that you are, right?
Exactly.
Yeah.
It is a challenging one to go, okay, let's strip off that.
Like if you say, what is the most popular library to do?
You almost have to say, okay, well, except for that one, what else can we do?
But I...
Yeah, exactly.
And I thought about that when I read the title.
I'm like, oh, is this going to be a bunch of niche things that are like second fiddle
to the stuff that you actually should be using?
But no, I think it was really good.
So maybe we could start by talking about the ones that everybody has installed already.
That's the stuff that comes in the standard library.
So you have, in the first chapter, you said, hey, look, there's a bunch of stuff you're
not using that's built in.
Developers with experience do tend to look in the standard library first because they've
been burned by carrying extra dependencies, which after a couple of years may not be as
well maintained.
The impression that I have is that more experienced developers tend to lean more heavily on the
standard library when choosing technology.
Even if some of the time there may be other third-party packages that might be a better
fit.
That's a decision that gets made.
That's really a trade-off.
And I do the same.
When I have to deploy an application to production and I know that this is a core service, I tend
to lean more heavily on choosing things out of the standard library when possible, as opposed
to adding third-party dependencies.
Whereas newer developers tend to get whatever is the latest and greatest on PyPy and run
with that.
So what seems to me to be the case is that more experienced developers have a much better
and deeper knowledge of what is available in the standard library.
So even though the book was intended to be focused on third-party libraries only, I did
want to squeeze in some of the absolute must-have, must-know standard library options, like the
collections package, which is the first section in this chapter.
If you watch any of Raymond Hittinger's talks, he plugs the collections module heavily, as
well he should, because it's awesome.
Yeah, I do feel strongly about that, that people really should know more about what's in the
standard library.
And my original version of the book had more of it in, but we decided that we wanted to focus
the book more on the third-party stuff.
So it got trimmed down.
Sure.
I think that makes a lot of sense.
Yeah, that makes a lot of sense.
Partly, I mean, there's nobody who is the advocate for the thing in the standard library.
It's just built in.
But when somebody makes their open source library, they set up some GitHub Pages thing, and they've
got some cool logo.
And, you know, like it's, there's somebody promoting it in a sense.
And so I can see that.
Yeah.
So a couple of the things that you talked about in the collection library, one of them, which
I think is pretty interesting and timely, is ordered dict.
Yeah.
So what's interesting about this section is that it may become redundant in December.
I don't know if you've been following the discussions on the Python dev mailing list, but the new
dictionary in Python 3.6, which I think the release date for the final version is December, the
dictionary is going to become ordered.
Whether that is going to be advertised as a requirement for the language spec, or whether
that's just going to be an implementation detail remains to be seen.
But yeah.
Sure.
Yeah.
And so every now and then you'll see people building special dictionaries for Python that
are ordered.
For example, the MongoDB library exchanges dictionaries at serialization, and the changing of order
causes more rights on the server for documents than if it doesn't.
And so they may went and created their own, and we've got this order dict.
So what's some of the problems you run into with like the regular dictionary?
Or like, why do you care about ordering, I guess?
The main use case for ordering that I've come across is usually when processing things that
are really mappings in a sequence, and they need to be processed in the sequence in which
they appear.
So I think the example I gave in the text is, yeah, and a common example is processing lines
in a file where the lines map to something else.
And you want to serialize them or persist them in some way, retaining the order that they appeared
in the original list.
Right, like maybe like a CSV you're going to load and you're going to say look up by
like some ID, which is a column.
If you write it back, you want to be able to save it the same order and not have to maintain
like two data structures or something.
Yeah.
Yeah, that's right.
Exactly.
Okay.
So there is right now in all the versions of Python, the collections.order dict, which is
a specialized dictionary that solves this problem.
It just so happens if you live right out at the very edge of new Python, you might not need
that in December, but a lot of people don't live there.
Right.
So I think it's still totally relevant.
Yes.
And from the discussions that I've been seeing on the Python dev mailing list, it probably
is going to remain in the library.
What the latest that I've seen is that the order of keyword arguments in function calls is
going to be guaranteed to be maintained.
But the requirement for normal dictionaries to be ordered may not be a specification of the
language spec, which means that other implementations of Python may not need to maintain that.
Right.
Okay.
So that's quite interesting.
One of the caveats that I mentioned about order dict towards the end of the section with
the big red triangle is beware creation with keyword arguments, which is exactly this problem.
When you create an order dict and you supply keyword arguments as you would with maybe a regular
dictionary, the problem is that the order is maintained with your specification because the
keyword arguments first get created as a regular dictionary before they get created as
an order dictionary.
And that's going to be changing for sure in 3.6.
Okay.
Oh, excellent.
That's good to know.
Yeah.
Because that happens at the call site before the order dict class ever gets any information.
It's just given a dictionary and it can do what it can do, but it's too late.
The order has already changed, right?
Yeah, exactly.
That's right.
And I think the other guarantee is that the dunder dict entry in classes is also going
to have a guarantee of the order being maintained.
Even though it's implemented as a regular dictionary, what the language spec requires and what actually
happens in practice are two different things.
So the developers of Python are trying to maintain the language spec as a spec even for other implementations
besides CPython, which is difficult to kind of keep in your head when all you work on is CPython,
which is largely the case for me.
But yeah, they're dealing with bigger problems than just whatever goes into CPython.
Yeah, that's an interesting thing to keep in mind because we often just think of CPython
equals Python language, but there's a lot of other implementations and extensions and forks
and whatnot.
Yeah, yeah, exactly.
Capital One has a special message for you.
They need Python pros who love to work with data.
Put your Python experience at work at Capital One and help them use data to make life better
for millions of customers.
Capital One is employing the latest tools and approaches to do data analytics and data science
from the ground up.
They're smart, creative professionals who love to explore new ways to interact with data.
They're interested in figuring out novel, advanced Python techniques and even more interested
in finding more people who will help them do that.
When you join their state-of-the-art Python community, you'll work with people you really
like, people who might be listening to this podcast right now.
Relentless innovation is their way of life.
Make it yours at Capital One.
Visit jobs.capitalone.com slash talkpython to learn more and apply today.
So another one I would say is also one of, if I had to pick the most useful thing to come
out of the collections library, I would say it's probably named tuple, which you highlight
in your book as well.
Yeah, yeah.
Named tuple.
Named tuple is kind of interesting.
I have recently started using it directly when creating tuple structures.
But most of my experience with named tuple really has been converting old code that used
regular tuples into using named tuples just to improve the maintainability aspect of that.
And it is very powerful in that respect.
Yeah, it doesn't change the performance much.
And it's an easy thing you can do because named tuples are compatible with the existing code,
but they definitely add a layer of maintainability, right?
So if you have a regular tuple and it's three things and you need to put some new item in the
middle to make it four, well, the code that was going, you know, T bracket two now is not
true, not accurate anymore, right?
But if you could refer to them by the names, the property names, that's fantastic, which
is what named tuples adds.
Great.
Yeah, that is a good one.
Yeah.
One that I've done a lot less with is Context Lib.
What's the story of that?
Ah, so what did you think of my example?
Just for the listeners, the example that I gave, the code snippet under Context Lib, is
creating a simple context manager that measures the time.
Well, it records the time before and after the execution of the body of the context manager
and then gives you a way to calculate the performance of that section.
I haven't gotten much feedback about the book yet because it is fairly new and I was curious
what your opinion was.
Well, I got to say, it did take me a moment of going back and let me look at this context
manager implementation.
It's just only three lines of code.
I can just, you know, basically the idea is you create a context manager instance by calling
this method and it will, when it enters, capture the start time.
When you leave the width block or suite, it captures the end time and then it tells you
how much time had passed.
And so the implementation is T equals get the perf counter.
T zero equals perf counter.
Yield a lambda, which does a computation and then compute the value that is actually used
in the lambda above.
And that, I was a little bit taken aback by that.
It was interesting.
Yeah.
I was worried that it was perhaps a little bit too complex.
And I didn't want to, the fact that the use of the lambda, I didn't want the use of the
lambda to overshadow the demonstration of how the context manager works.
But basically where the yield comes in is where the body of the context manager gets executed.
And if you return something from the yield, that's pretty much what you get at the end
of the line when you say with timing as thing.
The thing is what gets yielded out of the context manager.
And the little bit of cleverness in this particular example is that the lambda is a closure over
the namespace inside the timing function.
So it captures the storage location of T one and T zero.
So only when you evaluate the lambda later, do the values of T one and T zero actually
get used.
Yeah.
It's quite clever.
Yeah.
This particular example is not imaginary.
I use it quite a lot.
Yeah.
It's nice.
I appreciated it because it made me think and stop and not just read.
Yep.
Okay.
Yep.
Okay.
Oh, wait a minute.
Not necessarily.
Okay.
What's going on?
And you know, that was cool.
Like it's nice when code makes you do that.
If it's not just because you're confused and it's too messy or whatever.
It's cool.
Yeah.
My editor at O'Reilly, Dawn Shanafelt, she was really good about making sure that each of
these steps were explained in more detail.
And the editors at O'Reilly are really good.
They can pick up based on the style of your writing, whether you think you've explained it
sufficiently or not.
And they can prod you to say, are you sure you've explained this, but it seems like you
were a bit terse, perhaps add a few more points.
So all these bullets and points on the side where everything is spelled out in great detail,
that wasn't driven by me.
That was driven by the editors.
They're really, really good at what they do.
Yeah.
You did a good job as a team of breaking down what the steps meant.
Yeah.
That's cool.
So the other thing that you, that was built in was the concurrent.futures module in Python
3.
And I thought that was a really interesting way to think about sort of a unifying API between
process-based parallelism and thread-based parallelism.
Yeah.
I wanted to push that point because I think that's, that is the underutilized aspect of
concurrent on futures is that it gives you this, this really easy lever to switch paradigms.
For some processes, thread-based programming is valuable.
And for others, process-based parallelism is, is equally valuable.
And you get the same interface really just about.
So you can switch between those two paradigms really quite easily after the fact, which is
really interesting.
Usually for complex code involving parallelism, you end up with a structure that is hard to
change to fit it in a different paradigm unless you do a rewrite.
And the fact that concurrent.futures gives you the same API for both thread-based work and
process-based work is a really cool superpower.
Yeah, it totally is.
And maybe, you know, it definitely is a simplification because when you start talking about threading,
there's so many edge cases and interesting variations.
But maybe the general rule of thumb is if you spend most of your time waiting on the network,
then thread-based parallelism is probably good, especially if you're sharing a lot of data as
well.
And if you're doing a lot of computational stuff because of the GIL and you're not using
Cython or something, then, you know, you can't really parallelize that very much.
So multiprocessing and multiple processes for that is maybe a much better way to go.
But yeah, with the thread pool executor and what was the other one called?
The process pool executor.
Those two have exactly the same API.
And so if you write your code against those instead of directly against multiprocessing and
directly against the thread API, you literally change your import statement and it changes
where stuff runs and how, which is pretty cool that you try it out.
Yeah, that's really good.
One comment that I also want to make is if I make the choice between whether to use threads
or whether to use processes, it's not because of the GIL.
Because as you mentioned, Cython lets you drop the global interpreter lock.
That's not an issue for me.
I can write my number crunching code in Cython and use Python's normal threads and still access
all of the cores.
The distinction for me between whether to use process-based parallelism or thread-based is
really about whether I need to use, I need to be able to access the entire memory space
in the process.
So that is the main distinction about whether things are okay to be separated by process or whether
I really need the entire memory space to be accessible by all of the parallel parts of
execution.
So if that is the case, for example, if the batch of work that you need to operate on has
to all fit in the same memory space inside a process and you need to work on different sections
of memory concurrently, then I would use threads.
The presence of a global interpreter lock, while it's interesting, is not really a bottleneck
anymore in CPython because of Cython, because it makes it so easy to drop the GIL.
Right.
Awesome.
Okay.
Yeah.
That's a really interesting point.
And we will definitely be coming back to Cython.
But if you're working on some data structure that is really large and the threads are updating
multiple parts of it at the same time, then yeah, you want to keep that in the same process
space.
Yeah, absolutely.
It's really difficult to make that work with process-based parallelism.
I have been looking at ways of doing that, and I would like to find more about that,
about using memory mapped files to share memory between processes.
But I don't have much experience with that yet.
That's something that I would like to get into more.
Yeah, that would possibly be a solution.
But yeah, I don't know what the performance looks like.
It's interesting.
Okay.
Yeah, me neither.
All right.
So the next one that was built in was logging.
You said, you know, look, it's time to get over the print statement.
If you're trying to actually do debugging stuff, don't just spread it out.
Like, it's almost the same as you do logging, but you get a lot more.
Yeah.
So the experience that I have, this is quite a few years now.
The experience is I write out a new module or a new script using print statements.
And a couple of hours later or a couple of days later, it becomes something that I actually want to use and depend on.
And then I go back through the same code and I change all the print statements to logging statements.
And yeah, for the last couple of years, I've now just gotten into the habit of just beginning with logging.
You just put in the boilerplate, the setup line, and then creating your logger, and then you just run with that.
Yeah, it's pretty straightforward, right?
You import logging, you call logging.getlogger, and then you can say logger.debug, logger.info, warning.
And I agree with your sentiment.
You know, where I find it, I'll be totally happy with print for a while, and then I want to make the code that I was playing with a library and not an application.
Right.
Yeah.
And then all those print statements, it's like super hard to make them go away or to configure them.
It's just like, ah.
All right.
Just a removal.
Yeah.
So logging, excellent.
Another one that I really like in this space, although this is the built-in ones, is I really like logbook.
I think logbook is a nice external one.
But like you said, having stuff built in is great.
Okay.
That's a good tip.
I didn't know that I'm going to make it.
Yeah, I think that's Armin Roeniger.
I can't entirely remember, but I have to look.
It's really good.
Okay.
Let's see.
So another thing that you might want to do is run something on a scheduled basis, right?
Like every five minutes, I want to do this thing, or exactly on the hour, I want something to happen.
And the OSes have built-in ways to do this.
And I mean, I guess you could like spawn a thread or something to watch.
But there's some cool stuff built in for that, right?
Yeah, that's right.
So you're talking about the shared module, S-C-H-E-D.
This is a really good example of how you have to be aware of your biases.
For people who only ever work on POSIX systems or Linux, for example, right?
Cron is always there.
It does what it does really well.
There's a wealth of information available on the internet for how to use Cron.
So it seems bizarre that there would be this thing in Python that does exactly the same job.
But the thing is, Cron doesn't run on Windows.
Windows uses a separate system.
However, because Python includes the shared module, you can get the same or very similar functionality
to what you might get in Cron or the Windows task scheduler.
With a cross-platform Python module.
And that's really, really powerful if you're writing some service or library that needs to do these jobs on a timer.
Or at a particular time of the day or so on.
I look at shared as a really great example of what Python provides in terms of cross-platform support
for getting this kind of functionality, but in a cross-platform way where you can use the same code base on multiple platforms.
Yeah, and it's really nice.
And you basically set up the scheduler and you give it a priority and a frequency.
And then you say, and call this function whenever it's time.
And you can do that in either a elapsed time, like 10 minutes from now or every five minutes from now.
Something like that.
Or you can do it on a more, like once a minute exactly at the minute.
Right?
Yeah.
Nice.
Yeah, you can control completely when the target time is, or happens to be.
I can definitely see Shred becoming a part of the robotization, I guess, of the internet in a big way.
Automating things and creating bots and timers and work queues and so on.
Yeah, it's beautiful if you've got some embedded device running your Python code and needs to get home every now and then.
Just set that up, right?
Yeah.
Yeah, absolutely.
Nice.
I see we've got to the end of the standard library section.
We have?
There was one.
Yeah.
There was an additional one that I had in an earlier draft of the book, but we dropped it because it was too short, I guess.
And that's shlex.
There's a module called shlex in the standard library, which I wanted to include for no other reason than it has a split function, which will split strings like the normal split, except that it will retain quotes around sections.
So you can group chunks of words with quotes, just like you might imagine shell processing would process your commands.
If you put quotes around sections of things, then it treats those as one thing.
So the shlex module in the standard library has a split function that does that for you as well.
Oh, nice.
Yeah, you can almost escape the things you're putting on by putting in quotes.
Okay.
Yeah, awesome.
You don't have to do any quote processing yourself.
It's already in the standard library.
Yeah, excellent.
Okay, very nice.
So that was sort of the look inside of what's in the box if you just have Python.
And then you said, all right, let's look outside at external packages and why not start with a better way to install packages?
Yeah, absolutely.
So for anyone who doesn't know about Flit and you found that the normal process for creating and publishing a Python package to be arduous, Flit absolutely is the thing that you need to look at because it automates, for simple packages, it automates almost entirely everything that you need to do.
It's by Thomas Klaver.
He's very active in the Python scientific community.
And yeah, I think it's just awesome.
I'm using Flit at the moment for several of my own smaller projects.
Yeah, it's cool.
So if you want to submit something to PyPI, you have to create a setup PY with a lot of various settings, you know, set the license in the right way so people can discover it and who's the author and where's the documentation and what version it is, all those kinds of things.
And if you install Flit, you can basically say, I'd like to initialize this package and it just lets you, it basically takes you through a Q&A and then it generates the things it needs to upload your package, right?
That's right.
Yeah.
And the Q&A is pretty short.
I think it's four questions or something like that.
If you, another good tip is the cookie cutter project by Audrey Roy Greenfeld.
And there's a cookie cutter project for creating a skeleton for a Python package.
And it's quite eye-opening when you, when you run the cookie cutter and you see how many files it creates in a folder.
There's a manifest.in and there are several other extra files that are used just to, just to create and publish your package.
Whereas Flit does away with all of that.
You've really just got the Flit.ini.
Nice.
And you can get your package on PyPI.
And it's quite, quite simple stuff in the INI file.
It's not outrageous, right?
Yeah, exactly.
Nice.
And so then you can say things like Flit wheel upload, and it'll just take whatever active package.
package you happen to be in with the version specified in the files and just package it up and send it, right?
Yeah, exactly.
I haven't tried Flit yet for packages with extensions.
So, yeah, I don't want to say that it can do that as well, because I just haven't tried that myself.
But that's something that I do want to dig into as well.
Yeah, absolutely.
Okay.
Well, another thing that is very common is to create some kind of shell utility or app that has some kind of terminal output.
And there's not a lot of facilities in the standard library for, like, Keller output and nice sort of antsy style graphics, if you will.
So, one of the things you talked about is Colorama, which I thought was pretty cool.
I've looked at it a few times.
Yeah, I feel very strongly about Colorama.
And the reason is because we, generally speaking, we write software for people, for other people or for ourselves.
And you see the output from software, particularly in the terminal.
You have to deal with that a lot.
And I think that making that output friendlier and easy to read and easy to understand context, but, for example, by using green for good and red for bad, makes it a lot easier to use programs, really.
If we have to write software that works in the terminal as opposed to writing graphical user interfaces, there's no reason why we can't make that output appear better.
The prompt toolkit is another good library for making interactive user interfaces in the terminal.
And I didn't cover that in the book, but I think later we come to the PT Python interpreter, alternative interpreter.
So, we'll get to that later.
But that is part of this.
The use of color, I strongly believe, can help to make better user interfaces on the command line.
Oh, I totally agree with you.
Yeah.
What makes Colorama so great is that they completely abstract away, again, platform differences.
So, your code that uses Colorama will use the correct antsy codes in a bash shell, but when you're writing it in a Windows command prompt,
it will also use the correct color codes for that environment.
So, I think that's really powerful.
You're not really committing to a particular platform by using Colorama.
And it is a well-maintained package that I think support goes back to 2.64 and they include 3.5 as well.
Nice.
And you said also that you recommended Color Log as a way to add coloring to your log messages.
So, like, warning is one color, error is another, and so on.
Yeah, exactly.
And it's two or three lines, and you get that functionality, and all your existing logging messages will just get those colors.
It's a really easy drop-in replacement just to make sure that you have colorization for all the different logging levels of your logging messages.
Yeah, I think it's great.
You see something red go by, you know, obviously.
Pay attention, right?
It's great.
Exactly, yeah.
Or bold red, I see, for critical.
Yeah, absolutely.
So, another thing that you talked about on the terminal, the CLI, is accepting arguments.
So, built-in, we have argpars, but there's maybe some better ways.
And one of the ways you recommended was the Begins library.
Yeah.
What's Begins?
Begins is a library that I first heard about at PyCon Australia in 2014.
The author, Aaron Isles, gave a very strong demonstration of Begins.
And it struck me at that time how much you can really do with Python if you exploit all the features of the language that are available to you.
So, the point that I made in the book was that Begins, just from the perspective of an API design, is extremely aggressive with exploiting everything that Python provides to you.
And, for example, the annotation, the variable annotation format in the function definitions, Begins uses those annotations for the docstrings of each of your parameters so that you don't have to add that anywhere else.
And I really like the way the Begins API was designed to give you as much functionality as possible for as little input from you.
I like that trade-off very much.
Yeah.
It's really nice.
What I have heard from many people, though, is that they much prefer a slightly more rigorous specification format like what you can get now in the click library.
And docopt also gets a lot of love, which is another way of creating your command line interface by, not docopt, I forget the name now.
But there's another library where you can write out the help message of your CLI tool.
Yeah.
And it'll do it in reverse.
It's basically the reverse of Begins.
I think that is docopt.
Yeah.
Oh, okay.
It is docopt.
Yeah.
So, it's the reverse of Begins.
You write out your help message that will be printed when the user types help, and then it infers what all your parameters are.
That is also fairly popular.
Even so, I have found that for my own small scripts, Begins gets me going much, much faster.
And even subcommands are very, very easy to enable.
Right.
So, basically, you have some method.
You want to give some kind of CLI to it.
It takes some parameters, and you just give it a decorator or a subcommand decorator.
And now it is accessible, and it's part of the help text and all that.
That's correct.
Yeah.
Excellent.
We all love Python for its tremendous productivity benefits, but getting the best performance takes some work.
What if you could get out-of-the-box, easy access to high-performance Python?
Intel distribution for Python developers delivers just that.
Get close to 100 times better performance for certain functions when using NumPy, SciPy, Scikit-learn, linked with optimized native libraries like Intel Math Kernel Library, access efficient multi-threading, and Python projects like Numba and Scithon.
Try the Intel distribution for Python and experience performance today at talkpython.fm/Intel.
And profile your Python and native C, C++ applications for performance hotspots with Intel VTune amplifier.
With Intel, it's all about performance.
All right.
So let's move into the GUIs, the graphical interfaces.
And one of the first things that you talked about is creating interactive, dynamic graphs and things like that.
And while Matplotlib plays a big role there, you also talked about PyQT graph.
Why did you pick that over, say, Matplotlib?
The primary reason why I have selected PyQT graph over Matplotlib is for interactivity.
It's hard to imagine that you could have a highly performant charting library for Python, but that is exactly what PyQT graph is.
It's based on QT as the widget toolkit that runs in the back end.
But the interactivity is really good.
You can have graphs that draw spectra running at 50 frames a second quite easily.
And you can drag and zoom and pan all the while the animation is happening.
So you could have a data stream where you're plotting the data as it comes through live.
Oh, wow.
Whereas with Matplotlib, that degree of interactivity is not really there.
I see.
It's not because of a lack of ability.
It's because Matplotlib has been designed towards producing publication-ready type charts in a similar way to what Matlab's charting facilities were designed.
Whereas PyQT graph has been approached with a whole different use case in mind.
So in my chemical engineering work, PyQT graph has been very valuable for me to be able to plot live data and then examine it in real time, pan and zoom and move my moving data sets around.
That's awesome.
Yeah, if that was all that PyQT graph provided, that would already be enough.
And that was my largest use case for it.
But it has a fairly feature-complete widget library in the background that lets you plot, not plot, but create widgets on the fly for arbitrary Python data structures.
So you can get input cells and sliders and so on that can manipulate your data.
And PyQT graph provides all of that as well.
Yeah, that's excellent.
Yeah, if you want to embed some kind of like live data thing into your app, it sounds really cool for that.
Yeah, it definitely is a good choice.
And especially if you're already using PyQT, PyQT graph is a drop-in replacement.
Yeah.
You can add its chart windows as a widget inside your existing PyQT app.
Yeah, that's really great.
And there's a lot of interesting talk around PySide coming back to the same company that does Qt.
And that's, yeah, it sounds like it's going to be a real, like this is a vibrant growing area.
So that's great.
Yeah, I've got my eye on the resurgence of PySide as well.
Yeah, cool.
Yeah, I'm totally, totally excited for that.
So then the next thing that you talked about was one way to build your apps is using these graphical frameworks.
But a very popular one, even using CSS front-end frameworks like Bootstrap and stuff, is web development.
So there's another interesting library that lets you have Python logic in a desktop application,
but actually presents the user interface through a GUI.
You want to tell us about that?
Yeah, so I also included PyWebView, which is something that I found while doing the research for the book.
It's not something that I had used before.
But I was blown away at how such a powerful tool exists, and it could not be better known.
Most people know about the Electron framework and the Chrome-embreaded framework,
which can also make these desktop apps that rely on the WebKit engine to provide the visualization layer.
The interesting thing about PyWebView is that it doesn't require you to bundle something like Electron with your app.
It just uses the native browser.
I see.
Which is really amazing.
That is amazing.
Is it cross-platform?
Yeah, it's cross-platform.
So it will use Internet Explorer or Edge on Windows, and it'll use the WebView widget on OSX, which is what powers Safari,
and then on Linux it will use whatever is native there.
Interesting.
Yeah, I may be pushing up to the edge of the things I've actually played with.
But the Electron stuff, in order to implement that and put the logic in it, that's JavaScript, right?
Correct.
So if you want to have, well, no, not necessarily.
So if you just imagine a normal web application where you have your front-end layer that runs what we would say as in the browser with your CSS and your HTML and your JavaScript,
and then you have your back-end layer, which provides an API that receives REST calls or whatever, which we would typically write in Python.
You do exactly the same thing, but you just package it in one bundle as a desktop application.
Nice.
So from the perspective of the viewer, with PyWebView in particular, you don't really know that you're in a browser because you don't have all the same features and trimmings and buttons and menus that you get in a browser.
All you really get is the WebView window, and then you get to put in there whatever you want.
But you can power that with all these same technologies.
So you can specify the layout of your screen using HTML and style it with CSS.
And if you want some interactivity in the graphical layer itself, then you would have to write that with JavaScript as you would with a normal web application.
But your Python, that can provide much of the logic and back-end processing of what your use interface is advertising, can also run alongside that application on your desktop.
So from the perspective of a user of such an application, they would be oblivious to the fact that Python was being used at all.
Yeah, it looks really interesting.
And I kind of prefer CSS and HTML for GUI design.
So I may have to try this out.
Definitely worth looking into.
I definitely recommend it.
In the example that I used, I also used another Python library called Dominate, which allows you to create the HTML DOM and structures within the DOM directly from inside Python code.
But that was just me being too cute, I guess.
Just because you can.
You can also.
Yeah, exactly.
You can just write your HTML and CSS out as you normally wouldn't load that.
And that works fine.
Right.
So you would.
Could you do something like have like a Chameleon or Jinja 2 template and something like that and pull that in?
Yeah, absolutely.
Okay.
Definitely.
No question.
And the big benefit of PyWebView over using Electron is, again, that you don't have to distribute a fairly large browser engine alongside your app.
Yeah.
Excellent.
If you can find a way to bundle just the Python parts of your app, when you run it, it will use the native web widget of your target operating system.
Excellent.
Okay.
I really like that one.
And I definitely want to have a look at it as well.
So moving on to sort of the systems management, system tool stuff.
The first one you brought up was an example of something I was trying to do in one of my online classes that I was building.
And I'm like, oh, why is this so hard in the built-in process stuff?
And that's about managing processes with PSUtil.
Yeah.
So worry no more because there's a library called PSUtil that does everything you could possibly want in terms of accessing information about the system and more.
I have a feeling that PSUtil is going to be bad for the business for many monitoring companies, server monitoring frameworks, because it's so easy to run PSUtil in a daemon on your server and get it to send information back to you about which process is consuming how much memory.
If your particular application is misbehaving in some way or if the system itself has started to change how it is supposed to be operating, PSUtil makes all of that really easy.
I reckon in a day or two, you could probably whip something up that can give you as good performance monitoring as what you could get from a cloud provider currently.
Okay.
Wow.
Yeah.
You can ask for things like, what's the CPU percent on the system?
And if you have like eight cores, it'll give you an array of eight floating point numbers that are percents.
And you can say, what is the current process that I'm in?
How much memory is it using?
And things like that.
It's great.
Yeah.
And you can access all the other processes as well.
You can get similar information from all of them, from everything that's operating on your system.
Yeah.
Nice.
So that was for watching processes and system stuff.
Another thing that people often have to do is they have to watch a directory for when a file either changes or a new file arrives.
Like somebody's uploaded some new CSV file.
We've got to ingest it and do work on that.
And you talked about a thing called a watchdog for that.
Yeah.
Have you used that before?
I've not, but it sounds really cool.
And like the others you brought up, it's very nice that it's cross-platform, even though that implementation is quite different on the different OSs.
Yeah, that's right.
And just like psutil, it abstracts away some fairly complex work into a very nice, very easy to use API.
That is, again, cross-platform.
One of my requirements for selection in the book throughout is that every library had to work in a fairly easy way on all the three big target platforms.
So they should all be cross-platform.
And watchdog probably does the best job of hiding platform differences away because these notification systems are quite different on each of the target platforms.
And luckily, you don't have to worry about that whatsoever.
It completely hides away those differences.
And as you said, it gives you a way to monitor a particular directory for any changes.
Yeah, it's really nice.
So you just create a class driving from some built-in monitor event handler type of thing.
And you say, call this function when you create one or call this one when a function is modified.
And then you can just tell it to start observing.
And it actually does that in the background on a background thread, right?
Yeah, that's right.
Yeah, very cool.
So the other one you talked about, you alluded to before, is ptpython.
Which I've not played with this, but I'm thinking this is getting installed.
Because this is, I really don't love the REPL that much, the built-in one.
But this is cool.
I need to check this out.
So tell us about it.
Yeah, so ptpython is based on another Python library called Prompt Toolkit, which is a toolkit for making user interfaces in the shell or in a command line view.
And ptpython is a replacement Python interpreter, but it's supercharged for editing and editing history and bringing back previous functions and changing them.
And it has color support and a whole bunch of other features as well, which I could not get to in the discussion.
I pretty much the first thing that I install after updating PEP and setup tools in a new virtual env is ptpython.
And that's the interpreter that I use for doing any of that interactive kind of work.
Yeah, it makes a ton of sense.
Like, for example, if you, one of the things that drives me crazy in the REPL is I'll type out like a function or an if statement or a loop more likely.
And then I'll either make a mistake or I want to run it again slightly differently.
And then you've got to up arrow.
Like, okay, I know I'm going to up arrow five times and hit enter and then like sort of unroll the history so I can get back.
And then I got to remember the line I changed.
And like this one, if you say, I want to go back to some multi-line thing I worked on,
it actually pulls up the multi-line thing right there, which is already makes it worthwhile.
Plus the color and the auto-completion and all that.
That's great.
Yeah, that's right.
And so when you press up arrow and you get that multi-line statement that you did earlier,
I use the VI key bindings.
And that all works.
I can go to the top of the line, go down.
I can DD to delete a line or yank and paste.
So if you're used to Emacs, they have Emacs key binding support.
And if you're used to VI, you can enable VI key binding support.
And you get the power or much of the power of those keystrokes and commands inside every single line that you edit and enter inside PtPython.
Which is so much better than the built-in.
Yeah, that's fantastic.
If you run in a split screen in your terminal where you have, for example, your editor in the top half and a command line on the bottom half.
If you run PtPython in the bottom half, what's really interesting is if the key bindings match the editor that you're using,
you almost begin to feel like you're working in one environment because the key bindings work in your editor.
And then when you jump to the REPL, it works there as well the same way.
So that's really nice.
I work like that almost continuously.
Oh, that's really nice.
Yeah, I like it.
I'm definitely going to install it and check it out.
The next thing is moving on to the web APIs and HTTP services and so on is something I had not heard of,
but it's very nice.
It's called Hug for building APIs.
Yeah.
So just like we discussed earlier with Begins, what I really, really liked about Hug is how they try to maximally exploit the features of Python
to make as simple as possible use interface for you as a programmer to implement an API.
I have had experience before with the Django REST framework, which is an awesome industrial strength, very well designed, very sturdy and robust REST framework.
So I recommend that one strongly.
Flask also has a good REST framework.
Those are not bad choices at all.
But I did have the impression that very few people knew about Hug.
And for simpler kinds of applications, I think Hug makes it extremely easy to get a REST interface up.
Yeah.
And it's very service oriented, right?
It's not looking like some web framework that also happens to allow machines to talk to it and return JSON.
Like, for example, to take a function and make it a return JavaScript for get requests, you just say at Hug.get and you give it that decorator or a post or whatever, right?
And you make it a API.
Yeah, exactly.
And you're done pretty much.
And you get the documentation because it auto generates that from your function declarations.
And versioning is also pretty easy to add, which I had in the later section.
Yeah.
So basically, if you make a request to the base URL for the host that's running the Hug service, it will actually describe all the services and how you talk to them and what's the inputs, the outputs, everything.
And like you said, you can put versioning on it.
So basically, you don't have to go and change everything about your methods and try to somehow bolt versioning on.
You can just say in your decorator, this is for version two of the API.
It also does argument conversion and stuff like that, right?
That's right.
Yeah.
Nice.
So, cool.
It helps in the documentation, I guess, as well.
If you say, here's an integer and its name is this, like the documentation can say, hey, it takes an integer called this.
Nice.
Yeah.
Yeah, absolutely.
Documentation, particularly for things like this, it's really a pain to write by hand.
And no one should ever do that.
Definitely, you want to use a tool that makes it really easy to produce documentation and to keep the documentation up to date.
That's the key part, right?
Keep it up to date because it's easy to create it and then just leave it.
You know what?
Well, I guess that changed.
Yeah.
Sorry, that documentation was wrong.
Yeah.
Nice.
Okay.
So, one of the things that's pretty challenging, I think, let me rephrase that, is more challenging than I think it should be, is working with dates in Python.
And so, you have some cool libraries to work with that that you've found.
That's right.
So, the first option that I had there is not that unknown, I guess.
Many people who have had to deal with dates and times have used Arrow for several years now.
And the key thing about Arrow, or at least the key thing for me, I guess, is that it does away with this idea of having naive date times and so-called aware date times.
Aware date times are date time objects that carry with them the time zone that they apply to.
And naive date time objects do not have the time zone information attached.
And, yeah, things get really out of hand if you start mixing and matching those without an awareness of what you're doing.
And just by using Arrow, because it uses aware date time objects everywhere, simply by using Arrow, it means that you can avoid a certain class of problems where you're mixing up dates and times incorrectly.
Yeah, and you run into weird problems.
Like, if you try to subtract two date times, normally you get a time delta.
But if one of them is time zone aware and one's not, then it will crash, right?
So, no, you can't subtract these.
Yeah, that's right.
And worse is when you don't get crashes and you do arithmetic operations and the results that you're getting are not what you think you're getting.
For example, in one part of your code base, you might call the now function.
So, daytime.now, and then you get a time object.
And in a different part of your code base, you call a very similarly named function called UTCnow.
The problem is that the one gives you the time as it is in the UTC time zone, but without a time zone object attached.
And the first one gives you the time as it is now, but in your local time zone.
And the problem is that as a programmer, depending on the context of the code, you may perceive those two values to mean literally this moment in time right now.
But the values are vastly different.
They're obviously different by the extent of the times and differences.
And so, when you do operations on them, you get very strange results.
Or results that seem strange to you because of the assumptions you've made about what now actually means.
Right.
So, by using aware datetimes, you don't have those problems anymore.
The time deltas that you obtain by doing operations between these are always correct.
Yeah.
So, even if one is from .now and others .utcnow, it knows to normalize those to some common time zone before it does math.
Right?
Like, if it wants to look how far apart they are, I would say, no, those are actually, you know, either the same or like one millisecond apart or something like that.
Yeah, absolutely.
Nice.
So, that's coming from the universe into now.
Into, you know, bringing in the time and working with it.
The next library you talked about is about pulling time that's been saved already into a bunch of different formats and processing that.
Because parsing time can be super challenging.
I was talking on the previous episode with Anna Schneider.
They were pulling together data sources from all these different utilities.
And they said they have many, many different formats for time.
That there are over 700 different formats for time out there.
So, trying to just like deal with all that stuff is super painful.
So, pars datetime, which is what you talked about, really actually does an amazing job of that.
Even for human type stuff.
Yeah, that's right.
And this is another library that I discovered while doing the research for the book.
I had not used this one before.
I tried several libraries like this, but I was amazed.
In the section of this book, I give some examples where parsed datetime is used to parse fairly typical looking datetime strings.
But in the second half of the section, there's much more natural language type string sets that it also parses and does really well.
I had a lot of fun doing the section because I tried to find ways of writing my statement of what day it was in very different ways, in very strange ways.
And it seemed to get all of them.
Right.
Yeah.
The last option that I had in my list was a string that said two weeks and three days in the future.
And pass datetime correctly parsed that.
I know.
It's so amazing.
Pretty amazing.
Like when I had in mind of what would work, you know, you'll say, look, you can give it like 2016-07-16 or 7-16-2016.
These types of things.
And then I'll actually parse those all correctly.
But then you started to get more interesting.
And you said like yesterday, 10 minutes from now, three days ago.
And it just totally got all this.
And then you got to the most outrageous one, like you said.
Two weeks and three days in the future.
That's awesome.
Yeah, it's pretty cool.
I would really want to use this in an upcoming project.
I just need to find the right project.
Yeah, it's really good.
I have my PyPI package and I'm looking for the project in which to use it.
Exactly.
And I think I confused Arrow with parsed at time.
Arrow has the ability to give you like human relative time.
So you can say on any arrow time, you can say humanize and it'll say just now, ask it again a little bit later.
It'll say seconds ago or two hours ago or two hours in the future or something like that, which is really nice.
Yeah, that's awesome.
And the multilingual support is really good as well.
I see that as being hugely valuable in web services and web development.
No, I totally agree.
Then the last part that you looked at, you said, okay, let's, these are all very purpose focused packages that we've talked about.
Parsing date, time or scheduling something to recur every so often.
But there's a couple of general purpose libraries that you talked about.
And the first one, Boltons, is from a multi-time guest on the show, Mahmoud Hashabi.
And he put that out there from the guys at PayPal, which was great.
So you want to talk a little bit about what's good with Boltons?
Yeah, for sure.
The first thing, though, is that what's quite interesting to me, if you compare Python to some other languages,
is because the standard library is so big and it covers a lot of ground,
it's quite rare to find general purpose libraries in the Python ecosystem.
I thought that was quite interesting.
Boltons is one of the few ones that I did manage to find,
where the intention is literally just to be a general purpose library for use in very, very different spheres.
Most of the packages that you get on the package index are dedicated towards a singular purpose, usually,
to perform some function, like all the other libraries that we've looked at.
So I really thought that was interesting in doing the research,
that there are not very many general purpose libraries.
And my conclusion is that that must be because the standard library covers already most of the general purpose type of things that you need to do.
I agree that that's probably true.
I wonder if another reason, a secondary reason, is that it's pretty easy to bring in a bunch of small libraries.
It's not like you've got to download and get the header files and the lib files and the right, you know,
just statically linking and all that kind of stuff that you have to normally deal with.
It's if you just pip install and import a bunch of stuff, you're good to go.
So maybe it's also easier to have small libraries possibly.
But yeah, I think you're right that because a lot of stuff is built in,
a lot of people maybe put their energies towards fixing built-in stuff.
And it's just, you know, it's a 25-year-old standard library, right?
It's pretty polished at this point.
I think you make a lot of sense.
It also makes a nice parallel with the Node ecosystem where, similarly, there aren't too many general purpose libraries.
And that's probably because it's so easy to bring in a lot of smaller libraries to make up the feature set you require.
Yeah, absolutely.
So maybe we could just really quickly touch on a few of the things.
So one of the things that's in there that's pretty nice is the caching functionality.
So you start out with cache utils.
That's right.
So the killer feature of the cache functionality in Boltons is the way that you can share a cache among multiple function calls.
It's not that easy to do with the LRU cache that you get in the standard library.
It's in the functools module.
So you have to import functools.lrucache.
The one that you get in Boltons is very easy to share amongst many different function calls as a decorator.
That is the main attraction for me to use the cache in Boltons versus the one in the standard library.
The LRI cache is also kind of interesting.
I had to stretch a little bit to come up with an application to use both caches in the same code base.
It's definitely good to keep an eye on the LRI cache.
Yeah.
Okay.
And let's see.
There was some other stuff that I thought was pretty interesting in there.
One of them you had talked about was the at exit function, which I'd never used the at exit function.
Yeah.
The at exit function is pretty neat.
You can basically set something up to run when it exits.
Right.
Yeah.
So if you want to save some data structures and reload them at startup, you can just register these.
Here's the shutdown functions to make sure you run.
That's cool.
Yeah, exactly.
Another stuff that was in there that was nice was the iter tools that would give you like windowed chunked data.
So for example, you talked about displaying that data interactively using PyQt graph.
You could maybe combine that with the windowed iter tool.
The iter tool is a windowed iter behavior and take some sort of stream of data and always show the last 50 pieces of the data,
like in a few lines of code, right?
Yeah, absolutely.
For sure.
The chunking and the chunked iter and the windowed iter, in my opinion, they're much better than the recipes that the standard library gives in its iter tools documentation,
which I see as a fairly clunky way of piecing together building blocks from iter tools to get these same effects.
I think it would make a pretty good addition to the standard library to have this chunked iter and windowed iter functions.
Yeah, it's very possible that eventually some of these just become consumed into the standard library over time.
It's great.
You also have, there's also some nice debugging tools.
That was cool.
So you can say, yeah, PDB on signal, for example.
How would you use that?
Yeah, so you can attach this PDB on signal function inside your running application.
And then by default, a keyboard interrupt handler will automatically be added to your program so that when a crash does occur,
or when you, not a crash, but when you control C to stop your program, your program can stop at that point in a debug session.
So for example, with a long running loop, if it's taking too long and you're wondering whether the program is doing the correct thing,
or perhaps you suspect that it is no longer doing the correct thing,
you can control C and you can get a debug prompt inside the loop wherever you sent the signal.
Yeah, that's awesome.
Just, that could be pretty handy in the right situation.
Yeah, if you're wondering what the heck is this process doing?
Is it stuck talking to the database, stuck talking to the web service?
Is it just broken?
Yeah, let's have a look, right?
Yeah.
Yeah, very nice.
Okay, so let's, let's almost at the end so we should wrap it up.
But the last major piece that you talk about in the general libraries is Cython.
Yeah, so this 20 Libraries book actually came about as a follow on from an earlier video screencast series
that I did for O'Reilly, which was on Cython.
It's a huge five and a half hour long set of 75 videos covering how to get into Cython and how to start using it.
That sounds great.
I'll be sure to link to it from the show notes for everyone.
Yeah, sure.
I'll give you the link.
And yeah, Cython, I think, has started now to gain some, some mindshare in the Python community, but not that many people are still using it yet, because it does introduce some things that are more complex than what you usually have to deal with in a Python package.
For example, compiling with C extensions.
However, Cython, among many other things, Cython can give the average Python programmer two key things that have, that have long been desired.
The first one is Cython can speed up hotspots inside your source code easily affect a hundred or more if we're talking about basic math computation.
A hundred times is not something to take lightly.
It's the difference between, you know, running for a hundred days or running for one day for a very big long running process.
And the second thing that Cython can give you, again, for the right kind of situation, which might be mathematical computation, is an easy way to run your threads on different CPUs without the global interpreter lock interfering in any way whatsoever.
Something that I tweeted just yesterday was, there seems to be a misconception in some circles that you need OpenMP support to use parallelization in Cython.
And that's not the case.
You can get pretty good parallelization just with normal Python threads, as long as inside your Cython functions where you want to enable that, you release the GIL.
Right.
And there's a way you can even do that with context managers, right?
You can say with no GIL or something like that, right?
That's exactly right.
Yeah.
So for me as a Python programmer now in the situation that I'm in, the GIL is not really that big a problem for me.
It depends on the details of the situation.
But for heavy math computation, where I want to be able to access all the cores, and I simultaneously want my code to run faster than it normally might just with a plain CPython interpreter, Cython gives me both of those things in the same package.
It sounds really great.
I've not had a chance to do enough scientific work, but I can see it even being useful outside of scientific computational stuff.
For example, if you're writing, let's just say I'm writing some kind of ORM or something, and I'm spending a ton of time taking objects off the stream and the data layer and actually turning those into objects.
And just that processing there is like some big hotspot if I do a query that returns 100,000 records.
Maybe that loop could be written in Cython.
Is that right?
Yeah, absolutely.
So a good example is something that I've been doing at work for the past week, which is converting our protocol buffer code away from using Google's protocol buffer implementation to using a new tool called Pyrobuff.
Pyrobuff is itself written in Cython, and it generates a PYX file for you to use as your object implementation of the protocol buffer rather than just using your normal Python-based implementation of the protocol buffer.
I'm not going to go into what the protocol buffers are, but basically it's exactly what you were saying, object serialization that you shuffle between two places.
And Cython has been used to create the protocol buffer using Pyrobuff.
But in addition to that, I use the object, the particular object that that process generates inside another Cython file, which I can then use directly with no overhead from the Python interpreter.
Yeah, that's great.
That's really cool.
All right.
So you've definitely given me a broader view of where Cython is applicable, and that's cool.
So let's round it out with one final awesome thing that you point out.
And that's a GitHub repository or project that is just a huge collection of stuff like this that people have found awesome.
Like here's all the awesome Python packages for OCR.
Here's the awesome ones for e-commerce and so on.
And that's awesome Python on GitHub.
Yeah.
Awesome Python is so awesome that many of the other language communities have now begun to copy it.
So you can also find awesome Go and awesome Ruby and many of the other variations.
Very cool.
So, Kayla, this has been really interesting.
I learned a lot from your book and not necessarily many of the pieces that we talked about, but maybe even the ones that we didn't get a chance to cover.
There's a bunch of other interesting packages that we're using in conjunction with your demos that I'll leave it just vague.
And people can go check out the book, which at least a little while ago you could get as a free e-book from O'Reilly.
I'll link to it.
But highly recommended.
I think it was time well spent to go through it.
So thanks.
Okay.
Yeah, my pleasure.
Yeah.
So let me ask you, as I always do everyone, but it's a bit of a bigger list to pick from.
What's your favorite PyPI package?
If you have one out of all the books that you would say, like, okay, this is the thing people should take away if they're not going to get the book.
My pick for the PyPI package is pretty much anything inside the Bware project.
I very strongly feel that the contributions that Russell is making are very positive for the Python community and they're forward looking.
The things that he's working on in the Bware project are things that we need to have happen in our community and our space.
So for anyone who's thinking about finding a project online that they want to contribute to, maybe get a little bit of experience, that is a great place to go.
There's the Togo framework, which is intended to be used as a way of writing platform-native graphical user applications in Python.
But there are a whole bunch of other smaller projects that you can adopt and get into and dive in and play with the details.
There are projects for running Python on iOS, projects for running Python on Android, and a bunch of different other features.
He also has a project for packaging up Python for deployment to target machines, which is another issue that many people in Python feel has been, I guess, under-addressed.
The issue of deployment.
Yeah, I definitely think it's under-addressed for desktop spaces, absolutely.
Absolutely.
For sure.
Or mobile, for that matter.
But anywhere but the web or just your shell, I guess.
Yeah.
Yeah, okay, very cool.
That's great.
Check that one out.
So, Beware.
The name of the project is Beware.
If you're looking for something to work on, you could do much worse than that project.
If you're writing some code, what editor do you use?
Yeah, that's a great question.
So, I've been doing this a while now.
And for most of those years, I've been using Vim.
And since January, I've started using PyCharm because the scales have tipped the balance for me.
And the features that PyCharm now provides outweigh what I can do to the best of my knowledge with Vim configuration.
So, yeah, I'm now writing my Python code in PyCharm.
All right.
You and me both.
I love that one as well.
That's great.
Yep.
All right.
Final call to action for listeners out there.
First of all, check out the Beware project and contribute to that if you're looking to write some code.
Anything else?
Get your book.
Where do they get it?
Oh, you can get the book at O'Reilly.
I think if you search for 20 Python libraries you aren't using, that should be enough.
Google will find it for you.
And of course, then there's my Cython course as well.
If you do want to get more into Cython, you can check out my course.
As far as I know, I think it's the only video course currently available for Cython.
But I might be wrong about that.
But that's something else to check out.
And then maybe the last thing I would mention is just as a general comment, sometimes you see on forums like Reddit and other places, there's a lot of dissatisfaction with some of the decisions that the core Python development team make regarding certain features in the language and what gets included and what gets excluded and so on.
And I would encourage people to follow the newsletters and the mailing lists to see a bit more about the discussions that go into these decisions.
The core team has many, many difficult and complex issues to deal with regarding the features that they include or exclude.
And before I started the mailing list for Python Dev, I had the same thoughts about, you know, why was this designed that way?
Why didn't they include that?
Why is that not done?
But once you begin to follow the mailing list and you start to see the discussions and the complexities that they have to deal with, for example, in the pip project, that's another good example.
Once you begin to see the complexities that these teams are dealing with, you begin to understand why the decisions get made in the way that they do.
So I just want to make a point there that if anyone feels dissatisfied with what the core Python team has been doing, get involved and find out more about why the decisions are getting made in particular ways.
Yeah, I think that's great advice.
Certainly looking at how the trade-offs are being chosen is definitely important.
Thank you so much for sharing your book and all this research you did.
It's really helpful.
Sure, my pleasure.
I think people should, they should check out the book.
They'll definitely enjoy it.
Thanks for being on the show.
Yeah, thanks, Michael.
Great.
You bet.
Bye.
Okay, bye.
This has been another episode of Talk Python to Me.
Today's guest has been Caleb Hadding.
And this episode has been sponsored by Capital One and Intel.
Thank you both for supporting the show.
Are you a data scientist or Python developer who loves data?
If you're looking for a place to work on data science with truly big data that can affect millions of lives, then head on over to jobs.capitalone.com.com.
And check out the wide range of jobs that Capital One is trying to fill right now.
The Intel distribution for Python delivers the high-performance Intel C libraries built right into Python.
Get close to 100 times better performance for certain functions when using NumPy, SciPy, and Scikit-learn.
Check them out at talkpython.fm/Intel.
Are you or a colleague trying to learn Python?
Have you tried books and videos that just left you bored by covering topics point by point?
Well, check out my online course, Python Jumpstart, by building 10 apps at talkpython.fm/course to experience a more engaging way to learn Python.
And if you're looking for something a little more advanced, try my Write Pythonic Code course at talkpython.fm/pythonic.
You'll find the show notes and links from this episode at talkpython.fm/episode slash show slash 77.
Be sure to subscribe to the show.
Open your favorite podcatcher and search for Python.
We should be right at the top.
You can also find the iTunes feed at /itunes, Google Play feed at /play, and direct RSS feed at /rss on talkpython.fm.
Our theme music is Developers, Developers, Developers by Corey Smith, who goes by Smix.
Corey just recently started selling his tracks on iTunes, so I recommend you check it out at talkpython.fm/music.
You can browse his tracks he has for sale on iTunes and listen to the full-length version of the theme song.
This is your host, Michael Kennedy.
Thanks so much for listening.
I really appreciate it.
Smix, let's get out of here.
Stay tuned.
Don't put it on the ground.
Thank you.