[Tutor] Contrib: Shelve/DB example

Jackson david.jay.jackson@wcox.com
Tue, 6 Nov 2001 08:51:23 -0700


------------------------------------------------------------------------
This mail message contains standard MIME attachments.  If you see this
note, your e-mail program does not support MIME.  You may need a MIME-
compliant mail reader to read any non-text attachments in this message.
------------------------------------------------------------------------

--==IMail_v5.0==
Content-Type: text/plain; charset=us-ascii

I'm passing along these script which demo basic data storage and retrievial using the shelve module. Hopefully it'll short'en some new users learning curve :)

A Big thanks to the "tutor" and "python-help" folks who help shorten my learning curve.

David Jackson   







--==IMail_v5.0==
Content-Type: text/plain; name="shelve.howto"
Content-Transfer-Encoding: binary

#!/usrlocal/bin/python
import time,string,shelve,os
###########################################################
# David Jackson, 11/06/2001
# Creates datebase to track server outages
# Python script demostrating use of shelve 
# and multi-line raw_input()
# See rca_report.py: for demo of basic report creation
###########################################################
# define multi line input, contributed by Kalle
def get_input(prompt):
    lines = []
    while 1:
        s = raw_input(prompt)
        if s == ".":
            break
        lines.append(s)
    return "\n".join(lines)
####################################################

indx = time.strftime("%Y%j%H%m%S",time.localtime())
server = raw_input("Enter server: ")
date_opened = raw_input("Start date: ")
date_closed = raw_input("Date closed: ")
vendor = raw_input("Vender name: ")
vendor_ticket = raw_input("Vendor ticket: ")
print "-------------------------------------------"
print "Event and Resolution allow mult-line input"
print "Enter \".\" on blank line to exit field"
print "-------------------------------------------"
event = get_input("Describe event: ")
resolution = get_input("Describe resolution: ")
#
rca=shelve.open("rca.db)
os.chmod("rca.db",0666)
rca[server +":"+ indx]=[server,date_opened, date_closed,vendor,vendor_ticket,event,resolution]
rca.close()
rca=shelve.open("rca.db")
print rca.keys()




#!/usr/bin/python
import time,string,shelve
###############################################################
# David Jackson, 2001/11/06
# Uses datebase created by rca_event.py to create quick report
# after prompting for server name.
###############################################################
box = raw_input("Enter server name: ")
def getmatches(pattern, thedict):
	keys = [i for i in thedict.keys() if i.find(pattern)>=0]
	for k in keys:
			print  "Server: ",
			print  '%s' %(thedict[k][0])
			print "Date(opened/closed): ",
			print  '%s' %(thedict[k][1]),
			print  '%s' %(thedict[k][2])
			print "\nEvent:" 
			print  '%s' %(thedict[k][5])
			print "\nResolution:"
			print  "%s" %(thedict[k][6])
db = shelve.open("rca.db")
getmatches(box,db)





--==IMail_v5.0==--