2

Using the example below, both ToolOne and ToolTwo have two parameters each, and are independent functions in their own right, but also form the basis for a chain of functions for an output. I am trying to call ToolOne function, and then ToolTwo function within the MainTool function, however I keep running into issues with the number of parameters.

As far as I am aware, once I have done tOne = ToolOne() I believe that is the self argument taken care of, then I have two remaining parameters. I cannot find anything on how the other two parameters should be passed; maybe a list data type for the parameter and I am not sure at all for the messages parameter.

I would post an example of errors, however I don't have any internet connectivity on my development system.

import arcpy, os


class Toolbox(object):
    def __init__(self):
        """Define the toolbox (the name of the toolbox is the name of the .pyt file)."""
        self.label = "Lorem Ipsum"
        self.alias = "LoremIpsum"
        self.description = "Some dummy text for now"

        # List of tool classes associated with this toolbox
        self.tools = [
            ToolOne,
            ToolTwo,
            MainTool
        ]


class ToolOne(object):
    def __init__(self):
        """Define the tool (tool name is the name of the class)."""
        self.label = "Tool One"
        self.canRunInBackground = False

    def getParameterInfo(self):
        """Define parameter definitions"""
        [...]
        params = [param0, param1]
        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."""

        # Specific code to execute tool

        return


class ToolTwo(object):
    def __init__(self):
        """Define the tool (tool name is the name of the class)."""
        self.label = "Tool One"
        self.canRunInBackground = False

    def getParameterInfo(self):
        """Define parameter definitions"""
        [...]
        params = [param0, param1]
        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."""

        # Specific code to execute tool

        return


class MainTool(object):
    def __init__(self):
        """Define the tool (tool name is the name of the class)."""
        self.label = "Tool One"
        self.canRunInBackground = False

    def getParameterInfo(self):
        """Define parameter definitions"""
        [...]
        params = [param0, param1]
        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."""

        # How to execute tool one and the tool two

        return
1

With classes in python you instantiate them so you create an object. The object then provides you access to all the methods\functions within that class. You can create static methods that do not need an object to access the function within a class. Methods within a class that do not have @staticmethod flagged before it will need an object created which creates self. For example:

class ToolOne(object):
    def __init__(self):
        """Define the tool (tool name is the name of the class)."""
        self.label = "Tool One"
        self.canRunInBackground = False
    def WithinObjectFunction(self):
        """Define the tool (tool name is the name of the class)."""
        print("Do Something in Tool One")
class ToolTwo(object):
    def __init__(self):
        """Define the tool (tool name is the name of the class)."""
        self.label = "Tool One"
        self.canRunInBackground = False
    def WithinObjectFunction(self):
        """Define the tool (tool name is the name of the class)."""
        print("Do Something in Tool Two")
    @staticmethod
    def PublicFunction():
        print("This Function From Tool Two Does not need an object")
class MainTool(Object):
    def __init__(self):
        self.ToolOneObject = ToolOne()
        self.ToolTwoObject = ToolTwo()
        self.ToolOneObject.WithinObjectFunction() #prints out something
        self.ToolTwoObject.WithinObjectFunction() #prints out something
        ToolTwo.PublicFunction() #does not need an object or the self parameter
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.