Learning Python Week 9 — Time for Refactoring
--
It is just 2 months since I have started on this journey of learning Python and I am quite happy with the progress that I have made. I have written a reasonably-sized program to serve my own need and I am quite happy that I am able to tweak/improve the program as I go. I am very grateful for starting with the book ‘Automate the boring stuff by Al Swiegart’ — the book really covers such a vast array of topics and it has given me a broad overview of the many areas of application of python. I look forward to finishing the book this week and jump into applying all the knowledge on some real-world projects.
Now, as for the progress in my reading, this week there is not much progress as it was a holiday season. Though I took my laptop with me when I went on my vacation with the aim of finishing up the ‘automate the boring stuff’ book, I did not want to be glued to the laptop with kids around me. I could have covered a lot of ground if I had a hard copy of the book with me as I did get many small windows of time — somehow, these days I am drawn more and more towards hard-copy books.
Reading on the screen has its own advantages with the ability to highlight text and quickly look things up and to try the new commands immediately in the interactive python shell — but despite these advantages, somehow, the slowness factor imposed by the book has its own benefit —since I can’t type things immediately, I have to really visualize it in my head to understand the flow of the program. Also seeing the content laid out on the physical page allows me to have a visual image of the contents which is somehow impossible to get in an e-book. Anyway, given that I have bought a year-long subscription for Safari online (now called Oreilly learning), I have no choice but to benefit from the vast array of ebooks that are at my disposal.
This week after I returned back from my vacation, I set out to refactor my code — when I wrote the code in the 4th week, I was quite new to python and did not get exposed to the coding style conventions of python. But now that I have been exposed to PEP 8 guidelines, I wanted to reorient my brain to fully follow the pythonic way, I decided to rename all my functions and variables — I have documented what I am trying to do here in this StackOverflow question. It was not an easy task and at times, I felt it was too much effort for very little gain, I still persisted and converted all my variable and function names to use the underscore convention. For the first time, I decided to explore PyCharm and it charmed me with its cool capabilities for doing refactoring (I am a fan of IntelliJ IDEs from my early days as a Java developer from 2000). But PyCharm being quite complex, I needed more time to understand it better.
Hence, I continued to use Jupyter notebook and used the Find/Replace dialog to do the renaming manually one-by-one. And along the way, I picked up a few cool tips about Jupyter:
- Block comment/uncomment — CTRL + / as documented here. I also found a cheatsheet of Jupyter notebook keyboard shortcuts here
- I wanted to know how to save my Jupyter notebook file as python script and found a tip here. This is so cool — then, I opened the saved python file in Visual Studio Code as it supports Jupyter notebooks — Now I have one more reason to like VS Code as I am still deciding what general-purpose code editor I should settle on — Atom, Brackets or VS Code.
- But then I found a very useful pointer in the first comment in the above SO post — save the notebook automatically as .py file each time the notebook is saved using a post-save hook — this approach is way too cool.The steps I followed are mentioned here in this answer
- Even when using Jupyter, I was using # to comment my code as I was not sure how markdowns would work when I copy and paste my code into .py file. Now, with the above tip to save the notebook automatically as a .py file, the markdowns are automatically converted into comments with # as prefix. Now, I can start using markdowns a lot more :)
So, I am spending a lot more time in Jupyter notebook and learning the nuances of it. and I like it more and more :)