6.2.2. Stack

Simple stack class.
Start python section to interscript/core/stacks.py[1 /1 ]
     1: #line 5 "stacks.ipk"
     2: class stack:
     3:   __class_protocols__ = ['sequence','mutable']
     4:   def __init__(self, *args): self.s = list(args)
     5:   def __del__(self):
     6:     # unbind in FILO order!
     7:     while len(self.s): del self.s[-1]
     8: 
     9:   def pop(self):
    10:     tmp = self.s[-1]
    11:     del self.s[-1]
    12:     return tmp
    13:   def push(self,x): self.s.append(x)
    14:   def append(self,x): self.s.append(x)
    15:   def __len__(self): return len(self.s)
    16:   def __nonzero__(self): return len(self.s)!=0
    17:   def __getitem__(self,index): return self.s[index]
    18:   def __setitem__(self,index,value): self.s[index]=value
    19:   def __delitem__(self,index): del self.s[index]
    20:   def __getslice__(self,i,j): return self.s[i:j]
    21:   def __setslice__(self,i,j,seq): self.s[i:j]=seq
    22:   def __delslice__(self,i,j): del self.s[i:j]
    23:   def __mul__(self,i): return apply(stack, tuple(self.s * i))
    24:   def __rmul__(self,i): return apply(stack, tuple(self.s * i))
    25:   def __add__(self,s): return apply(stack, tuple(self.s + s.s))
    26:   def __cmp__(self, other): return cmp(self.s,other.s)
    27:   def __repr__(self):
    28:     s = 'stack('
    29:     if self.s: s = s + repr(self.s[0])
    30:     for i in self.s[1:]: s = s + ', '+repr(i)
    31:     return s+')'
    32: 
    33:   def __setattr__(self,attr,value):
    34:     if attr == 'top':
    35:       self.s[-1]=value
    36:     else:
    37:       self.__dict__[attr]=value
    38: 
    39:   def __getattr__(self,attr):
    40:     if attr=='top':
    41:       return self.s[-1]
    42:     else:
    43:       raise AttributeError,attr
    44: 
    45:   def __delattr__(self,attr):
    46:     if attr=='top':
    47:       del self.s[-1]
    48:     else:
    49:       raise AttributeError,attr
    50: 
    51:   def copy(self): return apply(stack,tuple(self.s))
    52:   def count(self, item): return self.s.count(item)
    53:   def index(self, item): return self.s.index(item)
    54:   def sort(self, order=None):
    55:     if order == None:
    56:       self.s.sort()
    57:     else:
    58:       self.s.sort(order)
    59:   def insert(self,index,item): self.s.insert(index,item)
    60:   def remove(self,item): self.s.remove(item)
    61:   def reverse(self): self.s.reverse()
    62: 
End python section to interscript/core/stacks.py[1]