@@ -1168,72 +1168,75 @@ def __setitem__(self, k, v):
11681168
11691169class Stack (object ):
11701170 """
1171- Implement a stack where elements can be pushed on and you can move
1172- back and forth. But no pop. Should mimic home / back / forward
1173- in a browser
1171+ Stack of elements with a movable cursor.
1172+
1173+ Mimics home/back/forward in a web browser.
11741174 """
11751175
11761176 def __init__ (self , default = None ):
11771177 self .clear ()
11781178 self ._default = default
11791179
11801180 def __call__ (self ):
1181- """return the current element, or None"""
1181+ """Return the current element, or None. """
11821182 if not len (self ._elements ):
11831183 return self ._default
11841184 else :
11851185 return self ._elements [self ._pos ]
11861186
11871187 def __len__ (self ):
1188- return self ._elements . __len__ ( )
1188+ return len ( self ._elements )
11891189
11901190 def __getitem__ (self , ind ):
1191- return self ._elements . __getitem__ ( ind )
1191+ return self ._elements [ ind ]
11921192
11931193 def forward (self ):
1194- """move the position forward and return the current element"""
1195- n = len (self ._elements )
1196- if self ._pos < n - 1 :
1197- self ._pos += 1
1194+ """Move the position forward and return the current element."""
1195+ self ._pos = min (self ._pos + 1 , len (self ._elements ) - 1 )
11981196 return self ()
11991197
12001198 def back (self ):
1201- """move the position back and return the current element"""
1199+ """Move the position back and return the current element. """
12021200 if self ._pos > 0 :
12031201 self ._pos -= 1
12041202 return self ()
12051203
12061204 def push (self , o ):
12071205 """
1208- push object onto stack at current position - all elements
1209- occurring later than the current position are discarded
1206+ Push *o* to the stack at current position. Discard all later elements.
1207+
1208+ *o* is returned.
12101209 """
1211- self ._elements = self ._elements [:self ._pos + 1 ]
1212- self ._elements .append (o )
1210+ self ._elements = self ._elements [:self ._pos + 1 ] + [o ]
12131211 self ._pos = len (self ._elements ) - 1
12141212 return self ()
12151213
12161214 def home (self ):
1217- """push the first element onto the top of the stack"""
1215+ """
1216+ Push the first element onto the top of the stack.
1217+
1218+ The first element is returned.
1219+ """
12181220 if not len (self ._elements ):
12191221 return
12201222 self .push (self ._elements [0 ])
12211223 return self ()
12221224
12231225 def empty (self ):
1226+ """Return whether the stack is empty."""
12241227 return len (self ._elements ) == 0
12251228
12261229 def clear (self ):
1227- """empty the stack"""
1230+ """Empty the stack. """
12281231 self ._pos = - 1
12291232 self ._elements = []
12301233
12311234 def bubble (self , o ):
12321235 """
1233- raise *o* to the top of the stack and return *o*. *o* must be
1234- in the stack
1235- """
1236+ Raise *o* to the top of the stack. *o* must be present in the stack.
12361237
1238+ *o* is returned.
1239+ """
12371240 if o not in self ._elements :
12381241 raise ValueError ('Unknown element o' )
12391242 old = self ._elements [:]
@@ -1249,7 +1252,7 @@ def bubble(self, o):
12491252 return o
12501253
12511254 def remove (self , o ):
1252- 'remove element *o* from the stack'
1255+ """Remove *o* from the stack."""
12531256 if o not in self ._elements :
12541257 raise ValueError ('Unknown element o' )
12551258 old = self ._elements [:]
0 commit comments