Python Workout

If you are looking to get more comfortable with Python, this is definitely a good place to start.

Python Workout

Over the last several years I have been hedging my bets around LabVIEW and trying to expand my options. As part of that, I have been trying to learn Python. I've taken a few classes and read a bunch of books. The latest book is Python Workout.

This book is a collection of 50 exercises split into 10 chapters. Each chapter focuses on a specific area of Python. The sections include numerics, strings, lists and tuples (arrays and clusters), dictionaries (maps) and sets, file i/o, functions, functional programming using comprehensions, modules and packages, objects, and iterators/generators. Each chapter includes a comprehensive reference section with links to topics in the Python help. Each exercise also includes 3 additional challenges called "beyond the exercise". The book gives a solution to the exercise itself, but the "beyond the exercise" challenges do not give a solution.

Going through this book and doing all the exercises is definitely a workout. The subtitle says 10 minutes, but that is very optimistic. There were a few exercises that took about 10 minutes, but only a few. In those cases it was doing just the exercise itself, not following any of the research links or doing the Beyond The Exercise challenges. Most of the exercises took me an entire lunch break. Some of that was certainly going down rabbit holes and exploring the dusty corners.

What did I get out of it?

Some of these things were covered specifically in the book and some of them were just me playing around, but overall I found the whole experience very useful.

  • Pythonic Code - I got really good at writing list comprehensions, iterators and generators and became really fluent with using dictionaries to solve problems. I also discovered and got really good at using some of the "batteries included" features. I felt like a Pythonista by the end.
  • TDD - I setup Pytest and did almost all the exercises using TDD. There were a few that used the filesystem that were a little hard to test, but I was able to do a lot. I was able to simulate reading from files using the io module, but listing directories was a challenge I could not figure out how to test easily. The speed of using Pytest puts any LabVIEW unit testing framework to shame.
  • PyCharm - I got really good at using Pycharm and it's built-in Venv support. I also made wide use of the refactoring tools. They 100% put LabVIEW to shame. So many things that are difficult to do in LabVIEW were literally a single click. I used the IPython interpreter quite a bit within PyCharm, so I got familiar with that as well.
  • Black - I got used to using Black a lot. I don't think I'll ever go back. It's an autoformatter. It's just amazing. You don't have to think about formatting at all, you can completely concentrate on solving the actual problem. Even had it hooked up so it just ran constantly in the background. Wish there was something like it for LabVIEW.
  • Type Hints/Mypy - I played around with using type hints and MyPy. I thought it worked quite well. Definitely helped give me a little more confidence in the robustness of my code.
  • Git - I got really good with doing basic text diffs and exploring some of the Git IDE integrations and committing patches and things like that (many of which you simply can't do in LabVIEW).