<?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 PYT: Prevent function call until tool is initialized, not when toolbox is initialized? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/pyt-prevent-function-call-until-tool-is/m-p/1535316#M72785</link>
    <description>&lt;P&gt;So, the title doesn't make a lot of sense, but what I have going on is:&lt;/P&gt;&lt;P&gt;I have a PYT containing several tools.&lt;/P&gt;&lt;P&gt;I've added a class variable to one of the tools that calls a function to create a dictionary.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;class Toolbox:
    def __init__(self):
        self.label = ""
        self.alias = ""
        self.tools = [tool1, tool2]
class tool1:
    def createDict():
        retDict = {}
        return retDict
    retDict = createDict()

    def __init__(self):
        self.label = ""
        self.description = ""

    def getParameterInfo(self):
        return params

     def isLicensed(self):
        return True

    def updateParameters(self, parameters):
        return

    def updateMessages(self, parameters):
        return

    def execute(self, parameters, messages):
        pass
        return

    def postExecute(self, parameters):
        return&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The issue that I'm having is that the function&amp;nbsp;(Lines 7-9) to create that class variable (Line 10) is called when I initialize the&amp;nbsp;&lt;STRONG&gt;toolbox&lt;/STRONG&gt;, not when I initialize that particular tool. This is making opening the toolbox take longer than it should, particularly given that I might want to use the other several tools and don't care about that dictionary getting created.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How can I make it so the function doesn't get called until I open that tool? I'm cool with waiting however long I need to; I just don't want to wait until I need to.&lt;/P&gt;&lt;P&gt;I'd be cool with like, adding it as a derived parameter in getParameterInfo(), but there isn't an option in Parameter data types to just use a python dictionary.&lt;/P&gt;&lt;P&gt;Any ideas? Thanks!&lt;/P&gt;</description>
    <pubDate>Wed, 04 Sep 2024 22:07:03 GMT</pubDate>
    <dc:creator>AlfredBaldenweck</dc:creator>
    <dc:date>2024-09-04T22:07:03Z</dc:date>
    <item>
      <title>PYT: Prevent function call until tool is initialized, not when toolbox is initialized?</title>
      <link>https://community.esri.com/t5/python-questions/pyt-prevent-function-call-until-tool-is/m-p/1535316#M72785</link>
      <description>&lt;P&gt;So, the title doesn't make a lot of sense, but what I have going on is:&lt;/P&gt;&lt;P&gt;I have a PYT containing several tools.&lt;/P&gt;&lt;P&gt;I've added a class variable to one of the tools that calls a function to create a dictionary.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;class Toolbox:
    def __init__(self):
        self.label = ""
        self.alias = ""
        self.tools = [tool1, tool2]
class tool1:
    def createDict():
        retDict = {}
        return retDict
    retDict = createDict()

    def __init__(self):
        self.label = ""
        self.description = ""

    def getParameterInfo(self):
        return params

     def isLicensed(self):
        return True

    def updateParameters(self, parameters):
        return

    def updateMessages(self, parameters):
        return

    def execute(self, parameters, messages):
        pass
        return

    def postExecute(self, parameters):
        return&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The issue that I'm having is that the function&amp;nbsp;(Lines 7-9) to create that class variable (Line 10) is called when I initialize the&amp;nbsp;&lt;STRONG&gt;toolbox&lt;/STRONG&gt;, not when I initialize that particular tool. This is making opening the toolbox take longer than it should, particularly given that I might want to use the other several tools and don't care about that dictionary getting created.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How can I make it so the function doesn't get called until I open that tool? I'm cool with waiting however long I need to; I just don't want to wait until I need to.&lt;/P&gt;&lt;P&gt;I'd be cool with like, adding it as a derived parameter in getParameterInfo(), but there isn't an option in Parameter data types to just use a python dictionary.&lt;/P&gt;&lt;P&gt;Any ideas? Thanks!&lt;/P&gt;</description>
      <pubDate>Wed, 04 Sep 2024 22:07:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/pyt-prevent-function-call-until-tool-is/m-p/1535316#M72785</guid>
      <dc:creator>AlfredBaldenweck</dc:creator>
      <dc:date>2024-09-04T22:07:03Z</dc:date>
    </item>
    <item>
      <title>Re: PYT: Prevent function call until tool is initialized, not when toolbox is initialized?</title>
      <link>https://community.esri.com/t5/python-questions/pyt-prevent-function-call-until-tool-is/m-p/1535468#M72786</link>
      <description>&lt;P&gt;Okay, I figured out two solutions here.&lt;/P&gt;&lt;P&gt;Solution 1 (unpreferred): Create a parameter and load the dictionary into it as a string (not preferred). This is kind of clunky and you end up having to import json for it to work. Also, this is a specific solution for a specific datatype, so not great.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-SPOILER&gt;&lt;LI-CODE lang="python"&gt;class Toolbox:
    def __init__(self):
        self.label = ""
        self.alias = ""
        self.tools = [tool1, tool2]
class tool1:
    def createDict():
        retDict = {}
        return retDict

    def __init__(self):
        self.label = ""
        self.description = ""

    def getParameterInfo(self):
        param0 = arcpy.Parameter(displayName="Dictionary",
                                 name="retDict",
                                 datatype="GPString",
                                 parameterType="Required",
                                 # multivalue= True,
                                 direction="Input")
        param0.value = str(tool1.createDict()).replace("'",'"')
        params = [param0]
        return params

     def isLicensed(self):
        return True

    def updateParameters(self, parameters):
        return

    def updateMessages(self, parameters):
        return

    def execute(self, parameters, messages):
        import json
        retDict = loads(parameters[0].value)
        return

    def postExecute(self, parameters):
        return&lt;/LI-CODE&gt;&lt;/LI-SPOILER&gt;&lt;P&gt;Solution 2 (preferred): Create a class variable, but just don't run it until you initialize the tool. That is, populate it in getParameterInfo().&lt;/P&gt;&lt;P&gt;This seems a lot cleaner -- no changing between data types, no extra modules, etc.&lt;/P&gt;&lt;LI-SPOILER&gt;&lt;LI-CODE lang="python"&gt;class Toolbox:
    def __init__(self):
        self.label = ""
        self.alias = ""
        self.tools = [tool1, tool2]
class tool1:
    def createDict():
        retDict = {}
        return retDict
    # I set the variable to initialize empty.
    # This is in part so I don't forget that it's there.
    retDict = {}

    def __init__(self):
        self.label = ""
        self.description = ""

    def getParameterInfo(self):
        param0 = ...
        # I used setattr() because doing self.retDict 
        # didn't take the change.
        setattr(tool1, "retDict", tool1.createDict())
        return params

     def isLicensed(self):
        return True

    def updateParameters(self, parameters):
        return

    def updateMessages(self, parameters):
        return

    def execute(self, parameters, messages):
        pass
        return

    def postExecute(self, parameters):
        return&lt;/LI-CODE&gt;&lt;/LI-SPOILER&gt;&lt;P&gt;Hopefully this helps someone down the line.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Sep 2024 13:51:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/pyt-prevent-function-call-until-tool-is/m-p/1535468#M72786</guid>
      <dc:creator>AlfredBaldenweck</dc:creator>
      <dc:date>2024-09-05T13:51:36Z</dc:date>
    </item>
  </channel>
</rss>

