#This tool creates a new field in a shapefile named HEX, and stores hex color values in it.
#Requires a file in Python dictionary format of Key: Value pairs representing Attribute: Hex colors.

import arcpy
import ast

#parameters
shpFile = arcpy.GetParameterAsText(0) #shapefile
field = arcpy.GetParameterAsText(1) #field from shapefile
hexDictFile = arcpy.GetParameterAsText(2) #file

#open hex dictionary file and store in variable
myFile = open(hexDictFile)
hexDict = ast.literal_eval(myFile.read())
myFile.close()

print hexDict["Cdh"]
#Add HEX field to shapefile
arcpy.AddField_management(shpFile,"HEX","TEXT","#","#","6","#","NULLABLE","NON_REQUIRED","#")

#initiate cursor
cursor = arcpy.UpdateCursor(shpFile)
row = cursor.next()

#iterate over rows
while row:
    #check current value of field
    currentVal = row.getValue(field)
    #if value exists set value
    if currentVal in hexDict:
        row.setValue("HEX", hexDict[currentVal])
    cursor.updateRow(row)
    row = cursor.next()