Select to view content in your preferred language

Unhashable type list errors

5555
1
Jump to solution
02-25-2013 11:43 AM
by Anonymous User
Not applicable
Original User: MCline19

For the following code, lines 79 & 97:  I am trying to send emails to multiple addresses.  But I am receiving this error:  Runtime error <type 'exceptions.TypeError'>: unhashable type: 'list'
I also tried lines 80 & 98 (which are commented out), but I then received this error:  Runtime error <type 'exceptions.TypeError'>: list indices must be integers, not str

I'm not sure how to get my email addresses into this code in a form that python will accept.

Any info would be greatly appreciated!


import arcpy, os, datetime, ctypes, smtplib, string from datetime import date, timedelta from arcpy import env env.workspace = r'G:\GISAdmin\NEW_ROADS_NOTIFICATION\Road_Exports'  # Scheduler. Determines the day of the week.  Program only checks for new road additions on weekdays. print ("Finding current date and time...") now = datetime.datetime.now() dayofweek = datetime.datetime.now().weekday() print now if dayofweek == 0:  print ("Monday") elif dayofweek == 1:  print ("Tuesday") elif dayofweek == 2:  print ("Wednesday") elif dayofweek == 3:  print ("Thursday") elif dayofweek == 4:  print ("Friday") else:  print ("End: Not programmed to run on weekends.")  # Setting up dates and file naming. todaysdate = str(datetime.date.today()) todaysdate = todaysdate.replace('-', '_') print todaysdate yesterday = str(date.today() - timedelta(1)) yesterday = yesterday.replace('-', '_') print yesterday friday = str(date.today() - timedelta(3)) # Actually finds 3 days ago. Only finds Friday on Monday.  For example, on Tuesday it would actually find Saturday. friday = friday.replace('-', '_') print friday todaysfile = r'G:\GISAdmin\NEW_ROADS_NOTIFICATION\Road_Exports\roads' + todaysdate + '.shp' print todaysfile yesterfile = r'G:\GISAdmin\NEW_ROADS_NOTIFICATION\Road_Exports\roads' + yesterday + '.shp' print yesterfile fridayfile = r'G:\GISAdmin\NEW_ROADS_NOTIFICATION\Road_Exports\roads' + friday + '.shp' print fridayfile comparefile = r'G:\GISAdmin\NEW_ROADS_NOTIFICATION\comparefile.txt' # had + 'todaysdate' print comparefile  # Export public_works.PW.Streets to shapefile and name according to the date. if dayofweek in [0, 1, 2, 3, 4]:  arcpy.CopyFeatures_management(r'G:\Department Projects\Public Works\Public Works.sde\public_works.PW.Streets', r'G:\GISAdmin\NEW_ROADS_NOTIFICATION\Road_Exports\roads' + todaysdate + '.shp')  print ("Copy features completed.") else:  print ("End: Not programmed to run on weekends (2).")  # Check for existing comparefile and delete if it exists if os.path.exists(comparefile):  os.remove(comparefile)  # Feature compare.  Compare yesterdays file with Todays new file. if arcpy.Exists(yesterfile):  result = arcpy.FeatureCompare_management(yesterfile, todaysfile, "FacilityID", "ALL", "", "0.003280833333 Feet", "0", "0", "", "", "CONTINUE_COMPARE", comparefile)  print ("Yesterday's file used in feature compare.") else:  result = arcpy.FeatureCompare_management(fridayfile, todaysfile, "FacilityID", "ALL", "", "0.003280833333 Feet", "0", "0", "", "", "CONTINUE_COMPARE", comparefile)  print ("Friday's file used in feature compare.")  # Open (in memory) the resultant output compare file and look for the word 'true' (flag that indicates dissimilar files).  Inside the comparefile, true indicates a change is detected and false indicates that features are the same in both files being compared.   with open(comparefile) as file:  truecount = 0  #linecount = 0  for line in file:   #linecount = linecount + 1   for word in line.split():    while word == '"true",':     truecount = truecount + 1     break #print linecount print ("Number of trues recorded: " + str(truecount)) file.close()  if truecount > 0:  print 'Changes detected. Sending email.'  SUBJECT = "CHANGE HAS OCCURED IN THE STREETS DATABASE"  TO = ['bblankner@cstx.gov','dkillian@cstx.gov','mcline@cstx.gov']  #TO = (['bblankner@cstx.gov']['dkillian@cstx.gov']['mcline@cstx.gov'])  FROM = 'bblankner@cstx.gov'  text = 'BREAKING NEWS!  The program has detected a change has occurred in the Public Works street spatial database since the last comparison.'  BODY = string.join((   "From: %s" % FROM,   "To: %s" % TO,   "Subject: %s" % SUBJECT ,   "",   text   ), "\r\n")  server = smtplib.SMTP('00.0.0.0') #removed actual server name for security  server.sendmail(FROM, [TO], BODY)  server.quit()  print ("Email sent!") else:  print ("No changes detected. Sending email.")  SUBJECT = "No change in the streets datebase"  TO = ['bblankner@cstx.gov','dkillian@cstx.gov','mcline@cstx.gov']  #TO = (['bblankner@cstx.gov']['dkillian@cstx.gov']['mcline@cstx.gov'])  FROM = 'bblankner@cstx.gov'  text = 'The program has determined that no change has occurred in the Public Works street spatial database since the last comparison.'  BODY = string.join((   "From: %s" % FROM,   "To: %s" % TO,   "Subject: %s" % SUBJECT ,   "",   text   ), "\r\n")  server = smtplib.SMTP('00.0.0.0') #removed actual server name for security  server.sendmail(FROM, [TO], BODY)  server.quit()  print ("Email sent!") print ('Tasks Complete')
0 Kudos
1 Solution

Accepted Solutions
MathewCoyle
Honored Contributor
You are inserting a list into a list. Get rid of the list brackets in your sendmail call.

TO = ['bblankner@cstx.gov','dkillian@cstx.gov','mcline@cstx.gov'] ... server.sendmail(FROM, TO, BODY)  # not [TO]


As an aside, there is no need to replicate so much code. Something like this would be cleaner and a step in the direction of pythonic.
def emailNotify(SUBJECT, text):  print 'Changes detected. Sending email.'  TO = ['bblankner@cstx.gov','dkillian@cstx.gov','mcline@cstx.gov']  FROM = 'bblankner@cstx.gov'  BODY = string.join((   "From: %s" % FROM,   "To: %s" % TO,   "Subject: %s" % SUBJECT ,   "",   text   ), "\r\n")  server = smtplib.SMTP('00.0.0.0') #removed actual server name for security  server.sendmail(FROM, [TO], BODY)  server.quit()  print ("Email sent!")  if truecount > 0:     print('Changes detected. Sending email.')     subject = 'CHANGE HAS OCCURED IN THE STREETS DATABASE'     message = 'BREAKING NEWS!  The program has detected a change has occurred in the Public Works street spatial database since the last comparison.'   else:     print('No changes detected. Sending email.')     subject = 'No change in the streets datebase'     message = 'The program has determined that no change has occurred in the Public Works street spatial database since the last comparison.'  emailNotify(subject, message)

View solution in original post

0 Kudos
1 Reply
MathewCoyle
Honored Contributor
You are inserting a list into a list. Get rid of the list brackets in your sendmail call.

TO = ['bblankner@cstx.gov','dkillian@cstx.gov','mcline@cstx.gov'] ... server.sendmail(FROM, TO, BODY)  # not [TO]


As an aside, there is no need to replicate so much code. Something like this would be cleaner and a step in the direction of pythonic.
def emailNotify(SUBJECT, text):  print 'Changes detected. Sending email.'  TO = ['bblankner@cstx.gov','dkillian@cstx.gov','mcline@cstx.gov']  FROM = 'bblankner@cstx.gov'  BODY = string.join((   "From: %s" % FROM,   "To: %s" % TO,   "Subject: %s" % SUBJECT ,   "",   text   ), "\r\n")  server = smtplib.SMTP('00.0.0.0') #removed actual server name for security  server.sendmail(FROM, [TO], BODY)  server.quit()  print ("Email sent!")  if truecount > 0:     print('Changes detected. Sending email.')     subject = 'CHANGE HAS OCCURED IN THE STREETS DATABASE'     message = 'BREAKING NEWS!  The program has detected a change has occurred in the Public Works street spatial database since the last comparison.'   else:     print('No changes detected. Sending email.')     subject = 'No change in the streets datebase'     message = 'The program has determined that no change has occurred in the Public Works street spatial database since the last comparison.'  emailNotify(subject, message)
0 Kudos