Python Jumpstart by Building 10 Apps Transcripts
Chapter: App 9: Real Estate Analysis App
Lecture: Python 3 AND Python 2 Compatible Code

Login or purchase this course to watch this video and the rest of the course contents.
0:00 This code is working perfectly in Python 3, as we saw. Let's see how it works in Python 2.
0:08 Now, there is a minor problem we are going to run into with our file IO here so we'll solve that really quickly,
0:14 but let's go add it the configuration and say hey, let's try to run this under Python 2.7.10. No module named statistics.
0:24 Well, we need that, right?, we are using statistics.mean it turns out, statistics was introduced later on in Python,
0:32 I think it was 3.4 but somewhere around that time frame, and it's definitely not in 2.7, so what do we do, do we just say well,
0:39 we've got to take the mean so forget it, you can't write this code, or another possibility would just be to say well,
0:44 we can't use the statistics module we are going to have to just write our entire own statistics mechanism.
0:50 I suppose there is some answer out on PyPi but as a way of demonstration, let me show you how we can fix this.
0:57 so that's the line where this is crashing, and if you don't know for sure, you click here it says line 3 click
1:02 and it will navigate you right there, yeah, it looks like that's the line that's crashing, and that's because there is no statistics module.
1:08 So what we can do, we are going to dig into this error handling later, just roll with it here ok, what we are going to do is
1:16 we are going to say we are going to try to run this code, and if it fails to import the statistics module,
1:21 we are going to fall back to something else, so let's say try, this is one of the, this is basically the key concept of the next application,
1:29 so don't worry too much about the details, what we'll say is we are going to try to run this code
1:34 and if there is some kind of error I'd rather run error code instead. So either we are going to successfully import this
1:39 or we are going to have to do something else. So let's add another file here, I'll call this statistics stand in for Python 2, ok,
1:50 name it whatever you want, it doesn't matter, let me just capture the name really quick here,
1:54 we are going to come over here, we are going to say if there is an error, we are going to instead import that now this is not going to work so well,
2:02 because we are going to still try to use this name below and it's this is going to be here, right, so what we can do is we can say as this,
2:10 so basically here is the real module name but forget it rename it to that and as long as it has a mean method, that takes data,
2:20 and let's just say return 0 for a minute, things will run. So remember, we were running on Python 2.7 first of all,
2:29 let's go run it back on Python 3, works, right, here is our mean method, now this is going to return 0,
2:37 we are going to put real numbers there in a minute, but let's just see that it doesn't crash in the same place,
2:41 there is going to be another crash we'll deal with hold on. Look, no errors, there is this problem with encoding
2:47 and it turns out that this do like so, turns out that that didn't work in Python 2.7 exactly in that manner, so we'll drop it, but now watch this,
2:59 it used our method when it wasn't available from the system, and that is in the case that it happens to be 2.7,
3:06 it could actually be Python 3 but Python 3.1 which preceded the sort of creation of that module.
3:14 So I guess it's up to us to go write some kind of a mean method, so we'll have total=0 we'll have count=0, now we could do len of data
3:27 but if it's a generator it's better if we just do it this way, for x in data:, count +=1, total+=x and now we want to return total divided by count,
3:41 unless that's 0 then that's king of be a problem so then we could say max of 1 and count so that way a count of 0 we won't get it
3:51 divided by 0 error but otherwise we'll use max. Let's run it and see if we get some decent numbers here,
3:56 look at that, in Python 2 we have exactly the same answers, right, that's Python 2, go back to Python 3, notice 234, 165, 234, 165, same numbers,
4:16 so how do we do that, besides ignoring the encoding, we said look, we'll try to use statistics if for whatever reason it's not available,
4:25 then we are going to fall back and use our own probably less performant, maybe bug written implementation,
4:32 but at least we have something to fall back to and we named it this, but we for the purposes of this file,
4:38 we sort of replaced the same name here as statistics. There is a lot to know about writing code that runs on Python 3 and Python 2,
4:47 here is just one example hopefully you'll find this useful.

Talk Python's Mastodon Michael Kennedy's Mastodon