Ejercicio en Python
Ejercicio en Python
class ContactModel(object):
def __init__(self):
# Create a database in RAM
self._db = sqlite3.connect(':memory:')
self._db.row_factory = sqlite3.Row
def get_summary(self):
return self._db.cursor().execute(
"SELECT name, id from contacts").fetchall()
def get_current_contact(self):
if self.current_id is None:
return {"name": "", "address": "", "phone": "", "email": "", "notes":
""}
else:
return self.get_contact(self.current_id)
class ListView(Frame):
def __init__(self, screen, model):
super(ListView, self).__init__(screen,
screen.height * 2 // 3,
screen.width * 2 // 3,
on_load=self._reload_list,
hover_focus=True,
can_scroll=False,
title="Contact List")
# Save off the model that accesses the contacts database.
self._model = model
def _on_pick(self):
self._edit_button.disabled = self._list_view.value is None
self._delete_button.disabled = self._list_view.value is None
def _add(self):
self._model.current_id = None
raise NextScene("Edit Contact")
def _edit(self):
self.save()
self._model.current_id = self.data["contacts"]
raise NextScene("Edit Contact")
def _delete(self):
self.save()
self._model.delete_contact(self.data["contacts"])
self._reload_list()
@staticmethod
def _quit():
raise StopApplication("User pressed quit")
class ContactView(Frame):
def __init__(self, screen, model):
super(ContactView, self).__init__(screen,
screen.height * 2 // 3,
screen.width * 2 // 3,
hover_focus=True,
can_scroll=False,
title="Contact Details",
reduce_cpu=True)
# Save off the model that accesses the contacts database.
self._model = model
def reset(self):
# Do standard reset to clear out form, then populate with new data.
super(ContactView, self).reset()
self.data = self._model.get_current_contact()
def _ok(self):
self.save()
self._model.update_current_contact(self.data)
raise NextScene("Main")
@staticmethod
def _cancel():
raise NextScene("Main")
contacts = ContactModel()
last_scene = None
while True:
try:
Screen.wrapper(demo, catch_interrupt=True, arguments=[last_scene])
sys.exit(0)
except ResizeScreenError as e:
last_scene = e.scene