taffners

Learn to Program: Crafting Quality Code read_restaurants

Mar 27th, 2013
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.83 KB | None | 0 0
  1. def read_restaurants(file):
  2.     """ (file) -> (dict, dict, dict)
  3.  
  4.    Return a tuple of three dictionaries based on the information in the file:
  5.  
  6.  
  7.    Georgie Porgie
  8.    87%
  9.    $$$
  10.    Canadian, Pub Food
  11.  
  12.    Queen St. Cafe
  13.    82%
  14.    $
  15.    Malaysian, Thai
  16.  
  17.    Dumplings R Us
  18.    71%
  19.    $
  20.    Chinese
  21.  
  22.    Mexican Grill
  23.    85%
  24.    $$
  25.    Mexican
  26.  
  27.    Deep Fried Everything
  28.    52%
  29.    $
  30.    Pub Food
  31.  
  32.    >>>read_restaurants('/Users/samantha/Desktop/Restaurant_List.txt')
  33.    
  34.    - a dict of {restaurant name: rating%}
  35.    {'Georgie Porgie':87}
  36.    - a dict of {price: list of restaurant names}
  37.    {'$$$':[Georgie Porgie]}
  38.    - a dict of {cusine: list of restaurant names}
  39.    {'Canadian':'Georgie Porgie', 'Pub Food':'Georgie Porgie'}
  40.    """
  41.     #open file for reading only
  42.     open_file = open(file, 'r')
  43.            
  44.     # lists to be completed by function
  45.     name_to_rating = {}
  46.     price_to_names = {'$': [], '$$': [], '$$$': [], '$$$$': []}
  47.     cuisine_to_names = {}
  48.  
  49.     # read first line
  50.     line = open_file.readline()
  51.  
  52.     #keep in while loop until the end of the file
  53.     while line != '':
  54.  
  55.         #name of resturant variable
  56.         name = line.strip('\n') #remove /n
  57.  
  58.         #Percent people like the resturant variable
  59.         percent = open_file.readline().strip('%\n')
  60.        
  61.         #Add to dict. the name_to_rating key being name and the value being percent
  62.         name_to_rating[name] = percent
  63.  
  64.         #How much it costs
  65.         dollar = open_file.readline().strip('%\n')
  66.        
  67.         #Add value name to corresponding $ already present in dict.
  68.         price_to_names[dollar].append(name)
  69.  
  70.         #kind_of_food the restaurant offers variable
  71.         kind_of_food = open_file.readline().strip('%\n')
  72.        
  73.     # If statements are result of the resturant serving more than one type of food
  74.  
  75.         #If the resturant serves more than one type of food:
  76.         if ',' in kind_of_food:
  77.             food_types = kind_of_food.strip().split(', ')
  78.             food_types.insert(1,name)
  79.             food_types.insert(3,name)
  80.             cuisine_to_names[food_types[0]] = list(food_types[1:2])
  81.             cuisine_to_names[food_types[2]] = list(food_types[3:len(food_types)])
  82.            
  83.         #If the type of food served by this resturant is already present in dict.    
  84.         elif kind_of_food in cuisine_to_names:
  85.             cuisine_to_names[kind_of_food].append(name)
  86.  
  87.         #If the resturant only offers one type of food and it is not already present in
  88.         #the dict.
  89.         else:
  90.             cuisine_to_names[kind_of_food] = [name]
  91.        
  92.        
  93.         empty_line = open_file.readline()
  94.         line = open_file.readline()
  95.     print(name_to_rating)
  96.     print(price_to_names)
  97.     print(cuisine_to_names)
  98.                  
  99.     open_file.close()
Add Comment
Please, Sign In to add comment