~mrooney/+junk/jurgensville

« back to all changes in this revision

Viewing changes to main.py

  • Committer: Michael Rooney
  • Date: 2010-10-10 04:56:54 UTC
  • Revision ID: mrooney@ubuntu.com-20101010045654-mddkkyu50e31kzp8
use itertools.groupby to simplify restaurant creation

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
import sys, os
2
 
import csv
 
2
import csv, itertools
3
3
 
4
4
class Restaurant(object):
5
5
    def __init__(self, ID, items):
16
16
        # The behavior of this when satifies(desired) is False is undefined.
17
17
        pass
18
18
 
 
19
    def get_combinations(self, restaurant, desired):
 
20
        # Generate the different ways a restaurant could provide the desired.
 
21
        pass
 
22
 
 
23
 
19
24
    def __cmp__(self, other):
20
25
        return cmp(self.ID, other.ID)
21
26
 
23
28
    restaurants = None
24
29
 
25
30
def get_restaurants(csv_data):
26
 
    restaurant_data = {}
27
 
    restaurants = []
28
 
 
29
31
    # Group the items by restaurant ID.
30
 
    for row in csv_data:
31
 
        restaurant_data[row['id']] = restaurant_data.get(row['id'], []) + [row]
32
 
 
 
32
    restaurant_data = itertools.groupby(csv_data, lambda x: x['id'])
33
33
    # Create restaurant objects.
34
 
    for rest_id, items in restaurant_data.items():
35
 
        restaurant = Restaurant(rest_id, items)
36
 
        restaurants.append(restaurant)
37
 
 
 
34
    restaurants = [Restaurant(rest_id, list(group)) for rest_id, group in restaurant_data]
38
35
    return restaurants
39
36
 
40
37
def load_csv(csv_path):