Proper arcpy.da.Editor use

21707
3
02-17-2015 04:32 PM
BlakeTerhune
MVP Regular Contributor

I'm on ArcGIS 10.2.2 and trying to use arcpy.da.Editor and an InserCursor on a versioned feature class. The feature class was registered without the option to move edits to base and resides in a 10.0 SDE on Oracle 11g (will be 10.2.2 SDE soon).

I found this helpful post by Leslie Morgan that really details my same situation, but there are no code samples. I would like to know if any of you have used the Python Editor in a similar setup and what your code looks like. More specifically, can the Editor class be used in a with statement like the Esri help article says or do I still have to start and stop the operation and edit session like users say?

Tags (1)
0 Kudos
3 Replies
BlakeTerhune
MVP Regular Contributor

Here is the basic way I'm using the Editor for now until I find a better way.

try:

    edit = arcpy.da.Editor(sdeconnection)

    print "edit created"

    edit.startEditing()

    print "edit started"

    edit.startOperation()

    print "operation started"

    # Perform edits

    with arcpy.da.InsertCursor(fc, fields) as fc_icursor:

        fc_icursor.insertRow(someNewRow)

    edit.stopOperation()

    print "operation stopped"

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

    print "edit stopped"

except Exception as err:

    print err

    if 'edit' in locals():

        if edit.isEditing:

            edit.stopOperation()

            print "operation stopped in except"

            edit.stopEditing(False)  ## Stop the edit session with False to abandon the changes

            print "edit stopped in except"

finally:

    # Cleanup

    arcpy.ClearWorkspaceCache_management()

BlakeTerhune
MVP Regular Contributor

Revisiting this, I've simplified the code a little by creating the edit session outside the try...except so you don't have to check if it exists.

edit = arcpy.da.Editor(sdeconnection)
print "edit created"
try:
edit.startEditing()
print "edit started"
edit.startOperation()
print "operation started"
# Perform edits
with arcpy.da.InsertCursor(fc, fields) as fc_icursor:
fc_icursor.insertRow(someNewRow)
edit.stopOperation()
print "operation stopped"
edit.stopEditing(True) ## Stop the edit session with True to save the changes
print "edit stopped"
except Exception as err:
print err
if edit.isEditing:
edit.stopOperation()
print "operation stopped in except"
edit.stopEditing(False) ## Stop the edit session with False to abandon the changes
print "edit stopped in except"
finally:
# Cleanup
arcpy.ClearWorkspaceCache_management()
Jack_Zhang
Occasional Contributor

Thanks Blake, that's a lot better and clear than the example in ESRI's doc.