<?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: Python Toolbox Question in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/python-toolbox-question/m-p/1030738#M60128</link>
    <description>&lt;P&gt;Thanks Johannes. I've adjusted my example file. It's cleaner, executes about the same as before. But makes better sense. Cheers&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# -*- coding: utf-8 -*-

class Toolbox(object):
    import arcpy
    
    def __init__(self):
        """Define the toolbox (the name of the toolbox is the name of the
        .pyt file)."""
        self.label = ""
        self.alias = ""

        # List of tool classes associated with this toolbox
        self.tools = [SomeTool, AnotherTool]
        
class BaseTool(object):
    import arcpy
    
    def __init__(self):
        """Define the toolbox (the name of the toolbox is the name of the
        .pyt file)."""
        self.label = ""
        self.alias = ""

        # List of tool classes associated with this toolbox
        self.tools = [SomeTool, AnotherTool]
        
    def Procedure1(self, parameter1, parameter2):
        #This is a procedure used by multiple tools in the pyt file.
        #code goes here...
        arcpy.AddMessage('hi from Procedure1. parameter1: {}; parameter2: {}'.format(parameter1,parameter2))
        
    def Procedure2(self, parameter1):
        #This is a procedure used by multiple tools in the pyt file.
        #code goes here...
        self.Procedure3(parameter1)
        
    def Procedure3(self, parameter1):
        #This is a procedure used by multiple tools in the pyt file.
        #code goes here...
        arcpy.AddMessage('message from Procedure2: {}'.format(parameter1))
    

class SomeTool(BaseTool):
    
    def __init__(self):
        """Define the tool (tool name is the name of the class)."""
        self.label = "Some Tool"
        self.description = " "
        self.canRunInBackground = False

    def getParameterInfo(self):
        """Define parameter definitions"""
        params = None
        return params

    def isLicensed(self):
        """Set whether tool is licensed to execute."""
        return True

    def updateParameters(self, parameters):
        """Modify the values and properties of parameters before internal
        validation is performed.  This method is called whenever a parameter
        has been changed."""
        return

    def updateMessages(self, parameters):
        """Modify the messages created by internal validation for each tool
        parameter.  This method is called after internal validation."""
        return

    def execute(self, parameters, messages):
        """The source code of the tool."""
        def Procedure4(inParameter):
            #This procedure is just for SomeTool
            return('phrase passed into Procedure4: {}'.format(inParameter))
            #code goes here...
            
        aVariable = Procedure4('blah')
        
        self.Procedure1(aVariable,'something else')
        self.Procedure2('hi there')
        
        return

class AnotherTool(BaseTool):
    def __init__(self):
        """Define the tool (tool name is the name of the class)."""
        self.label = "Another Tool"
        self.description = " "
        self.canRunInBackground = False

    def getParameterInfo(self):
        """Define parameter definitions"""
        params = None
        return params

    def isLicensed(self):
        """Set whether tool is licensed to execute."""
        return True

    def updateParameters(self, parameters):
        """Modify the values and properties of parameters before internal
        validation is performed.  This method is called whenever a parameter
        has been changed."""
        return

    def updateMessages(self, parameters):
        """Modify the messages created by internal validation for each tool
        parameter.  This method is called after internal validation."""
        return

    def execute(self, parameters, messages):
       
        self.Procedure2('whatever')
        
        return&lt;/LI-CODE&gt;</description>
    <pubDate>Thu, 25 Feb 2021 21:30:44 GMT</pubDate>
    <dc:creator>ChrisHolmes</dc:creator>
    <dc:date>2021-02-25T21:30:44Z</dc:date>
    <item>
      <title>Python Toolbox Question</title>
      <link>https://community.esri.com/t5/python-questions/python-toolbox-question/m-p/1029130#M60049</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I have a python toolbox, that has the following tools:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="ChrisHolmes_0-1614007521632.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/6568iFCC980DE0C9CF620/image-size/medium?v=v2&amp;amp;px=400" role="button" title="ChrisHolmes_0-1614007521632.png" alt="ChrisHolmes_0-1614007521632.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Some of these scripts use the same procedures, so I moved those procedures to the top of the file above all of the individual classes. Which seems to make sense, I don't have exact copies of the same procedure inside each class, fewer lines of code, smaller file...&lt;/P&gt;&lt;P&gt;The top of the file looks like this (this is not all the procedures that are at the top just the first few):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy
import os
import re
import shutil
import configparser
import subprocess
from time import gmtime, strftime


def OpenAprx(aprx):
    #open the passed in mxd file in a new arcmap instance
    arcpy.AddMessage('Opening: {}'.format(aprx))
    subprocess.Popen([r'C:\Program Files\ArcGIS\Pro\bin\ArcGISPro.exe',aprx])

def CreateImage(prj, mapName, lyrsOn, lyrsOff, lyrExtent, lytName, mapScale, pngFilePath):
    arcpy.AddMessage('CreateImage start {}'.format(strftime("%H:%M:%S", gmtime())))
    arcpy.AddMessage('prj: {}'.format(prj))
    SetLayers(prj, mapName, lyrsOn, 'on')
    arcpy.AddMessage('SetLayers ON completed {}'.format(strftime("%H:%M:%S", gmtime())))
    arcpy.AddMessage('len lyrsOff: {}'.format(len(lyrsOff)))
    if len(lyrsOff) &amp;gt; 0:
        SetLayers(prj, mapName, lyrsOff, 'off')
        arcpy.AddMessage('SetLayers OFF completed {}'.format(strftime("%H:%M:%S", gmtime())))
    ExportImage(prj, mapName, lyrExtent, lytName, mapScale, pngFilePath)
    arcpy.AddMessage('ExportImage completed {}'.format(strftime("%H:%M:%S", gmtime())))

def SetLayers(prj, mapName, lyrs, status):
    arcpy.AddMessage('SetLayers prj = {}'.format(prj))
    aprx = arcpy.mp.ArcGISProject(prj)
    m = aprx.listMaps(mapName)[0]
    
    for lyr in m.listLayers():
        if lyr.name.upper() in lyrs:
            if status == 'on':
                lyr.visible = True
            else:
                lyr.visible = False
    #aprx.save()
    del aprx&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This all seems great to me but the problem is the code executes way slower than when the procedures are all copied in the individual classes. Why is this?&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Mon, 22 Feb 2021 15:34:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-toolbox-question/m-p/1029130#M60049</guid>
      <dc:creator>ChrisHolmes</dc:creator>
      <dc:date>2021-02-22T15:34:42Z</dc:date>
    </item>
    <item>
      <title>Re: Python Toolbox Question</title>
      <link>https://community.esri.com/t5/python-questions/python-toolbox-question/m-p/1029647#M60061</link>
      <description>&lt;P&gt;I won't pretend that I know how everything gets complied to interpretation but I would think you are essentially creating global functions that have to be compiled/indexed separately than being contained to that a class as a method, so there is some overhead with having to reach out to that function to perform the process.&lt;/P&gt;</description>
      <pubDate>Tue, 23 Feb 2021 18:02:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-toolbox-question/m-p/1029647#M60061</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-02-23T18:02:50Z</dc:date>
    </item>
    <item>
      <title>Re: Python Toolbox Question</title>
      <link>https://community.esri.com/t5/python-questions/python-toolbox-question/m-p/1029716#M60064</link>
      <description>&lt;P&gt;Ya, that's kind of what I was thinking. Thanks. Still searching around a bit for a proper way of doing this &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 23 Feb 2021 19:54:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-toolbox-question/m-p/1029716#M60064</guid>
      <dc:creator>ChrisHolmes</dc:creator>
      <dc:date>2021-02-23T19:54:11Z</dc:date>
    </item>
    <item>
      <title>Re: Python Toolbox Question</title>
      <link>https://community.esri.com/t5/python-questions/python-toolbox-question/m-p/1029884#M60083</link>
      <description>&lt;P&gt;You could possibly put those functions into a base class, and then use class inheritance for the classes that would use them.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 24 Feb 2021 03:28:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-toolbox-question/m-p/1029884#M60083</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-02-24T03:28:32Z</dc:date>
    </item>
    <item>
      <title>Re: Python Toolbox Question</title>
      <link>https://community.esri.com/t5/python-questions/python-toolbox-question/m-p/1030279#M60104</link>
      <description>&lt;P&gt;I think you're on the right track. Here's what I came up with, which is working and is executing in an acceptable amount of time. The actual .pyt file is quite long so I set up an 'example.pyt' which shows how the code&lt;/P&gt;&lt;LI-CODE lang="python"&gt;class Toolbox(object):
    import arcpy
    
    def __init__(self):
        """Define the toolbox (the name of the toolbox is the name of the
        .pyt file)."""
        self.label = ""
        self.alias = ""

        # List of tool classes associated with this toolbox
        self.tools = [SomeTool, AnotherTool,]
        
    def Procedure1(self, parameter1, parameter2):
        #This is a procedure used by multiple tools in the pyt file.
        #code goes here...
        
    def Procedure2(self, parameter1):
        #This is a procedure used by multiple tools in the pyt file.
        #code goes here...
        self.Procedure3('something')
        
    def Procedure3(self, parameter1):
        #This is a procedure used by Procedure2
        #code goes here...

class SomeTool(object):
    
    def __init__(self):
        """Define the tool (tool name is the name of the class)."""
        self.label = "Some Tool"
        self.description = " "
        self.canRunInBackground = False

    def getParameterInfo(self):
        """Define parameter definitions"""
        params = None
        return params

    def isLicensed(self):
        """Set whether tool is licensed to execute."""
        return True

    def updateParameters(self, parameters):
        """Modify the values and properties of parameters before internal
        validation is performed.  This method is called whenever a parameter
        has been changed."""
        return

    def updateMessages(self, parameters):
        """Modify the messages created by internal validation for each tool
        parameter.  This method is called after internal validation."""
        return

    def execute(self, parameters, messages):
        """The source code of the tool."""
        def Procedure4(inParameter):
            #This procedure is just for SomeTool
            
            #code goes here...
            
        aVariable = Procedure4('blah')
        
        sT = Toolbox()
        sT.Procedure1(aVariable,'something else')
        sT.Procedure2('something else')
            
        del sT
        
        return

class AnotherTool(object):
    def __init__(self):
        """Define the tool (tool name is the name of the class)."""
        self.label = "Another Tool"
        self.description = " "
        self.canRunInBackground = False

    def getParameterInfo(self):
        """Define parameter definitions"""
        params = None
        return params

    def isLicensed(self):
        """Set whether tool is licensed to execute."""
        return True

    def updateParameters(self, parameters):
        """Modify the values and properties of parameters before internal
        validation is performed.  This method is called whenever a parameter
        has been changed."""
        return

    def updateMessages(self, parameters):
        """Modify the messages created by internal validation for each tool
        parameter.  This method is called after internal validation."""
        return

    def execute(self, parameters, messages):
        aT = Toolbox()
        aT.Procedure2('something else')
            
        del aT
        
        return&lt;/LI-CODE&gt;&lt;P&gt;flows.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 24 Feb 2021 23:26:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-toolbox-question/m-p/1030279#M60104</guid>
      <dc:creator>ChrisHolmes</dc:creator>
      <dc:date>2021-02-24T23:26:30Z</dc:date>
    </item>
    <item>
      <title>Re: Python Toolbox Question</title>
      <link>https://community.esri.com/t5/python-questions/python-toolbox-question/m-p/1030357#M60110</link>
      <description>&lt;P&gt;I think Jeff meant something like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy


class Toolbox(object):

    def __init__(self):
        """Define the toolbox (the name of the toolbox is the name of the
        .pyt file)."""
        self.label = ""
        self.alias = ""

        # List of tool classes associated with this toolbox
        self.tools = [SomeTool, AnotherTool,]
        


class BaseTool(object):
    # This class contains all methods that are used by multiple tools in the toolbox.

    def Procedure1(self, parameter1, parameter2):
        #This is a procedure used by multiple tools in the pyt file.
        #code goes here...
        
    def Procedure2(self, parameter1):
        #This is a procedure used by multiple tools in the pyt file.
        #code goes here...
        self.Procedure3('something')
        
    def Procedure3(self, parameter1):
        #This is a procedure used by Procedure2
        #code goes here...


class SomeTool(BaseTool):  # this class inherits every method from BaseTool
    
    def __init__(self):
        """Define the tool (tool name is the name of the class)."""
        self.label = "Some Tool"
        self.description = " "
        self.canRunInBackground = False

    def getParameterInfo(self):
        """Define parameter definitions"""
        params = None
        return params

    def isLicensed(self):
        """Set whether tool is licensed to execute."""
        return True

    def updateParameters(self, parameters):
        """Modify the values and properties of parameters before internal
        validation is performed.  This method is called whenever a parameter
        has been changed."""
        return

    def updateMessages(self, parameters):
        """Modify the messages created by internal validation for each tool
        parameter.  This method is called after internal validation."""
        return

    def execute(self, parameters, messages):
        """The source code of the tool."""
        
        # BaseTool contains Procedure1 and Procedure2, and SomeTool inherits them, so you can just call them with self.Procedure1
        aVariable = self.Procedure4('blah')
        self.Procedure1(aVariable,'something else')
        self.Procedure2('something else')


    def Procedure4(inParameter):
        #This procedure is just for SomeTool
        #code goes here...



class AnotherTool(BaseTool):
    def __init__(self):
        """Define the tool (tool name is the name of the class)."""
        self.label = "Another Tool"
        self.description = " "
        self.canRunInBackground = False

    def getParameterInfo(self):
        """Define parameter definitions"""
        params = None
        return params

    def isLicensed(self):
        """Set whether tool is licensed to execute."""
        return True

    def updateParameters(self, parameters):
        """Modify the values and properties of parameters before internal
        validation is performed.  This method is called whenever a parameter
        has been changed."""
        return

    def updateMessages(self, parameters):
        """Modify the messages created by internal validation for each tool
        parameter.  This method is called after internal validation."""
        return

    def execute(self, parameters, messages):
        self.Procedure2('something else')&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Feb 2021 06:36:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-toolbox-question/m-p/1030357#M60110</guid>
      <dc:creator>JohannesLindner</dc:creator>
      <dc:date>2021-02-25T06:36:50Z</dc:date>
    </item>
    <item>
      <title>Re: Python Toolbox Question</title>
      <link>https://community.esri.com/t5/python-questions/python-toolbox-question/m-p/1030738#M60128</link>
      <description>&lt;P&gt;Thanks Johannes. I've adjusted my example file. It's cleaner, executes about the same as before. But makes better sense. Cheers&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# -*- coding: utf-8 -*-

class Toolbox(object):
    import arcpy
    
    def __init__(self):
        """Define the toolbox (the name of the toolbox is the name of the
        .pyt file)."""
        self.label = ""
        self.alias = ""

        # List of tool classes associated with this toolbox
        self.tools = [SomeTool, AnotherTool]
        
class BaseTool(object):
    import arcpy
    
    def __init__(self):
        """Define the toolbox (the name of the toolbox is the name of the
        .pyt file)."""
        self.label = ""
        self.alias = ""

        # List of tool classes associated with this toolbox
        self.tools = [SomeTool, AnotherTool]
        
    def Procedure1(self, parameter1, parameter2):
        #This is a procedure used by multiple tools in the pyt file.
        #code goes here...
        arcpy.AddMessage('hi from Procedure1. parameter1: {}; parameter2: {}'.format(parameter1,parameter2))
        
    def Procedure2(self, parameter1):
        #This is a procedure used by multiple tools in the pyt file.
        #code goes here...
        self.Procedure3(parameter1)
        
    def Procedure3(self, parameter1):
        #This is a procedure used by multiple tools in the pyt file.
        #code goes here...
        arcpy.AddMessage('message from Procedure2: {}'.format(parameter1))
    

class SomeTool(BaseTool):
    
    def __init__(self):
        """Define the tool (tool name is the name of the class)."""
        self.label = "Some Tool"
        self.description = " "
        self.canRunInBackground = False

    def getParameterInfo(self):
        """Define parameter definitions"""
        params = None
        return params

    def isLicensed(self):
        """Set whether tool is licensed to execute."""
        return True

    def updateParameters(self, parameters):
        """Modify the values and properties of parameters before internal
        validation is performed.  This method is called whenever a parameter
        has been changed."""
        return

    def updateMessages(self, parameters):
        """Modify the messages created by internal validation for each tool
        parameter.  This method is called after internal validation."""
        return

    def execute(self, parameters, messages):
        """The source code of the tool."""
        def Procedure4(inParameter):
            #This procedure is just for SomeTool
            return('phrase passed into Procedure4: {}'.format(inParameter))
            #code goes here...
            
        aVariable = Procedure4('blah')
        
        self.Procedure1(aVariable,'something else')
        self.Procedure2('hi there')
        
        return

class AnotherTool(BaseTool):
    def __init__(self):
        """Define the tool (tool name is the name of the class)."""
        self.label = "Another Tool"
        self.description = " "
        self.canRunInBackground = False

    def getParameterInfo(self):
        """Define parameter definitions"""
        params = None
        return params

    def isLicensed(self):
        """Set whether tool is licensed to execute."""
        return True

    def updateParameters(self, parameters):
        """Modify the values and properties of parameters before internal
        validation is performed.  This method is called whenever a parameter
        has been changed."""
        return

    def updateMessages(self, parameters):
        """Modify the messages created by internal validation for each tool
        parameter.  This method is called after internal validation."""
        return

    def execute(self, parameters, messages):
       
        self.Procedure2('whatever')
        
        return&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 25 Feb 2021 21:30:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-toolbox-question/m-p/1030738#M60128</guid>
      <dc:creator>ChrisHolmes</dc:creator>
      <dc:date>2021-02-25T21:30:44Z</dc:date>
    </item>
  </channel>
</rss>

