- Podcasts
- Python Bytes
- #109 CPython byte code explorer
About this episodeTopics covered in this episode: [play:1:01] Python Descriptors Are Magical Creatures [play:3:38] Data Science Survey 2018 JetBrains…
- [play:1:01] Python Descriptors Are Magical Creatures
- [play:3:38] Data Science Survey 2018 JetBrains
- [play:8:04] cache.py
- [play:11:54] Setting up the data science tools
- [play:14:03] chartify
- [play:15:23] CPython byte code explorer
- Extras
- Joke
Transcript
From the publisherThis transcript comes from the podcast’s own feed, shown as the publisher wrote it.
#109 CPython byte code explorer
Hello and welcome to Python Bytes, where we deliver Python news and headlines directly to your earbuds. This is episode 109, recorded December 10th, 2018. I'm Michael Kennedy. And I'm Brian Okken. And this episode is brought to you by DigitalOcean. Thank you, thank you, DigitalOcean. Tell you more about them later.
Right now, Brian, how is everything going? It's going really well. How about you? Oh, it's super. I'm starting to think of year in review, like what was the most amazing Python stories of the year and things like that. So looking forward to sharing those with everyone, actually. Yeah, that'd be great.
Yeah. So you and I actually did an episode along with Dan Bader on Talk Python, which we'll drop here on this channel as well for the year in review in Python news, which is like this, but more stuff in more depth. So that'll be a good thing for, you know, all those people traveling for the holidays, right?
Yeah. Give them something to listen to. All right. They may be stuck in an ice storm in Chicago, O'Hare, but they can listen to some good Python news. Yeah. That's right. Speaking of good Python news, what do you got to kick us off this time? I have a Python descriptor. I have Python descriptors are magical creatures.
And if you read from it, that'll call the getter and you can have custom functions for that. It's one of the cool things about Python. And, and one that I'm glad that it's been highlighted because some people forget this is around, especially if you come from a language that doesn't have this sort of thing.
But the consumer doesn't care. They don't have to know. Yeah. And I personally actually have used a get and set methods before, but the getters and setters, but there's a deleter also. And I don't think I use that very much. And it's kind of probably a neat thing to stick in place if you're doing this anyway to make sure if it's invalid for somebody to try to delete an attribute, you may want to intercept that.
So we've talked about the JetBrains Python survey and that data science featured heavily in it. But they also did a separate data science survey for just data scientists and asked data science-y questions only. So they pulled about 1,600 people who are data scientists based in the U.S., Europe, China, and Japan.
So one of the key takeaways was that most people assume, or currently most people use Python, and then they assume that Python will remain the primary programming language at least for five years. Yeah, and that's essentially forever in computer time. That's right. If you're planning past five years, you've got either a lot of faith in where things are going or you're doing it wrong.
Those actually could be the same thing. All right. They also talked about what are the main tools people are using for machine learning stuff. And they said Keras is the main one for professional developers. Whereas if you're an amateur data scientist, you're more likely to use Microsoft Azure machine learning services rather than libraries.
Like machine learning is a service. I haven't ever used it, though. Okay. So let's see. Main programming languages. Obviously, there are other languages. And if you look back just a couple years, right, R was a machine learning and data science-y language that was more popular than Python was for data science.
PyCharm was 38%. And RStudio was 23%. So that's pretty interesting. Yeah. Yep. All right. So if you're in the data science space, maybe this will help you keep your pulse on, keep the pulse of what's going on there. I want to highlight a little tool. So like I talked about properties just as a nice technique of people should make sure they understand how those work.
You have to get your answer to that. If you find the, if you get past the same arguments again, just return the answer that you've already calculated. If you have some function that you're calling with relatively bounded set of inputs and it's at all computationally expensive or it goes to a service and it gets an answer back.
Like you said, if the input is the only thing that drives it, it's not like, well, what's the weather at the zip code? Because that could always change. But it's like, you know, what's the limit of this integral when passing in, you know, this lower bound, you know, like discrete integral or something.
Right. And that, that only works in process. Yeah. One of the things I wanted to highlight is a project called cache.py that saves all this stuff off to a file. This would be helpful, especially if you've got a, like a command line tool that gets called lots of times. It isn't going to be able to store everything in memory.
So being able to save it in a file might be helpful. The interface is just a decorator to say, hey, this function, you can cache the results. So you throw a decorator on it called, it's just cache.cache. Decorator onto your function. And it just works. And there's a whole bunch of customization you can do.
But like you said, if this is a command line tool you're stringing together and you want it to keep that data for a certain amount of time or just always keep it so that it's like, well, if you pass me seven, the answer is always going to be this. Right? Yeah. That's, yeah. It's great that that'll keep it on the file system.
And it uses pickle, right? I'm not sure. Yeah. Let's see. Yeah. Currently uses pickle and inspect under the hood, making it not portable. So you can't like take your cache file and move it to windows when you ran it on Linux or something, I believe. Yeah. Because of memory structure and different versions of Python and so on.
So what, remind me, what was the built-in one that works in memory? It's on functools and it's an LRU cache, I believe. Okay. Functools, LRU cache. Yeah. Yeah. Yeah. I brought this up also mostly because I know a lot of people teach them, learn on the job or teach them. I'm not bragging that I have a computer science degree, but this is one of those topics that you probably don't come up with on your own.
It's a clever thing and a nice, useful tool for your toolbox, but it's not something that's obvious. It wasn't obvious to me until I learned about it. Yeah. Same here. I think the first time I learned about this was when I started studying design patterns and stuff like that. And somehow it came up in there.
I'm like, oh, that's pretty clever. Yeah. Yeah. When you are working with code and it's slow, to me, it seems like there's two things that are really, really powerful that can just go, oh, well, now it's a hundred times faster. That's cool. And that was like one line of code. You know, one is using the wrong kind of data structure.
Like if you're using a list, but you really should use a set because you're testing for membership on a big set, something like that or dictionaries or whatever. The other one is this kind of caching, right? Like if you're doing something and it takes a long time, even if it's going out to the internet and calling a service, like if you think that data changes once a day, it'd be totally great to put like a one minute cache on that.
If you're calling it a bunch of times. Yeah. And it can, like you said, it can, it can make a massive improvement speed up and it's like sort of an obvious of, you know, after you see it, you're like, well, yeah, duh. I didn't even think of that. Absolutely. So I really think, I think this is a cool one because it takes that idea and it just makes it easy to carry it across different processes or different runs of the same process.
Okay. So before we get on the next one, let me just tell you all about DigitalOcean. They're doing all sorts of cool stuff. Our infrastructure runs on it. Really, really nice and reliable. One of the things I want to highlight this time is their work with Kubernetes, Docker and coordinating Docker, orchestrating Docker stuff with Kubernetes is a big deal these days.
And so they're launching a new Kubernetes cluster over at DigitalOcean. So a really nice way to manage and deploy your container workloads in the cloud. And if you go to pythonbytes.fm/DigitalOcean and you're a new user, you get $100 credit to Kubernetes all the way if you want. You can run a lot of Kubernetes for $100 in the cloud.
Last week, I covered an hour and a half video about being an expert on Python. How about we cut this down to like a four minute one? So I think this one is really good for people who are getting into data science and they have a little bit of a challenge. If you're an expert, this is definitely not the video for you.
But this is called Setting Up the Data Science Tools. And so it's part of a larger video series. But it basically shows you how to set up the Anaconda distribution, TensorFlow, Keras, Jupyter, all those things. And it actually talks about using Conda, Conda virtual environments, creating notebooks and switching between virtual environments.
Cool. Yeah, it's great. I was just talking to somebody who was really new to Python and super eager to get going. But he was having a problem because he was working on a computer that he didn't have admin access to. And so when he would try to pip install something, it would try to put it in the system-wide thing, which you'd have to to make that happen.
And also, somebody like me that is used to virtual environments, it's still not obvious how to do that in an Anaconda environment. Exactly. I have to look it up every time because I'm all about pip. And I was like, wait a minute. It's a different way to activate. It's like a global activate command.
Where's the list? How do I know what exists? Yeah, it's different. So I'm sure I could actually use this as well. Cool. Beginner means beginner to Anaconda in data science tools. Not true beginner, right? Yeah. Awesome. All right. Speaking of data science, I bet data scientists draw a lot of graphs, right?
Yeah. Well, lots of people draw a lot of graphs. Last time I tried to use Boca or Bokeh. I keep saying that wrong. You don't need to email me that I'm saying it wrong. I know it's Bokeh. It's B-O-K-E-H. It is a very powerful charting tool. I believe it's not the most simplest interface to figure out as a newbie.
I do find it a little overwhelming because you can do everything, right? You can specify so much detail. I'm like, sometimes I'm just like, you know, I could just use like a histogram. Wouldn't that? That would be awesome. Can we just do a histogram? Yeah. And if I got a bunch of different, you know, I want to be able to pick the colors fairly easily.
And I don't really care, but I just want it to look nice. Yeah. They also have a bunch of nice examples, example notebooks and stuff that walk you through using it. So yeah, it's a great little resource. Yeah. Speaking of Jupyter and examples and notebooks and stuff, I want to stick with that for the last one here.
So it compiles your source code into bytecode. And those bytecodes are interpreted on top of the CPython, like a big loop that just runs goes, okay, what's the next bytecode? Let's do that. So understanding what those bytecodes are, how complex is something? Is it an atomic operation or does it take multiple steps?
All of those things you might wonder about. So this was sent to us by Anton Helm. And it's created by this guy named Jeremy Tulup. And what it is, is it's a plugin for JupyterLab, not Jupyter Notebooks, but the more full-featured JupyterLab. And what it does is it lets you look at the bytecode of various things that you're, various operations that you're working on.
So if you pull up that thing, Brian, the link there, you can see there's a little animated gif that shows you what's happening. So it's creating like an A, B, and a C equals A plus B. And there's just on the right as you type, it just shows you the bytecode of those. So I think this is a great way to explore working with Python.
If you want to understand more of this low-level bytecode thing. Yeah. This would be awesome just in teaching, especially if you're going to talk about how names work in Python. This would be kind of fun to use to see how it all points to the same thing and whatnot. Yeah. Another example that's cool, if you go to the very bottom, there's a bunch of little animated gifs here.
And the very bottom one shows the two operations looping over just the numbers 0 to 9. And you can either do this by a while loop, you create the while loop, and you have i less than 10, i plus equals 1, or you can just say 4i in range of 0 to 10. And they show it side by side comparing the disassembled bytecode of both of them.
And surprise, surprise, the foreign loop is a lot fewer bytecode operations. So it's probably faster. That's cool. Cool, right? There's even a demo that shows that you can see the, have Python 3.6 and Python 3.7 running side by side. Yeah. In the same JupyterLab view, you can have different versions of Python with the same code to understand how bytecodes have evolved over time.
So somebody passed around this little video called How the Internet is Made. And we're going to put a link to it. And it's hard to describe, but it's just this complete silliness of these like old time videos of how things are made and stuff. It gets shipped from here to there and gets rolled across the field with barrels and stuff.
Yeah. And it starts in Austria, I believe. So the internet is mined in Austria. It's put into a special internet wheelbarrow, which is pretty trippy. It's like a hovercraft. It's mixed up into like a gray goo and then it's shipped off along these pipes. Anyway, it's a good joke. People can check it out.
So it's not going to upset folks at work, right? It's all about just the visuals. Well, I think it was a good one, Brian. And I'm glad I forced you to put it in there. So next week, we've got a kind of a year in review thing that you're putting in, right? Yeah, absolutely. So you and I had recorded a Talk Python year in review, top 10 Python stories of the year, not just of the week.
And that's coming out next time. So be sure to check that out. And it'll be a lot of fun. Yeah. Nice. Okay. All right. Well, thank you for doing all of this year with me, Brian. Yeah. Thank you. You bet. Bye. Bye. Thank you for listening to Python Bytes. Follow the show on Twitter via at Python Bytes.
