Persevere Through Obstacles
00:00 So far, we have looked at the code itself, our first impression, and the first impression of the question, which started with a how, so we try to focus more on improvements that we can do on the code itself to make it work.
00:16 And maybe we can start with running the code like it is right now and then going into the cleanup. What do you think, Martin? That sounds good.
00:26 We mentioned already before that Pylance tells us that there’re, like, an undefined variable. You might expect that there’s going to be an error here. So if I run the original file, there’s currently nothing happening, which is just because in the code snippet that we got here, there’s no function call.
So Python just defines these two functions but doesn’t actually run any code. But if I would now go ahead and call
you see that Pylance in line 29 already tells us again that, well, there’s actually two things that’s missing—a parameter
main() function needs something, and I think that’s very good that you’re kind of, like, you’re just hitting an obstacle. You’ve got this code, you want to help, and then you’re like, “Oh wait, actually I have to put something in.” So now you have to try to understand what this
X is that the
main() function has there. Good point.
So my thought process was I’m going to run this and I know I’m going to run into an error because of line 25, but I don’t even get to that. Like I’m going to run into another error before, which is that
main() is missing one required positional argument,
01:32 And that’s just like Philipp said, I’m just confronted with a bunch of obstacles to actually get to the point of helping this person with a question. Let’s go ahead and try to make this a working example.
01:45 I’m going to start with keeping the naming of this variables, which this is not a great name and we’ll refactor it later, but for now I just want to get it to work. Yeah.
And so I need to figure out what is
X, right? So I need to look into this code and see where is it used. And in that case it looks like it’s a dictionary structure where the person is performing some look-up on two keys,
"t", which means that I’m going to here have to have a dictionary that has, let’s say,
"s" and that points to something. I have no idea what it points to.
I’m just going to say
"t" is going to point to
Like, I don’t know what
X is. I’m just trying to figure out from the code what is the data structure I need to actually get this to run. Okay, now I’m passing this in as
X and then I can try running it again.
02:40 And now I’m actually not running into any error, so that’s good.
And why is that? Because
returns, so— Yeah, that’s going into the more details of the code. So one thing that happens here in line 22 is you’re starting with a
try statement, and you are catching the exception in line 26, like any exception basically.
So my guess would be if anything in this code now throws an exception, we won’t see it because it’s in this
except block. Nice.
What we can see here is also, like, that if you use this very general exception, you catch all of the exceptions. So what we ran into now is that this person’s probably trying to catch some of these exceptions that are happening inside of
load_model(), but it’s also catching the exception that’s raised here, that you have, like, an undefined variable. So we don’t even come to this error, but it’s still a problem in the code basically that makes it not run the way that we would want to.
Become a Member to join the conversation.