Advertisement
Guest User

contacts.py

a guest
Jun 28th, 2019
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.14 KB | None | 0 0
  1. """
  2. Program for storing contacts via command line
  3. Usage:
  4.    Add: -a <first> <last> <number>
  5.    Remove: -rm <first> <last> <number>
  6.    List: -l
  7. """
  8.  
  9.  
  10. import argparse
  11. import pickle
  12.  
  13. #Argument parser
  14.  
  15. parser = argparse.ArgumentParser(description="Save contacts in this program")
  16. group = parser.add_mutually_exclusive_group()
  17. group.add_argument('-a', '--add', action='store_true', help='add a contacts: -a <first> <last> <number>')
  18. group.add_argument('-rm', '--remove', action='store_true', help='delete a contact: -rm <first> <last> <number>')
  19. parser.add_argument('-l', '--list', action='store_true', help='list contacts')
  20. parser.add_argument('first', help='first name of contact', nargs='?')
  21. parser.add_argument('last', help='last name of contact', nargs='?')
  22. parser.add_argument('number', help='phone number of contact', nargs='?')
  23. args = parser.parse_args()
  24.  
  25. #  Set attrubutes for a contact
  26.  
  27. class Contact:
  28.    
  29.     def __init__(self, first, last, number):
  30.         self.first = first
  31.         self.last = last
  32.         self.number = number
  33.  
  34.     def __len__(self):
  35.         global contacts
  36.         return len(contacts)
  37.  
  38.     def __iter__(self):
  39.         return self
  40.  
  41. contacts = []
  42.  
  43. def list_contacts():
  44.     try:
  45.        
  46.         print("Contacts:")
  47.         f = open('c.data', 'rb')
  48.         contacts = pickle.load(f)
  49.         f.close()
  50.         for contact in contacts:
  51.             print(contact)
  52.         print(str(len(contacts)) + " contacts total.")
  53.         if len(contacts) == 0:
  54.             print("No contacts, why don't you add some first?")
  55.  
  56.     except FileNotFoundError:
  57.         print("No contacts, why don't you add some first?")
  58.        
  59. def delete_contact(contact):
  60.     try:
  61.         # load pickle data, remove the contact, reload the data
  62.         f = open('c.data', 'rb')
  63.         contact = pickle.load(f)
  64.         contacts.remove(contact)
  65.         contact = pickle.dump(contact, f)
  66.         f.close()
  67.         num_contacts = len(f.readlines())
  68.         if num_contacts == 0:
  69.             print("No contacts, why don't you add some first?")
  70.         else:
  71.             print("Contact " + args.first +''+ args.last + " deleted")
  72.     except ValueError or  FileNotFoundError:
  73.         f = open('c.data', 'rb')
  74.         num_contacts = len(f.readlines())
  75.        
  76.         if num_contacts == 0:
  77.             print("No contacts, why don't you add some first?")
  78.  
  79. def add_contact(contact):
  80.     while True:
  81.  
  82.         contacts.append(contact)
  83.         f = open('c.data', 'ab')
  84.         pickle.dump(contact, f)
  85.         f.close()
  86.         print("Contact added")
  87.         if args.first and args.last and args.number == None:
  88.             print("No contact entered")
  89.         break
  90.  
  91. def Main():
  92.     first = args.first
  93.     last = args.last
  94.     number = args.number
  95.     # The issue is on this line, can't append this as one object
  96.     contact = Contact(first, last, number)
  97.     if args.add and args.first and args.last and args.number:
  98.         add_contact(contact)
  99.     elif args.remove and args.first and args.last and args.number:
  100.         delete_contact(contact)
  101.     elif args.list:
  102.         list_contacts()
  103.  
  104. if __name__ == "__main__":
  105.     Main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement