Assignment-2

Create your own version of the Mayoral Excuse Machine in Python that takes in a name and location, selects an excuse at random and prints an excuse (“Sorry, Richard, I was late to City Hall to meet you, I had a very rough night and woke up sluggish”). Use the “excuses.csv” in the Github repository.

Extra credit if you print the link to the story as well


In [1]:
user_input1 = input("What's the name of the person you were supposed to meet? ")


What's the name of the person you were supposed to meet? Radhika

In [3]:
user_input2 = input("Where were you supposed to go? ")


Where were you supposed to go? New York

In [41]:
inputs = {
    'name': user_input1,
    'place': user_input2
       
}

In [42]:
inputs


Out[42]:
{'name': 'Radhika', 'place': 'New York'}

In [20]:
conn.rollback()

In [17]:
#CSV into Database
import pg8000

excuses = []

conn = pg8000.connect(user='postgres', password='password', database='mayor')
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE mayor_excuses (
   excuse varchar(150),
   headline varchar(150),
   hyperlink varchar(150)

)
""")
conn.commit()

In [18]:
cursor.execute("COPY mayor_excuses(excuse,headline,hyperlink) FROM '/Users/Radhika/Downloads/Lede/12julylede/excuse.csv' DELIMITER ',' CSV")
conn.commit()

In [23]:
import pg8000

m_excuse = []

conn = pg8000.connect(user='postgres', password='password', database='mayor')
cursor = conn.cursor()
cursor.execute("SELECT excuse,headline,hyperlink FROM mayor_excuses")
for row in cursor.fetchall():
    dict_exc = {'excuse': row[0], 'headline':row[1], 'hyperline': row[2]}
    m_excuse.append(dict_exc)
len(m_excuse)


Out[23]:
11

In [24]:
m_excuse


Out[24]:
[{'excuse': 'the fog was unexpected and did slow us down a bit',
  'headline': "De Blasio Blames 'Rough Night' and Fog for Missing Flight 587 Ceremony",
  'hyperline': 'http://www.dnainfo.com/new-york/20141112/rockaway-park/de-blasio-arrives-20-minutes-late-flight-587-memorial-angering-families'},
 {'excuse': 'we had some meetings at Gracie Mansion',
  'headline': "De Blasio 30 Minutes Late to Rockaway St. Patrick's Day Parade",
  'hyperline': 'http://www.dnainfo.com/new-york/20150307/belle-harbor/de-blasio-30-minutes-late-rockaway-st-patricks-day-parade'},
 {'excuse': 'I had a very rough night and woke up sluggish',
  'headline': "De Blasio Blames 'Rough Night' and Fog for Missing Flight 587 Ceremony",
  'hyperline': 'http://www.dnainfo.com/new-york/20141112/rockaway-park/de-blasio-arrives-20-minutes-late-flight-587-memorial-angering-families'},
 {'excuse': "I just woke up in the middle of the night and couldn't get back to sleep",
  'headline': "De Blasio Blames 'Rough Night' and Fog for Missing Flight 587 Ceremony",
  'hyperline': 'http://www.dnainfo.com/new-york/20141112/rockaway-park/de-blasio-arrives-20-minutes-late-flight-587-memorial-angering-families'},
 {'excuse': 'we had some stuff we had to do',
  'headline': "De Blasio 30 Minutes Late to Rockaway St. Patrick's Day Parade",
  'hyperline': 'http://www.dnainfo.com/new-york/20150307/belle-harbor/de-blasio-30-minutes-late-rockaway-st-patricks-day-parade'},
 {'excuse': 'I should have gotten myself moving quicker',
  'headline': "De Blasio Blames 'Rough Night' and Fog for Missing Flight 587 Ceremony",
  'hyperline': 'http://www.dnainfo.com/new-york/20141112/rockaway-park/de-blasio-arrives-20-minutes-late-flight-587-memorial-angering-families'},
 {'excuse': 'I was just not feeling well this morning',
  'headline': "De Blasio Blames 'Rough Night' and Fog for Missing Flight 587 Ceremony",
  'hyperline': 'http://www.dnainfo.com/new-york/20141112/rockaway-park/de-blasio-arrives-20-minutes-late-flight-587-memorial-angering-families'},
 {'excuse': 'breakfast began a little later than expected',
  'headline': "De Blasio 15 Minutes Late to St. Patrick's Day Mass, Blames Breakfast",
  'hyperline': 'http://www.dnainfo.com/new-york/20150317/midtown/de-blasio-15-minutes-late-st-patricks-day-mass-blames-breakfast'},
 {'excuse': 'the detail drove away when we went into the subway rather than waiting to confirm we got on a train',
  'headline': 'Mayor de Blasio Is Irked by a Subway Delay',
  'hyperline': 'http://www.nytimes.com/2015/05/06/nyregion/mayor-de-blasio-is-irked-by-a-subway-delay.html?ref=nyregion&_r=0'},
 {'excuse': 'we waited 20 mins for an express only to hear there were major delays',
  'headline': 'Mayor de Blasio Is Irked by a Subway Delay',
  'hyperline': 'http://www.nytimes.com/2015/05/06/nyregion/mayor-de-blasio-is-irked-by-a-subway-delay.html?ref=nyregion&_r=0'},
 {'excuse': 'we need a better system',
  'headline': 'Mayor de Blasio Is Irked by a Subway Delay',
  'hyperline': 'http://www.nytimes.com/2015/05/06/nyregion/mayor-de-blasio-is-irked-by-a-subway-delay.html?ref=nyregion&_r=0'}]

In [58]:
#tried without defining a fun to test it
#import random
#randomz = random.choice(m_excuse)
#possible_keys = [v for k,v in randomz.items() if k=='excuse']
#col = random.choice(possible_keys)

In [59]:
# randomly picking up an excuse from the database, mayor.
import random
def random_excuse(string_in):
    randomz = random.choice(m_excuse)
    possible_keys = [v for k,v in randomz.items() if k=='excuse']
    col = random.choice(possible_keys)
    return col

In [60]:
random_excuse(m_excuse)


Out[60]:
"I just woke up in the middle of the night and couldn't get back to sleep"

In [36]:
#sentence = 'Sorry', user_input1, 'I was late to', user_input2, 'to meet you, because',col

In [66]:
sentence = 'Sorry {} I was late to {} to meet you, because {}. See the news: {}'

In [67]:
output = sentence.format(inputs['name'], inputs['place'], random_excuse(m_excuse), randomz['hyperline'])

In [68]:
print(output)


Sorry Radhika I was late to New York to meet you, because we need a better system. See the news: http://www.dnainfo.com/new-york/20150307/belle-harbor/de-blasio-30-minutes-late-rockaway-st-patricks-day-parade

In [ ]: