Question: Best Practice? (module 'shelve')
Thomas Ploch
Thomas.Ploch at gmx.net
Tue Jan 9 19:56:53 EST 2007
Thomas Ploch schrieb:
> Hello fellows,
>
> I just wanted to know, if there is any best practice concerning
> following code:
>
> import re, shelve
>
> class TextMatcher:
> def __init__(self, patterns, email=False, dbName='textmatch.db'):
> self._initPatterns(patterns)
> self.email = email
> self.dbName = dbName
> if self.email:
> self.emailList = []
> self.emailList.append(
> re.compile(r'[a-zA-Z0-9_.]+@\w+\.\w+'))
>
> def match(self, src, url):
> self.matchDict = {}
> self.matchDict[url] = {}
> # The next 2 functions just add stuff to self.matchDict
> if self.email:
> self._addEmails(src, url)
> self._addPatterns(src, url)
> # Is it good practice to open, write and close the db straight
> # away? Or is it better to leave it open until the whole program
> # has finished, and close it then?
> self.openDB(self.dbName)
> self.db[url] = self.matchDict[url]
> self.db.close()
> # I want to del the matchDict each time so it can't grow big.
> # Is this good, or should it be left open, too?
> del self.matchDict
>
> def openDB(self, dbName=None, modeflag='c'):
> if dbName == None:
> self.db = shelve.open('textmatch.db', flag=modeflag)
> else:
> self.db = shelve.open(dbName, flag=modeflag)
>
>
s/del self.matchDict/self.matchDict.clear() and ignore the second
question. When I read the sent message, it came to my mind. :-)
Thomas
More information about the Python-list
mailing list