#!/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 row (dictionary) of actor info for the given nm. Info is name and birthdate. Returns None if nm is not found.''' curs = conn.cursor(MySQLdb.cursors.DictCursor) # results as Dictionaries curs.execute('select name,birthdate from person where nm = %s', [nm]) return curs.fetchone() def lookupByNM(conn,nm): '''Returns actor's name and birthdate as a string''' info = getActor(conn,nm) if info is None: return 'There is no actor with nm {nm} in the database'.format(nm=nm) else: 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: DSN = dbconn2.read_cnf() DSN['db'] = 'wmdb' # the database we want to connect to dbconn2.connect(DSN) print lookupByNM(sys.argv[1])