slice() objects in Python

I’ve been using Python for a while now. I’ve generally learned it organically, figuring out what I need to know to solve the problem at hand, and moving on to the next one. This has worked out OK, but has left some gaps.

For example, while reading Writing Idiomatic Python just a few years ago, I was surprised to learn about dictionary comprehension, set comprehension, and generator expressions. I don’t need those things, but they sure are nice.

Lately, I’ve been flipping through Fluent Python. One thing that has stood out as an important part of understanding the language, that I just had no idea about: slice objects.

I’ve known for a while that, behind the scenes, code like this:


…gets interpreted as something more like this:


This is useful, for example, if you want to write your own classes that support numeric or key-based indexes. You just provide your own implementation of __getitem__.

What about this, though?


That’s python’s syntax for “slicing” a sequence. We’re asking for the third-to-seventh items from the sequence, while skipping every two. Translated into a __getitem__ call, it looks like this:


“slice” is a built-in class for representing a slice operation. It has no connection to the actual data you’re dealing with. It simply represents the abstract idea of “every other item from third-to-ninth” of some (any!) sequence. The metaphor isn’t perfect, but think of it as a piece of paper with a hole cut out of it, which you can lay over any sequence and see just the items you are interested in.

This might be an interesting language detail. Is it useful?

The canonical example seems to involve emulating named keys in fixed-width data. Let’s say we have just the data columns of this file (hours of daylight in Springfield, VA, for 2016). The first column is the day of the month, and the rest all refer to a particular month. For February 13, we would look at the 13th row, 3rd column.

By examining the file, we can figure out that the January column starts 8 characters in, and ends at character 13. Each month’s data starts 9 more characters in. This took some trial and error, but this produces all of the slices we need (ignoring the first column):

JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC = [slice(x*9+8,x*9+13) for x in range(0,12)]

That’s not going to win any prizes for clear, readable code. It does enable you to now use those variables in slicing operations, though. Which of these is easier to read?

daylight_hours = row[MAR]



I think it’s useful.

Interesting things I learned while training to be a Fairfax County Election Officer

I spent a few hours yesterday training to be a Fairfax County Election Officer. At some point in the next few weeks I will be assigned a precinct for the March 1st primary.

These are a few things I found noteworthy:

  • Voter ID Cards: if you don’t have a driver’s license,  state ID, or any of the myriad accept forms of ID,  you can get a Voter ID.
  • Cards that have an expiration date are valid for voting up to a year after that date.
  • ExpressVote-right_webThe ExpressVote machine “neither votes nor is particularly fast”.  It’s an accessible way to fill out a ballot,  for people who are unable to do it on paper.  The machine can be operated by touch-screen,  a combination of headphones and a braille keypad, or the breath input built in to some wheelchairs. The screen also has a high-contrast mode for color-blind voters. It prints out a little slip of paper. Just like the regular ballot, you have not actually voted until you feed it into the vote scanning machine, the DS200.
  • If the DS200 had legs it would look like Gonk.
  • Voters who are over 65 or disabled can vote curbside.
  • There is no trash and no souvenirs– every bit of detritus produced by the voting process gets filed away in a particular places. Spoiled ballots? There’s an envelope for that. The zip-tie that keeps the DS200 closed until just before the polls open? Envelope 7C.
  • Those zip ties have serial numbers.

Geeky bits:

  • The laptops in the precinct used for checking voters in are on a physical LAN, and share (peer-to-peer) an MS-SQL database, exported from the county registrar’s master database within the last day or so. Each laptop starts out with a complete copy of the database, on a thumb drive. It seems like this would be unnecessary if all of the laptops are networked (since a single database could propagate out to the other stations, through the same mechanism they use to keep in-sync), but it’s probably a significant speed-up, and more fault tolerant.
  • There’s no connectivity to the internet or elsewhere on on those laptops, and no other software besides the “Pollbook” app.
  • Attempting to do anything else on the laptops is probably  a felony.
  • The DS200’s are not networked at all, all votes are stored on a thumb drive. At the end of the polling day, the machine spits out a paper tape with the day’s results, along with images of any right-in votes. The Chief Election Officer uses that print out to fill out the final paperwork and report on the precinct’s results.
  • When using the DS200, you’ll see a count of votes for the current election, and a”protected count”. This second number is the total number of votes cast over the lifetime of the machine.

Explaining Reproducible Builds

Reproducible Builds is the sort of idea, that once I started to grok it, I started thinking about how I would explain it to others.

Luckily, I work in a place where, every quarter, there’s a chance to give a lightning talk, or short presentation. The audience is generally tech-savvy, but only some people are part of systems management, devops, or related disciplines where they have probably already encountered the idea. Here’s what I came up with:

What would the Manhattan Project have done with an iPhone?

A coworker, Seph Coster, tweeted:

My first instinct was to respond with a joke. Something like “#oppenheimerselfie”.

I think what Seph was imagining, is the computing power an iPhone represents. In terms of raw capability, the devices we carry around in our pockets (and even on our wrists) definitely outclass the vacuum-tube based computers of that era. It would have been transformative.


I don’t know what the rules of this single device time travel scenario are, exactly– but I don’t think they could have put an iPhone to much use. The iPhone doesn’t support freedom 0, “The freedom to run the program as you wish, for any purpose“.

Even if the Project had the interest and resources to to compile their code into a form that would run on the iPhone, the iPhone will refuse to run any software that hasn’t been “signed” by Apple and distributed via the App Store. Yes, app developers can transfer their own code onto their own devices, but only if they’ve paid their yearly $100 to Apple.

Among myriad other things, both of those paths for getting code to the device require an internet connection (which doesn’t exist yet), and place a company (which doesn’t exist yet) in control of what software can run, or who is an authorized developer.

Could they have gotten around those restrictions? Maybe. Reverse engineering modern DRM systems (or figuring out how to jailbreak), with 1950’s technology, would be big undertaking. Perhaps as big as the rest of the Manhattan Project.

I haven’t brought up one particular very sweet solution yet– if they managed to reverse-engineer WiFI or other wireless protocols, and run a web server, they could theoretically run web apps. It’s a stretch, but perhaps more plausible than routing around the restrictions above.

That aside, almost any other common computing device, any mac, any PC, or phones and tablets running Android, would be more useful.


Previously, I wrote a bit about trying out Red Hat Enterprise Linux Workstation on my laptop: RHEL Notes, RHEL Notes II. I think, ultimately, I was taken in by a pretty simple critical thinking error (I’m sure there’s a fancy name for it):

  • Thing X costs money, under most circumstances
  • I can get thing X for free
  • Therefore, I should certainly use thing X

RHEL Workstation is a perfectly nice desktop, but there’s nothing that makes it a better choice for me, than Fedora, Debian, Ubuntu, Arch, Elementary (and on and on). I want software that’s more up-to-date, doesn’t require a license key, and doesn’t include arbitrary limits on what software I can use.

For now, I’m using Fedora 22.