[Tutor] wheel.py

Kirk Bailey idiot1@netzero.net
Sat, 28 Sep 2002 03:08:44 -0400


ok, I long ago wrote code to randomly pick a line from a file and return it for
use in a web page, and it handles banners and quotes and such. But I wanted to
see if I could write a script to play such data IN A SEQUENCE, so it would go
around and around in an orderly manner- a wheel, so to speak, with each entry a
'spoke'. This way I could have a source file and it would display them in a
preset pattern, and begin again when the end  was reached. Note this thing
rewrites the file with the count for the next item in the first line, and resets
it to zilch when the end is reached. The source file is a flat plain vanilla
text file, and any text editor will serve- and html code is fine, as are lines
of any length, so it could handle a book. 

Here is that script (latter portion of letter).

Here is a link to the demo page:
	http://www.tinylist.org/testwheel.shtml

Here is a link to a page listing the actual working script in the server:
	http://www.tinylist.org/viewwheel.shtml

The script:

#!/usr/local/bin/python
import string, sys              #declare what to import- not much really.
def gangstrip(thing):                   # ok, everybody STRIP!
        index=0                         # This strips out whitespace chars
        while index < len(thing):       #240 define exit
                thing[index]=string.strip(thing[index])
                index=index+1           # increase the counter
#
file=sys.argv[1]                # extract the file to examine
f1=open(file,'r')               # open it,
totalfile=f1.readlines()        # read it all in,
f1.close()
gangstrip(totalfile)            # see, them newline chars really mess things up!
next=int(totalfile[0])          # read that first line in, conveting to a
number,
wheel=totalfile[1:]             # then all the other lines into a seperate
variable,
size=len(wheel)                 # which we want to measure the size of.
if next >= size:                # if next has reached or exceeded the limit,
        next = 0                # start over at the beginning.
# comment out the next line if not needed, remove the '#' if it is.
#print "Content-type: text/html\n"      # this tells the http server what this
is
print wheel[next]               # then print the spoke,
next= next + 1                  # and increment the count.
f1=open(file,'w')               # now increment the count,
f1.write(str(next)+"\n")        # and write that as a string to the file,
for i in wheel:                 # and write out all the spokes
        f1.write(i+"\n")        # followed by the wheel.
f1.close()                      # and close the file
# that's it!



-- 

end

Respectfully,
             Kirk D Bailey


+---------------------"Thou Art Free." -Eris-----------------------+
| http://www.howlermonkey.net  mailto:highprimate@howlermonkey.net |
| KILL spam dead!      http://www.scambusters.org/stopspam/#Pledge |
| http://www.tinylist.org  +--------+   mailto:grumpy@tinylist.org |
+------------------Thinking| NORMAL |Thinking----------------------+
                           +--------+
-------------------------------------------
Introducing NetZero Long Distance
Unlimited Long Distance only $29.95/ month!
Sign Up Today! www.netzerolongdistance.com