I decided that in 2019 I would dive into Python. I’ve used Python before, but I’m not at the level I want to be with it. I’ve developed a comfort with Perl over the last 20+ years, and it’s lived up to the name of “Swiss army knife” or “Swiss army chainsaw” of scripting languages. I’m sure I’ve written hundreds (thousands?) of Perl scripts to get things done over the years.
Python has been a bit different. I nearly jumped into Python in my former career when we were about to embrace Zope, but that didn’t happen, so I didn’t really get into Python. I started using Python a lot more seriously when I realized that support for Python on the Raspberry Pi was pushing way ahead of Perl on the platform. Add to that the stuff happening with CircuitPython and it’s the direction I want to go.
So let’s get started! First of all, I had to get Python3 installed, as Mac OS X ships with an older version. This is similar to how things worked with Perl, so I got Homebrew running, got Python3 and PIP working, and started writing some Python.
I should note that learning a new (computer) language is probably easier now than it was 25 years ago. I remember printing out documentation and reading through it, highlighting things to try when I got in front of a computer. Twenty-five years ago I didn’t have a laptop or an always-on Internet connection, and the resources were not quite as abundant. Anyway. I wrote some code!
Above is a simple Python script to list every file in a folder. This is (or should be) dead simple and is needed in many scripts. When I ran it in Mu, it showed some output, but not all of the output I expected. I ran it again, and it showed something different. I ran it again, and got output I did not expect.
So I did what anyone who knows the command line would do, and ran the script there. It worked as expected. Perhaps it’s a bug in Mu? I searched the issues in github, and did some Google searches, and didn’t find anything. Weird. I liked Mu and didn’t want to abandon it, as I want to use it with CircuitPython, but is there some weird buffering bug?
I remembered that one of the most amazing things about when I first started using Perl was how fast it was. At least compared to AppleScript or UserTalk at the time. Maybe I’m going to fast…
I added one line to slow things down. I know that in other Python scripts I’ve used or written in the past few years that time.sleep was often used. Adding in a slight delay made the expected output get outputted. Weird. Whatever. Now I know… I guess?
When I started with Perl I wrote a bunch of simple scripts to learn the language. (I’ve done this with Processing, Arduino, and other languages/environments.) When I learned how arrays worked in Perl I wrote a script named “array.pl” and kept it around as an example. Same with “hash.pl” and “time.pl” and so on. As I was learning things I’d reference these files to get a quick look at how a function worked.
I wrote a script named “random.py” and it was dead simple, with just 3 lines, and the first line didn’t really do anything but I’m in the habit of using shebang lines because I’ve been around the *nix block a few dozen times. So really, two lines, an import and a print.
When I tried to run a Python script just slightly more complex than “Hello World” I got an error… Python nerds may have figured out what went wrong at this point.
Just for fun I dropped the code into Mu and tried to run it… of course you have to save your script before you can run it so… Ahhh, I guess I cannot name my script after an internal function/file that Python uses. At this point I’m just angry, and disappointed that now I have to somehow know what is and is not an acceptable filename for a Python script. I’m sure in the future I’ll name one wrong and it won’t work and I’ll spend more time than I should trying to remember this. (Hopefully I’ll find this blog post.)
I changed the name to random-test.py and it worked fine. Damn you, Python! But I remain undeterred, and I plan to continue on with Python. In fact, all of the stuff above is part of replacing an old Perl script I had running on Raspbian to be written in Python. So… progress! Welcome to 2019, Python.