Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 659a3b5

Browse files
committed
Optimized the hell out of listmessages().
Changed numericprog regexpr to make it faster to check. Removed now unnecessary checks for os.curdir, os.pardir.
1 parent c054d70 commit 659a3b5

1 file changed

Lines changed: 8 additions & 10 deletions

File tree

Lib/mhlib.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ def listfolders(self):
140140
folders = []
141141
path = self.getpath()
142142
for name in os.listdir(path):
143-
if name in (os.curdir, os.pardir): continue
144143
fullname = os.path.join(path, name)
145144
if os.path.isdir(fullname):
146145
folders.append(name)
@@ -160,7 +159,6 @@ def listsubfolders(self, name):
160159
subfolders = []
161160
subnames = os.listdir(fullname)
162161
for subname in subnames:
163-
if subname in (os.curdir, os.pardir): continue
164162
fullsubname = os.path.join(fullname, subname)
165163
if os.path.isdir(fullsubname):
166164
name_subname = os.path.join(name, subname)
@@ -189,7 +187,6 @@ def listallsubfolders(self, name):
189187
subfolders = []
190188
subnames = os.listdir(fullname)
191189
for subname in subnames:
192-
if subname in (os.curdir, os.pardir): continue
193190
if subname[0] == ',' or isnumeric(subname): continue
194191
fullsubname = os.path.join(fullname, subname)
195192
if os.path.isdir(fullsubname):
@@ -227,7 +224,6 @@ def makefolder(self, name):
227224
def deletefolder(self, name):
228225
fullname = os.path.join(self.getpath(), name)
229226
for subname in os.listdir(fullname):
230-
if subname in (os.curdir, os.pardir): continue
231227
fullsubname = os.path.join(fullname, subname)
232228
try:
233229
os.unlink(fullsubname)
@@ -239,9 +235,9 @@ def deletefolder(self, name):
239235

240236
# Class representing a particular folder
241237

242-
numericprog = regex.compile('[1-9][0-9]*')
238+
numericprog = regex.compile('^[1-9][0-9]*$')
243239
def isnumeric(str):
244-
return numericprog.match(str) == len(str)
240+
return numericprog.match(str) >= 0
245241

246242
class Folder:
247243

@@ -284,13 +280,15 @@ def listallsubfolders(self):
284280
# As a side effect, set self.last to the last message (or 0)
285281
def listmessages(self):
286282
messages = []
283+
match = numericprog.match
284+
append = messages.append
287285
for name in os.listdir(self.getfullname()):
288-
if name[0] != "," and \
289-
numericprog.match(name) == len(name):
290-
messages.append(string.atoi(name))
286+
if match(name) >= 0:
287+
append(name)
288+
messages = map(string.atoi, messages)
291289
messages.sort()
292290
if messages:
293-
self.last = max(messages)
291+
self.last = messages[-1]
294292
else:
295293
self.last = 0
296294
return messages

0 commit comments

Comments
 (0)