Shallow Thoughts : : programming

Akkana's Musings on Open Source Computing and Technology, Science, and Nature.

Fri, 08 May 2026

Unit Testing TkInter Apps

I've been making a lot of tweaks lately to MetaPho, in particular its Python/TkInter based replacement for my C/GTK2 image viewer Pho.

Pho has always had quite a few modes: it can be fullscreen, in a window sized for the current image, or in a fixed-size window; images can be scaled to the window/screen size, or you can zoom in/out, or you can view them at full size (pixel for pixel). It's fairly common that when I fix a bug in one mode, it introduces a new bug in a different mode because of the way the scaling code works.

Ideally, in a complicated program, you guard against problems like that with automated tests. But that's hard to do in a GUI (graphical user interface) app. A window comes up, but how do you make it do different things? How do you check whether it's showing the right thing, or if it's the right size?

I've tried a couple times to find hints on how to unit test Python scripts in either Tk or GTK, but there's not much help available. I think most people just give up and don't test their GUIs — just as I've always given up.

This time, I decided to really dive in and see if I could write a TkInter unit test script for testing all those different TkPho modes. It wasn't easy, but now I have a basic framework that I should be able to use for other GUI apps as well.

Read more ...

Tags: , ,
[ 13:55 May 08, 2026    More programming | permalink to this entry | ]

Sat, 21 Feb 2026

A More Time Zone Tolerant datetime Class

Yesterday I signed in to the billtracker, and got an error page when trying to display my bill list:

[ ... ]
   File "/var/www/nmbilltracker/billtracker/app/models.py", line 766, in location_html
     if self.last_action_date > self.scheduled_date.replace(tzinfo=None):
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 TypeError: can't compare offset-naive and offset-aware datetimes

Python's datetime class drives me crazy.

Any given datetime object might or might not have a timezone. Those that do are called "timezone aware" or just "aware" datetimes; those without a timezone are called "unaware" or "naive". Any given function might or might not return a timezone-aware datetime. If you ever mess up and call a function that returns a timezone when you didn't expect one, or vice versa, or if a function you call changes in that respect, now you have a hidden time bomb that will crash your program the next time you do any sort of comparison with or subtraction from another datetime, and by then, you may have no idea way of finding out where the problematic time came from so you can guard against it happening again.

Read more ...

Tags: , ,
[ 18:53 Feb 21, 2026    More programming | permalink to this entry | ]

Tue, 27 Jan 2026

A "Purple" Map of the 2024 US Presidential Election

(This is Part 1, a single state.)

[A screenshot of a red/blue/purple voting map, by precinct,
           of New Mexico, with a popup showing details for one precinct] I went looking for a "purple map" of the 2024 presidential election — one of those maps that colors areas from red to blue depending on how they voted.

And I couldn't find one! Well, I found lots of JPEGs and PDFs and such, but I couldn't find a single map that was interactive and let me zoom in and actually see the county-level data I was interested in.

Getting Data

It's not for lack of data. I'm happy to report that this year, searching for 2024 presidential election county data got several useful hits. I settled on the MIT Election Lab data, which has a GitHub repository with a bunch of state-level files (that, weirdly, are all zipped, so you have to unzip each one individually).

Read more ...

Tags: , , , , , , , , ,
[ 12:41 Jan 27, 2026    More programming | permalink to this entry | ]

Mon, 26 May 2025

Using Census Population Data

[Census interactive map showing population in blocks] A couple of us in the local League of Women Voters chapter have been talking about how our county's school board is elected. There are five school districts, to go with the five elementary schools ... but when it comes time to vote for the school board, the voting districts aren't the same as the school districts. For example, a parent whose kid goes to Barranca might be voting for the school board rep from the Aspen district. This confuses pretty much everybody.

Apparently the reason it's set up this way is that the voting districts need to have roughly equal population, which the actual school districts don't. That made us curious about how the populations of the actual school districts compared. But it turns out if you ask that question, no one has those numbers, or at least, we couldn't find anyone who would release them.

"No problem!" I chirped. "I can get population data from the Census website, and combine that with the GIS for the school districts!"

Little did I know, when I promised that, what a soul-sucking pit of despair the Census website is, and how difficult it is to get data out of it.

Read more ...

Tags: , ,
[ 10:39 May 26, 2025    More programming | permalink to this entry | ]

Sat, 04 May 2024

Creating an Image with Wrapped Text using the Python Imaging Library

I stumbled onto the page for this year's Asimov's Magazine Readers' Award Finalists. They offer all the stories right there -- but only as PDF. I prefer reading fiction on my ebook reader (a Kobo Clara with 6" screen), away from the computer. I spend too much time sitting at the computer as it is. But trying to read a PDF on a 6" screen is just painful.

The open-source ebook program Calibre has a command-line program called ebook-convert that can convert some PDF to epub. It did an okay job in this case — except that the PDFs had the wrong author name (they all have the same author, so I'm guessing it's the name of the person who prepared the PDFs for Asimov's), and the wrong title information (or maybe just no title), and ebook-convert compounded that error by generating cover images for each work that had the wrong title and author.

I went through the files and fixed each one's title and author metadata using my epubtag.py Python script. But what about the cover images? I wasn't eager to spend the time GIMPing up a cover image by hand for each of the stories.

Read more ...

Tags: , ,
[ 13:52 May 04, 2024    More programming | permalink to this entry | ]

Sun, 10 Mar 2024

How Common is Easter in March?

"Easter is March 31 this year," my husband said. "I think that's rare, having Easter in March."

"I guess so," I said.

"There's a Cray somewhere that they use to calculate the date each year," he joked.

And of course that made me want to find out if Easters in March really are rare.

Read more ...

Tags: ,
[ 12:49 Mar 10, 2024    More programming | permalink to this entry | ]

Fri, 22 Sep 2023

Drag-and-Drop in Python Qt6

I had a need for a window to which I could drag and drop URLs.

I don't use drag-and-drop much, since I prefer using the commandline rather than a file manager and icon-studded desktop. Usually when I need some little utility and can't immediately find what I need, I whip up a little Python script.

This time, it wasn't so easy. Python has a GUI problem (as does open source in general): there are quite a few options, like TkInter, Qt, GTK, WxWidgets and assorted others, and they all have different strengths and (especially) weaknesses.

Drag-and-drop turns out to be something none of them do very well.

Read more ...

Tags: , ,
[ 18:45 Sep 22, 2023    More programming | permalink to this entry | ]

Thu, 07 Sep 2023

Los Alamos Voting Data on a Folium Choropleth Map

Somebody in a group I'm in has commented more than once that White Rock is a hotbed of Republicanism whereas Los Alamos leans Democratic. (For outsiders, our tiny county has two geographically-distinct towns in it, with separate zip codes, though officially they're both part of Los Alamos township which covers all of Los Alamos county. White Rock is about half the size of Los Alamos.)

After I'd heard her say it a couple times, I got curious. Was it true? I asked her for a reference, but she didn't have one. I decided to find out.

Read more ...

Tags: , , ,
[ 11:58 Sep 07, 2023    More programming | permalink to this entry | ]