Aqui un ejemplo mas bonito y simplificado:
Código Python:
Ver originalclass Book(object):
def __init__(self, idbook, title, author, edition, editorial):
self.idbook = idbook
self.title = title
self.author = author
self.edition = edition
self.editorial = editorial
def __str__(self):
return "%s %s %s %s %s" % (self.idbook, self.title, self.author, self.edition, self.editorial)
def __getattribute__(self, attr):
return object.__getattribute__(self, attr)
class BookManager:
def __init__(self):
self.book_list = []
def append(self, idbook, title, author, edition, editorial):
book = Book(idbook, title, author, edition, editorial)
self.book_list.append(book)
def search(self, key, by="idbook"):
for index, book in enumerate(self.book_list):
if book.__getattribute__(by) == key:
return index
def remove(self, key, by="idbook"):
index = self.search(key)
if index != None:
self.book_list.pop(index)
return index
def __str__(self):
s = ""
for book in self.book_list:
s += str(book) + '\n'
return s
#Ejemplo:
bm = BookManager()
bm.append("001", "Title1", "Author1", "Edition1", "Editorial1")
bm.append("002", "Title2", "Author2", "Edition2", "Editorial2")
bm.append("003", "Title3", "Author3", "Edition3", "Editorial3")
bm.append("004", "Title4", "Author4", "Edition4", "Editorial4")
bm.append("005", "Title5", "Author5", "Edition5", "Editorial5")
bm.search("000")
bm.search("001")
bm.remove("004")
bm.remove("004")
print bm