[Tutor] fortune-like utility was [Tutor Digest ...]

Martin A. Brown martin at linux-ip.net
Tue Aug 30 11:23:57 CEST 2011

Good morning and welcome to the list,

 : Just planning it out in my head so far, like pseudocode. I hope 
 : to get started soon. I'm just looking to have a little dialog box 
 : come up, display a random quote of the day, and then hit enter 
 : and it's gone. Should be a nice, simple way to get started with 
 : Python.

N.B. Most of the questions and answers here are about the python 2.x 
series, so just note that there are some minor differences with 
python 3.x.

I would suggest starting with the data/format.  I think somebody 
else has already remarked that with a few thousand quotations, you 
will not have much difficulty loading it all into memory, so here 
are some tips on how to approach the problem by creating a CLI 
application first, moving onto the GUI parts after you have worked 
out the functionality to read a file and produce the quotation.

I don't have the book you are recommending, but here are some things 
that I would be thinking about if I were writing a utility which 
behaves like the venerable unix game called 'fortune'.

  1. Learn how to read line-oriented data from a file [0].  Let's 
     assume that your first pass at this has only one quotation per 

     This little functon works much like the 'cat' utility:

       import sys
       def main(fname):
           f = open(fname,'r')
           for line in f:
               print line.rstrip()
       main(sys.argv[1])  # python this.py <filename>

     Now, put the lines into a list instead of printing them:

       def main(fname):
           quotations = list()
           f = open(fname,'r')
           for line in f:
               quotations.append( line.rstrip() )
           return quotations

  2. Play with the random module to get your 'pick a quotation at 
     random' behaviour.

        import random
        print random.choice(quotations)

  3. Experiment with the vagaries of 'print'.  It produces a 
     newline.  Learn how to suppress the newline when printing 
     (even if you don't want to do that this time).

  4. Experiment with string handling.  Learn how to break a string 
     into lists.  Learn how to make a single string out of the 
     contents of a list.  In short, become familiar with using

        split()  http://docs.python.org/library/stdtypes.html#str.split
        join()   http://docs.python.org/library/stdtypes.html#str.join

  5. Start thinking about what you would have to change in order to 
     handle multi-line or formatted quotations.  This is where you 
     can start thinking about treating your data as records rather 
     than lines, as above.

I suspect that you will have all of the above understood in fairly 
short order.  At that point, you could make the foray into the 
various GUI bits.  I will be no help there, as I have never used any 
of the GUI toolkits--but this list has many others who should be 
able to help here.

I hope the 'Sweet Irene the Disco Queen' flooding in your region was 
not too bad, Frank.


P.S. Here's a possibly obnoxious tip, assuming the 'fortune' file 
     format [1] suits your tastes for storing your quotations:

       f = open(fname,'r')
       quotations = ''.join(f.readlines()).split('%')

 [0] http://docs.python.org/library/stdtypes.html#file-objects
 [1] http://www.faqs.org/docs/artu/ch05s02.html

Martin A. Brown

More information about the Tutor mailing list