Arcpy da Editor confusing

10776
26
Jump to solution
07-30-2014 01:43 AM
Geography
New Contributor

Hi all,

I'm still new to arcpy and python. PLEASE can somebody explain why the first two lines of code create a runtime error, but the last two lines run fine?

>>> with arcpy.da.Editor(workspace) as edit:

...     edit.startEditing(False, True)

...    

Runtime error

Traceback (most recent call last):

  File "<string>", line 2, in <module>

AttributeError: 'Workspace Operation object' object has no attribute 'startEditing'

>>> edit = arcpy.da.Editor(workspace)

>>> edit.startEditing(False, True)

THX!!

Tags (3)
0 Kudos
26 Replies
RiyasDeen
Occasional Contributor III

May be this helps.

import arcpy

import os

fc  = str(sys.argv[1])

templine = fc + "_line_temp"

workspace = os.path.dirname(fc)

fields = []

for f in arcpy.ListFields(fc):

  if f.type != "OID":

  fields.append(f.name)

arcpy.SplitLine_management(fc, templine)

arcpy.DeleteFeatures_management(fc)

# if feature class is not in feature dataset then use arcpy.da.Editor(workspace)

with arcpy.da.Editor(os.path.dirname(workspace)) as edit:

  edit.startEditing(False, False)

  edit.startOperation()

  with arcpy.da.SearchCursor(templine,["Shape@"]) as sc:

  with arcpy.da.InsertCursor(fc, ["Shape@"]) as ic:

  for row in sc:

  ic.insertRow(row)

  del ic

  del sc

  edit.stopOperation()

  edit.stopEditing(True)

arcpy.Delete_management(templine)

0 Kudos
Geography
New Contributor

I'll mark it as correct answer, even so I think that this approach would be against the advice from the help:

Cursors should be scoped to a single edit operation.

Or does it mean something else.

Again my best thanks to you Riyas!!

0 Kudos
ToddUlery
Occasional Contributor

First thought, not a solution yet, but you do need to nest your actual edits inside the edit sessions. I think its ok to have a search outside, but if there are updates, they need to go inside the operation.

Riyas Deen suggests this already. Just wanted to stress that at minimum it should be considered. I am looking into edits with topology feature datasets.

0 Kudos
ToddUlery
Occasional Contributor

I might even suggest not setting the edit session inside of the search. Set variable outside for search, and then call the session and then establish your search loop/update. Again, another thought.

0 Kudos
ToddUlery
Occasional Contributor

Still a litle confused about the env object.

env = EnvVar()

I assume you are setting this within a function you created. Need to look at that.

0 Kudos
Geography
New Contributor

Todd many thanks for your reply's!! Why are you concerned about the env object? As you can see from the 1st code section (which I posted at

0 Kudos
Geography
New Contributor

THANKS EVERYBODY for the fruitful discussion. Unfortunately I won't be able to look into your suggestions for the next couple of days, but I promise to take a closer look on all hints and suggestions you gave to me!!

0 Kudos