Brought to you by Michael and Brian - take a Talk Python course or get Brian's pytest book


Transcript #58: Better cache decorators and another take on type hints

Return to episode page view on github
Recorded on Tuesday, Dec 19, 2017.

00:00 Hello and welcome to Python Bytes, where we deliver Python news and headlines directly to your earbuds.

00:05 This is episode 58, recorded December 19th, 2017.

00:09 I'm Michael Kennedy.

00:10 And I'm Brian Okken.

00:11 And we have a bunch of awesome stuff to share with you.

00:13 First, I want to say this episode is brought to you by DigitalOcean, so thank you, DigitalOcean.

00:17 Yeah, thanks, DigitalOcean.

00:19 Yes, thank you, DigitalOcean.

00:22 Indeed. Love their stuff. Tell me more about it later.

00:24 Let's start with monkeying around a bit.

00:27 I like them. There's so much monkey, I'm not sure why, in Python.

00:30 Here's a lot of monkey, you're right.

00:31 Yeah, we got monkey patching and stuff.

00:33 But this is monkey type from Instagram.

00:37 And in episode 54, we talked about PyAnnotate, which is a way to add type annotations to your code while it's running.

00:47 But it was from Dropbox, but it was only Python 2 at the moment.

00:51 This one is a similar sort of thing, but it's from Instagram, and it's Python 3 only.

00:56 And it doesn't do the comments.

00:59 It does the Python 3 style type annotations.

01:01 So I'm kind of really excited to try this out.

01:04 Yeah, that sounds really cool.

01:06 You know, I'm definitely heartened to see a lot of people who have large code bases, Dropbox, Instagram, and so on, making these tools.

01:14 They're going to bring everybody along to modern Python really nicely.

01:18 It's very good.

01:19 And I like the way the types are moving.

01:22 I was kind of lukewarm on types for Python at first.

01:27 But using it to try to solidify the quality of your code base for large code bases makes total sense.

01:34 And I like what they're doing with it.

01:36 I really like adding the type hints.

01:38 Just in a couple of places where the autocomplete falls down, like, you know, last week we talked about Mongo Engine.

01:46 You do a query for Mongo Engine.

01:47 And as far as the tooling, it's just like that's a random thing that came back.

01:52 It has no idea what it is.

01:53 But if you add just a few type hints, the rest of your application can automatically, well, editor, can detect the rest of your applications working with one of these concrete types.

02:01 So just a little type hint here and there will go a long ways.

02:05 And I guess that's probably for new code, right?

02:07 But for older code, you want to switch to Python 3 from Python 2, having that as a solid foundation.

02:14 So you really know what you're working with as you move it around.

02:17 I think that that's even really valuable right there on its own.

02:20 The article we linked to is one of the things it talks about is what happens is you're actually running your code and it pays attention to which types are going through different parts of your code.

02:30 And the little, you can run it through while you're running tests, but it did have a note, which I thought was interesting to say, if you have the style of testing where you're using a lot of mock objects, the types are going to be all messed up.

02:45 So be aware of that.

02:46 And you may want to generate your types some other way.

02:50 Oh, right.

02:50 Because it will see the mock and I don't think that's what's supposed to go there.

02:53 Not the thing it is mocking.

02:54 Yeah, exactly.

02:55 Interesting.

02:56 Cool.

02:56 So sometimes you can make your code fast by optimizing it.

03:02 Maybe it talks to a database and you're going to put the right kind of indexes in there.

03:06 And of course, that should be fast.

03:08 But other times you're working with things that are out of your control.

03:12 You need to call the web service to get some kind of stock quote or whatever.

03:16 And you can only be as fast as that web service or whatever, unless you're willing to hang on to it for a little bit and do some caching.

03:24 Right?

03:24 Yeah.

03:25 So Python, I don't know how many people know, but there's something called functools in Python.

03:31 And in there, there's a decorator called LRU underscore cache.

03:35 And you can put that onto any function and it will look at the arguments going in there.

03:41 And if it sees that argument series again, it can have multiple arguments.

03:45 If it sees all the same arguments it's seen before, it will just return that value instantly.

03:49 So that's pretty cool, right?

03:51 I did not know that.

03:52 That is cool.

03:53 Yeah.

03:53 Yeah.

03:54 So suppose I'm calling like a weather API and I'm doing it from my website and I've got all these different people coming in and calling it.

04:03 And it turns out to be slow.

04:04 I could actually throw that onto it.

04:06 And it would say if two people ask for the same zip code, potentially it's just going to return that instantly, just out of memory.

04:12 So that's really cool.

04:13 But it only works in certain ways.

04:16 For example, if that method takes a list, well, lists are not hashable.

04:21 LRU cache thing.

04:22 Decorator requires all the stuff it's going to hold to be cacheable, for example.

04:26 So it's cool, but it's kind of limited in a certain way.

04:30 So there's this other project that's kind of inspired by this idea and it goes much further and has a lot more options called cache tools.

04:39 I'm guessing you probably haven't heard of cache tools if you haven't heard of LRU cache.

04:41 I have not.

04:42 So this is a project that has a bunch of different cache implementations as well as a more flexible decorator.

04:51 Actually, a couple of decorators you can use in exactly the same way.

04:54 So it defines like a basic cache, an LFU cache, so least frequently used, because eventually your cache may run, you know, get full and run out of memory.

05:03 Or you can say only hold 100 items.

05:05 Well, when you get to 101, which one do you throw away?

05:08 Well, you can use the LFU cache and throw away the one that was least frequently used.

05:12 An LRU cache, the one that was least recently used.

05:16 Or have what's called a TTL cache, which is a time to live.

05:19 Like cache everything for five minutes.

05:21 I'm sure we have the memory for this.

05:22 How could that be bad, right?

05:23 Yeah.

05:26 And then you get that call in the middle of the night.

05:28 Server's down.

05:30 The TTL cache, that seems like, for instance, your example of grabbing some weather data or something.

05:36 I mean, you could hold on to the weather data for at least a few minutes before refreshing that.

05:41 Right.

05:42 And what's really cool about the TTL one is it naturally expires the data in a way that you can predict and might understand.

05:49 So if you're like, look, the weather's not going to change that much in 10 minutes.

05:52 Just cache everything for 10 minutes.

05:54 And it automatically will go get a new one after the 11th minute, right?

05:58 So that's a really nice way to do it.

06:00 So these are cache implementations.

06:02 And then there's what's called a memoizing.

06:04 So memoization is this concept that we've been talking about.

06:07 Memoizing decorators, cacheutils.cache, which is like the one we talked about before.

06:13 But you can plug in all sorts of stuff.

06:14 You can plug in any of the cache implementations we've talked about or even a straight dictionary.

06:19 It takes a function that will generate the hash so it can cache non-hashable things.

06:25 Because you could generate some kind of indicator like an ID out of a database object or something.

06:29 You could pass interesting things like a weak value, a weak ref.weak value dictionary.

06:35 So you don't actually hold on to the memory of the things, which is pretty cool.

06:39 And it even has like a locking object you can pass for thread safety if you've got to recreate stuff in the hash and stuff.

06:46 So really, it's like the idea of the LRU cache and functools, but way more flexible and configurable.

06:51 Yeah, this is nice.

06:52 It's insanely easy to use them, right?

06:54 You just throw a decorator on a slow function and now it's a fast function.

06:57 Just make sure you understand what that means.

06:59 Yeah, so definitely in conjunction with a safe measure before you prematurely optimize.

07:06 Yeah, it's cool though.

07:07 Speaking of going fast.

07:09 Yeah, speaking of going fast.

07:10 One of the things that people often start with with a new project is to use for a database.

07:17 Instead of deciding which one they're going to use down the road, they'll throw SQLite in or SQLite.

07:24 I don't know how you pronounce that.

07:25 But since it's built into Python and you don't have to install anything extra, I guess Python calls it SQLite 3.

07:33 I don't know.

07:33 Were there two of them before that?

07:35 Yeah, I guess so.

07:35 That's something folks use and then they sometimes migrate to something else.

07:40 And sometimes there's a lot of applications that stick with it.

07:42 There's an article called Going Fast with SQLite in Python that talks about some of the ways this fellow came up with to make it quickly.

07:51 Make it run faster.

07:53 Yeah, that's great.

07:54 So SQLite is really awesome.

07:55 It's an embedded database that ships with Python.

07:58 You don't have to do anything to have it.

08:00 You just have it.

08:00 It runs in process.

08:02 So there's like zero latency over the network or overhead or anything like that.

08:06 It's actually really powerful if you're willing to have a sort of in-process databases.

08:12 Yeah.

08:12 And I had the impression that it was simpler than it is.

08:16 But it does quite a bit of cool things as I was reading through this.

08:20 Like I didn't know you could do user-defined functions.

08:23 And you've got control over transactions and auto-commits and things like that.

08:28 It's gotten to be pretty feature-rich.

08:30 It's cool.

08:30 It's a pretty dense article, but I think it's a good throw yourself into the deep end if you want to jump into SQLite 3.

08:38 Yeah, yeah, it's definitely cool.

08:39 It's certainly a good way to get started so you don't have to worry about extra servers and network connectivity and keeping that safe and all that.

08:46 Very cool.

08:46 So before we get on to the next item, I want to talk quickly about DigitalOcean.

08:52 This website that hosts the podcast, delivers the podcast feed, and a lot of the other stuff that I'm doing online runs on DigitalOcean.

08:58 Very, very nice experience.

09:01 They've got incredibly fast, reliable, and cheap servers.

09:05 Five, ten dollars.

09:06 You can have servers based on SSDs.

09:09 Really up and running in 30 seconds.

09:12 You just SSH in.

09:13 Get them all set up and ready to roll.

09:15 So if you want nice, fast servers, check them out at DigitalOcean.com and let them know that we sent you their way.

09:22 And they've been really good about sponsoring the show, and I really appreciate that.

09:25 It's great.

09:26 Yeah, definitely.

09:26 Thank you.

09:27 Thank you, DigitalOcean.

09:29 One of the things I think many people got their first programming experience on is a graphing calculator.

09:36 Right?

09:36 Remember back when you were in middle school or something, you had like a TI, whatever.

09:40 You could make it do dumb things.

09:42 Well, yeah.

09:43 I even started programming my HP calculator.

09:45 It didn't even graph.

09:46 So I was programming that.

09:48 Yeah, exactly.

09:49 Yeah.

09:49 So HP or TI instruments or whatever.

09:53 One of the really cool finds we've dug up recently is this graphing calculator by this company called NumWorks.

10:02 And you might think, okay, why is this graphing calculator very interesting?

10:06 Well, the way that you program it is you program it in Python.

10:12 Yeah, that's cool.

10:13 It's really cool.

10:14 So the programming language literally is Python.

10:16 And it will do sort of visual math.

10:20 It's got even a free emulator so you can run it on your Mac or your PC and check it out there.

10:26 It does graphing, all kinds of stuff.

10:29 And then it even has a way for you to work with the hardware through gives you like some stats on 3D printing and things like that if you want to do other more hardware oriented things with it.

10:42 But definitely this concept of a full-blown graphing calculator where you program it in Python, that's awesome.

10:49 And hackable and all.

10:50 And yet it's still supposedly it's going to be okay for use on the SAT even in August next year.

10:58 Yeah, that's actually a big deal.

10:59 Like I know some of the graphing calculators are banned because people use them to cheat or they do too much or whatever.

11:04 Yeah.

11:04 At least for now until they figure out how pip install SAT helper.

11:09 Yeah, exactly.

11:10 Nice.

11:11 Yeah, it's pretty fun.

11:12 Check that out.

11:12 It's nice to see that showing up in the calculator space because that really is like the first programmable things lots of kids really have to interact with.

11:19 We don't really see it too much anymore.

11:20 But there's well in consumer things.

11:23 But in in test measurement, we see some programmable features show up in different sorts of devices.

11:30 And having the programmable language be Python in more places is good.

11:34 Yeah, definitely.

11:35 We talked about last week having Python be the programming language of Excel.

11:39 Right.

11:39 For example.

11:40 Right.

11:40 Yeah.

11:41 It seems like a really, really great choice for if you want to add a little programmability to whatever it is you're doing.

11:47 Python seems like a great choice for that language.

11:50 So nice to see it here as well.

11:51 It's nice.

11:52 One of the problems you might have with the data science space, if you work with a Jupyter notebook and you just have access to the notebook, but you need a library that's not on the server, what are you going to do?

12:02 I didn't know this was a problem, actually.

12:04 I haven't been using Jupyter notebooks enough to run into this issue.

12:08 But a lot of people get their Python and Jupyter from installing like a Conda package or some other bundled thing.

12:19 And you can't just go off and pip install.

12:21 I didn't know you couldn't.

12:23 But Jake Vanderplass, he wrote up this article on installing Python packages.

12:30 And there's a couple, I'm not even going to try to write this, but I pulled out some of the cheats for pip and Conda and on how to install from within it.

12:40 And there's some magical incantations.

12:42 But the article also goes through about all the different reasons why you have to do this.

12:46 Yeah.

12:46 And they're not obvious at all.

12:48 Like I would have never figured those out.

12:50 Yeah.

12:50 So it's good that somebody figured it out.

12:52 Yes.

12:53 Thank you, Jake.

12:55 That's awesome.

12:55 So if you're doing data science and Jupyter notebooks, this is really, really cool.

12:59 So last thing I want to share for us is the videos from PyCon DE, as in Deutschland, 2017 are online.

13:08 So Miroslav, let us know on Twitter about this.

13:12 And there are a ton of interesting talks over here.

13:15 So quite a bunch of cool talks.

13:19 I'm not sure how many, but I would, I don't know, guess 50 or so.

13:23 Are they all in German?

13:24 Here's the thing.

13:25 They're, as far as I can tell, I've only seen English ones.

13:28 And I looked through a bunch of them.

13:29 So there's a cool talk called Technical Lessons from Pythonic Refactoring at Yelp by a woman named Yenny Chung.

13:39 And a bunch of other ones.

13:41 It's kind of hard to read all the titles right here, but I've looked through and I'm definitely filling up my playlist with stuff that I need to start watching because there's a lot of cool stuff here.

13:51 Eve Hilpisch, who was on my podcast, talks about why Python has taken over in finance, for example, right?

13:58 And we don't even have it in Excel yet.

14:00 So there's lots of cool stuff here.

14:02 It was in Karlsruhe in Germany, which is a lovely place.

14:05 I wish I could have gone to the conference, but second best thing, watch it online.

14:08 I'm really glad that we have those.

14:10 Can't wait.

14:11 Yeah, all the Pycons do such a good job of getting their content online straight away, you know, within a day or two of the presentation.

14:17 So it really makes a big difference, especially since they sell out.

14:21 Yeah, definitely.

14:22 You were definitely going to be at, I'm going to be at the one in Cleveland, Pycon US.

14:26 Are you going to make it?

14:26 Yeah, I think so.

14:27 That's the plan.

14:28 That's the plan.

14:29 It's going to be fun.

14:29 All right.

14:30 But for now, we'll enjoy the ones in Germany.

14:32 Any news into the year?

14:34 Into the year.

14:35 Yeah, no, I'm trying to come up with some fun Python projects to work on in my free time.

14:40 Get a Raspberry Pi and do something with it.

14:43 Maybe plug it into Home Assistant.

14:44 Things like that.

14:46 But I haven't done anything.

14:47 Yeah, me either.

14:48 I've got two of them sitting there.

14:50 But yeah.

14:51 If you could just put some code on them and make them do some cool stuff.

14:54 Right on.

14:54 All right.

14:55 Well, Brian, thanks again for sharing the news with everyone.

14:59 It's great to chat with you as always.

15:00 Yeah, thank you.

15:01 Thank you for listening to Python Bytes.

15:03 Follow the show on Twitter via at Python Bytes.

15:06 That's Python Bytes as in B-Y-T-E-S.

15:09 And get the full show notes at pythonbytes.fm.

15:12 If you have a news item you want featured, just visit pythonbytes.fm and send it our way.

15:16 We're always on the lookout for sharing something cool.

15:20 On behalf of myself and Brian Okken, this is Michael Kennedy.

15:22 Thank you for listening and sharing this podcast with your friends and colleagues.

Back to show page