Thursday, May 9, 2019

Moving Bash scripts to Python...

So, I have a bunch of hardware test scripts that became a bit more than Bash could comfortably handle (e.g., I needed n-dimensional arrays, when Bash stops at 1), so I decided to start moving things to Python, truly my favorite "Git 'er done" language on the planet.

But I quickly hit a block.  Many of my scripts need to check the hardware state every second, mainly to check the "I'm On Fire" bit.  To do this, I use Bash's read -rs -t1 -n1 command, which waits up to one second to grab a character into the $REPLY variable.

Remarkably, Python has nothing like this simple function!  WTF?  Not even in a standard package?

I went on a quest to find this missing link, even going to the Hive Mind at StackExchange for advice.  And I wound up answering my own question.  It turns out I had entered Python's "Dead Zone", asking for things that looked easy, yet were too hard to include in either the language or a standard package included with the language.

If you didn't click the link above, please do so now.  The "correct" answer (IMHO) is to simply let Bash do what Bash does best.

I suppose I really should generate a PEP for this, but now that I have my answer, I'm just too lazy to change it: It works!

And doesn't that really make it abide with the Zen of Python?