#!/usr/local/bin/python2.7 '''Lists all the actors in the database This is written with less HTML, allowing that to be supplied by the Jinja2 templating engine Written Fall 2016 Scott D. Anderson ''' import sys import MySQLdb import dbconn2 # ================================================================ # The functions that do most of the work. def getList(conn): '''Returns a list of actor info (names and birthdates)''' curs = conn.cursor(MySQLdb.cursors.DictCursor) # results as Dictionaries curs.execute('select name,birthdate from person') return curs.fetchall() def getNames(conn): '''Returns a string of LI elements, listing all actors and birthdates''' rows = getList(conn) lines = [ u'<li>{name} born on {birthdate}</li>'.format(**row) for row in rows ] return "\n".join(lines) def getConn(): dsn = dbconn2.read_cnf('/home/cs304/.my.cnf') dsn['db'] = 'wmdb' # the database we want to connect to return dbconn2.connect(dsn) def main(): '''Returns an HTML listing all actors and birthdates''' conn = getConn() actorlist = getNames(conn) return actorlist # ================================================================ # This starts the ball rolling, *if* the script is run as a script, # rather than just being imported. if __name__ == '__main__': print main()