#100DaysOfCode in Python Transcripts
Chapter: Days 34-36: Refactoring / Pythonic code
Lecture: Concepts: what did we learn

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