A Group of Friends

An exercise in creating Python classes

We'll be storing a group of friends, and then allowing rerieval of information about them in a few ways.

In class Friend, each friend instance should store a name, a color (favorite color) and a category (an integer between 1 and 4 indicating some personal feature of your friend).

class FriendGroup has the methods to digest and retrieve Friends. You need to store the Friend instances in a dictionary in this class, with the key = name. Methods should include the following:

  • add(name, color, category) # adds a new friend or replaces fields of an existing friend.

  • delete(name) # delete a friend if found. Returns True if successfully deleted, else False.

  • change_cat(old_category,new_category) changes the category of those Friends with old_category, giving each a brand new_category. Returns the number of changes made.

  • get_cat(name) returns the category of the named friend, or None if not found

  • get_friend(name) returns the Friend instance if found, else None.

  • get_names() # Returns a list of all Friends' names, or empty list if none.
In [ ]:
class Friend:
    
    def __init__(self,name,color,category):
        # your excellent code here to store name and category
        
    
class FriendGroup:
    
    def __init__(self):
        # your more excellent code here to create a FriendGroup
        
    def add(self,name,color,category):
        # more
        
    def delete(self,name):
        # even more
        
    def get_cat(name):
        # less
        
    def change_cat(self,old_category,new_category):
        # more or less
    
    def get_friend(self,name):
        # less is more
        
    def get_names(self):
        # all of them
            
  • You need to create a program that reads an input file of commands and writes an output file of results. All commands and output will be in CSV format.
  • Each command will be on a line by itself.
  • Only one command will output its results to the output file: print (below)

Here are the commands that do not write to the ouptut file, with code examples:

  • Adding a friend to the group:
  • add,harry,blue,4
  • Deleting a friend:
  • delete,harry
  • Changing categories:
  • change_cat,4,2

This command will write, to the output file, the name of each friend and her favorite color and category -- one per line, in alphabetical order.

  • Write all names-categories to output file
  • print

Output will look like:

harry,blue2
voldy,black,1
In [ ]: