<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Struggling with acpy undoOperation in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/struggling-with-acpy-undooperation/m-p/473909#M37041</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Just a hunch, I think the problem is you've lost the original 'edit' reference with the implicit 'edit' set with your statement:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;with arcpy.da.Editor(workspace) as edit&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;In other words, I think your statements previous to this block were 'forgotten' (because they're outside the 'scope' of your 'with' block):&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;edit = arcpy.da.Editor(workspace)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;edit.startEditing(True, False)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;edit.startOperation()&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So, edit.startOperation apparently did not throw an error but the later edit.UndoOperation did.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Notice the sample code structure working with a file gdb (as in your code) given by the webhelp under the subtopic "Edit sessions and with statements" does not use the coupled start/stop statements:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
# The below code structure for edit session with statements from
# &lt;A href="http://resources.arcgis.com/en/help/main/10.2/index.html#//018w00000005000000" rel="nofollow noopener noreferrer" target="_blank"&gt;Editor (arcpy.da)&lt;/A&gt;

import arcpy

# Open an edit session and start an edit operation
with arcpy.da.Editor(workspace) as edit:
&amp;nbsp;&amp;nbsp;&amp;nbsp; # &amp;lt;your edits&amp;gt;

&amp;nbsp;&amp;nbsp;&amp;nbsp; # If an exception is raised, the operation will be aborted, and 
&amp;nbsp;&amp;nbsp;&amp;nbsp; #&amp;nbsp;&amp;nbsp; edit session is closed without saving

&amp;nbsp;&amp;nbsp;&amp;nbsp; # If no exceptions are raised, stop the operation and save 
&amp;nbsp;&amp;nbsp;&amp;nbsp; #&amp;nbsp;&amp;nbsp; and close the edit session
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;...looking further down to the samples on that same webpage, a try/except block is used in an example to take advantage of any potentially returned ExecuteError exception so that arcpy 'handles' the edit session by aborting and closing, without any further code:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import arcpy

fc = 'C:/Portland/Portland.gdb/Land/Parks'
workspace = 'C:/Portland/Portland.gdb'
layer_name = 'Parks'

try:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeFeatureLayer_management(fc, layer_name)
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; layer_name, 'NEW_SELECTION',
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; """CUSTODIAN = 'City of Portland'""")
&amp;nbsp;&amp;nbsp;&amp;nbsp; with arcpy.da.Editor(workspace) as edit:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CalculateField_management(
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; layer_name, 'Usage', '"PUBLIC"', 'PYTHON')

except arcpy.ExecuteError:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print(arcpy.GetMessages(2))
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I think (you'll need to test), you can as needed raise your own error, and likewise raise a specific exception of your own to trigger the same abort and close... that's my hunch.&amp;nbsp; Working within SDE and versions, I believe I'd implement my own start/stop/abort/etc controls, but for a file gdb (as in your case), it should suffice to use an exception.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Hope that helps,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Wayne&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 20:57:05 GMT</pubDate>
    <dc:creator>T__WayneWhitley</dc:creator>
    <dc:date>2021-12-11T20:57:05Z</dc:date>
    <item>
      <title>Struggling with acpy undoOperation</title>
      <link>https://community.esri.com/t5/python-questions/struggling-with-acpy-undooperation/m-p/473908#M37040</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I have developed several toolbars but after some use I decided to try and add undo button to my tool bars. However, I am struggling to implement the arcpy undoOperation function in&amp;nbsp; just the Arc Python window, let alone one of my toolbars. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I have been trying &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;workspace='E:\CLWNEZ\LFM_CLWNEZ\M6004\m6004_Classification_Base.gdb'
layer='m6004_Classification_Base'
edit = arcpy.da.Editor(workspace)
edit.startEditing(True, False)
edit.startOperation()
with arcpy.da.Editor(workspace) as edit:
 arcpy.CalculateField_management(layer, "LFM", 5000, "PYTHON")
edit.undoOperation()&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;But I keep getting this error message when I run this in the Python window. &lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;Runtime error 
Traceback (most recent call last):
&amp;nbsp; File "&amp;lt;string&amp;gt;", line 1, in &amp;lt;module&amp;gt;
AttributeError: 'Workspace Operation object' object has no attribute 'undoOperation'&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm sure I'm doing something incorrect but not sure what yet. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Fred&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 15 Nov 2013 21:34:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/struggling-with-acpy-undooperation/m-p/473908#M37040</guid>
      <dc:creator>FredKellner1</dc:creator>
      <dc:date>2013-11-15T21:34:50Z</dc:date>
    </item>
    <item>
      <title>Re: Struggling with acpy undoOperation</title>
      <link>https://community.esri.com/t5/python-questions/struggling-with-acpy-undooperation/m-p/473909#M37041</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Just a hunch, I think the problem is you've lost the original 'edit' reference with the implicit 'edit' set with your statement:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;with arcpy.da.Editor(workspace) as edit&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;In other words, I think your statements previous to this block were 'forgotten' (because they're outside the 'scope' of your 'with' block):&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;edit = arcpy.da.Editor(workspace)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;edit.startEditing(True, False)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;edit.startOperation()&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So, edit.startOperation apparently did not throw an error but the later edit.UndoOperation did.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Notice the sample code structure working with a file gdb (as in your code) given by the webhelp under the subtopic "Edit sessions and with statements" does not use the coupled start/stop statements:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
# The below code structure for edit session with statements from
# &lt;A href="http://resources.arcgis.com/en/help/main/10.2/index.html#//018w00000005000000" rel="nofollow noopener noreferrer" target="_blank"&gt;Editor (arcpy.da)&lt;/A&gt;

import arcpy

# Open an edit session and start an edit operation
with arcpy.da.Editor(workspace) as edit:
&amp;nbsp;&amp;nbsp;&amp;nbsp; # &amp;lt;your edits&amp;gt;

&amp;nbsp;&amp;nbsp;&amp;nbsp; # If an exception is raised, the operation will be aborted, and 
&amp;nbsp;&amp;nbsp;&amp;nbsp; #&amp;nbsp;&amp;nbsp; edit session is closed without saving

&amp;nbsp;&amp;nbsp;&amp;nbsp; # If no exceptions are raised, stop the operation and save 
&amp;nbsp;&amp;nbsp;&amp;nbsp; #&amp;nbsp;&amp;nbsp; and close the edit session
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;...looking further down to the samples on that same webpage, a try/except block is used in an example to take advantage of any potentially returned ExecuteError exception so that arcpy 'handles' the edit session by aborting and closing, without any further code:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import arcpy

fc = 'C:/Portland/Portland.gdb/Land/Parks'
workspace = 'C:/Portland/Portland.gdb'
layer_name = 'Parks'

try:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeFeatureLayer_management(fc, layer_name)
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; layer_name, 'NEW_SELECTION',
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; """CUSTODIAN = 'City of Portland'""")
&amp;nbsp;&amp;nbsp;&amp;nbsp; with arcpy.da.Editor(workspace) as edit:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CalculateField_management(
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; layer_name, 'Usage', '"PUBLIC"', 'PYTHON')

except arcpy.ExecuteError:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print(arcpy.GetMessages(2))
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I think (you'll need to test), you can as needed raise your own error, and likewise raise a specific exception of your own to trigger the same abort and close... that's my hunch.&amp;nbsp; Working within SDE and versions, I believe I'd implement my own start/stop/abort/etc controls, but for a file gdb (as in your case), it should suffice to use an exception.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Hope that helps,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Wayne&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 20:57:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/struggling-with-acpy-undooperation/m-p/473909#M37041</guid>
      <dc:creator>T__WayneWhitley</dc:creator>
      <dc:date>2021-12-11T20:57:05Z</dc:date>
    </item>
  </channel>
</rss>

