This collection is inspired by Joseph Strout's collection titled Python Scripts for newbies (and Non-Newbies); in fact, one of the scripts is also in that collection.
- celsius (View script): Takes Fahrenheit temperatures from the command line, and prints their Celsius equivalents. I'm used to the metric system, and am not accustomed to the Fahrenheit temperatures used in the US--is 80 degrees hot, or just warm?--so I use this script after watching the weather forecast.
- pyprint (View script):
Format a plain ASCII file, adding page breaks and line feeds. This is
handy for sending output to my dot-matrix printer; under Linux, just
do
pyprint filename >/dev/lp1(or to whatever your printer device is). - letters.py (View script):
A simple guessing game about letters. The game comes from a research project described in Russ Walter's book The Secret Guide to Computers.
A word about The Secret Guide: I was introduced to the book in my early teens, when my brother brought home a copy. I'd already become a computer geek by that time, writing BASIC programs for my trusty Commodore, and The Secret Guide provided lots of project ideas; Walter's discussion of how Eliza worked led to an Eliza of my own (now lost). Since then the book has mutated to match the current state of personal computing--the 8-bit microcomputers have been dropped for Windows 95 and MacOS--but it still gives an excellent overview of the history that's brought us to our current pass; few books bother to cover this material, but I think it's important.
- identd.py (View script): This script implements the ident network protocol, which is sometimes used for simple authentication, for Linux. (Determining which user created a given socket is platform-specific, so the script will need rewriting for different operating systems.) The script is designed to be called from inetd, so it can simply use stdin and stdout for its input and output, and doesn't need to mess with the details of socket handling (since inetd will take care of that). For some socket code, see fingerd.py below.
- fingerd.py (View script):
This is another simple network server that implements the protocol
used by
finger, but it doesn't need to be called from inetd, and it uses the SocketServer module from the standard Python library. It's a good example of how using existing modules can really simplify your work; it does more than identd.py, yet is half its size. - tabview.py (View script): Reads a tab-delimited file into memory, and displays it in a spreadsheet-like manner. Requires the curses module, and will probably be helpful if you want to learn curses programming in Python.
-
fortune.py (View script) and
strfile.py (View script):
The BSD-games package includes two programs called
strfileandfortune.strfiletakes a text file containing quotations (slogans, ASCII art, whatever...) separated by lines containing only a '%' symbol, and produces an output file namedfilename.datthat contains a index for the table. Thefortuneprogram then uses this index to pick a random quotation and print it out.strfile.pyprocesses an input file whose name is specified on the command line, and writes outfilename.dat, which should be compatible with the C versions of these programs.fortune.pycontains a single function,get(filename), that mimics what thefortuneprogram does; it opens the index file, randomly chooses a quotation, and returns a string containing it. These scripts illustrate random access to a disk file, and a simple usage of thestructmodule, complicated by the fact that the code has to work on both 32-bit and 64-bit machines..
Recipes
- Directory Watcher: a function that sits and watches a directory for any changes to the files within it.
- Filtering Elements from a Namespace: a function that discards XML elements that come from a given namespace.