Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import pickle
- from tabulate import tabulate
- UI = '''
- 1. Добавить новый контакт
- 2. Посмотреть контакты
- 3. Найти контакт
- 4. Изменить контакт
- 5. Удалить контакт
- 6. Сброс всех контактов
- 7. Выход
- '''
- class Person(object):
- def __init__(self, name=None, mail=None, phone=None):
- self.name = name
- self.mail = mail
- self.phone = phone
- def __str__(self):
- return f"""Person(<Имя: {self.name}, Почта: {self.mail}, Телефон: {self.phone}>)"""
- class Addressbook(object):
- def __init__(self, database):
- self.database = database
- self.persons = {}
- if not os.path.exists(self.database):
- file_pointer = open(self.database, 'wb')
- pickle.dump({}, file_pointer)
- file_pointer.close()
- else:
- with open(self.database, 'rb') as person_list:
- self.persons = pickle.load(person_list)
- def add(self):
- name, mail, phone = self.details()
- if name not in self.persons:
- self.persons[name] = Person(name, mail, phone)
- else:
- print('Контакт с таким именем уже есть.')
- def search(self):
- name = input('Введите имя: ')
- if name not in self.persons:
- print('Такого контакта нет.')
- else:
- print(self.persons[name])
- def viewall(self):
- dataset = []
- headers = ['Имя', 'Адрес эл.почты', 'Телефон']
- dataset = ((view.name, view.mail, view.phone, ) for view in self.persons.values()) if self.persons else ()
- if dataset:
- print(tabulate(dataset, headers))
- else:
- print('В адрессной книге нет контактов.')
- def details(self):
- name = input('Имя: ')
- mail = input('Почта: ')
- phone = input('Телефон: ')
- return name, mail, phone
- def delete(self):
- name = input('Введите имя: ')
- if name in self.persons:
- del self.persons[name]
- print('Контакт удалён.')
- else:
- print('Контакта с таким именем нет.')
- def update(self):
- name = input('Введите имя: ')
- if name in self.persons:
- print('Введитет новые данные.')
- name, mail, phone = self.details()
- self.persons[name].__init__(name, mail, phone)
- print('Контакт изменен.')
- else:
- print('Контакт с таким именем не найден.')
- def reset(self):
- self.persons = {}
- def __del__(self):
- with open(self.database, 'wb') as db:
- pickle.dump(self.persons, db)
- def __str__(self):
- return UI
- """
- def __del__(self):
- pass
- @staticmethod
- def print_contacts():
- '''Вывести информацию.'''
- print('В книге {} контактов'.format(Addressbook.contacts))
- #contact_dict.sort()
- for item1, item2 in contact_dict.items():
- print('{0}: {1}, {2}'.format(item1, item2[0], item2[1]))
- # print('{0}: {1}, {2}'.format(self.name, self.mail, self.phone), end="")
- """
- def main():
- app = Addressbook('dict_app.data')
- choice = ''
- while choice != '7':
- print(app)
- choice = input('Choose: ')
- if choice == '1':
- app.add()
- elif choice == '2':
- app.viewall()
- elif choice == '3':
- app.search()
- elif choice == '4':
- app.update()
- elif choice == '5':
- app.delete()
- elif choice == '6':
- app.reset()
- elif choice == '7':
- print("Exiting.")
- else:
- print("Invalid choice.")
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement