Latest / Talk Python To Me / #275: Beautiful Pythonic Refactorings
Transcript
- 0:00Do you obsess about writing your code just the right way before you get started?
- 0:03Maybe you have some ugly code on your hands and you need to make it better.
- 0:07Either way, refactoring could be your ticket to happier days.
- 0:10On this episode, we'll walk through a powerful example of iteratively refactoring some code
- 0:16until we eventually turn our ugly duckling into a Pythonic beauty.
- 0:19Connor Hoekstra is our guest on this episode to talk us through refactoring some web-scraping Python code.
- 0:25This is Talk Python To Me, episode 275, recorded July 9th, 2020.
- 0:30Welcome to Talk Python To Me, a weekly podcast on Python, the language, the libraries, the ecosystem, and the personalities.
- 0:49This is your host, Michael Kennedy.
- 0:51Follow me on Twitter where I'm @mkennedy.
- 0:54Keep up with the show and listen to past episodes at talkpython.fm.
- 0:57And follow the show on Twitter via at Talk Python.
- 1:00This episode is brought to you by us over at Talk Python Training.
- 1:04Python's async and parallel programming support is highly underrated.
- 1:09Have you shied away from the amazing new async and await keywords because you've heard it's way too complicated
- 1:14or that it's just not worth the effort?
- 1:17With the right workloads, a hundred times speed up is totally possible with minor changes to your code.
- 1:23But you do need to understand the internals.
- 1:25And that's why our course, Async Techniques and Examples in Python, show you how to write async code successfully as well as how it works.
- 1:33Get started with async and await today with our course at talkpython.fm/async.
- 1:39Connor, welcome to Talk Python To Me.
- 1:41Thanks for having me on.
- 1:42Excited to be here.
- 1:42I'm excited too.
- 1:43It's going to be beautiful, man.
- 1:44Hopefully.
- 1:45Hopefully.
- 1:47Yeah.
- 1:47It's going to be a beautiful refactorings.
- 1:49So I am a huge fan of refactoring.
- 1:51I've seen so many people try to just overthink the code that they're writing.
- 1:57They're like, well, I got to get it right.
- 1:58And I got to think about the algorithms and the way I'm writing it and all this stuff.
- 2:02And what I found is you don't really end up with what you want in the end a lot of times anyway.
- 2:07And if you just go in with an attitude of this code is plastic, it is malleable, and I can just keep changing it.
- 2:13And you always are on the lookout for making it better.
- 2:15You end up in a good place.
- 2:16Yeah, I completely agree.
- 2:17Refactoring is not a one-time thing or something that happens only, you know, two years from when you initially write the code.
- 2:23I heard once actually that refactoring goes a lot in hand with legacy code.
- 2:29And there's a number of different definitions for legacy code.
- 2:33But one definition is legacy code is code that isn't actively being written.
- 2:38So if you write something once and then you consider it done, and then the next week, like no one's working on it, that technically, according to that person's definition is legacy code.
- 2:46So that can be refactored.
- 2:48You know, you can refactor something you wrote earlier in the day.
- 2:50It doesn't have to be a year later or 10.
- 2:52Yeah, absolutely.
- 2:53I mean, you just, you get it working, you know a little bit more, you apply that learning back to it.
- 2:58And with the tooling these days, it's really good.
- 3:01It's not just a matter of, you know, if you go back to 1999 and you read Martin Fowler's refactoring book, he talks about these are the steps that you take by hand to make sure you don't make a mistake.
- 3:12And now the steps are highlight, right click, apply, refactoring.
- 3:16I mean, that's not 100% true.
- 3:18And the example we're going to talk through is not like that exactly.
- 3:21But there are steps along the way where it is, potentially.
- 3:24Definitely.
- 3:24Linters and static analyzers are heavily underutilized, I feel.
- 3:29And so many of them will just automatically apply the changes that you want to do.
- 3:32And it's fantastic for huge code bases.
- 3:34It would be almost impossible to do it by hand.
- 3:37Yeah, absolutely.
- 3:38It would definitely be risky.
- 3:39So maybe that's why people sometimes avoid it.
- 3:42Now, before we get into that, though, let's start with your story.
- 3:44How do you get into programming into Python?
- 3:46I know you're into a lot of languages.
- 3:47We're going to talk about that.
- 3:48But Python too?
- 3:49Python also?
- 3:49Yeah.
- 3:50So the shorter, it's a long story, but the shorter version of it is my degree in university,
- 3:56which wasn't computer science, required at least two introductory CS courses.
- 4:02So the first intro course was in Python.
- 4:04The second one was in Java.
- 4:06And then I ended up really, really enjoying the classes.
- 4:09I ended up taking a couple more, but ultimately stuck with the career that I had entered into,
- 4:15which was actuarial science.
- 4:17That's like insurance statistics.
- 4:18Yeah.
- 4:19So you were in some form of math program, I'm guessing?
- 4:21Yeah, yeah.
- 4:22Yeah, cool.
- 4:22It's very, very boring to explain.
- 4:25But if you like math, it's a great career.
- 4:27Yeah, awesome.
- 4:28And so I ended up, for my first job at a university, I ended up working at a software company, basically,
- 4:34that very simply explained, created the insurance calculator that many insurance companies use.
- 4:41And after working there for about four or five years, I had just fallen in love with the
- 4:47software engineering side of my job and had decided that I wanted to transition full time to like a purely technical company.
- 4:54So it's several years or a couple years later.
- 4:58And now I work for NVIDIA as a senior library software engineer.
- 5:02And that's how I got into programming.
- 5:04And our code base that we work on is it's completely open source and primarily uses C++14 and Python 3.
- 5:11That's where Python enters.
- 5:13That sounds like a dream job.
- 5:14Yeah, that sounds awesome.
- 5:15Yeah, I absolutely love it.
- 5:16Yeah, so you're working on the Rapids team, right?
- 5:19Which works on doing a lot of the computation that might be in Pandas, but over on GPUs.
- 5:24Is that roughly right?
- 5:25Yeah, that's a great description.
- 5:27So yeah, within NVIDIA, I work for an organization called Rapids.
- 5:31We have a number of different projects.
- 5:33So specifically, I work on CUDF.
- 5:36That is C-U-D-F.
- 5:37So the CU is two letters C-U from CUDA, which is like the parallel programming language that NVIDIA has made.
- 5:46And the DF stands for data frame.
- 5:48And so this is basically a very similar library to Pandas.
- 5:53The difference being that it runs on the GPUs.
- 5:56So sort of the one liner for Rapids is it's a completely open source, end-to-end data science pipeline that runs on the GPUs.
- 6:03So if you're using Pandas and it works great for you, like there's no reason to switch.
- 6:07But if you run into a situation where you have a performance bottleneck, CUDAF can be like a great drop-in replacement.
- 6:13We don't have 100% parity with like the Pandas library, but we have enough that a lot of Fortune 500 companies that pick up and use us are able to very easily transition their existing code in Pandas to CUDAF.
- 6:25Right.
- 6:25Change an import line, go much faster, something incredible like that.
- 6:28That's the goal.
- 6:29That's the dream.
- 6:31Yeah, I just recently got a new Alienware, a high-end Alienware desktop.
- 6:35And it's the first GeForce I've had in a long time that's, you know, not like, I don't know, some AMD Radeon in a MacBook or something like that.
- 6:43So I'm pretty excited to have a machine that I can now test some of these things out on at some point.
- 6:47Yeah.
- 6:47Acceleration on different devices is, it is very exciting.
- 6:51Awesome.
- 6:52All right.
- 6:53Well, let's start by introducing real briefly a little bit about refactoring.
- 6:58We've talked a tiny bit about it in general.
- 7:00And then we're going to dive into a cool example that you put together that really brings a lot together.
- 7:06And what I love about your example is it's something you've just gone and grabbed off the internet.
- 7:11It's not like a contrived, like, well, let's do this and then unwind the refactorings until it does it.
- 7:16It's like you just found it.
- 7:17And like, well, let's see what this thing does.
- 7:19That's going to be fun.
- 7:19But let's just start with a quick definition of refactoring.
- 7:23Maybe how do you know when you need it?
- 7:25How do you know when you need refactoring?
- 7:27For me, I have a sort of number of anti-patterns in my head that when I recognize them in the code, some people might refer to them as sort of technical debt.
- 7:37This idea that the first time you write things or maybe initially when you write things, you don't have the full picture in mind.
- 7:42And then as time goes on, you start to build up technical debt in your code.
- 7:46And a refactoring can be reorganizing or restructuring your code or rewriting little bits of it to basically reduce tech, to make it more readable, maintainable, scalable, and just in better, in general, better code.
- 7:58That's sort of the way I think of it.
- 7:59Yeah, it is pure sense, right?
- 8:01It should not change the behavior, at least in terms of like inputs, outputs.
- 8:05Exactly.
- 8:06Yes.
- 8:06The easiest code to refactor is code with tests, whether that's unit tests or regression tests or any of the other number of tests that there are.
- 8:14If you have a code base that has zero tests, refactoring is very, very dangerous because you can refactor something and completely change the behavior and not know about it, which is not ideal at all.
- 8:24Somewhat suboptimal, indeed.
- 8:26You know, Martin Fowler, when he came up with the idea of refactoring, or at least he publicized, I don't know, I'm sure the ideas were basically there before.
- 8:33One of the things that struck me most was not the refactorings, but was this idea of code smells.
- 8:40And it's like this aesthetic of, right, like I look at the code and it, yeah, it works, but like your nose kind of turns out, you're kind of like, ew, no, ew, but it still works, right?
- 8:49It's like not broken, but it's not nice.
- 8:53And, you know, there's all sorts of code smells like too many parameters, long method, things like that.
- 8:59But they rarely have clear cutoffs, right?
- 9:02Like, well, if it's over 12 lines, the function is too large, but under that, it's totally fine, right?
- 9:06Like that's not, it's never really super clear cut.
- 9:08So I think this whole idea of refactoring, much like refactoring itself, requires like going over it and over it as sort of through your career to refine, like what the right aesthetic to achieve is.
- 9:20And it probably varies by language as well a little.
- 9:22Yeah.
- 9:22If you start to do it like consciously when you're looking at code and asking yourself, like when you have that code smell feeling like something's not right here.
- 9:31If you are conscientiously like paying attention to what it is, like slowly over time, you will start to pick up on exactly what it is about it.
- 9:37Like a very, very small one for me.
- 9:39And I think this is mentioned in maybe a clean code or it might've been Martin Fowler's book.
- 9:44It's like declaring a variable earlier than it needs to be declared.
- 9:48So you might declare like all your variables at the top of a function, but then like two of them you use immediately.
- 9:53But the other three you don't use until the last, you know, four lines of the function.
- 9:57Small things like that.
- 9:58It seems simple, but I've made the change where I've put the declaration closer to where it's get used.
- 10:03And then you realize, oh, wait a second, this isn't actually reference.
- 10:07Like it's set to something, but then it's not actually used later on.
- 10:10So I can just delete this.
- 10:11And it's because it was at the top of the function.
- 10:12You can't see where it's being declared or if it's used somewhere else that like you actually just have an, a phantom unused variable that can be deleted.
- 10:20It's simple things that lead to better changes later on.
- 10:24Well, and just mental overhead.
- 10:26Like you said, the technical debt side of things.
- 10:28So for example, there's the variable that was at the top.
- 10:32Literally when the code was written, it was being used, but it's been modified over the years.
- 10:37And now no longer is it being used, but because it's separated from where it's declared to where it's used.
- 10:41You don't want to mess with that.
- 10:43Like if you start messing with that, you're earning more work, right?
- 10:47You're asking for more.
- 10:47I'm just going to make the minor change.
- 10:50I don't want to break anything.
- 10:51Who knows?
- 10:51And then the next person that comes to try to understand it, they got to figure out, well, why is there that like set count variable?
- 10:58Like, I don't feel like it's being used, but it's there.
- 11:01And like, you know, you just got to, it's another thing to think about that's in the way.
- 11:04Yeah, for sure.
- 11:05Yeah.
- 11:06So certainly I think it's viable.
- 11:07There are fantastic tools that will like highlight this variable is unused or this assignment.
- 11:13Is it meaningless or something like that?
- 11:15So there are options, but still it's better to not let that stuff live in the code.
- 11:20Yeah.
- 11:21A hundred percent agree.
- 11:21Let's talk about this example that you've got here and maybe you should give a little background on your language, enthusiasm and programming competition interests and so on.
- 11:33Your interest in coding competitions, I think it's probably worth touching on already.
- 11:38But then this example is from you trying to reach out and understand it and do some analysis of those environments or those ecosystems, right?
- 11:46What's the background with this, these different languages and coding competition?
- 11:50Yeah, so I initially got into competitive programming, quote unquote.
- 11:54So just the one sentence description is there's a number of websites online, HackerRank, LeakCode, CodeForces, that they host these one to three hour contests where they have three to four or five problems that start out easy and then get harder as you progress through them.
- 12:09And you can choose any language you want to solve them in.
- 12:11And the goal is just to get a solution that passes as quickly as possible.
- 12:16So it's not necessarily about how efficient your code is.
- 12:20It has to run within a certain time limit.
- 12:22But if you can get it to run or pass in Python versus C++ versus Java, any code solution works.
- 12:28I started doing these to prepare for technical interviews.
- 12:31So if you're interviewing for companies like Google, Facebook, etc., a lot of their interview questions are very similar to the questions on these websites.
- 12:38And so I at one point was looking for a resource online, like for YouTube videos that just explain this stuff.
- 12:44But at the time, I couldn't really find any.
- 12:46So I started a YouTube channel covering the solutions to these problems.
- 12:50And I thought it would be better to solve it in a number of languages than opposed to just C++.
- 12:55So I started solving them in C++, Python, and Java.
- 12:59And that's sort of what led to my interest in competitive programming.
- 13:03Even though I'm not interviewing actively anymore.
- 13:06I just find these, they're super fun.
- 13:08It keeps you sort of on your toes in terms of your data structure and algorithms knowledge.
- 13:12And you can treat them as like code katas.
- 13:15I'm not sure if you're familiar with the concept of just sort of writing one little small program and trying it a couple times in different languages.
- 13:22And you learn different ways of solving the problem that you might not would have initially solved the problem that way.
- 13:28This example, I decided to just figure out what are the top languages that people use to solve these competitive programming problems on a given website.
- 13:38So the site that I chose was Code Forces.
- 13:40Yeah.
- 13:41And you're like, hey, I'm working on this new data frame library that's like pandas.
- 13:45Let me see how I can use pandas to solve this problem and get some practice or something, right?
- 13:49Yeah, yeah.
- 13:50So when I had just started NVIDIA, I knew that the pandas library existed, but I had zero experience with it.
- 13:56And I knew that it had this sort of group by reduction functionality that if you had a big table of elements, you could get these sort of statistics on, you know, what's the top language or what's, you know, the average time it takes for people to submit very easily with this kind of library.
- 14:10So I thought, what better way to learn pandas than by trying to build a simple example that uses this library for something that I'm interested in.
- 14:18And so the first thing that I did was I googled, you know, how to scrape HTML tables using pandas.
- 14:24And then it brought me to this blog that at the end of the day has about 60 lines of code.
- 14:29And it's a tutorial blog.
- 14:30So it walks you through how to get this code off of an HTML table.
- 14:36And basically the PyCon talk that I gave, it came out of doing this.
- 14:40I had no plans of giving a PyCon talk on this.
- 14:43I just after having gone through it and sort of refactoring one by one, I realized that like I could give a pretty simple talk to like Connor like five years ago.
- 14:53That didn't know about any of the I didn't know about list comprehension.
- 14:57I didn't know about enumerate.
- 14:58I didn't know about all the different techniques I was using.
- 15:00And I figured it would be at least for some individuals out there.
- 15:04It would be a useful talk highlighting the things that I didn't know when I first started coding in Python.
- 15:09But that now are like second nature for me.
- 15:12And that's sort of where the talk came from.
- 15:13Yeah.
- 15:13And it's really interesting.
- 15:14The example is cool.
- 15:15I do think that a lot of the refactorings were let's try to make a more Pythonic version of this and more idiomatic.
- 15:23version of this like misunderstanding the for in loop, for example, and treat them all right.
- 15:29So in a lot of ways, it's a cool refactoring.
- 15:32But it's also kind of leveraging more of the native bits of the language, if you will.
- 15:37Absolutely.
- 15:38Yeah.
- 15:38Yeah.
- 15:38So you went and grabbed this code and it does two basic things.
- 15:42It goes and downloads some HTML and then pulls it apart using, I think, LXML HTML parser.
- 15:50And then it's going to loop over the results that it gets from the HTML parser and turn this into basically a list or a dictionary.
- 15:59Then you're going to feed that over to pandas.
- 16:00Ask pandas some pretty interesting questions.
- 16:03And most of the challenge or most of the messy code lived in this HTML side of things, right?
- 16:09Yeah.
- 16:09That's a pretty good description of what's happening.
- 16:11Cool.
- 16:12So let's go and just talk through some of the issues you identified and then the fix, basically knowing like how did you identify that as a problem?
- 16:21And then what fix did you apply to it?
- 16:25Now, there's a lot of code and it's hard to talk about code and audio.
- 16:28So we'll maybe try to just like as high level as possible, talk about like the general patterns and what we fixed.
- 16:33The first part of the code would go through and it would create an empty list and it would create like an index to keep track of where it was and then did a loop over all of the elements.
- 16:42Increment the index, add a thing to the list, print out some information as it went, right?
- 16:46Yep.
- 16:46And I think the first thing that you talked about was the code comments, actually.
- 16:51You're like, what is this code comment here?
- 16:53It just says we're looping over these things.
- 16:55Well, what do you think a loop is?
- 16:56Why do we have this comment?
- 16:57Yeah, even worse was like arguably the second comment some might argue is add some value.
- 17:03But the first comment above the line that creates an empty list, it says create empty list.
- 17:10And it's only a what is that six characters if you don't include the spaces.
- 17:13And I think that's definitely one of the things that's called out in a number of refactoring books is comments should add value that is not explicitly clear from the code.
- 17:24I think even beginners are able to tell that you're creating an empty list there.
- 17:28There's no reason to basically state what the code is doing.
- 17:31Typically, comments should say why if it's not clear why something is being done a certain way or something that's implicit and not explicitly clear from what the code is doing.
- 17:40Yeah.
- 17:40In terms of refactoring, I love this idea of these comments are sort of almost warning signs.
- 17:46Because if I find myself writing one of these comments to make stuff more clear, I'm like, wait a minute, wait a minute.
- 17:51If this is just describing what's here, something about what I'm doing is wrong.
- 17:56Maybe the variable name is not at all clear what the heck it is.
- 17:59Or maybe it could use a type annotation to say what types come in instead of here's a list of strings.
- 18:03Like how about list bracket string goes there to just say what type it is.
- 18:06It's Python 3 after all.
- 18:07And, you know, from the Code Smells book, Fowler had this great description of calling these types of comments deodorant for Code Smells.
- 18:16So there's something wrong.
- 18:18It smells a little less bad if we like lay it out, set the stage.
- 18:21But every time I see one of those, I'm like, you know what?
- 18:24I just need to rename this function to like a short version of what this comment would say.
- 18:27Or rename this variable.
- 18:29Or like restructure and break these things apart.
- 18:31Because if it needs a comment, it's probably just too complicated.
- 18:34There's an individual in the C++ community.
- 18:36His name is Tony Van Eerd.
- 18:37And he has a rule, or not a rule, but a recommendation that you should grep your code base for step one, step two, step three.
- 18:45And guaranteed you're going to get like one or two matches.
- 18:48And a lot of times it's these steps of comments on top of pieces of code and like a larger function.
- 18:54And odds are you could make that code a lot better by refactoring each of those steps into its own small function.
- 19:00And just whatever the step, like if you put step one in a description, you've already given that piece of code a name.
- 19:05You just need to take the next step, put it in a function and give that function that name.
- 19:09Yes, exactly.
- 19:10Which is exactly what you said.
- 19:11Exactly.
- 19:12I think there was even some tool way, way, way back in the early days of C# that if you would highlight some code to refactor it and you highlighted a comment, it would function nameify.
- 19:23It would try to guess the function name by using the comment, turning it into a function, you know, like something that would work as a identifier in the language.
- 19:31Anyway, it's totally a good idea.
- 19:34So there's a couple of things going on here.
- 19:36One is like, why is there a print statement?
- 19:38Nobody needs this.
- 19:39Once you take that out, though, you are able to identify this.
- 19:42Well, let's take a step back.
- 19:43First, if you have an integer and you're incrementing it every time through the loop so that it stays in sync with the index of the elements you're looping over, that's probably not the best way to do it, right?
- 19:54Like Python has a built-in enumerate.
- 19:56Yeah.
- 19:56This is probably one of the most common things I see in Python.
- 20:00Sadly, in certain languages, they don't have this function.
- 20:04But in Python, it's right there built into the language.
- 20:07And as you mentioned, it's called enumerate.
- 20:09So you can pass whatever thing you're looping over to enumerate.
- 20:13And that's going to bundle it with an index that you can then inline destructure into an index and the element that you were getting from your ranged for loop before.
- 20:22So anytime you see an index, IDX or I or something that's keeping track of the index and that's getting it could be J.
- 20:31Sometimes it's J.
- 20:32Sometimes it's J.
- 20:32Sometimes it's K.
- 20:33X or Y if you're being really creative.
- 20:36And yeah, like there is a built-in pattern for basically avoiding that.
- 20:39And it makes me extremely happy.
- 20:41Like it happens actually not just once in this piece of code, but twice where you can make use of enumerate.
- 20:47And once you see it, it's very hard to unsee it.
- 20:49But like I said, this was something that I learned enumerate from Python.
- 20:53And this was not something that I knew of and I didn't learn in school.
- 20:57So there's a lot of Python developers and just developers in many languages out there that I think they're just not aware.
- 21:02And as soon as you tell them, I think they'll agree.
- 21:05Oh, yeah, this is way better than what I was doing before.
- 21:07Yeah.
- 21:07You just need to be aware of it.
- 21:09You know, you always run into these issues.
- 21:10You've got to create the variable.
- 21:11Then like, why is the variable there?
- 21:12Then you've got to make sure you increment it.
- 21:14Do you increment it before you work on that with the value or do you increment it after?
- 21:18Is it zero based?
- 21:19Is it one based?
- 21:20All of these things are just like complexities that are like, what is happening here?
- 21:23Like, what if you have a have an if test continue and you skip the loop, but you forget to increment it?
- 21:29Like there's all these little edge cases.
- 21:30And you can just with enumerate, you can say, you know, it's always going to work.
- 21:34You can even set the start position to be one if you want it to go one, two, three.
- 21:37Beautiful.
- 21:38Yeah, that's a great point.
- 21:39Yeah, there are use cases where you're going to run into bugs.
- 21:42Whereas with enumerate, you know, at least you're not going to have a bug with that index.
- 21:46Right.
- 21:46It's always going to be tied to the position with the starting place the way you want it.
- 21:50So yeah, yeah, that's really nice.
- 21:52But it's not super discoverable, right?
- 21:54Like there's nothing in the language that screams and waves its hands.
- 21:57It says, yeah, you're in a for loop.
- 21:59We don't have this concept of a numerical for loop, but this is actually better than what
- 22:03this is what you wanted.
- 22:04You didn't even know you wanted it.
- 22:05Yeah, it has to be something that you stumble across.
- 22:07Interestingly, some languages go is the one that comes to mind.
- 22:12They actually build in the enumerate into their range based for loop.
- 22:16So in Go, they have built in basically the destructuring.
- 22:21And if you don't want the index, if you just want a range based for loop and you want to
- 22:25ignore the index, then you're just supposed to use the underbar to say, I don't need the
- 22:29index for this loop.
- 22:30But it's interesting that like Go is a more recently created language than Python, at least.
- 22:35When they decided like they thought it was such a common use case that they would think that
- 22:40most people need it more often than they wouldn't.
- 22:42So they built it into their for loop.
- 22:43So with that language, you can't avoid learning about it because it's in their for loop.
- 22:48It's a syntax error to not at least say I explicitly ignore this.
- 22:51Yeah.
- 22:51Oh, interesting.
- 22:52I didn't know that about Go.
- 22:53So now you've got this little cleaner.
- 22:55You look at it again and you say, well, now what we're doing is we're creating a list,
- 22:59an empty list, which we commented, create empty list.
- 23:02That was cool.
- 23:03Took that comment out, but it was very helpful in the beginning to help you understand.
- 23:06No, just kidding.
- 23:06And then you say we're going to loop over these items and then append something to that
- 23:10list.
- 23:11Well, that's possible.
- 23:12But this is one of your anti-patterns that you like to find and get rid of, right?
- 23:18This is an anti-pattern that I call initialize, then modify.
- 23:21And actually, the enumerate example previously also falls into this anti-pattern.
- 23:27So anytime you have a variable that it doesn't need to be a for loop, but many, many times it
- 23:32is that inside each iteration of that for loop, you are then modifying what you just
- 23:37initialized outside.
- 23:38That is initializing and then modifying.
- 23:40And my assertion is that you should try to avoid this as much as possible.
- 23:44And when it comes to the pattern of initializing an empty list, and then in each iteration
- 23:48of your for loop, you're calling append.
- 23:50That is built in to the Python language as something that can be used as a list comprehension,
- 23:56which is so much more beautiful, in my opinion, compared to just a raw for loop and then
- 24:02appending for each iteration.
- 24:03Yeah.
- 24:03Every now and then, there's like a complicated enough set of tests or conditionals or something
- 24:09going on in there that maybe not.
- 24:11But I agree with you most of the time.
- 24:13That just means what I really wanted to write was a list comprehension.
- 24:17It is.
- 24:17So, you know, bracket item for item in such and such, if such and such, right?
- 24:22That's what you got to do.
- 24:24Yeah.
- 24:24List comprehension, once you start to use it, moving to a language that doesn't have it
- 24:27makes you very sad because it's such a convenient syntax.
- 24:31It totally makes you sad.
- 24:32And I really, really wish list comprehensions had some form of sorting clause because at
- 24:40that point, you're almost into like in-memory database type of behaviors, right?
- 24:45Like I would love to say projection, you know, thing, transform thing, for thing in collection,
- 24:50where the test is, order by, whatever, right?
- 24:54I mean, you can always put a sorted around it, but it'd be lovely if they're like, it's
- 24:58already got those nice steps.
- 24:59I like to write it on three lines, right?
- 25:01The projection, the set, and the conditional, like just one more line, put the order by in
- 25:06there, but maybe someone, or maybe I should put a PEP in there.
- 25:09Who knows?
- 25:10I was going to say that sounds like a future pep, but.
- 25:11It definitely does.
- 25:13I mean, it would be easy to implement, just transform it to a sorted and pass that as a
- 25:17key or something like that.
- 25:18But anyway, it would be really cool.
- 25:19But they're very, very nice, even without that.
- 25:23And once you have it as a list comprehension, then it unlocks the ability to do some other
- 25:29interesting stuff, which you didn't cover in yours because it didn't really matter.
- 25:32But if you have square brackets there, and those brackets are turning a large data collection
- 25:37into a list, if you put rounded brackets, all of a sudden you have a much more efficient
- 25:43generator.
- 25:43Yep.
- 25:43That is something I don't call out at that point.
- 25:46But at the end of the talk, I allude to a article that was mentioned on the other podcast
- 25:51that you co-host, Python Bytes.
- 25:53Yeah.
- 25:53Thanks for the shout out on that one, by the way.
- 25:54Yeah, no, it was a great article, but it mentions generator expressions right after it mentions
- 25:59list comprehension.
- 25:59And I mentioned that these things go hand in hand and that you should familiarize yourself
- 26:03because if at any point you're passing a list comprehension to like an algorithm, like
- 26:08any or all or something, you can drop the square brackets and then just pass at the generator
- 26:13and it'll become much more efficient.
- 26:15So it's good to know both of them.
- 26:17And there's no way to go from a for loop really quickly and easily to a generator.
- 26:22That's a good thing.
- 26:23That's a good thing.
- 26:24Right.
- 26:25There's not like for yield, I and whatever, right?
- 26:28Like there, but with the comprehensions, it's square brackets versus rounded parentheses, right?
- 26:34It's so, it's so close that if that makes sense, it's like basically no effort to make
- 26:38it happen.
- 26:38Yeah.
- 26:38Yeah, nice.
- 26:39Okay.
- 26:39So we've got into a list comprehension, which is beautiful.
- 26:43And then you say, all right, it's time to turn our attention to this doubly nested for loop.
- 26:48And it's going to go over a bunch of items.
- 26:50And it's going to go over a bunch of the items and pull out an index and then, you know, go and work with that index.
- 26:57So it's another enumerate.
- 26:59And then I think another thing that's pretty interesting that you talk about, I don't remember exactly where it came in the talk, but you're like, look,
- 27:06what you're doing in this loop is actually looping from like the second onward for all the items.
- 27:13And that really is just a slice.
- 27:15Yeah.
- 27:15Yeah.
- 27:16So in this nested for loop, the outer for loop is basically reads for J in range of one to the length of your list.
- 27:26So you're basically creating a range of numbers from one to the length of your list.
- 27:31And then right inside that for loop, you're creating a basically a variable that's the Jth element of your list.
- 27:40So all you're doing is skipping the very first element of your list.
- 27:44But the way you're doing this is generating explicit indices based on the range function and the length function.
- 27:52And I thought at first that they must be doing this because we need access to the index later or we need access to our elements later.
- 28:01But that wasn't the case.
- 28:02It just seemed like the only reason they were doing all of this was to skip over the first element.
- 28:07And so very nicely, once again, Python has very, very many nice features.
- 28:12They have something called slicing where you can basically pass it the syntax, which is square bracket, and then something in the middle and square brackets.
- 28:20And in order to skip the first one, you just go one colon.
- 28:23Yeah, one to the end.
- 28:24And that's beautiful because you don't even have to check the length of the items.
- 28:28You just say go to the end, which avoids errors of like, do I have to plus one here?
- 28:32Do I not?
- 28:33Is it minus one?
- 28:34Like, what is the ending piece?
- 28:35But you don't have to worry about just from I skip the first one and the rest.
- 28:38Yeah, it's so convenient.
- 28:39You avoid making a call to Len.
- 28:41You avoid making a call to range.
- 28:43And you avoid your local assignment on the first line of your for loop.
- 28:47You can basically remove all of that and just use slicing and you're good to go.
- 28:52And yeah, slicing is slicing is a really, really awesome feature.
- 28:55It actually comes from a super old language that was created in the 60s called APL.
- 29:00And Python is one of the only languages that has something called negative index slicing,
- 29:04where you can pass it a negative one so that it wraps around sort of to the last element,
- 29:09which is a super, super, it sort of looks weird.
- 29:12But once you use it, it's so much more convenient than doing like a Len minus one or something like
- 29:18that.
- 29:18It's it is a little bit unreadable.
- 29:20But once you know what it does, it's great.
- 29:22It's great.
- 29:22It's like I want the last three.
- 29:23I don't want to care how long it is.
- 29:25I just want the last three.
- 29:26And that's yeah, it's fantastic slicing, I think is fairly underused for people who
- 29:31come from other languages.
- 29:32But yeah, and it fits the bill because there's so many of these little edge case.
- 29:36You talk about errors and programming, like off by one errors are a significant part of
- 29:40problems programming, right?
- 29:42And it just skips that altogether.
- 29:44It's beautiful.
- 29:44Yeah.
- 29:44So the next thing to do is so you're parsing this stuff out of the Internet, which means
- 29:51you're working with 100% strings.
- 29:52But some of the time you need numerical data.
- 29:55So you can ask questions like, is this the sixth or seventh or whatever?
- 29:58And so they have this is going to be fun to talk about.
- 30:02They have try value equals int of data.
- 30:06So pass the integer has the potentially integer like data over to the int initializer.
- 30:11Either that's going to work, or it's going to throw an exception, which case you will say
- 30:16except pass.
- 30:17Well, not you, the original article had that right.
- 30:19So it's this try pars except pass.
- 30:23Otherwise, it's going to be none or it's going to be set to the string value or something
- 30:26to that effect.
- 30:27So what do you think about this?
- 30:28How do you feel when you saw that?
- 30:29Yeah.
- 30:29So my initial reaction was that this is four lines of code that can potentially be done
- 30:36in a single line using something called a conditional expression.
- 30:40So in many other languages, they have something called a ternary operator, which is typically
- 30:45a question mark, where you can do an assignment to a variable based on a conditional predicate.
- 30:50So something that's just asking true or false.
- 30:53And if it's true, you assign it one value.
- 30:55And if it's false, you assign it another value.
- 30:56So in Python, they have something called a conditional if expression, which has the syntax assigned
- 31:03to value using the equal sign, ask your question.
- 31:06So in this case, we just ask, is it an int?
- 31:09Or sorry, it's so the first thing that returns, it's actually backwards from ternary operator.
- 31:13So the line reads data equals int of data if, and then check your predicate.
- 31:19And in Python, we can just call is numeric on our value, which will return us true or false
- 31:24based on whether it's a number.
- 31:25So if that returns true, then it'll end up assigning to data int of data.
- 31:30Otherwise, you can just assign it itself data.
- 31:33And then it's not going to do any transformation on that variable because it's not numeric.
- 31:38It's one line of code.
- 31:39It's more expressive, in my opinion, and it avoids using try and accept.
- 31:43And it's preferable from my point of view.
- 31:46I would say it's probably preferable from my point of view as well.
- 31:49I have mixed feelings about this, but I do think it's nice under certain circumstances.
- 31:55One, for example, if you say try, do a thing, accept, pass, a lot of linters and PyCharm
- 32:00and whatnot will go, this is too broad of a clause.
- 32:03You're catching too much.
- 32:04And you're like, okay, well, now to make the little squiggly in the scroll bar go away,
- 32:10I have to put a hashtag disable check, whatever, right?
- 32:14And I'm like, well, now it's five lines, one with a weird exception to say, no, no, this time
- 32:18it's fine.
- 32:19So that's not ideal.
- 32:20I definitely think that it's more expressive to use this conditional if one liner.
- 32:26The one situation where I might step back and go, you know, let's just do the try is if
- 32:32there's more variability in the data.
- 32:34So this assumes that the data is not none and that it's string like, right?
- 32:38But if you got potentially objects back or you got none some of the time, then you need a
- 32:43little bit more of a test.
- 32:44I mean, you could always do if data and data is numeric.
- 32:47That's okay.
- 32:48But then it's like if data and is instance of string data and like there's some level
- 32:54where there's enough tests that it becomes, you kind of like, fine, just let it crash.
- 32:58Right.
- 32:58And then we'll just catch it and go.
- 33:00But we were talking before we hit record.
- 33:03Also, like there's a performance consideration potentially.
- 33:06Yeah, definitely.
- 33:06And it's interesting.
- 33:07I'll let you speak to what you found.
- 33:09But on the YouTube comments of the PyCon talk, that was one of the probably the most discussed
- 33:15things was whether or not the conditional expression was less performant than the original try and
- 33:22accept because a couple individuals commented that it was it was more Pythonic to use the
- 33:26try and accept and therefore it might be more performant.
- 33:29But you can share with what you found.
- 33:31Sure.
- 33:31Well, I think in terms of the Pythonic side, like certainly from other languages, like say
- 33:36C, C++, there's more of this.
- 33:38It's easier to ask for forgiveness than permission style of programming rather than the alternative.
- 33:44Look before you leap.
- 33:45Right.
- 33:45Because in like C, it can be a page fault and the program just goes poof and goes away.
- 33:50If you do something wrong, whereas this, it's just going to throw an exception.
- 33:52You're going to catch it or something like that.
- 33:54So there's like this tendency to do this style.
- 33:56But in terms of performance, I wrote a little program because I wanted to I'm like, maybe this
- 34:01is faster.
- 34:01Maybe it's slower.
- 34:02Like, let's think about that.
- 34:03Right.
- 34:03So I wrote a little program, which I'll link to.
- 34:06There's a simple gist.
- 34:07I'll link to it in the show notes.
- 34:08It creates one million, a list with one million items.
- 34:11And it uses a random seed that is always the same.
- 34:14So there's no, there's zero variability, even though it's random.
- 34:17It's like predictable random.
- 34:19And it builds up this list of either strings or numbers randomly, a million of them.
- 34:24About two third strings, one third number.
- 34:26And then it goes through and it just tries both of them.
- 34:28It says like, let's just convert this as many of them as we can over to integers and do it
- 34:33either with the tri-except pass or just do it with this is numeric test.
- 34:36It is six times.
- 34:38I got about 6.5 times faster to do the test, the one line test than it is to let it crash
- 34:45and realize that it didn't work.
- 34:46Yeah.
- 34:46So there you go.
- 34:47You heard it here on Talk Python To Me.
- 34:49That's right, man.
- 34:50Conditional expressions faster than try-except.
- 34:52Talk Python To Me is partially supported by our training courses.
- 34:57How does your team keep their Python skills sharp?
- 35:00How do you make sure new hires get started fast and learn the Pythonic way?
- 35:04If the answer is a series of boring videos that don't inspire or a subscription service you pay way too much for and use way too little, listen up.
- 35:14At Talk Python Training, we have enterprise tiers for all of our courses.
- 35:17Get just the one course you need for your team with full reporting and monitoring.
- 35:22Or ditch that unused subscription for our course bundles, which include all the courses and you pay about the same price as a subscription once.
- 35:29For details, visit training. talkpython.fm/business or just email sales at talkpython.fm.
- 35:37I mean, there's a lot of overhead to throw in an exception and catching it and dealing with all that.
- 35:42Now, right, this is a particular use case that varies and like all these benchmarks like might vary.
- 35:49Like if you've got 95% numbers and 5% strings, it might behave differently.
- 35:54Like, so there's a lot of variations, but here's an example you can play with in what seems like a reasonable example to me.
- 35:59It's faster to do the is numeric test.
- 36:02So a lot faster, right?
- 36:03Not like 5% faster, but 650% faster.
- 36:07So it's worth thinking about.
- 36:08Yeah.
- 36:08Yeah, for sure.
- 36:09Let's see.
- 36:10So come through.
- 36:11And in the end, you had, I mean, a ton of stuff was here.
- 36:14It was like 20 lines of code just for these two loops.
- 36:17And now you've got it down to four lines of code by basically an outer loop and inner loop, grab the data and append it with this little test that you've got.
- 36:27Much nicer.
- 36:28I agree.
- 36:28Yeah.
- 36:29So you went, I think if you look at the overall program at this point, you were doing some analysis or like some reporting.
- 36:35You said it started at 60 lines of code and now it's down to 20.
- 36:39Yeah.
- 36:39That's pretty good.
- 36:40Roughly.
- 36:40Depending on if you count, you know, empty lines and whatnot, but it was about 60 down to about 10 or 20 lines.
- 36:46And at this point, I had sort of pointed out that I had made a mistake.
- 36:52So like this was fantastic.
- 36:53At least I had thought that, you know, I'd taken a code.
- 36:56I'd taken a code snippet from a blog, reduced it by, you know, roughly 75% or 67%, depending on how you measure it.
- 37:04But that I had made an even bigger mistake than I had realized.
- 37:09And it was that when I had originally, I'd shown Googling for, you know, how to scrape HTML using pandas that I read the second results.
- 37:17And the third result was actually what I should have chosen.
- 37:21And it was that I had pandas actually has a read HTML method in the library.
- 37:28And so the point that I go on to make, if you use that, you go from, you know, 10 or 20 lines down to like four lines of code.
- 37:34And you're just invoking this one pandas API, read HTML.
- 37:38And it's so much better.
- 37:40So, you know, refactoring is fantastic.
- 37:41But there's some quote about like the best code is no code.
- 37:44If you don't have to write anything to do what you want to do and you can just use an existing library.
- 37:50That's the best thing that you can do because that's going to be way more tested than the custom code that you've written.
- 37:55It's going to save you a ton of time.
- 37:57And you're going to end up with ultimately less code to maintain yourself.
- 38:01And what's better than having someone else maintain the code that you're using for you?
- 38:05Exactly right.
- 38:07It gets better for no effort on your part.
- 38:10Yeah.
- 38:10It might get faster or it might handle more cases of like broken HTML or who knows.
- 38:15But you don't have to keep maintaining that.
- 38:17It's just read underscore HTML on pandas.
- 38:20Just it's probably getting maintained.
- 38:21Yeah.
- 38:21And so like one of the things that I've echoed in some of the other talks that I've given is knowing your algorithms.
- 38:27In C++, definitely there's a whole standard library.
- 38:30There's a lot of built-in functions.
- 38:32I guess they're not so much called algorithms.
- 38:34They call them built-in functions in Python.
- 38:36But like there's a whole page where I was just looking at it the other day.
- 38:39And there's a ton of them that I'm just not aware of.
- 38:42Everyone knows about map, filter, any all.
- 38:45Like I just saw, I think it was called div mod, which was a built-in function for giving you both like the quotient and the remainder.
- 38:51Which is like, there's definitely been a couple times where I've needed both of those and you do those operations separately.
- 38:56And it's like, if I just knew about it, you can in a single line, you know, you can destructure it using the iterable unpacking.
- 39:02Knowing your algorithms is great, but also knowing your libraries.
- 39:06Knowing your collections.
- 39:06Like the more you get familiar with what exists out there, the less you have to write and the more readable your code is.
- 39:13Because if everybody knows about it, we have a common knowledge base that it's transferable from every project you work on.
- 39:19Right.
- 39:20Yeah.
- 39:20Your final version basically had two really meaningful lines.
- 39:23One was request.get.
- 39:25The other was pandas.readHTML.
- 39:28You don't have to explain to anyone who has done almost anything with Python what request.get means.
- 39:32Like, oh yeah.
- 39:33Okay.
- 39:33So got it.
- 39:34Next.
- 39:35Right.
- 39:35We all know how that works.
- 39:36We know it's going to work.
- 39:37And so on.
- 39:38And it's really nice.
- 39:39I think, though, what you've touched on here actually is really important, but it also shows why it's kind of hard to get really good at a language.
- 39:48And the reason is there are so many packages, right?
- 39:51You go to PyPI.
- 39:52Let me try pyp.org.now.
- 39:54Every time I go there, it's always more, right?
- 39:57So 245,000 packages.
- 39:59If you want to learn to be a good Python programmer, you need to at least have awareness at a lot of those and probably some skill set in some of them.
- 40:06Because like Panda is one of those.
- 40:08Request is another one, right?
- 40:09The four-line solution that you came up with was building on those two really cool libraries.
- 40:15And so to be a good programmer and effective means like keeping your eye on all those things.
- 40:20And I just think that's, it's both amazing, but it's also kind of tricky because it's like, well, I'm really good with for loops and I create functions now.
- 40:26You're like, great.
- 40:27You've got 200,000 packages of study.
- 40:29Go.
- 40:30There's some quote that I've heard before where being a language expert is 10% language, 90% ecosystem.
- 40:36And it's, you can't be a guru and insert any language if you don't know the tools, if you don't know the libraries.
- 40:43It's so much more than just learning the syntax and learning the built-in functions that come with your language.
- 40:49It takes years and it definitely doesn't happen overnight.
- 40:52It's a challenge for all of us.
- 40:53Yeah, for sure.
- 40:54You know, maybe it's worth a shout out to awesome-python.com right now as well, which like has different categories you maybe care about.
- 41:01And then we'll like highlight some of the more popular libraries in that area.
- 41:05That sounds awesome.
- 41:05That's a good one.
- 41:06Yeah, for sure.
- 41:07So you went through and you did nine different steps.
- 41:10You actually have those called out very clearly in your slides.
- 41:13You can get the slides from the GitHub repo associated with your talk, which I'll link to in the show notes, of course.
- 41:19But all of this refactoring talk was really part of the journey to come up with a totally different answer, which was what are the most popular languages for these coding competitions?
- 41:28Yeah.
- 41:29Ultimate goal was to scrape the data and then to use pandas in order to do that analysis.
- 41:34And at the end of the day, I believe the number one, I definitely know the number one language was C++ at about, I think it was 89%.
- 41:42And that typically is the case because certain websites, they give the same time limit per language.
- 41:50So a website like HackerRank, they vary by language.
- 41:53So Python, your execution time that you're allotted is 10 times more for Python.
- 41:59So even though Python's slower, they give you a proportionate amount of time.
- 42:03But most websites don't do that.
- 42:04So the CodeForces website, it gives like you, I think, two seconds execution time, regardless of the language you use.
- 42:12And so due to that, most people choose the most performant language, which is C++.
- 42:16But in second place was Python.
- 42:18And I know a lot of competitive programmers that for the problems where performance isn't an issue that you're trying to solve for, they always use Python because it's about a fraction of the number of lines of code to solve it in Python than it is in any other language.
- 42:32Sometimes you can solve a problem in one line in Python.
- 42:35And the next closest language is like five lines, which is a big deal when time matters.
- 42:40Yeah.
- 42:40Yeah.
- 42:41Are you optimizing execution time or developer time in this competition?
- 42:45Right?
- 42:45Yeah.
- 42:46It definitely matters what you're trying to solve for.
- 42:48So yeah, C++ was first.
- 42:49Python was second.
- 42:50Java was third.
- 42:52And then there was a bunch of fringe languages.
- 42:54The top three were C#, Pascal, and Kotlin.
- 42:57And yeah, you can see a full list if you go watch the PyCon talk.
- 43:00But it was cool, yeah, to find out what was used and what wasn't.
- 43:03Yeah, it sure was.
- 43:04And it was cool to see the evolution of what you created to answer that question, which is pretty neat.
- 43:09All right, well, let's just talk a little bit about Rapids.
- 43:12Because I know that people out there are, there's a lot of data scientists, and they're probably interested in that project.
- 43:18So we did mention a tiny bit that it's basically take Handa's data frames, apply something like that, that API, pretty close, not 100% identical and everything, but pretty close.
- 43:30And it runs on GPUs.
- 43:31So why are GPUs better?
- 43:33Like, I have a really fast computer.
- 43:35I have a core I9 with like six cores I got a couple years ago.
- 43:38That's a lot of cores, right?
- 43:39So yeah, well, first thing I should highlight, too, is that Rapids is more than just QDF.
- 43:46So QDF is the library I work on.
- 43:48We also have QIO, QGraph, QSignal, QSpacial, QML.
- 43:53And each of those sort of map to a different thing in like the data science ecosystem.
- 43:59So QDF definitely is the analog of pandas.
- 44:03QML, I think the sort of analog you can think of is like scikit-learn.
- 44:07But also, too, like none of this is meant as replacements.
- 44:10They're just meant as alternatives.
- 44:12Like if performance is not an issue for you, like stick with what you have.
- 44:16There's no reason to switch.
- 44:17Yeah, don't do it because, for example, I couldn't run it on my MacBook, right?
- 44:21Because I have a Radeon.
- 44:22Right, right.
- 44:23If you do want to try it out, I think they're on the Rapids.
- 44:26So if you go to rapids.ai, we have a link to a couple examples using like Google CoLab
- 44:32that are hooked up to like free GPUs that you can just take it for a spin.
- 44:35And you need the hardware, but you can go try it out.
- 44:38But like our pitch is sort of like this is useful for people that have issues with compute.
- 44:43Right.
- 44:43And for different pieces, you're going to want different projects.
- 44:46So if you're doing pandas like sort of data manipulation, QDF is what you want.
- 44:50But yeah, why are our GPUs faster?
- 44:53It's just a completely different device and a completely different model.
- 44:56So GPUs, typically it's in the G of the GPU.
- 45:00We're known for being great for graphics processing, which is why it's called a GPU.
- 45:06But at some point, someone coined the term.
- 45:08He actually works on the Rapids team, Mark Harris.
- 45:12He coined the term GPGPU, which stands for general processing GPU compute.
- 45:17It's now typically referred to as just GPU computing.
- 45:20But it's this idea that even though the GPU model is great for graphics processing, there
- 45:26are other applications that GPUs are also amazing for.
- 45:31The next best one is matrix multiplication, which is why they sort of became huge in neural
- 45:36nets and deep learning.
- 45:37But since then, we've basically discovered that there's not really any domain that we can't
- 45:42find a use for GPUs for.
- 45:43So there is a standard library in the CUDA model called Thrust.
- 45:48So if you're familiar with C++, the standard library is called STL, and it has a suite of algorithms
- 45:54and data structures that you can use.
- 45:56Thrust is the analog of that for CUDA.
- 45:59And it has reductions, it has scans, and it basically has all the algorithms that you
- 46:05might find in your C++ STL.
- 46:07And if you can build a program that uses those algorithms, you've just GPU accelerated your
- 46:12code.
- 46:12However, using Thrust isn't as easy as some might like.
- 46:16And a lot of data scientists, they're currently operating in Python and R, and they don't want
- 46:21to go and learn C++ and then CUDA and then master the Thrust library just in order to accelerate
- 46:27their data science code.
- 46:28The Rapids goal is to basically bring this GPU computing model for a sort of general purpose
- 46:35acceleration of data science compute or whatever compute you want to the data scientists.
- 46:41And so if they're familiar with the Pandas API, let's just do all that work for them.
- 46:45Put the so so Rapids is built heavily on top of Thrust and CUDA.
- 46:50And so we're basically just doing all this work for the data scientists so that they can take
- 46:54their Pandas code, like you said, hopefully just replace the import, and you're off to
- 46:59the races.
- 46:59And some of the performance wins are pretty impressive.
- 47:03Like I'm not on the marketing side of things.
- 47:05But in the talk I mentioned, I just happened to be listening to a podcast called the NVIDIA
- 47:10AI podcast.
- 47:11And they had, I believe his name was Kyle Nicholson.
- 47:13And by swapping out CUDA for Pandas for their model, they were able to get a 100x performance
- 47:22win and a 30x reduction in cost.
- 47:26That's 30 times, not 30%.
- 47:28Yeah.
- 47:28So 30,000 times, like multiplicatively, which is massive.
- 47:32That's the difference between something running.
- 47:34So if it's 100x in terms of performance, that's the difference between something running in 60
- 47:38seconds or an hour and 40 minutes.
- 47:41And if you can also save 30x, if that cost you 100 bucks, and now you only have to pay $3,
- 47:47it seems like a no brainer for those individuals that are impacted by performance bottleneck.
- 47:52Like I said, if you're hitting Pandas, and it runs in a super short number of seconds,
- 47:57it's probably not worth it to switch over.
- 47:59Yeah.
- 47:59Well, and you probably, you tell me how realistic you think this is, but you could probably do
- 48:04some kind of import, conditional import, like in the import, you could try to get the rapid
- 48:11stuff working.
- 48:12If that fails, you could just import Pandas as the same thing.
- 48:15One is PD, the other is PD.
- 48:17And maybe it just falls back to just working on regular hardware, but faster when it works.
- 48:21What do you think?
- 48:22That is definitely possible.
- 48:23There's going to be limitations to it, though, obviously, if you have a sort of QDF data frame,
- 48:29like I don't think you wouldn't be able to do it piecemeal.
- 48:32But if you have a large product,
- 48:34what I'm thinking is if you wrote it for the Rapids version, but then let it fall back to
- 48:40Pandas, not the other way around.
- 48:41If you take arbitrary Pandas code, you try to rapidify it, that might not work.
- 48:44But it seems like the other one may well work.
- 48:47And that way, if somebody tries to run it, they don't have the right setup.
- 48:50It's just slower possible.
- 48:51What do you think?
- 48:52There's definitely a way to do that, to make that work.
- 48:54It might require a little bit of, you know, some sort of boilerplate framework code that
- 48:59is doing some sort of checking, you know, is this compatible else?
- 49:02But like, that definitely sounds automatable.
- 49:04Like, yeah.
- 49:05Yeah.
- 49:05That sounds cool.
- 49:06Because that would be great to have it just fall back to like, not not working, just not
- 49:10so fast.
- 49:10Right?
- 49:11Yeah, yeah.
- 49:11The future of computing is headed to a place where we can dispatch compute to like different
- 49:17devices without having to like, manually specify that, like, I need this code to run on the CPU
- 49:24versus the GPU versus the TPU versus in the future, I'm sure there's going to be a QPU for
- 49:29quantum processing unit.
- 49:31Like, like, exactly.
- 49:32Currently, we all think serially or most of us that don't work at NVIDIA, we think serially
- 49:37in terms of like, the way that CPUs do compute, but I think in 10 or 20 years, we're all going
- 49:43to be learning about different devices.
- 49:44And it's going to be too much work to, in our head, always have to be keeping track of which
- 49:49devices it's going to, at some point, there's going to be a programming model that comes out
- 49:54that just automatically handles when it can go to the fast device, and when we can just
- 49:58send it to the CPU.
- 49:59Yeah, absolutely.
- 50:00So just while you were talking, I pulled it up on that Alienware gaming machine I got.
- 50:05It has a GeForce RTX 270, which has 2,304 cores.
- 50:12So that's a lot.
- 50:14That's a lot of cores.
- 50:16And if you look somewhere, Google claims that it achieves 7.5 teraflops in the super, increases
- 50:23that to 9 teraflops, which is just insane.
- 50:27It's like a core i7 doing like 0.35, 0.28 or something like that.
- 50:33So anyway, the numbers, they just are like, they boggle the mind when you think of how
- 50:37much computation graphics cards do these days.
- 50:40I think top of the line, I might get this wrong, but like the modern GPUs are capable of 15 teraflops.
- 50:46It's an immense amount of compute that's hard to fathom, especially when coming from a CPU sort of way of thinking.
- 50:51Yeah, absolutely.
- 50:52Yeah, the only reason I didn't get a higher graphics card is every other version required water cooling.
- 50:57I'm like, that sounds like more effort than I want for a computer.
- 50:59I'll just go with this one.
- 51:01Yeah.
- 51:03All right.
- 51:04Well, Rapid sounds like a super cool project.
- 51:06And maybe we should do another show with the Rapid's theme across these things to talk a little bit more deeply.
- 51:12But it sounds like a great project.
- 51:14Glad you're working on it.
- 51:14I work on the C++ lower engine of it, but I'd be happy to connect you with some of the Python folks that work on that side of things.
- 51:22And I'm sure they'd love to come on.
- 51:23Yeah, that'd be fun.
- 51:24All right.
- 51:25Now, before we get out of here, I've got to ask you the two questions.
- 51:27If you're going to write some Python code, what editor do you use?
- 51:30So I am a VS Code convert.
- 51:33That's what I typically use day to day.
- 51:35Nice.
- 51:35Yeah, that's quite a popular one these days.
- 51:36And then notable PyPI package.
- 51:39Then you ran across like, oh, people should know about this.
- 51:41Yeah.
- 51:41So I like to recommend there's a built in standard library, which I'm pretty sure most Python developers are familiar with.
- 51:49Itertools, which has a ton of great functions.
- 51:52But less well known is a PyPI package called more hyphen itertools.
- 51:57And I'm not sure if this one's been recommended on the show before.
- 52:00But if you like what's in itertools, you'll love what's in more itertools.
- 52:05It has a ton of my favorite algorithms, chunked being one of them.
- 52:09You basically pass it a list and a number, and it gives you a list of lists consisting of that many things.
- 52:15It's like paging for lists.
- 52:17Yeah, yeah.
- 52:17And there's tons of neat functions.
- 52:19Another great one that's so simple, but doesn't exist built in all underscore equal.
- 52:25It just checks, given a list, are all of the elements the same?
- 52:28And it's a simple thing to do.
- 52:30You can do it with all, but you have to check is every element equal to the first one or the last one.
- 52:35So there's just a ton of really convenient functions and algorithms in more itto tools.
- 52:40That's the one I recommend.
- 52:40Yeah, that's cool.
- 52:41And you can combine these with like generator expressions and stuff.
- 52:44They are all these, you know, pull some element out of each object that's in there and generate that collection.
- 52:50Ask if all those are equal.
- 52:52And they go to all these ideas go together well there.
- 52:54Yeah, they compose super nicely.
- 52:55Yeah, for sure.
- 52:56All right.
- 52:57Final call to action.
- 52:58People are interested in doing refactoring, making their code better, maybe even checking out Rapids.
- 53:03What do you say?
- 53:04I'd say if you're interested in what you heard on the podcast, check out the PyCon talk.
- 53:08It's on YouTube.
- 53:09If you search for PyCon 2020, you'll find the YouTube channel.
- 53:14And yeah, if you're definitely interested in rapids.ai, check us out there.
- 53:18I assume all this stuff will be in the show notes as well.
- 53:20So maybe that's easier than YouTube searching.
- 53:23Yeah, well, and then also you talked about YouTube channel a little bit.
- 53:26Maybe just tell people how to find that.
- 53:27We'll put a link in the show notes as well.
- 53:29So they can, if they want to watch you, you know, talk about some of these solutions and these competitions.
- 53:34Yeah.
- 53:34So my online alias is code underscore report.
- 53:37If you search for that on Twitter or YouTube or Google, I'm sure all the links will come up.
- 53:42And yeah, you can find me that way.
- 53:43Awesome.
- 53:43All right.
- 53:44Yeah, we'll link to that as well.
- 53:45All right.
- 53:45Well, Connor, thank you so much for being on the show.
- 53:47It was a lot of fun to talk about these things with you.
- 53:48Thanks for having me on.
- 53:49This was awesome.
- 53:50Yeah, you bet.
- 53:50Bye-bye.
- 53:51This has been another episode of Talk Python To Me.
- 53:54Our guest on this episode was Connor Hoekstra, and it's been brought to you by us over at
- 53:58Talk Python Training.
- 53:59Want to level up your Python?
- 54:01If you're just getting started, try my Python Jumpstart by Building 10 Apps course.
- 54:06Or if you're looking for something more advanced, check out our new async course that digs into
- 54:11all the different types of async programming you can do in Python.
- 54:14And of course, if you're interested in more than one of these, be sure to check out our
- 54:18everything bundle.
- 54:19It's like a subscription that never expires.
- 54:21Be sure to subscribe to the show.
- 54:23Open your favorite podcatcher and search for Python.
- 54:25We should be right at the top.
- 54:27You can also find the iTunes feed at /itunes, the Google Play feed at /play, and the
- 54:32direct RSS feed at /rss on talkpython.fm.
- 54:36This is your host, Michael Kennedy.
- 54:38Thanks so much for listening.
- 54:39I really appreciate it.
- 54:40Now get out there and write some Python code.
- 54:42I really appreciate it.
- 55:03Thank you.