@@ -1168,72 +1168,65 @@ def __setitem__(self, k, v):
1168
1168
1169
1169
class Stack (object ):
1170
1170
"""
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.
1174
1174
"""
1175
1175
1176
1176
def __init__ (self , default = None ):
1177
1177
self .clear ()
1178
1178
self ._default = default
1179
1179
1180
1180
def __call__ (self ):
1181
- """return the current element, or None"""
1181
+ """Return the current element, or None. """
1182
1182
if not len (self ._elements ):
1183
1183
return self ._default
1184
1184
else :
1185
1185
return self ._elements [self ._pos ]
1186
1186
1187
1187
def __len__ (self ):
1188
- return self ._elements . __len__ ( )
1188
+ return len ( self ._elements )
1189
1189
1190
1190
def __getitem__ (self , ind ):
1191
- return self ._elements . __getitem__ ( ind )
1191
+ return self ._elements [ ind ]
1192
1192
1193
1193
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 )
1198
1196
return self ()
1199
1197
1200
1198
def back (self ):
1201
- """move the position back and return the current element"""
1199
+ """Move the position back and return the current element. """
1202
1200
if self ._pos > 0 :
1203
1201
self ._pos -= 1
1204
1202
return self ()
1205
1203
1206
1204
def push (self , o ):
1207
1205
"""
1208
- push object onto stack at current position - all elements
1209
- occurring later than the current position are discarded
1206
+ Push element to stack at current position, discard all later elements.
1210
1207
"""
1211
- self ._elements = self ._elements [:self ._pos + 1 ]
1212
- self ._elements .append (o )
1208
+ self ._elements = self ._elements [:self ._pos + 1 ] + [o ]
1213
1209
self ._pos = len (self ._elements ) - 1
1214
1210
return self ()
1215
1211
1216
1212
def home (self ):
1217
- """push the first element onto the top of the stack"""
1213
+ """Push the first element onto the top of the stack. """
1218
1214
if not len (self ._elements ):
1219
1215
return
1220
1216
self .push (self ._elements [0 ])
1221
1217
return self ()
1222
1218
1223
1219
def empty (self ):
1220
+ """Return whether the stack is empty."""
1224
1221
return len (self ._elements ) == 0
1225
1222
1226
1223
def clear (self ):
1227
- """empty the stack"""
1224
+ """Empty the stack. """
1228
1225
self ._pos = - 1
1229
1226
self ._elements = []
1230
1227
1231
1228
def bubble (self , o ):
1232
- """
1233
- raise *o* to the top of the stack and return *o*. *o* must be
1234
- in the stack
1235
- """
1236
-
1229
+ """Raise element (present in the stack) to the stack top; return it."""
1237
1230
if o not in self ._elements :
1238
1231
raise ValueError ('Unknown element o' )
1239
1232
old = self ._elements [:]
@@ -1249,7 +1242,7 @@ def bubble(self, o):
1249
1242
return o
1250
1243
1251
1244
def remove (self , o ):
1252
- 'remove element *o* from the stack'
1245
+ """Remove element from the stack."""
1253
1246
if o not in self ._elements :
1254
1247
raise ValueError ('Unknown element o' )
1255
1248
old = self ._elements [:]
0 commit comments