From 0b569bb3992467850926dab9f4e8048a70a6d904 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Tue, 4 May 2004 08:21:52 +0000 Subject: [PATCH] Stacks based on lists work better and read more clearly when they grow and shrink on the right. --- Lib/multifile.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/Lib/multifile.py b/Lib/multifile.py index 23d9d317193..e82a3fdeafb 100644 --- a/Lib/multifile.py +++ b/Lib/multifile.py @@ -38,13 +38,13 @@ class MultiFile: def __init__(self, fp, seekable=1): self.fp = fp - self.stack = [] # Grows down + self.stack = [] self.level = 0 self.last = 0 if seekable: self.seekable = 1 self.start = self.fp.tell() - self.posstack = [] # Grows down + self.posstack = [] def tell(self): if self.level > 0: @@ -88,8 +88,7 @@ class MultiFile: marker = line.rstrip() # No? OK, try to match a boundary. # Return the line (unstripped) if we don't. - for i in range(len(self.stack)): - sep = self.stack[i] + for i, sep in enumerate(reversed(self.stack)): if marker == self.section_divider(sep): self.last = 0 break @@ -130,9 +129,9 @@ class MultiFile: def push(self, sep): if self.level > 0: raise Error, 'bad MultiFile.push() call' - self.stack.insert(0, sep) + self.stack.append(sep) if self.seekable: - self.posstack.insert(0, self.start) + self.posstack.append(self.start) self.start = self.fp.tell() def pop(self): @@ -143,9 +142,9 @@ class MultiFile: else: abslastpos = self.lastpos + self.start self.level = max(0, self.level - 1) - del self.stack[0] + self.stack.pop() if self.seekable: - self.start = self.posstack.pop(0) + self.start = self.posstack.pop() if self.level > 0: self.lastpos = abslastpos - self.start