def run(sort=False): session = DBSession() count = {} episodes = session.query(Episode) for ep in episodes: seen = set() for writer in ep.teleplay: count[writer] = count.get(writer, 0) + 1 seen.add(writer) for writer in ep.story: if writer not in seen: count[writer] = count.get(writer, 0) + 1 results = [{"name": r.name, "count": count[r]} for r in sorted(count.keys(), key=lambda x: x.id)] if sort: results.sort(key=lambda x: x["count"], reverse=True) return results
from startrek import Episode from startrek import Person # Read CSV files into list. try: with open("episodes.csv") as episodes_f: with open("people.csv") as people_f: episodes = list(csv.reader(episodes_f)) people = list(csv.reader(people_f)) except IOError as e: print >>sys.stderr, "error: %s" % (e) sys.exit(1) # Start ORM classes up, and get a connection to the DB. Base.metadata.create_all(engine) session = DBSession() # Create a table of id-to-Person based on the contents of the people file. people_rec = {} for i, name in people[1:]: i = int(i) p = Person(id=i, name=name.decode("utf-8")) people_rec[i] = p session.add(p) # Construct an Episode for each record in the episode file, and add it to the # databse. for i, season, ep, title, airdate, teleplay, story, director, stardate, url in episodes[1:]: # Extract the fields that exist more or less as is.
from startrek import Episode from startrek import Person # Read CSV files into list. try: with open("episodes.csv") as episodes_f: with open("people.csv") as people_f: episodes = list(csv.reader(episodes_f)) people = list(csv.reader(people_f)) except IOError as e: print >> sys.stderr, "error: %s" % (e) sys.exit(1) # Start ORM classes up, and get a connection to the DB. Base.metadata.create_all(engine) session = DBSession() # Create a table of id-to-Person based on the contents of the people file. people_rec = {} for i, name in people[1:]: i = int(i) p = Person(id=i, name=name.decode("utf-8")) people_rec[i] = p session.add(p) # Construct an Episode for each record in the episode file, and add it to the # databse. for i, season, ep, title, airdate, teleplay, story, director, stardate, url in episodes[ 1:]: