else
This commit is contained in:
parent
49437bf62c
commit
b3bb5256da
2 changed files with 13 additions and 7 deletions
|
@ -7,9 +7,9 @@ A class to imitate the [Stack](http://en.wikipedia.org/wiki/Stack_(abstract_data
|
||||||
class Stack:
|
class Stack:
|
||||||
Class to replicate a Stack data structure.
|
Class to replicate a Stack data structure.
|
||||||
Attributes: maxlen, name
|
Attributes: maxlen, name
|
||||||
Methods: pop, populate, push, top, truncate
|
Methods: copy, pop, populate, push, top, truncate
|
||||||
|
|
||||||
def deepcopy(self):
|
def copy(self):
|
||||||
Return a deep copy of this Stack as a new object
|
Return a deep copy of this Stack as a new object
|
||||||
|
|
||||||
def pop(self, count=1):
|
def pop(self, count=1):
|
||||||
|
|
|
@ -7,12 +7,14 @@ class Stack:
|
||||||
"""
|
"""
|
||||||
Class to replicate a Stack data structure.
|
Class to replicate a Stack data structure.
|
||||||
Attributes: maxlen, name
|
Attributes: maxlen, name
|
||||||
Methods: pop, populate, push, top, truncate
|
Methods: copy, pop, populate, push, top, truncate
|
||||||
"""
|
"""
|
||||||
def __init__(self, maxlen=None, name=""):
|
def __init__(self, maxlen=None, name=""):
|
||||||
|
self.initialized = False
|
||||||
self.maxlen = maxlen
|
self.maxlen = maxlen
|
||||||
self.name = str(name)
|
self.name = str(name)
|
||||||
self.data = []
|
self.data = []
|
||||||
|
self.initialized = True
|
||||||
|
|
||||||
def __get__(self):
|
def __get__(self):
|
||||||
return self.data
|
return self.data
|
||||||
|
@ -24,9 +26,13 @@ class Stack:
|
||||||
return self.data[index]
|
return self.data[index]
|
||||||
|
|
||||||
def __setattr__(self, key, value):
|
def __setattr__(self, key, value):
|
||||||
if key == "name":
|
if key == "initialized":
|
||||||
|
pass
|
||||||
|
elif not self.initialized:
|
||||||
|
pass
|
||||||
|
elif key == "name":
|
||||||
value = str(value)
|
value = str(value)
|
||||||
if key == "maxlen" and value is not None:
|
elif key == "maxlen" and value is not None:
|
||||||
if value < 0:
|
if value < 0:
|
||||||
raise StackError()
|
raise StackError()
|
||||||
if len(self) > value:
|
if len(self) > value:
|
||||||
|
@ -35,7 +41,7 @@ class Stack:
|
||||||
e = ("Cannot set `maxlen` below current `len` or %s items "
|
e = ("Cannot set `maxlen` below current `len` or %s items "
|
||||||
"would be lost. Try Stack.pop(%s) or Stack.truncate(%d)")
|
"would be lost. Try Stack.pop(%s) or Stack.truncate(%d)")
|
||||||
raise StackError(e % (sizediff, sizediff, value))
|
raise StackError(e % (sizediff, sizediff, value))
|
||||||
if key == "data" and self.maxlen is not None:
|
elif key == "data" and self.maxlen is not None:
|
||||||
if len(value) > self.maxlen:
|
if len(value) > self.maxlen:
|
||||||
raise StackError("List is longer than `maxlen` (%d/%d)" %
|
raise StackError("List is longer than `maxlen` (%d/%d)" %
|
||||||
(len(value), self.maxlen))
|
(len(value), self.maxlen))
|
||||||
|
@ -50,7 +56,7 @@ class Stack:
|
||||||
display += str(self.maxlen - len(self))
|
display += str(self.maxlen - len(self))
|
||||||
return display
|
return display
|
||||||
|
|
||||||
def deepcopy(self):
|
def copy(self):
|
||||||
"""
|
"""
|
||||||
Return a deep copy of this Stack as a new object
|
Return a deep copy of this Stack as a new object
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in a new issue