#!/usr/bin/env python #-*- coding: iso8859-15 -*- ########################################################## # TAD v1.0 # ########################################################## # Autor: Juan Miguel Taboada Godoy # # Fecha: Szczecin, 06 de Marzo de 2006 # # Descripción: Tipos abstractos de datos basicos # # Versión: 2007030600 # # # # Codigo fuente bajo licencia GNU/GPL # # Centrologic (Computational Logistic Center) # # http://www.centrologic.com - info@centrologic.com # ########################################################## # Stack {{{1 ## \internal ## Stack's definition class Stack: # Constructor {{{2 def __init__(self): self.lst = [] # }}}2 # Show the Stack {{{2 def __repr__(self): # Inicialization first=True # Add the head str="<=" # For all the list for element in self.lst: # Show coma if (first): first=False else: str="%s, " % (str) # Add element if (type(element)==type('abc')): str="%s'%s'" % (str,element) else: str="%s%s" % (str,element) # Add the tail str="%s%s" % (str,"=>") # Return the result return str # }}}2 # Empty {{{2 ## Say if is empty the Stack ## \param self - def empty(self): return len(self.lst) == 0 # }}2 # Pop {{{2 ## Pop an element ## \param self - def pop(self): if (self.empty()): raise PopError,"Stack is already empty" else: result = self.lst[-1] del self.lst[-1] return result # }}}2 # Push {{{2 ## Push an elemento to the stack ## \param self - ## \param element Element to push def push(self, element): self.lst.append(element) # }}}2 # }}}1 # ### EXCEPCIONES ### ################################# # EXCEPTION CLASSES {{{1 # Basic exceptions # Except (General Exception) {{{2 class Except(Exception): def __init__(self,string): self.string=string def __str__(self): return self.string #}}}2 # IOError (Input/Output error) {{{2 class IOError(Exception): def __init__(self,string): self.string=string def __str__(self): return self.string # }}}2 # Exceptions in TADs # PopError (Error doing pop in a Stack) {{{2 class PopError(Exception): def __init__(self,string): self.string=string def __str__(self): return self.string # }}}2 # }}}1