Handlers speculative: how to get a random word notifier in GNOME 3

I’ve been using my “fan merinos” random word generating script for a long time, and increasingly regularly as I take part in a few forums with transient pseudonymity and it’s a useful way to quickly get a pseudonym:

$ rw 1
mortally

Sure. Pseudonym activate. Yours, “Mortally”.

The result has been though that I increasingly return to my desktop to find that I’ve opened about 15 terminals and run rw in each and abandoned them.

How useful it would be to have a GNOME notification generated via a keyboard shortcut! Eg I could press Ctrl+Alt+R and get:

A GNOME notification reading: convalescence identities teases handlers speculative

Putting all this together takes a bit of work. First, the shell script to create the notification:

#!/bin/bash

WORDS=/usr/share/dict/words
NUM_WORDS=5 # fits nicely in a notification for me

notify-send "$(cat "$WORDS" | grep -v "'" | grep -v "[A-Z]" | shuf -n $NUM_WORDS | xargs echo)" -a "Random words"

Save to, eg ~/bin/rw-notify.sh, and then make it executable: chmod 700 ~/bin/rw-notify.sh

If you run the command from a terminal you then get the notification.

But for some reason I haven’t worked out, GNOME’s keyboard shortcuts won’t run a shell script. If you try and add a shortcut that runs this command, it will simply do nothing.

So, next create another file: ~/.local/share/applications/rw-notify.desktop:

#!/usr/bin/exo-open
[Desktop Entry]
Name=Random Words Notify
Exec=[YOUR HOME DIR]/bin/rw-notify.sh
Type=Application

Replacing [YOUR HOME DIR] with the location of your home dir, eg:

Exec=/home/mary/bin/rw-notify.sh

This will let you activate an application called “Random Words Notify” from the GNOME launcher.

Last step, binding it to a key.

First, you need to have /usr/bin/exo-open available on your system (for some context, see GNOME bug 343896). On Debian and Ubuntu, you’ll need to install exo-utils and on Fedora exo.

You will also need to make the desktop file itself executable: chmod 700 ~/.local/share/applications/rw-notify.desktop

Now, finally open the keyboard shortcuts editor (open the Activities view and start typing “Keyboard”). Scroll to the bottom and press the + to add a new custom shortcut:

Screenshot of the GNOME keyboard shortcut dialog

Then press “Set shortcut” followed by the keys you want (in my case, Ctrl+Alt+R), and fill in a name and, as the command, the full path of the rw-notify.desktop file, eg /home/mary/.local/share/applications/rw-notify.desktop:

Screenshot of the GNOME Add Custom Shortcut dialog

Press “Add” and now Ctrl+Alt+R will bring up 5 random words in a notification for you:

A GNOME notification reading: convalescence identities teases handlers speculative

All shell scripts and commands in this post (enclosed in the <code> HTML tags) are in the public domain.

fan merinos; or how to have a little fun searching logs

At an engineering training with Greg Sabo in my first week at Stripe, he showed a cute trick: using a shell command to generate two random words when testing.

For example, every time I reconfigure my mail server, I send a distressing number of emails in this style:

echo "Testing" | mail -s "Mary Test 1" mary
echo "Testing" | mail -s "Mary Test 2" mary
echo "Testing" | mail -s "Mary Test 3" mary

(I usually lose count around Test 4, for the record.)

Likewise, in testing the Stripe create charges API function, one might run this from the documentation:

curl https://api.stripe.com/v1/charges \
-u sk_test_BQokikJOvBiI2HlWgH4olfQ2: \
-d amount=400 \
-d currency=usd \
-d source=tok_189fCj2eZvKYlo2CjCzCPbk5 \
-d description="Charge for test@example.com"

Wouldn’t those be both more fun and somewhat easier to find in mailboxes, logs and dashboards as, say, Mary test fan merinos and Charge for cellular ascendents respectively? It would be! Thanks Greg!

Implementation-wise, on very recent Ubuntu, the trick is to add something to your bash profile along the times of:

rw () {
cat /usr/share/dict/words | grep -v "'" | grep -v "[A-Z]" | shuf -n 2 | xargs echo
}

Background: shuf is a command that behaves like head and tail, only it returns a selected number random lines. I’m filtering out single quotes (grep -v "'") in its input so as to not unduly annoy xargs, and filtering capital letters (grep -v "[A-Z]") as a proxy for filtering out proper names.

From there:
$ rw
newscaster mucky
$ echo Mary test $(rw)
Mary test equitable rough

For systems without shuf installed, there’s a lot of potential solutions to shuffling a text file at Stack Overflow, this answer has a great roundup.

As a note of caution, you don’t want to run rw live in front of other people or send them the output unchecked; a random selection of 2 English words has some reasonable chance of being disgusting, offensive, strange, inappropriate, etc. Generate some memorable phrases privately in advance!

Slightly related: xkcd: Password strength.

Change the number of words

Added 28 Sep 2016

A slightly improved version of rw that allows a variable number of words to be returned, defaulting to 2:

rw () {
  NUM=$1
  if [ -z "$NUM" ]
  then
    NUM=2
  fi
  cat /usr/share/dict/words | grep -v "'" | grep -v "[A-Z]" | shuf -n $NUM | xargs echo
}

$ rw
reverberations drumming
$ rw 1
bards
$ rw 7
protections proving unfortunately blubbered uninstallers pitchmen locality

Use python-flickrapi 1.2 even after the Flickr SSL transition

On June 27 2014, Flickr changed their API to be SSL-only. The Python flickrapi library was one of many pieces of software that used HTTP to connect to Flickr’s API, and that therefore broke for some users on June 27.

flickrapi supports HTTPS connections as of version 1.4.4, released on June 18 2014. If you are able to upgrade to a new version of flickrapi, you can get the latest flickrapi version from PyPI and ignore the rest of this post.

However, as of mid-2014, many Linux distros, including Ubuntu 14.04 (supported until 2019), still package flickrapi version 1.2, which cannot connect to Flickr’s API over HTTPS and is therefore now non-functional. Since developers may for various reasons choose to use their distro’s version of python-flickrapi, I’ve written a very very small Python class that overrides flickrapi’s FlickrAPI class to connect to Flickr over HTTPS rather than HTTP, and allows continued use of the Flickr API.

You can download my Python module that allows this: flickrapissl. See the README for usage.

More falsehoods programmers believe about time

Noah Sussman has Falsehoods programmers believe about time, including:

All of these assumptions are wrong

  1. There are always 24 hours in a day.
  2. Months have either 30 or 31 days.
  3. Years have 365 days.
  4. February is always 28 days long.
  5. Any 24-hour period will always begin and end in the same day (or week, or month).

As is usual with these kinds of things, he’s only scratching the surface (even though there’s a lot more than in that excerpt). Andrew and I came up with several more already, on the subject of timezones:

  1. All timezones are vertical lines around the globe evenly spaced in 15 degrees intervals.
  2. All timezones are a whole number of hours offset from UTC.
  3. All timezones are no more than 12 hours offset from UTC.
  4. Two cities within some sufficiently small distance must be in the same timezone.
  5. Two cities with the same longitude must be in the same timezone.
  6. A city further to the east of another city must have a time ahead of or equal to the more western city.
  7. There will only be one timezone within any political boundary.
  8. Within a sufficiently large political boundary, there will be different timezones.
  9. Timezone designations like ‘EST’ are unambiguous.*
  10. Daylight savings shifts occur on the same day around the globe.
  11. Or at least within a hemisphere.
  12. Or at least within a continent.
  13. Or at least within a nation.
  14. Daylight savings shifts occur on predictable dates announced ‘sufficiently far’ in advance that there can be an exhaustive listing of them accurate for the next couple of decades.
  15. Well, at least the next few years.
  16. OK, surely at least this month?

* Both Australia and the United States call their east coast timezone this in winter, and guess what: it’s never the same time in New York as it is in Sydney, and the daylight savings status is seldom the same either. (If you’ve seen Australians call it ‘AEST’, well, yes, we do. Sometimes.)