Six Colors
Six Colors

by Jason Snell & Dan Moren

This Week's Sponsor

Save 20% on the award-winning Audio Hijack with coupon code 6C20AH!

By Jason Snell

Vacation scripting: Python goes to the beach

Pyto does its thing.

Upon arriving at our tropical resort for a week on vacation, my wife and daughter sat out on the deck doing crossword puzzles and I broke out my iPad with Magic Keyboard and tried to solve a puzzle of my own—namely, debugging a Python script. (This is how this family rolls.)

Literally minutes after leaving our house to go to the airport, I got an alert indicating that our home network might be down. Perfect timing, as always. Just before I left I had noticed a notification on my phone that our eero base station had restarted overnight. Given a five-hour flight to Hawaii to stew on that one, and a frantic text message from our housesitter who couldn’t watch anything on our TV, I took a shot in the dark and tried to remotely restart the eero.

The good news is, that totally worked. Within a minute, the network came back and our housesitter was happily binging the HBO Max shows she only gets to see at our house. I checked the weather page on my home server to verify that everything was up and running—and discovered that all of my precious charts and graphs were seriously messed up.

Here’s what happened: Last summer, after teaching myself Python, I decoded the file format of the raw weather data saved out by WeatherCat, the Mac app I use to log data from my home weather station and wrote a script to parse and chart that data exactly how I wanted to.

When I wrote those scripts, I used data files from a time of healthy networking in my house—so every line of every file corresponded to a single minute of data. And here’s the problem: on Saturday, my network died. Now, the Davis WeatherLink IP data logger connected to my weather station does its job as a data logger and caches data—but (at least by default) it doesn’t cache minute-by-minute details. Instead, it saves data every three or four minutes. This led to my scripts drawing some very weird charts, where Friday and Sunday were normal but Saturday appeared to be only about 8 hours long.

This is the very definition of an issue that could wait until I got home, but I starting thinking about how to fix the problem and decided it might be a fun puzzle to solve while unwinding on our first day of vacation. I didn’t bring my Mac with me, but I recently bought Emma Labbé’s Pyto—which, for $10, provides a pretty complete Python development environment on iOS.

I used Screens to connect to my Mac server at home, and copied my script and the current WeatherCat data file to a folder in iCloud Drive. After modifying the file commands to directly address a file on iCloud drive instead of the Mac file paths the script normall uses, I dug in on my iPad.

(For the record, my solution was to save a variable containing the time of the previous line, and if it was more than a minute old, insert the current weather data for every minute until the current minute. This has the result of keeping temporal integrity, though it does make the resulting chart look a little chunky.)

Pyto handled the job pretty well, including providing helpful error messages when I mistyped the name of a variable or a command. I had to remind myself how to generate time differences and properly parse text into a time object using strptime, but I got there in the end. My only issues with Pyto involved the app occasionally failing to respond when I ran a script. Force-quitting the app and relaunching it solved that issue, and I never lost any data.

Before and after
Chart before (above) and after.

I was able to run the script on my iPad, view the charts (generated by matplotlib) right within Pyto, and verify it was all working well. Then I reconnected to my Mac using Screens, compared the iPad-edited script with the one resiting on my server using BBEdit, imported all the relevant changes (but omitting the changes I had made to read files on the iPad), and ran the script on the Mac. My charts were good, the puzzle was solved, and I closed my iPad to enjoy a Hawaiian beer and watch a sunset with my family.

(Of course, today I wrote a Six Colors post about all this while sitting on our lanai and staring out at a volcano. Like I said, this is how this family rolls.)

If you appreciate articles like this one, support us by becoming a Six Colors subscriber. Subscribers get access to an exclusive podcast, members-only stories, and a special community.

Search Six Colors