Skip to content

Instantly share code, notes, and snippets.

@ricardosiri68
Last active August 29, 2015 14:22
Show Gist options
  • Select an option

  • Save ricardosiri68/e9bc4ae5b28357e879c7 to your computer and use it in GitHub Desktop.

Select an option

Save ricardosiri68/e9bc4ae5b28357e879c7 to your computer and use it in GitHub Desktop.
import os
# CAMBIAR CLS POR Cimport os
# CAMBIAR CLS POR CLEAR
"""agregar, buscar, eliminar, modificar, listar, salir
Datos: cedula, nombre, apellido, telefono, correo"""
env_msg = {
'continue': "Pulse Enter para continuar...",
'separation': 43 * "-"
}
class Nodo:
def __init__(self, no, ap, ce, te, co):
self.siguiente = None
self.anterior = None
(self.nombre, self.apellido, self.cedula,
self.telefono, self.correo) = no, ap, ce, te, co
def setdat(self):
return (
self.nombre,
self.apellido,
self.cedula,
self.telefono,
self.correo
)
def __repr__(self):
return self.cedula
class Lista:
def __init__(self):
self.inicio = None
def vacio(self):
'''si el nodo raiz es nulo entonces la lista esta vacia'''
return not self.inicio
def agregar(self, *args):
'''agrega nodos'''
nodo = Nodo(*args)
if self.vacio():
self.inicio = nodo
else:
inicio = self.inicio
self.inicio = nodo
self.inicio.siguiente = inicio
inicio.anterior = self.inicio
def get_all(self):
'''generador que recupera todos los elementos de la lista'''
nodo = self.inicio
while nodo:
yield nodo
nodo = nodo.siguiente
def buscar(self, cedula):
'''un generador para los resultados de busqueda segun la cedula'''
for nodo in self.get_all():
if nodo.cedula == cedula:
# yield es un generador, busca referencia de en castellano de
# un generador de python en internet
yield nodo
nodo = nodo.siguiente
def eliminar(self, nodo):
'''interfaz que elimina una seria de nodos segun su cedula'''
# para entender esto, basicamente tenes que pensar que estas
# quitando un eslabon de una cadena y uniendola nuevamente sin
# el eslabon que quitaste
if nodo.anterior:
nodo.anterior.siguiente = nodo.siguiente
if nodo.siguiente:
nodo.siguiente.anterior = nodo.anterior
# si el nodo que queres borrar se trata del nodo raiz
if nodo is self.inicio:
self.inicio = nodo.siguiente
# luego borras el eslabon =)
del nodo
def modificar(self, nodo, campo, valor):
'''modifica el campo de un nodo'''
if not hasattr(nodo, campo):
raise AttributeError('Nodo no tiene el atributo %s' % campo)
else:
setattr(nodo, campo, valor)
def rec(texto):
a = raw_input(texto)
return a
def clear():
os.system("clear") # os.system("cls")
class Interfaz:
lista = Lista()
opc = 7
def __init__(self):
while self.opc != 6:
clear()
self.opc = self.menu()
try:
self.options[self.opc](self)
except KeyError:
self.invalid_option()
def invalid_option(self):
rec("Ingrese una opcion valida/Enter para continuar...")
def add(self):
'''accion de agregar un nodo a la lista'''
a = rec("Nombre: ")
e = rec("Apellido: ")
b = rec("Cedula: ")
c = rec("Telefono: ")
d = rec("Correo: ")
self.lista.agregar(a, e, b, c, d)
rec("Agregado correctamente/Enter")
def search(self):
'''accion de buscar un nodo en la lista segun su cedula'''
clear()
if self.lista.vacio():
rec("Lista Vacia / Enter para continuar...")
else:
cedula = rec("Busqueda(cedula): ")
for nodo in self.lista.buscar(cedula):
print nodo.setdat()
print env_msg['separation']
rec(
"lista vacia/enter"
if self.lista.vacio() else env_msg['continue']
)
def show_list(self):
'''imprime la lista completa'''
clear()
for nodo in self.lista.get_all():
print nodo.setdat()
print env_msg['separation']
rec("lista vacia/enter" if self.lista.vacio() else env_msg['continue'])
def remove(self):
'''accion de eliminar un nodo de la lista'''
clear()
cedula = rec("Busqueda/Dato a eliminar(cedula):")
resultados = list(self.lista.buscar(cedula))
cant = len(resultados)
print "Resultado de la busqueda: %s datos" % cant
print env_msg['separation']
for nodo in resultados:
print nodo.setdat()
print env_msg['separation']
option = rec("Eliminar (s/n)")
if option == "s":
self.lista.eliminar(nodo)
cant -= 1
print "Quedan %s resultados" % cant
rec("Sin resultados / Pulse Enter para continuar..." if not cant else
"Pulse Enter para continuar...")
def edit(self):
'''accion de modificar un nodo de la lista'''
clear()
if self.lista.vacio():
rec("Lista Vacia / Enter para continuar...")
else:
cedula = rec("Busqueda/Dato a modificar(cedula): ")
resultados = list(self.lista.buscar(cedula))
cant = len(resultados)
clear()
print "Resultado de la busqueda: %s datos" % cant
print env_msg['separation']
for nodo in resultados:
print nodo.setdat()
print env_msg['separation']
a = rec("Modificar (s/n)")
if a == "s":
self.edit_nodo(nodo, )
rec(
"Sin resultados / Pulse Enter para continuar..."\
if not cant else env_msg['continue']
)
def edit_nodo(self, nodo):
'''interfaz para editar un nodo segun su campo'''
option = 1
while option:
clear()
print nodo.setdat()
print "_______________________"
print "1_ Modificar Nombre"
print "2_ Modificar Apellido"
print "3_ Modificar Cedula"
print "4_ Modificar Telefono"
print "5_ Modificar Correo"
print "0_ Dejar de modificar"
print "_______________________"
options_campo = {
1: ('nombre', 'Nombre: '),
2: ('apellido', 'Apellido: '),
3: ('cedula', 'Cedula: '),
4: ('telefono', 'Telefono: '),
5: ('correo', 'Correo: ')
}
try:
option = int(rec("Ingrese una opcion: "))
value = rec(options_campo[option][1])
self.lista.modificar(nodo, options_campo[option][0], value)
except KeyError:
if option:
rec('Opcion de edicion de campo invalida')
options = {
1: add,
2: search,
3: show_list,
4: remove,
5: edit,
}
def menu(self):
print "_________________________"
print "1_ Agregar"
print "2_ Buscar"
print "3_ Listar"
print "4_ Eliminar"
print "5_ Modificar"
print "6_ Salir"
print "_________________________"
a = int(rec("Ingrese una opcion: "))
return a
if __name__ == "__main__":
interfaz = Interfaz()
clear()
import unittest
from nodos import Lista, Nodo
class TestLista(unittest.TestCase):
def setUp(self):
self.lista = Lista()
def test_add(self):
self.lista.agregar('homero', 'simpson', '1', '1234', 'hm@ga.com')
self.assertEqual(
('homero', 'simpson', '1', '1234', 'hm@ga.com'),
self.lista.inicio.setdat()
)
def test_list(self):
self.lista.agregar('homero', 'simpson', '1', '1234', 'hm@ga.com')
self.lista.agregar('marge', 'simpson', '2', '4321', 'mg@ga.com')
self.lista.agregar('bart', 'simpson', '3', '1111', 'bt@ga.com')
self.assertEqual(len(list(self.lista.get_all())), 3)
def test_search(self):
self.lista.agregar('homero', 'simpson', '1', '1234', 'hm@ga.com')
self.lista.agregar('marge', 'simpson', '2', '4321', 'mg@ga.com')
self.lista.agregar('bart', 'simpson', '3', '1111', 'bt@ga.com')
nodo = list(self.lista.buscar('1'))[0]
self.assertEqual(
nodo.setdat(),
('homero', 'simpson', '1', '1234', 'hm@ga.com')
)
class TestEditLista(unittest.TestCase):
def setUp(self):
self.lista = Lista()
self.lista.agregar('homero', 'simpson', '1', '1234', 'hm@ga.com')
self.lista.agregar('marge', 'simpson', '2', '4321', 'mg@ga.com')
self.lista.agregar('bart', 'simpson', '3', '1111', 'bt@ga.com')
self.nodo = list(self.lista.buscar('3'))[0]
def test_nombre(self):
self.lista.modificar(self.nodo, 'nombre', 'test')
self.assertEqual(
self.nodo.setdat(),
('test', 'simpson', '3', '1111', 'bt@ga.com')
)
def test_apellido(self):
self.lista.modificar(self.nodo, 'apellido', 'tomson')
# - hola senior tomson -
# - creo que le habla a ud -
self.assertEqual(
self.nodo.setdat(),
('bart', 'tomson', '3', '1111', 'bt@ga.com')
)
def test_cedula(self):
self.lista.modificar(self.nodo, 'cedula', '4')
self.assertEqual(
self.nodo.setdat(),
('bart', 'simpson', '4', '1111', 'bt@ga.com')
)
def test_telefono(self):
self.lista.modificar(self.nodo, 'telefono', '3333')
self.assertEqual(
self.nodo.setdat(),
('bart', 'simpson', '3', '3333', 'bt@ga.com')
)
def test_correo(self):
self.lista.modificar(self.nodo, 'correo', 'bt_test@ga.com')
self.assertEqual(
self.nodo.setdat(),
('bart', 'simpson', '3', '1111', 'bt_test@ga.com')
)
class TestRemove(unittest.TestCase):
def setUp(self):
self.lista = Lista()
self.lista.agregar('homero', 'simpson', '1', '1234', 'hm@ga.com')
self.lista.agregar('marge', 'simpson', '2', '4321', 'mg@ga.com')
self.lista.agregar('bart', 'simpson', '3', '1111', 'bt@ga.com')
self.lista.agregar('lisa', 'simpson', '4', '2222', 'ls@ga.com')
self.lista.agregar('maggie', 'simpson', '5', '2222', 'mg@ga.com')
def test_maggie(self):
lista = list(self.lista.get_all())
maggie = list(self.lista.buscar('5'))[0]
lista.remove(maggie)
self.lista.eliminar(maggie)
self.assertEqual(list(self.lista.get_all()), lista)
def test_homero(self):
lista = list(self.lista.get_all())
homero = list(self.lista.buscar('1'))[0]
lista.remove(homero)
self.lista.eliminar(homero)
self.assertEqual(list(self.lista.get_all()), lista)
def test_bart(self):
lista = list(self.lista.get_all())
bart = list(self.lista.buscar('3'))[0]
lista.remove(bart)
self.lista.eliminar(bart)
self.assertEqual(list(self.lista.get_all()), lista)
def test_lisa(self):
lista = list(self.lista.get_all())
lisa = list(self.lista.buscar('4'))[0]
lista.remove(lisa)
self.lista.eliminar(lisa)
self.assertEqual(list(self.lista.get_all()), lista)
def test_marge(self):
lista = list(self.lista.get_all())
marge = list(self.lista.buscar('2'))[0]
lista.remove(marge)
self.lista.eliminar(marge)
self.assertEqual(list(self.lista.get_all()), lista)
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment