Quantcast
Channel: Forums - Python
Viewing all articles
Browse latest Browse all 2485

Unhashable type list errors

$
0
0
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!


Code:

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')


Viewing all articles
Browse latest Browse all 2485

Trending Articles