Python 3.11: A Guided Tour Through Code Transcripts
Chapter: Error Messages
Lecture: Demo: Better Error Messages
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Let's get started by writing the code. We're gonna need a file to do that. Trying to break this up into a bunch of different files per topic for you.
0:09
Of course. This one we're gonna call better messages for better error messages.
0:14
Now, what I want to do is look at this these types of error messages that we explore just a
0:19
little bit before in the presentation from a couple of different angles.
0:24
So let me drop in some code here. We're gonna use this named tuple is a really simple class and
0:32
it has a name and a weight and a speed that will let us interact with those fields to do interesting
0:37
things. We also want to have that function we explored. So let me drop those in notice at the bottom.
0:44
This green box is showing the hot keys that I hit. So for example under there. So if I hit cmd+Alt+l,
0:52
wait for it to go away and see it fix that formatting. But also down here it tells you what I did.
0:57
You also see that if I press the button it will show the command of the hot keys. So if you see anything happen on the screen you're like wait,
1:04
how'd that happen? Just check out that box at the bottom.
1:06
That's called Presentation Assistant, if you care, it's an extension or a plug in for a PyCharm.
1:11
Okay, so we've got this function here. That is the one we explored with the dictionaries.
1:17
We also have the three things from our thing class and we're working with trying to add up the weights and
1:23
here we're just doing simple math or stream combination order list combining which is it? We don't know. We're gonna find out in order to call these.
1:33
Especially the first one we're gonna need some interesting data.
1:37
So let's just write a main method that creates a data dictionary that we're gonna use and some keys for what
1:44
we want. So what we want is we would like to have this this string right here in order to
1:51
get that out of the dictionary. We go to the dictionary and we ask for the region and we asked
1:55
for the country and we ask for the size and we ask for the units and we get that value ideally Or if we don't traverse it correctly we get a crash.
2:03
Now this main method obviously doesn't run, you probably know we've got to do this dunder name equals dunder main
2:12
convention. Alright so we can run it now. Nothing should happen.
2:16
But it should be working now notice we're using our virtual environment python here to run this.
2:26
So I'm gonna copy that because we also want to run this with python 3.10. Now you could set up multiple virtual environments and swap between them.
2:33
I'll just run it in the terminal on 3.10. So let's go and try to print out maybe what we get if we call up here we call Func 1
2:42
passing all of those values and we string, we strip it and we upper The miles and if we do this right. What are we gonna get the func 1 data B C.
2:54
D. And E. This should work. There should be no problem. Sure enough. We get to the units, get the miles out and we see the stripped uppercase miles as
3:05
you would expect. What if we ask for something wrong? What if we asked a D Is bigness rather than size and we try to call this function again
3:18
What's gonna happen? Well first of all let's run this in terminal and I'm pretty sure python will be
3:31
Python 3.11 for me. But if I type 3.10 I can explicitly call that. Well it crashed and exactly like you saw in the slides right there.
3:40
Return a.get(b).get(c).get(d).get(e).strip().upper. Somewhere along that line. In the chain of calling. Not get one of the things that was called on.
3:52
Either it was or returned. None that's all we know. And again this is Python 3.10 as you can see. But if I run the exact same code over here in 311.
4:04
There it is. Look at that. It's telling us that this return value. Get D. Was a none type. So when we tried to call get(e) on it.
4:14
It crashed. What what key did we break? We broke the key D remember?
4:21
Bigness is not in there obviously. So when this tried to access a key here that wasn't there. It returned none. Beautiful. Right. Really nice.
4:31
That's what's gonna show up in our logs and it's something that we can use right away.
4:36
This will save you time in the debugger or other types of analysis tools to try to look at the
4:43
values and figure out what went wrong. We know right here where the error actually is. So it helps us to focus in a little bit more.
4:50
Now let's look at another example when you look at that function two for function two,
4:54
we're gonna need some values for are things we have bob, sarah and jake. And let's see. We'll get PyCharm to tell us what these are.
5:03
This is their weight in kilograms. And then their speed and running I guess. I don't know. Well I guess there is a faster runner.
5:13
Okay Maybe maybe she could run that fast. We'll see now if we just call func2 to see. It takes three things to T1, T2, T3.
5:26
All this total weight. When you print out the total weight here we'll say the total weight is.
5:33
And notice I want to use an F. String but I forgot to put the beginning. Never mind. PyCharm will help us. If I just type T.
5:42
W. It'll select auto total weight here automatically and then if I select this it will complete it and put
5:49
the F. At the beginning. There you go kilograms. we run this again. There should be no problem.
5:55
This is perfectly fine. We have three things. They all have weight properties.
6:00
We're gonna add them up except for him. We never made it that far did we? Let's comment this out. Here we go. The total weight is 207 kg.
6:10
Fantastic. Again. What happens if we did this wrong? What if we set T2 to none? Check it out being one like this this right here,
6:24
we tried to de reference the weight field or property on thing 2. And guess what thing 2 is none. So instead of just saying,
6:34
well something along this line here was none. This weight problem. So that's really, really nice. Let's look at the other one.
6:43
The final one will say Func 3. Remember this is the addition one here, A plus B plus C plus D. So we say 1,2,3 and 4.
6:59
Run that 10. Sure enough. But what if we had like this one was the string three.
7:06
Now we could convert it to a number but directly this will be an error in Python. If we run this code over here, you can see something along there.
7:20
Those four editions there was an unsupported combination of int and stir which one we don't know although is really explicit
7:29
right here. Normally these would be variables and you wouldn't see their values. But in Python,
7:33
3,11. Hey, look at that. There was an error in this thing and it's that plus
7:39
right there, see the little carrot that goes up and says that's the one that the end and the stir
7:44
went bad on so I'm gonna copy that out so we can write one more on a code.
7:48
Let's suppose we had Sarah is too, so we'll say Sarah equals T two, Mob Equals T one. Let's suppose I wanna work with bob and Sara for a minute.
8:04
I want to print out something like the weight of bob and Sarah is, I'm gonna do a little F string here. It's bob.weight and Sarah.weight,
8:23
but let's suppose. I thought Sarah was spelled like that. Oops, now PyCharm is helping me here but imagine it was a little more subtle.
8:31
We weren't really sure this era wasn't caught name error. There is no Sara spelled like this, but here's the part.
8:40
That's awesome. Did you mean Sarah with an H and it's right here we're using it right in that F string, awesome, awesome, awesome. So really cool,
8:50
Better error handling features, better error reporting when something goes wrong for us using Python 3.11.
8:58
So this is definitely one of the biggest features of 3.11.
9:02
Just easier to write code and develop code when you're getting started on a project and then if something goes wrong
9:09
in production you have so much more information captured if you save the exception details then if you just had
9:17
3.10 or something before. Ok, big thumbs up for me.