Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Clean Up the Code

00:00 All right, with a more cleaned-up main() function, it’s time to get into the load_model() function. There is still much going on.

00:08 So my first impression is still if I have to understand the load_model() function, I am trying to go through the lines, but I’m not truly finding my way.

00:19 So maybe you have an idea how to clean up this one.

00:24 This seems like a lot of leftover structure that probably has some sort of meaning in the original code, but in the example that we’re getting here, it doesn’t really seem to mean much.

00:35 So we might just get from the understanding that we’re talking about raising exceptions and how to have them bubble up, basically. That’s, like, our understanding of the question at the moment.

00:45 So maybe we go a little bit rough on here and take out a lot of stuff to make it just easier to understand. Yeah. First of all, I would take this nested if statement that’s, like, the quickest and easiest thing to take out. There’s, like, two if statements on line nine and now on line 11 that just give us different conditions for when load_model() should raise some sort of error.

01:09 So we don’t need this extra nested if statement. So we have this to happen now, and at this point, we’re kind of like getting to the issue. I think we’ve maybe cleaned away enough that we’re starting to see what is actually the issue with the code in relation to the question that this person had. And at this point I get the feeling, “Aha!

01:28 They want the exception to bubble up.” But what they’re doing here is they’re mixing up raising exceptions and also catching them with a tryexcept block in the same context.

01:38 Like, they’re doing this in the same function: they raise an exception and then right away catch it afterwards. This is somewhat of a mix-up of two different concepts. Yeah.

01:49 It definitely took me a while to even just see that from the original state of the code. I like your little “aha!” moment there because that’s exactly the moment that you want to have right away.

02:00 You want a person to look at your code and be like, “Aha! Okay, so I see that there is a mix-up of those things. Let me help you fix that.” So basically, all the work that Martin has done so far is work that you should put into your question, but that not should your helper have to do until to get this “aha!” moment to actually help you. Yeah. It’s really nice that this happened.

02:26 So this is now the point that we actually can go into the refactoring and into, like, helping what’s going on there. But before we do, I, I want to address one last thing, which is important to note.

02:39 We were saying before to have more generic names for your functions, so we renamed the predict_raw() function into the main() function.

02:48 We kept the load_model() one because it was a descriptive name. One thing why it might make sense to also adjust your code for your helper is not to make it just more generic, but also to remove any names that might be a security breach if you share them with the Internet. So for example, you write your code in your company, and you are—by contract—not allowed to share code that you write while you are working for this company.

03:18 So you have to try to abstract this question and the variable names and the function names as good as possible to make the example generic and not expose any information from within your company.

Become a Member to join the conversation.