please comment - my first classes
Hilbert
hilbert at microsoft.com
Fri Mar 14 01:53:52 EST 2003
Hello,
I started writing a script to keep track of my
bookmarks.
This is the first script where I'm using classes.
I wanted to ask your opinions so that I know
I'm on the right (or wrong) path
I want to turn this into a cgi script, so the input
part is just for testing. I'm not sure if this is the
correct way for setting up the groups class.
Thanks,
Hilbert
#----------------------------script starts
here--------------------------------
class bookmark:
def __init__(self,group,name,url):
self.group=group
self.name=name
self.url=url
def Print(self):
print self.group,':',self.name,':',self.url
def AsTuple(self):
return( (group,name,url) )
class bookmark_group_db:
def __init__(self):
self.lastKey = -1
self.groups = {}
def Add(self,name):
self.lastKey += 1
self.groups[self.lastKey]=name
def Has(self,key):
if self.groups.has_key(key):
return 1
else:
return 0
def PrintAll(self):
for i in self.groups.keys():
print i,':',self.groups[i]
class bookmark_db:
def __init__(self):
self.lastKey = -1
self.bookmarks = {}
def Add(self,group,name,url):
self.lastKey += 1
self.bookmarks[self.lastKey]=bookmark(group,name,url)
def AddBookmark(self,src):
self.lastKey += 1
self.bookmarks[self.lastKey]=src
def PrintAll(self):
for i in self.bookmarks.keys():
self.bookmarks[i].Print()
def Find(self,sub):
matches = bookmark_db()
for i in self.bookmarks:
if self.bookmarks[i].name.find(sub)>-1 or
self.bookmarks[i].url.find(sub)>-1:
matches.AddBookmark(self.bookmarks[i])
return (matches)
def Size(self):
return (len(self.bookmarks))
def inputBookmark(bm_local,g_local):
name = raw_input('bookmark name : ')
url = raw_input('bookmark url : ')
print 'available groups:'
g_local.PrintAll()
while 1:
try:
group = int(raw_input('select group# : '))
except ValueError:
print 'must enter a number'
else:
if g_local.Has(group):
break
else:
print 'no such group:',group
bm_local.Add(group,name,url)
g = bookmark_group_db()
g.Add('weather')
g.Add('maps')
b = bookmark_db()
b.Add(0,'weather','www.weather.com')
b.Add(1,'map','maps.yahoo.com')
inputBookmark(b,g)
print
b.PrintAll()
print
print 'bookmark db size:',b.Size()
c=b.Find('weather')
print
print 'following bookmark(s) match "weather":'
c.PrintAll()
#------------------------------------------------------------------
More information about the Python-list
mailing list