bankaccount.py account_test.py ===__str__ method an object's state - the values of its attributes at any given moment e.g., __balance of our bank account displaying the state of an object is a common task. common - define a method that returns a string representing the object's state. Then, this can be printed. In Python: __str__ it is called automatically when you pass an object as an argument to the print function. bankaccount2.py account_test2.py recall printing? and format, which returns a string? ==== make all of a class's data attributes private (using the __) use methods to change attributes of instances accessor method: returns a vlaue from a class's attribute e.g., get_balance() for the bank account mutator method: changes an object - stores or changes an attribute in some way === pickling - "serializing an object is the process of converting the object to a stream of bytes that can be saved to a file for later retrieval." import pickle outputfile = open('mydata.dat','wb') - open it for binary writing pickle.dump(object,file) import pick >>> import pickle >>> phonebook = {'chris':'44444','sally':'55555'} >>> phonebook {'chris': '44444', 'sally': '55555'} >>> out = open('temp.dat','wb') >>> pickle.dump(phonebook, out) >>> out.close() >>> #let's read the dictionary back in. We'll put >>> # it in a different variable this time >>> input = open('temp.dat','rb') >>> x = pickle.load(input) >>> x {'chris': '44444', 'sally': '55555'} >>> [pickle_objects.py] This creates a separate dictionary for each person. [unpickle_objects.py] import pickle def main(): end_of_file = False input_file = open('info.dat', 'rb') while not end_of_file: try: person = pickle.load(input_file) display_data(person) except EOFError: end_of_file = True input_file.close() # The display_data function displays the person data # in the dictionary that is passed as an argument. def display_data(person): print('Name:', person['name']) print('Age:', person['age']) print('Weight:', person['weight']) print() # Call the main function. main() === We can store objects in lists and dictionaries. [contact.py] # The Contact class holds contact information. THIS IS JUST THE CLASS class Contact: # The __init__ method initializes the attributes. def __init__(self, name, phone, email): self.__name = name self.__phone = phone self.__email = email # The set_name method sets the name attribute. def set_name(self, name): self.__name = name # The set_phone method sets the phone attribute. def set_phone(self, phone): self.__phone = phone # The set_email method sets the email attribute. def set_email(self, email): self.__email = email # The get_name method returns the name attribute. def get_name(self): return self.__name # The get_phone method returns the phone attribute. def get_phone(self): return self.__phone # The get_email method returns the email attribute. def get_email(self): return self.__email # The __str__ method returns the object's state # as a string. def __str__(self): return "Name: " + self.__name + \ "\nPhone: " + self.__phone + \ "\nEmail: " + self.__email ==== [contact_manager.py] # This program manages contacts. import contact import pickle # Global constants for menu choices LOOK_UP = 1 ADD = 2 CHANGE = 3 DELETE = 4 QUIT = 5 # Global constant for the filename FILENAME = 'contacts.dat' # main function def main(): # Load the existing contact dictionary and # assign it to mycontacts. mycontacts = load_contacts() # Initialize a variable for the user's choice. choice = 0 # Process menu selections until the user # wants to quit the program. while choice != QUIT: # Get the user's menu choice. choice = get_menu_choice() # Process the choice. if choice == LOOK_UP: look_up(mycontacts) elif choice == ADD: add(mycontacts) elif choice == CHANGE: change(mycontacts) elif choice == DELETE: delete(mycontacts) # Save the mycontacts dictionary to a file. save_contacts(mycontacts) def load_contacts(): try: # Open the contacts.dat file. input_file = open(FILENAME, 'rb') # Unpickle the dictionary. contact_dct = pickle.load(input_file) # Close the phone_inventory.dat file. input_file.close() except IOError: # Could not open the file, so create # an empty dictionary. contact_dct = {} # Return the dictionary. return contact_dct # The get_menu_choice function displays the menu # and gets a validated choice from the user. def get_menu_choice(): print() print('Menu') print('---------------------------') print('1. Look up a contact') print('2. Add a new contact') print('3. Change an existing contact') print('4. Delete a contact') print('5. Quit the program') print() # Get the user's choice. choice = int(input('Enter your choice: ')) # Validate the choice. while choice < LOOK_UP or choice > QUIT: choice = int(input('Enter a valid choice: ')) # return the user's choice. return choice # The look_up function looks up an item in the # specified dictionary. def look_up(mycontacts): # Get a name to look up. name = input('Enter a name: ') # Look it up in the dictionary. print(mycontacts.get(name, 'That name is not found.')) # The add function adds a new entry into the # specified dictionary. def add(mycontacts): # Get the contact info. name = input('Name: ') phone = input('Phone: ') email = input('Email: ') # Create a Contact object named entry. entry = contact.Contact(name, phone, email) # If the name does not exist in the dictionary, # add it as a key with the entry object as the # associated value. if name not in mycontacts: mycontacts[name] = entry print('The entry has been added.') else: print('That name already exists.') # The change function changes an existing # entry in the specified dictionary. def change(mycontacts): # Get a name to look up. name = input('Enter a name: ') if name in mycontacts: # Get a new phone number. phone = input('Enter the new phone number: ') # Get a new email address. email = input('Enter the new email address: ') # Create a contact object named entry. entry = contact.Contact(name, phone, email) # Update the entry. mycontacts[name] = entry print('Information updated.') else: print('That name is not found.') # The delete function deletes an entry from the # specified dictionary. def delete(mycontacts): # Get a name to look up. name = input('Enter a name: ') # If the name is found, delete the entry. if name in mycontacts: del mycontacts[name] print('Entry deleted.') else: print('That name is not found.') # The save_contacts funtion pickles the specified # object and saves it to the contacts file. def save_contacts(mycontacts): # Open the file for writing. output_file = open(FILENAME, 'wb') # Pickle the dictionary and save it. pickle.dump(mycontacts, output_file) # Close the file. output_file.close() # Call the main function. main() =====