Doing some Python
Last three weeks I have been doing some Python. I got a desktop automation project where the main codebase ran in a Raspberry Pi computer. I did not choose Python initially but a part of the codebase was already developed it. The project was in extreme hurry and I did not have time to switch it to a language I knew better.
I had used Python before in 2008 in scientific computing classes and in programming teaching practice classes during my MSc studies. The former case used Python as a wrapper around some native numerical vector computation libraries and the latter case dealt with extreme basic language constructs like loops. These were both very academic use cases.
The Pi application used Python 3. I started by copy-pasting together code examples from Google and testing throughfully. The code processed Flic.io button clicks and ran some TCP requests. The buttons part was simple but the TCP part turned out moderately hard:
- Flic.io loop was tied to the main thread.
- TCP connection has to be open and handled in other threads.
- Sockets are not threadsafe.
This led to the the solution with non-blocking IO and complex queue(locks)-based approach. At the end I got it properly working but the code turned out pretty long and complicated compared to the similar code I have written in Node.js where IO is asynchronous by default and where you cannot have race conditions. The server side daemon coordinating our Pi setups was indeed written in Node.js.
In the later stage the project also required 2 GUI applications running on Windows. It came as a nice surprise that Python has a built-in GUI toolkit. The alternatives considered were Qt (C++, too slow to develop, requires messy setup with compilers and IDEs), Node.js/Electron (Node.js has no cross-platform GUI toolkit at all and Electron has miserable performance on low-power devices), SWI-Prolog (it has XPCE and I know the language pretty well but I wanted to avoid dealing with concurrent threaded IO in another language).
I managed to build the apps with Python and tkinter. Another good surprise was support for .pyw bundles that allow to start applications on Windows by clicking the file icon. This also allowed me to put together a trivial NSIS installer that placed the working launch icon to the user's desktop.
Things I like about Python
- Built-in cross-platform GUI toolkit tkinter.
- Lots of other good stuff in standard library.
- Bundling to pyz/pyw files.
- Installed on Linux distros by default.
- Relatively low memory usage.
and not like
- Documentation sucks (how does non-blocking socket.recv behave?).
- Too many IO approaches (blocking, coroutines, async, etc).
- Lots of obsolete Python 2 example code that blows up under Python 3.
- Weird strings (binary stuff should be separated into Buffer-like blobs).
- No application-based package manager like NPM (Pip is system-wide).
This will likely be my single Python project for now. I generally do not develop GUI applications or Raspberry Pi applications. However, if I had to, I might use Python again.