talkpython / mastering-pycharm-course

Course demos and handouts for Talk Python's Effective PyCharm course
https://training.talkpython.fm/courses/explore_pycharm/mastering-pycharm-ide
GNU General Public License v2.0
1.13k stars 716 forks source link

Your turn: 4-Refactoring - Expected Final Version Gives Diff Results #33

Closed mcfarland03 closed 3 years ago

mcfarland03 commented 4 years ago

I'm a Python noob so I may just be confused, but the latest podcast resulting from expected_final_version is 1 less than the latest podcast resulting from the smelly_podcast. I don't think was intended. You can fix this by adding "+1" at the end of line 19 on the program.py file under expected_final_version. Not sure this is the sexy Python way of fixing it.

Also, when I follow the instructions:

Next up, we have "GET LATEST SHOW ID". We want a method that will return that value. So highlight just max(episode_data.keys()) in the line:

latest_show_id = max(episode_data.keys())

Choose extract method again. Notice that this time, PyCharm found another usage (duplicate code!) we can replace that with this method call too (automatically).

Refactoring to Method doesn't find another usage for duplicate code.

For the instructions:

Finally, notice the remaining section label "DISPLAY RESULTS".

Highlight that and extract it to display_results().

Rerun your program (again, and again). It's still working right? OK then, carry on.

I end up with a display_results() method with "latest_show_id" and "oldest_show_id" as parameters.

mikeckennedy commented 3 years ago

Hi @mcfarland03

Yes, it's weird that range includes the start index but not the ending one. This omit the end probably has to do with things like mapping to array / list indexes and such:

lst = [2, 4, 5, 6]
range(0, len(lst)) -> [0, 1, 2, 3]

But it's not exactly zero based at the start and always throws me off. If you look at the docs:

Screen Shot 2020-11-11 at 10 10 14 PM

It does say this is expected and really your only recourse is to add the +1 at the end as you did. So it can catch us out for sure, but I don't know of a better solution.