Select to view content in your preferred language

'list' object has no attribute 'split'

14569
4
Jump to solution
02-21-2014 06:09 AM
TonyAlmeida
MVP Regular Contributor
I have the following code and i am getting the following error, i am not sure why because the code works in python window in ArcMap but it doesn't work as a Add-in tool. Any would be appreciated.

error
Traceback (most recent call last):   File "C:\Users\talmeida\AppData\Local\ESRI\Desktop10.1\AssemblyCache\{BB229966-100D-4482-A3FE-B298BD262597}\AddPoints_addin.py", line 55, in onMouseDownMap     add_fields = add_fields.split(';') AttributeError: 'list' object has no attribute 'split'


Add-in tool code
import arcpy import pythonaddins import os from arcpy import env  class Add_points(object):     """Implementation for AddPoints_addin.Add_points (Tool)"""     def __init__(self):         self.enabled = True         self.cursor = 3 # Can set to "Line", "Circle" or "Rectangle" for interactive shape drawing and to activate the onLine/Polygon/Circle event sinks.     def onMouseDownMap(self, x, y, button, shift):          fc = "TonyTwoWay.DBO.TT"         workspace = r"C:\Users\talmeida\AppData\Roaming\ESRI\Desktop10.1\ArcCatalog\Connection to dsd15_sqlexpress.sde"         arcpy.env.overwriteOutput = True         #arcpy.ChangeVersion_management('TonyTwoWay.DBO.TT','TRANSACTIONAL','dbo.DEFAULT', "")         # Start an edit session. Must provide the worksapce.         edit = arcpy.da.Editor(workspace)          # Edit session is started without an undo/redo stack for versioned data         #  (for second argument, use False for unversioned data)         edit.startEditing(True)          # Start an edit operation         edit.startOperation()          list = []         with arcpy.da.SearchCursor(fc, ["AddressID"]) as cursor:             for row in cursor:                 try:                     if "CC" in row[0]:                         list.append(int(row[0].strip("CC")))                    except TypeError:                     pass                 del cursor          list.sort()         AddressID = list[-1] + 1         AddressID = 'CC' + str(AddressID)          row_values = [(x, y, (x, y), AddressID)]         cursor = arcpy.da.InsertCursor(fc, ["X_Coord", "Y_Coord", "SHAPE@XY", "ADDRESSID"])          for row in row_values:             cursor.insertRow(row)         del cursor #####################################################         fcTarget = "TonyTwoWay.DBO.TT"         fcJoin = "testParcelsAdmint"         add_fields = ["ACCOUNT","SiteNum","OwnerName","SiteAddres","SiteNumSfx","predir","StreetType","SubName"]          # fix args         if type(add_fields) != list:             # from script tool             add_fields = add_fields.split(';')          # do not need this scratch file         fcOutput = r'in_memory\temp_join'         arcpy.SpatialJoin_analysis(fcJoin, fcTarget,fcOutput, 'JOIN_ONE_TO_MANY')           # grab oid field from points         oid_t = arcpy.Describe(fcTarget).OIDFieldName          # init rowW and rowR         curR = arcpy.SearchCursor(fcOutput)         join_dict = dict([(r.JOIN_FID,[r.getValue(f) for f in add_fields]) for r in curR])         del curR          # Now update the new target         curW = arcpy.UpdateCursor(fcTarget)         for row in curW:             t_oid = row.getValue(oid_t)             if t_oid in join_dict:                 for f in add_fields:                     row.setValue(f, join_dict[t_oid][add_fields.index(f)])             curW.updateRow(row)         del row, curW         arcpy.AddMessage('Updated all records sucussefully')            # Stop the edit operation.         edit.stopOperation()          # Stop the edit session and save the changes         edit.stopEditing(True)                  arcpy.RefreshActiveView()         pass


This part of the code works in python window in Arcmap.
import arcpy arcpy.env.overwriteOutput = True  fcTarget = "TonyTwoWay.DBO.TT" fcJoin = "testParcelsAdmint" add_fields = ["ACCOUNT","SiteNum","OwnerName","SiteAddres","SiteNumSfx","predir","StreetType","SubName"]  # fix args if type(add_fields) != list:     # from script tool     add_fields = add_fields.split(';')  # do not need this scratch file fcOutput = r'in_memory\temp_join' arcpy.SpatialJoin_analysis(fcJoin, fcTarget,fcOutput, 'JOIN_ONE_TO_MANY')   # grab oid field from points oid_t = arcpy.Describe(fcTarget).OIDFieldName  # init rowW and rowR curR = arcpy.SearchCursor(fcOutput) join_dict = dict([(r.JOIN_FID,[r.getValue(f) for f in add_fields]) for r in curR]) del curR  # Now update the new target curW = arcpy.UpdateCursor(fcTarget) for row in curW:     t_oid = row.getValue(oid_t)     if t_oid in join_dict:         for f in add_fields:             row.setValue(f, join_dict[t_oid][add_fields.index(f)])     curW.updateRow(row) del row, curW arcpy.AddMessage('Updated all records sucussefully')
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JasonScheirer
Esri Alum
You're shadowing the built-in list type by defining a variable as list. Don't do list = [], rename it something like cc_list.

View solution in original post

0 Kudos
4 Replies
JasonScheirer
Esri Alum
Instead of

if type(add_fields) != list:


do

if not isinstance(add_fields, list):


or even better:

if isinstance(add_fields, basestring):
0 Kudos
TonyAlmeida
MVP Regular Contributor
jscheirer thank you for the response.

I replaced the line
if type(add_fields) != list:


With broth of your suggestions.
first with
if not isinstance(add_fields, list):

I get the following error.
Traceback (most recent call last):
  File "C:\Users\talmeida\AppData\Local\ESRI\Desktop10.1\AssemblyCache\{BB229966-100D-4482-A3FE-B298BD262597}\AddPoints_addin.py", line 53, in onMouseDownMap
    if not isinstance(add_fields, list):
TypeError: isinstance() arg 2 must be a class, type, or tuple of classes and types


then i replaced it with
if isinstance(add_fields, basestring):

I got this error
Traceback (most recent call last):
  File "C:\Users\talmeida\AppData\Local\ESRI\Desktop10.1\AssemblyCache\{BB229966-100D-4482-A3FE-B298BD262597}\AddPoints_addin.py", line 83, in onMouseDownMap
    edit.stopOperation()
SystemError: error return without exception set

ArcMap seems to just freeze.
My arcmap process icon just keeps going and going.
No point or anything is created after maybe 3 minutes of thinking.
0 Kudos
JasonScheirer
Esri Alum
You're shadowing the built-in list type by defining a variable as list. Don't do list = [], rename it something like cc_list.
0 Kudos
TonyAlmeida
MVP Regular Contributor
after replacing list = [] with CC_list = []. I get passed that error but now come to this error.
Traceback (most recent call last):
  File "C:\Users\talmeida\AppData\Local\ESRI\Desktop10.1\AssemblyCache\{BB229966-100D-4482-A3FE-B298BD262597}\AddPoints_addin.py", line 82, in onMouseDownMap
    edit.stopOperation()
SystemError: error return without exception set


current code i am using.
import arcpy
import pythonaddins
import os
from arcpy import env

class Add_points(object):
    """Implementation for AddPoints_addin.Add_points (Tool)"""
    def __init__(self):
        self.enabled = True
        self.cursor = 3 # Can set to "Line", "Circle" or "Rectangle" for interactive shape drawing and to activate the onLine/Polygon/Circle event sinks.
    def onMouseDownMap(self, x, y, button, shift):

        fc = "TonyTwoWay.DBO.TT"
        workspace = r"C:\Users\talmeida\AppData\Roaming\ESRI\Desktop10.1\ArcCatalog\Connection to dsd15_sqlexpress.sde"
        arcpy.env.overwriteOutput = True
        #arcpy.ChangeVersion_management('TonyTwoWay.DBO.TT','TRANSACTIONAL','dbo.DEFAULT', "")
        # Start an edit session. Must provide the worksapce.
        edit = arcpy.da.Editor(workspace)

        # Edit session is started without an undo/redo stack for versioned data
        #  (for second argument, use False for unversioned data)
        edit.startEditing(True)

        # Start an edit operation
        edit.startOperation()

        CC_list = []
        with arcpy.da.SearchCursor(fc, ["AddressID"]) as cursor:
            for row in cursor:
                try:
                    if "CC" in row[0]:
                        CC_list.append(int(row[0].strip("CC")))   
                except TypeError:
                    pass        
        del cursor

        CC_list.sort()
        AddressID = CC_list[-1] + 1
        AddressID = 'CC' + str(AddressID)

        row_values = [(x, y, (x, y), AddressID)]
        cursor = arcpy.da.InsertCursor(fc, ["X_Coord", "Y_Coord", "SHAPE@XY", "ADDRESSID"])

        for row in row_values:
            cursor.insertRow(row)
        del cursor
#####################################################
        fcTarget = "TonyTwoWay.DBO.TT"
        fcJoin = "testParcelsAdmint"
        add_fields = ["ACCOUNT","SiteNum","OwnerName","SiteAddres","SiteNumSfx","SiteStreet","predir","StreetType","SubName"]

        # fix args
        if not isinstance(add_fields, list):
            # from script tool
            add_fields = add_fields.split(';')

        # do not need this scratch file
        fcOutput = r'in_memory\temp_join'
        arcpy.SpatialJoin_analysis(fcJoin, fcTarget,fcOutput, 'JOIN_ONE_TO_MANY')


        # grab oid field from points
        oid_t = arcpy.Describe(fcTarget).OIDFieldName

        # init rowW and rowR
        curR = arcpy.SearchCursor(fcOutput)
        join_dict = dict([(r.JOIN_FID,[r.getValue(f) for f in add_fields]) for r in curR])
        del curR

        # Now update the new target
        curW = arcpy.UpdateCursor(fcTarget)
        for row in curW:
            t_oid = row.getValue(oid_t)
            if t_oid in join_dict:
                for f in add_fields:
                    row.setValue(f, join_dict[t_oid][add_fields.index(f)])
            curW.updateRow(row)
        del row, curW
        arcpy.AddMessage('Updated all records sucussefully')

        # Stop the edit operation.
        edit.stopOperation()

        # Stop the edit session and save the changes
        edit.stopEditing(True)        

        arcpy.RefreshActiveView()
        pass
    
0 Kudos