Silly Moon Names: a Nice Beginning Python Project
Every time the media invents a new moon term -- super blood black wolf moon, or whatever -- I roll my eyes.
First, this ridiculous "supermoon" thing is basically undetectable to the human eye. Here's an image showing the relative sizes of the absolute closest and farthest moons. It's easy enough to tell when you see the biggest and smallest moons side by side, but when it's half a degree in the sky, there's no way you'd notice that one was bigger or smaller than average.
Even better, here's a link to an
animation
of how the moon changes size and "librates" -- tilts so that we can
see a little bit over onto the moon's far side -- during the course of
a month.
Anyway, the media seem to lap this stuff up and every month there's a new stupid moon term. I'm sure nearly every astronomer was relieved to see the thoroughly sensible Gizmodo article yesterday, Oh My God Stop It With the Fake Moon Names What the Hell Is a 'Black Moon' That Isn't Anything. Not that that will stop the insanity.
If You Can't Beat 'Em, Join 'Em
And then, talking about the ridiculous moon name phenom with some friends, I realized I could play this game too. So I spent twenty minutes whipping up my own Silly Moon Name Generator.
It's super simple -- it just uses Linux' built-in dictionary, with no sense of which words are common, or adjectives or nouns or what. Of course it would be funnier with a hand-picked set of words, but there's a limit to how much time I want to waste on this.
You can add a parameter ?nwords=5
(or whatever number)
if you want more or fewer words than four.
How Does It Work?
Random phrase generators like this are a great project for someone just getting started with Python. Python is so good at string manipulation that it makes this sort of thing easy: it only takes half a page of code to do something fun. So it's a great beginner project that most people would probably find more rewarding than cranking out Fibonacci numbers (assuming you're not a Fibonacci geek like I am). For more advanced programmers, random phrase generation can still be a fun and educational project -- skip to the end of this article for ideas.
For the basics, this is all you need: I've added comments explaining the code.
import random def hypermoon(filename, nwords=4): '''Return a silly moon name with nwords words, each taken from a word list in the given filename. ''' fp = open(filename) lines = fp.readlines() # A list to store the words to describe the moon: words = [] for i in range(nwords): # This will be run nwords times # Pick a random number between 0 and the number of lines in the file: whichline = random.randint(0, len(lines)) # readlines() includes whitespace like newline characters. # Use whichline to pull one line from the file, and use # strip() to remove any extra whitespace: word = lines[whichline].strip() # Append it to our word list: words.append(word) # The last word in the phrase will be "moon", e.g. # super blood wolf black pancreas moon words.append("moon") # ' '.join(list) combines all the words with spaces between them return ' '.join(words) # This is called when the program runs: if __name__ == '__main__': random.seed() print(hypermoon('/usr/share/dict/words', 4))
A More Compact Format
In that code example, I expanded everything to try to make it clear for beginning programmers. In practice, Python lets you be a lot more terse, so the way I actually wrote it was more like:
def hypermoon(filename, nwords=4): with open(filename, encoding='utf-8') as fp: lines = fp.readlines() words = [ lines[random.randint(0, len(lines))].strip() for i in range(nwords) ] words.append('moon') return ' '.join(words)
There are three important differences (in bold):
Opening a file using "with" ensures the file will be closed properly when you're done with it. That's not important in this tiny example, but it's a good habit to get into.
I specify the 'utf-8' encoding when I open the file because when I ran it as a web app, it turned out the web server used the ASCII encoding and I got Python errors because there are accented characters in the dictionary somewhere. That's one of those Python annoyances you get used to when going beyond the beginner level.
The way I define words all in one line (well, it's conceptually
one long line, though I split it into two so each line stays under 72
characters) is called a list comprehension. It's a nice compact
alternative to defining an empty list []
and then
calling append()
a bunch of times, like I did in the
first example.
Initially they might seem harder to read, but list comprehensions can actually make code clearer once you get used to them.
A Python Driven Web Page
Finally, to make it work as a web page, I added the CGI module. That isn't really a beginner thing so I won't paste it here, but you can see the CGI version at hypermoon.py on GitHub.
I should mention that there's some debate over CGI in Python.
The movers and shakers in the Python community don't approve of CGI,
and there's a plan to remove it from upcoming Python versions.
The alternative is to use technologies like Flask or Django.
while I'm a fan of Flask and have used it for several projects,
it's way overkill for something like this, mostly because of all
the special web server configuration it requires (and Django is
far more heavyweight than Flask). In any case,
be aware that the CGI module may be removed from Python's standard
library in the near future. With any luck, python-cgi will still be
available via pip install
or as Linux distro packages.
More Advanced Programmers: Making it Funnier
I mentioned earlier that I thought the app would be a lot funnier with a handpicked set of words. I did that long, long ago with my Star Party Observing Report Generator (written in Perl; I hadn't yet started using Python back in 2001). That's easy and fun if you have the time to spare, or a lot of friends contributing.
You could instead use words taken from a set of input documents. For instance, only use words that appear in Shakespeare's plays, or in company mission statements, or in Wikipedia articles about dog breeds (this involves some web scraping, but Python is good at that too; I like BeautifulSoup).
Or you could let users contribute their own ideas for good words to use, storing the user suggestions in a database.
Another way to make the words seem more appropriate and less random might be to use one of the many natural language packages for Python, such as NLTK, the Natural Language Toolkit. That way, you could control how often you used adjectives vs. nouns, and avoid using verbs or articles at all.
Random word generators seem like a silly and trivial programming exercise -- because they are! But they're also a fun starting point for more advanced explorations with Python.
[ 14:24 Aug 01, 2019 More humor | permalink to this entry | ]