Select to view content in your preferred language

How to check if in an Edit Session using python and arcpy

8187
23
03-08-2017 12:52 PM
VishalShah2
Occasional Contributor II

I am currently working on a tool that uses the updateCursor function but the tool needs to be in an edit session to use. However some who may use the tool may end up not being in an edit session before running the tool and get an error. Rather than come ask me every time about the error, I'd like to implement an if statement in the script in the beginning that checks if the user is currently in an edit session, if they are then run tool. if not, a dialogue box explaining they need to be in an edit session and ask if they want the script to turn editor on. Formatted as such:

#if edit session is on:
    #run tool
#elif edit session is off:
    #dialogue box explaining user has to be in edit session and asking if they want the script to turn editor on (Yes or no option):
        #if yes:
            #run tool
        #if no:
            #quit

I am trying to do this without using the work done by Mark Cederholm as referenced in this post: How do I access ArcObjects from Python?, and code examples provided by Matt Wilkie in his "Snippits.py" file.

0 Kudos
23 Replies
VishalShah2
Occasional Contributor II

Joshua is this what you had in mind?

import arcpy
import pythonaddins
import os

toolPath = r'path to toolboox.tbx'
workspace = 'some workspace'
editor = arcpy.da.Editor(workspace)


if editor.isEditing == 'true':
    pythonaddins.GPToolDialog(toolPath, 'CustomExportTool')
elif editor.isEditing == 'false': 
    pythonaddins.MessageBox("You must be in an edit session to run the Custom Export Tool. If you wish to turn an edit session on, please click 'yes'. If you do not wish to turn an edit session on, please click 'no'.", "Turn Editor On?", 4)
    msg = pythonaddins.MessageBox("You must be in an edit session to run the Custom Export Tool. If you wish to turn an edit session on, please click 'yes'. If you do not wish to turn an edit session on, please click 'no'.", "Turn Editor On?", 4)
        if msg == 'yes':
            editor.startEditing(False, False)
            pythonaddins.GPToolDialog(toolPath, 'CustomExportTool')
        elif msg == 'no':
            quit
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Several comments:

  • As has been discussed throughout this thread, calling isEditing will always return False unless an edit session was started by calling startEditing because the Editor is only aware of itself, i.e., it can't tell if another edit session is already open on a workspace.

  • Even if the Editor was aware of more than itself, the code still wouldn't work because what is returned from calling isEditing are Python booleans, not strings.  The code would have to read:  if editor.isEditing == True: or simply if editor.isEditing: .  The same goes for how you are checking for false.

  • When checking the return value from the MessageBox, string comparisons are case sensitive.  Neither of the conditions as written will ever be met because what is returned is 'Yes' or 'No' .
VishalShah2
Occasional Contributor II

Joshua, I am currently working on another tool where setting the user input equal to a variable is crucial. Is this the right way to do it in terms of setting the variable equal to something the user can do an input and then a conditional to check on what they input?

if editor.isEditing == True:
    pythonaddins.GPToolDialog(toolPath, 'CustomExportTool')
elif editor.isEditing == False: 
    pythonaddins.MessageBox("", "", 4)
    msg = pythonaddins.MessageBox("", "", 4)
        if msg == 'Yes':
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Before I get to your question, Line 04 will present the user with a message box, but it doesn't do anything because the return result is never stored.  Line 05 is the important line.

I feel like the code you posted isn't directly related to the question you are asking.  I get the impression your other tool isn't using just a message box to get user input.  If there is another coding question you have, it might be good to start another thread and post the code directly related to your question. 

VishalShah2
Occasional Contributor II

The code is completely different but I wanted to know if the way i have msg = to the message box if that will read whatever the user input to use later in the script? In a sense I wanted to see if I am placing the variable in the right way

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

If an edit session doesn't exist, when exactly does the user see the error?  Right away, or is it after the tool has ran for a while?  What is the function or method call that is generating the error?  What specifically is the error?

0 Kudos
VishalShah2
Occasional Contributor II

Hi Joshua, so the error pops up milliseconds to like 2 seconds after you start running the tool. The tool is a custom export tool that has the function updateCursor function, which updates the fields to a particular value rather than leaving the blank fields as null values. The error states that updateCursor can not be used outside of the edit session and the rest of the tool does not run. Hence why I want to create this check to see if edit session is active and if not, to provide a prompt for the user that enables the user to select 'Yes' or 'No' to allow the script to turn an edit session on and then run the tool.  

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

iamurray‌ provided some good links, I suggest you read over them.

Given the tools generates an error anywhere from "milliseconds to like 2 seconds," which is pretty quick (it isn't like it runs for tens of minutes and then errors out), I would go with a try except block.  Python handles EAFP (easier to ask forgiveness than permission) much better than other languages, so generating exceptions and handling them isn't seen as a big deal.  Unlike some of the code examples in the StackExchange thread that Ian referenced, you really don't want to trap all exceptions with a generic except clause.  It is better to trap the specific exception relating to edit sessions because you want other errors to still propagate through to the user.

0 Kudos
IanMurray
Frequent Contributor

I wonder if instead of trying to check if an edit session is going on, just check if a LOCK file is present for that workspace the file you are hoping to edit is in for the file in question.  A file GDB creates a LOCK file of the same name regardless of whether it is being editted within an ArcMap Edit Session or using an Editor Object(FeatureClassName.ed.lock).  Its not perfect since obviously more than one person can be accessing the dataset and create a lock on it, but at least you can check if the data is being editted at all.

Just spitballing here.

0 Kudos
VishalShah2
Occasional Contributor II

I've never worked with exceptions before so I was wondering if you have an example of what it may look like and the formatting of the script so I may try that out.

0 Kudos