If you wanna get sh** done … Python is there for your rescue.

How I wish life could be automated. Well, at least the boring parts of it. I recently realized that if every aspect of life could be automated, Python would have been my language of choice to do so.

No, you won't hear from me in this post how Python is the best language I've encountered.You won't write a line of code in Python that will make you shed a tear of happiness because you just wrote the most amazing line of code in your life - this is saved for C++. But if you want to get things done quickly and neatly, then Python is there for you. The abundance of libraries and the ease at which you can do things in the language makes me smile. The language has been created for lazy programmers as myself. I skimmed through a python book once to the point where the syntax made sense to me. From that point on, anything I needed I found on line. I would like to share in this post some of the nice things I was able to achieve with Python that made my life much happier.

In my previous group at Microsoft, we worked for a long time on a highly impact-full feature. The design and development of this feature took over a year. It was an amazing ride, and for such an amazing ride you take with you only the best tools. So my obvious choice was C++. By the end of the project I had written tens of thousands of  C++ lines of code. the feature itself was shipped as a DLL, and was intended to be called by another layer of the stack. Our biggest concern was testing. How do you verify the correctness of so many scenarios supported by this huge DLL. More specifically, we wanted to find a way to test our DLL in isolation (without taking dependencies on other layers of the stack). We realized that a testing harness was called for, and my initial approach (given that it was a native C++ DLL) to write this testing harness in C++ as well.  However, call it luck or call it coincidence, it so happened that I got my hands on a Python book. As I mentioned before, I skimmed through this book, learning all the necessary stuff I wanted to learn about the language. I immediately suspected that since the interpreter was written in C, the language might have a good way of interfacing with native code. Indeed, my suspicions were correct. Python in fact has a great way of interacting with native code (the ctypes library), and specifically interacting with native DLLs. After a small conversation with my manager, I decided to give it a shot. 

After a month worth of Python coding I was finally ready to demo my testing harness. It was in fact a great success. It was even more successful than I originally anticipated for it to be. Using this new harness I was able to write many scenario (integration) tests that usually required a full fledged system with all the layers of the stack available. The testing harness allowed myself and others working on this project to write python scripts that would call the DLL (as a blackbox) and test the specific scenario, enabling to validate the correctness of the system. Not only that, the testing harness proved itself extremely useful for finding systematic bugs that would otherwise take a very long time to reproduce or detect. It was only then that I realized the power of a great scripting language. It was only then that I realized the importance of using the right tool for the right task. As much as I love C++, Python was the best tool to use at that point, and I'm happy I chose it.

Since then, I've adopted Python as a step brother to C++. If I need to get *** done, I don't think about it twice. I go for Python. A nice IDE that I found to be extremely useful is the PyCharm IDE, I highly recommend it. As a last story about how I find Python to be useful, I'll tell about a manual task that my new manager (at my new team at Bing) asked me to do. My manager asked me to go through pairs of URLs (page url and image url) and figure out some properties about them. Unfortunately, that task could not be automated, and required that I open each pair of URLs in a browser and look at them to detect some anomalies.  However, given that I'm a lazy programmer (yea, yea I already said that), I figured that the fact that the task can't be automated does not mean that the process of reviewing them can't be automated. So I immediately wrote a python script that read the hundreds of URL pairs from a file, and opened the browser with each pair for me, awaiting a keyboard stroke to move on to the next pair. Yep, in less than an hour of work, I was able to make my life so much easier and so much more comfortable. 

To conclude. I highly recommend picking up python. It is a great tool for most of the stuff that you want to automate. For additional resources on the language I also recommend reading the book: "Automate the boring stuff with Python: Practical  Programming for Total Beginner" by Al Sweigart. Enjoy.

Comments (0)

Skip to main content