#100DaysOfCode in Python Transcripts
Chapter: Days 34-36: Refactoring / Pythonic code
Lecture: Concepts: what did we learn
0:00 Lets go over what we've learned so far, 10 refactorings. One. Having a big if elif else block is not really maintainable and looks pretty ugly.
0:11 Rather, use a dict and just look up the items. Much better. Next, keeping an index when looping over a sequence. In Python, you can just use enumerate.
0:25 And look at that, you can even give it a start value of one and you get the same result but it looks way more concise. Next, context managers.
0:37 Don't use files like this. If an exception gets raised, the file handle won't be closed. This is a bit better because the finally block
0:47 will make sure that your file handle will get closed. Yet, much better, is to use a with statement
0:54 which automatically closes the file handle after it's done. Four, use builtins and learn about the standard library.
1:05 For example, here we saw a pretty verbose example how to get the maximum duration of a couple of workouts. Not the best way.
1:13 You can just use max and min which are built into Python. Here are two examples how you can do it in one line of code.
1:23 Tuple unpacking and named tuples. You need to swap variables and using a temp variable? Or what about indexing a tuple? By indices? Not optimal.
1:36 Use tuple unpacking. For example, you can just swap the variables around. No need for a temporary variable.
1:44 Or, to access to access elements in a tuple, make an attribute thanks to a named tuple. Now you can just print, work out that day,
1:52 work out that routine, and work out that duration. And it's way more readable. List comprehensions and generators.
2:01 Once you need to build up a sequence, you don't really need to build up that list. You can use a list comprehension. That's way shorter.
2:10 Or use a generator which when your dataset grows, will be more efficient. Here's another generator to get a random day.
2:20 String formatting and concatenation. Don't concatenate strings like this. It's ugly and less performant.
2:29 Rather, use f-strings if you're on three dot six or later. Otherwise, use format as a more elegant way to format your strings.
2:37 Another thing we saw was string concatenation when you build up your long string, don't do this. It's not efficient.
2:44 Python will make a new string object with every operation. You rather want to put all the string objects in a list and just join them together.
2:53 And here you see an example of that. PEP 8 and the Zen of Python, your new best friends. You will refer back to them, a lot.
3:05 First of all, to understand Python a bit better, why things are designed the way they are, read through Zen of Python and, I guess,
3:13 print it out and put it next to your screen because it's a very concise list and it really explains a lot about Python.
3:21 And equally important, look at the PEP 8 style guide and try to abide those principles because they make for more readable code
3:28 and more consistency across code bases. There's an awesome resource under PEP8.org that makes it easier to understand.
3:36 And I forgot to mention in the lesson, you probably have a shortcut under PyCharm or Vim that you can just run PEP 8 checks
3:44 or Flake 8 upon save and catch those errors early on. Definitely something where you want to go from average to awesome.
3:54 Explicit is better than implicit. That's literally quoted from the Zen. Don't do try-except paths, like never do that.
4:01 Don't mute all the exceptions, being kind of a black hole, beam into an obscure box, it's just bad. Rather, be explicit in catching your exceptions.
4:12 So in this case, dividing num one by num two. You can have a ZeroDivision error or a type error or any other exception
4:19 and all have their specific message and handling. Code quality overall. Which the Software Improvement Group has nicely wrapped into ten principles.
4:31 In short, keep your methods small, don't pass along too many function arguments, keep your code organized in modules or files,
4:39 and automate your testing. Of course, there's more to it. It's a whole study in itself. So you can read their building maintainable software book
4:49 but I also recommend Clean Code by Uncle Bob and Refactoring by Martin Fowler. That's really where you want to go from coding horror
4:58 to writing awesome code. Your code can be very Pythonic, but if you're writing methods of like 50 lines or more, you still want to go back
5:06 to general code quality principles. Alright, now it's your turn. Keep calm and code in Python.