Building a Twitter Bot (my 2nd) for Native Plants of Hawaii and Puerto Rico — A Reflection

Another day, another Twitter bot.

Github: https://github.com/liamtrotzuk/island-native-flora-twitter-bot

Twitter:

A little over a month ago, I decided it would be fun to do a new project to practice Python, by using an old project format I’d experimented with a few years ago (a Twitter bot) to share data on a subject I’ve become quite passionate about in the past few years (what flora is native to what parts of the world). After discarding a few ideas, I settled on making 2 different bots — one that tweets flora native to Hawaii, and one that tweets flora native to Puerto Rico. Both of the islands are overseas territories (many would say colonies) of the United States, so I had their botanical datasets already handy, and both are hotspots of endemic biodiversity, which is to say native species found nowhere else on the planet. 

Ultimately, my primary goal of creating these bots was to produce yet another tool that encourages folks to plant and grow flora that is native to whatever place they live (truly, it’s a killer heuristic to guide your cultivation of plants — whether that’s a single potted plant on your stoop or a vast forested estate). But my secondary goal was to practice Python, and it yielded 2 main takeaways. These learnings are probably obvious to folks more seasoned than I am in this kind of thing (making computers do what you want), but it felt worth detailing them nonetheless, in case any other learners stumble across this little writing:

1. Did I get better at Python, or did I just have better data sources and pre-existing code to work from?

The last Twitter bot that I wrote (an account that Tweets a fable by Aesop every day) was — looking over it today — garbage. It works, but barely. I wrote it at a time when I knew hardly any Python, and was hoping that sheer force of will and relentless Googling would make something that worked. It ended up working, but only in the sense that a room of monkeys jumping on typewriters for a billion years will eventually replicate the works of Shakespeare or Sun Tzu or (most topically) Aesop. I don’t even like to look at the code anymore — it’s shameful. And while the newer native botanical script isn’t great, it’s a lot neater than its fabulist predecessor. So what changed?

The obvious answer is that I got better at writing Python, but that’s not the whole story. A different part of it was that I just had better data sources and means to access them, provided by earlier code that I wrote, which is still running away in the background from a year ago.

For example: the Hawaii/PR Twitter bots use environmental datasets, refreshed monthly, from one of my Google Cloud Storage buckets. But even though I wrote the bots in Python, these datasets aren’t generated by Python. They’re generated by an older set of R scripts that run monthly from a Google Cloud VM to scrape various USDA plant/NOAA weather/NCSS soil data sources, fix up the data in a particular way, and upload it to that Storage bucket. And when I wrote these scripts, I had no intention of using them to provide data for Twitter bots. In fact, I originally wrote the code so that I could feed it into the US-focused native plant finder web app that I wrote in Shiny. But even when I was writing the code, I had the feeling that it could be useful for other applications besides the initial amateur web app I went on to create. All to say —even when you’re learning a new language (in my case, Python), you can still benefit from the code/data that you wrote/produced in your old language of choice (in my case, R). Just make sure the old code is good enough to keep working most of the time. 

It’s possible there’s already a well-established term for that concept of compounding benefits from your own personal body of ‘digital infrastructure’ (corny term, but best I can do), in the same way that computer people like to talk about ‘tech debt’? If so, I’d love to learn it. But what struck me about this realization, enough to write about it, is just how fine the line is between A. re-using the code you’ve already written or B. using your improved skills to just write better code. I ultimately had to do both, which leads to Learning 2:

1. No, I actually got better at writing Python.

I can ultimately attribute only 40% of the relative ease of constructing this Twitter bot to the quick access to data provided by code I’d already written. A solid 60% was just because I actually am better at the language (though still pretty garbage). Turns out that practicing a language for 2 years and becoming halfway proficient in a few key packages (pandas, requests, BeautifulSoup) can yield some improvement.

In summary: these learnings are clearly simplistic, but they have been useful revelations nonetheless, and hopefully well-earned in the service of encouraging more folks to plant native.