I am developing a Python toolbox, and came across a peculiar behavior: The tool objects does not remember any instance variables between function calls. Consider this code:
import arcpy
class Toolbox(object):
def __init__(self):
self.label = "Test"
self.alias = "Test"
self.tools = [Tool]
class Tool(object):
def __init__(self):
self.label = "Test tool"
self.x = "INIT"
def getParameterInfo(self):
param0 = arcpy.Parameter(
displayName = "Parameter",
name = "parameter",
datatype = "String",
parameterType = "Required",
direction = "Input")
return [param0]
def updateParameters(self, parameters):
self.x = "UPDATE PARAMETERS"
def updateMessages(self, parameters):
parameters[0].setWarningMessage(self.x)
self.x = "UPDATE MESSAGES"
def execute(self, parameters, messages):
arcpy.AddMessage(self.x)
The way I would expect this code to behave is this:
- First
__init__
is called, andx
is set to"INIT"
. - Then
updateParameters
is called, andx
is changed to"UPDATE PARAMETERS"
. - Then
updateMessages
is called. It displays a warning with the text"UPDATE PARAMETERS"
and then changex
to"UPDATE MESSAGES"
. - Finally, when the tool is executed it outputs
"UPDATE MESSAGES"
.
However, when I run the tool both the warning and the execution output is "INIT"
. My conclusion is that ArcMap does not use the same instance of the class throughout, but instead creates a new instance for every function call (updateParameters
, updateMessages
, and execute
).
Is my conclusion correct? If so, why does ArcMap behave that way? From an OOP perspective it seems very strange.
This behavior causes big problems for me. The first parameter of the tool I am developing is a text file. When the user has picked one I want to populate the other parameters with different default values depending on the content of the file. Since the file could be rather large I do not want to have to parse it every time any parameter changes, but now it seems like I have to since there is no way to know if the parameter has changed or not.
Answer
One thing I have noticed (with both add-ins or custom script tools/PYT's) is that when ArcMap or Catalog load, it will instantiate the classes contained within these custom tools as the application is loaded. I do not think these start a new instance of the class when the tool is ran (unless you refresh the PYT), but maybe I am wrong. Haven't tested this fully. It is interesting if it does create a new class instance whenever any method call is made.
To get around this, you may want to to include a function that can be called to parse your text file if a parameter is altered so that you can explicitly control when the text file is reloaded. A simple example:
def parseText(txt):
with open(txt, 'r') as f:
return f.readlines()
So then, in your PYT you can call this function if the parameter has been changed by the user:
def updateParameters(self, parameters):
if parameters[0].altered:
text_content = parseText(r'C:\path\to\your_file.txt')
self.x = "UPDATE PARAMETERS"
You can also call this in the __init__
of the class to get an initial cache of the content. Python is pretty quick when parsing text files, even if they are large. If you are loading every line of text into a list and you are concerned about memory, perhaps you would be better off having the function return a generator.
Unfortunately, I do not think you can get around not reloading the text file if the user changes a parameter. Perhaps you can do all the control for the text file in the UpdateParameters
with some if statements, like if parameters[0].value == 'this' and parameters[1].value == 'another thing'
and handle the text file differently for whatever combination of input parameters you get.
I am with you though, I do not like the default behavior of the PYTs.
EDIT:
Wow, you are right. It is spinning up a TON of instances! Here's a simple way to track # of instances:
>>> class Test(object):
i = 0
def __init__(self):
Test.i += 1
>>> t = Test()
>>> t2 = Test()
>>> t3 = Test()
>>> t3.i
3
And now if we apply that same logic to a PYT, try running this in ArcMap:
import arcpy
import pythonaddins
class Toolbox(object):
def __init__(self):
self.label = "Test"
self.alias = "Test"
self.tools = [Tool]
class Tool(object):
i = 0
def __init__(self):
self.label = "Test tool"
self.x = "INIT"
Tool.i += 1
def getParameterInfo(self):
param0 = arcpy.Parameter(
displayName = "Parameter",
name = "parameter",
datatype = "String",
parameterType = "Required",
direction = "Input")
return [param0]
def updateParameters(self, parameters):
if parameters[0].altered:
self.x = "UPDATE PARAMETERS"
pythonaddins.MessageBox('# of instances: {}, x = {}'.format(self.i, self.x), 'test')
def updateMessages(self, parameters):
parameters[0].setWarningMessage(self.x)
self.x = "UPDATE MESSAGES"
pythonaddins.MessageBox('# of instances: {}, x = {}'.format(self.i, self.x), 'test')
def execute(self, parameters, messages):
arcpy.AddMessage('# of instances: {}, x = {}'.format(self.i, self.x))
I got 9 instances right off the bat! It spins up new ones every time I change a parameter, and it keeps climbing!
No comments:
Post a Comment