Programming with Python: It’s time to get ANSI fancy

0

During the darkest days of lockdown, I had fun practicing jazz guitar chords and writing Python programs. I write a lot of little improvised programs, for everything from updating phone codes to solving math puzzles to simulations related to game theory. Python is just the latest in a long list of languages ​​I’ve used – Basic, Forth, Pascal, Lisp and Ruby are a few others – but the one I remember with the most fondness is Turbo Pascal.

My programs are so small and ad hoc that it’s never worth investing too much time writing graphical user interfaces (GUIs), so Windows was a nuisance rather than a liberation. Although I played around with Visual Basic and Delphi for several years, both eventually metastasized into monsters so big I threw them away. I wrote myself a little toolkit in Turbo Pascal 5 that created simple windows, menus and selection widgets from ASCII characters, pretty well. I’ve shared my code in Byte and I’m proud to say I’ve seen it on screens in more than one science lab where they had similar minimalist requirements.

Now I’m using QPython 3.6 on Android, which has no native capability for color or cursor control, just scrolling the teletype output. There are many add-on graphics libraries like Kivy, way more than I need, and there’s a simple interface for Android dialogs and media components called SL4A that I use occasionally, but it’s still not what I had with Turbo.

The impetus to recreate my Turbo widgets was ultimately sparked by, of all things, Wordle. I love playing Josh Wardle’s nifty and elegant little puzzler (almost as much as I deplore posting screenshots on Facebook or bragging about stats). It’s just hard enough to maintain interest, but still simple enough to ask real questions about strategy, which is exactly what I like in a game. I never cheat, but I do use tools that some purists might consider it cheating, namely a mobile version of the Oxford Dictionary with wildcard search and an anagram solver; once I have three or four letters they will complete the job in minutes.

Getting those three out of four letters early enough is what my Python program does, finding efficient first guesses using the known frequency distribution of English letters at every position in a five-letter word: it produces delightful combinations such as AUDIT SNORE CLAMP and CAMEO GRINDING UNITS Whelk. I found out that my old buddy David Tebbutt, one of the founding editors of personal computer world, also likes Wordle and he sent me the list of letter frequencies in a nice nerd-aid example. It also has access to Wordle’s own internal wordlist, but in a fit of prideful righteousness I decided that was a step too far for me.

The scrolling teletype interface of my Python Wordle Helper program became a real annoyance, and while researching how to write a clear screen command using OS calls, I discovered how to issue ANSI codes from Python, and thus how to make a terminal 256 color character base better than Turbo’s. QPython can in theory access Linux library, but it refuses to work for me and anyway it’s pretty awful. I defined and have now written a set of widgets that does everything I want, allowing simple lines of code to invoke a box, window, select list, input box, bar chart, chart or bar graph.

I doubt my new widgets will prove as popular with nerds in science labs as those Turbo originals; these date back to when IBM-compatible PCs running DOS were still ubiquitous in engineering and process control contexts. I know there are still far too many public institutions such as hospitals and libraries that keep such dinosaurs, but they will at least work on Windows XP.

Of course, anyone under 70 uses a smartphone instead of a computer anyway, and an ANSI terminal on a smartphone screen is about as welcome as a turd on sushi. So I’m going to keep my little ANSI world to myself, go back and rewire some of my old programs using the new interface. My games of 5-Card Drawer and 5-Card Stud Poker are actually quite splendid.

Featured Resources

Escape the ransomware maze

Conventional endpoint protection tools are no longer the best defense

Free download

Add value to Microsoft Teams beyond voice connectivity

How AudioCodes can understand your broader business communication needs and fill in the gaps

Free download

Go ahead, dream big: Dell EMC PowerVault ME4 platform

Deliver fast, affordable storage optimized for large projects in growing businesses

Free download

Share.

About Author

Comments are closed.