#!/usr/local/bin/python2.7 '''Allows you to look up one actor by NM 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 getActor(conn,nm): '''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 where nm = %s', (nm,)) return curs.fetchone() 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(nm): '''Returns some text about the given actor''' conn = getConn() info = getActor(conn,nm) return '''{name} was born on {birthdate}'''.format(**info) # ================================================================ # This starts the ball rolling, *if* the script is run as a script, # rather than just being imported. if __name__ == '__main__': if len(sys.argv) < 2: print "Usage: {name} nm".format(name=sys.argv[0]) else: print main(sys.argv[1])