Random signatures with Python

In this article I’ll show you how to make a script that will randomly choose some text (quotes, jokes, *nix advocacy, or whatever your heart desires) from a file named .sigdata and plop it in .signature. We will then make a crontab to run the script every 30 minutes.

Here is the python code:

#!/usr/local/bin/python
# adjust the above to your system.
# find where python is by doing:
# “whereis python” (minus the quotes)
# in console.

import string, random

foo = open(“/home/keskoy/.sigdata”).read()
foo = string.split( foo, “\n\n” )
foo = filter( None, map( string.strip, foo ))
bar = random.choice(foo)
foo2 = open(‘/home/keskoy/.signature’,'w’)
foo2.write(bar)
foo2.close()

cut and paste the code into a file named “randomsig.py” (minus the quotes of course) and be sure to replace the “keskoy” anywhere in the script with your own home dir.

Now create a file named .sigdata in your home directory. Put whatever quotes/sentences/jokes/*nix advocacy (or whatever else strikes your fancy) in .sigdata. Be sure that you press return before any text reaches 80 columns of characters wide, this way any mail client or newsreader can easily view your signature. Also, make sure that each group of text is no longer then 4 lines long because any signature over 4 lines will be considered rude by many usenet readers. Make sure you separate the signatures in .sigdata with 2 returns and 2 returns only (this is very important for the script to work)

Here is an example of what .sigdata should look like:

I Love *NIX!!

Question: Why did the chicken cross the road?
Answer: to get smooshed by a truck (haw haw)!

This is my signature. I’m not very creative so I
just type some random text for people to read. Do
you like my signature? I hope you do since I went
out of my way to make it.

now it’s time to make a crontab file (if you already have one you can just add the below line to it). Also, be sure to leave a blank line after the last command in your crontab or your crontab will not run. If you don’t have a crontab create a file named .CRONTAB in your home dir. Now add the following to your already existing crontab or newly created .CRONTAB file:.

30 * * * * /home/your-home-dir/randomsig.py

Fill in “your-home-dir” with your home directory. Now type “crontab .CRONTAB” (minus the quotes) and that should be it. enjoy!