[GIS] Running Python toolbox tool both standalone from other tools within same Python toolbox

arcgis-10.1arcpypythonpython-toolbox

I have created a Python toolbox for work that has two tools inside. These tools use the toolbox template and so have a init, getParameterInfo, etc.

I want to be able to run one tool both standalone and call it inside other tools within that toolbox. I can't seem to get the parameters correct though.

class foo(object)
    def __init__(self)
        #stuff
    def getParameterInfo(self):
        # list parameters for UI tool
    def execute(self, parameters, messages)
        print parameter[0]
        return

class bar(object)
    def __init__(self)
        #stuff
    def getParameterInfo(self):
        # list parameters for UI tool
    def execute(self, parameters, messages)
        foo("hello, world)
        return

I have tried adding a parameter to the init(self, parameter) or the foo class but I can't get it to work.

I am new to Object-Oriented Programming (OOP) and ArcGIS in general.

Best Answer

The simplest option is to have your execute method call a function instead of doing the actual processing. This makes it easily callable by any tool.

class Foo(object)
    def __init__(self)
        #stuff
    def getParameterInfo(self):
        # list parameters for UI tool
    def execute(self, parameters, messages)
        somefunc(parameters[0].value, parameters[1].value)

class Bar(object)
    def __init__(self)
        #stuff
    def getParameterInfo(self):
        # list parameters for UI tool
    def execute(self, parameters, messages):
        somefunc(parameters[0].value, parameters[1].value)
        anotherfunc(parameters[2].value, parameters[3].value)
        return

def somefunc(arg1, arg2):
    #do something
    return

def anotherfunc(arg1, arg2):
    #do something else
    return

If you want those functions contained in the tool classes:

class Foo(object)
    def __init__(self)
        #stuff
    def getParameterInfo(self):
        # list parameters for UI tool
    def execute(self, parameters, messages)
        self.somefunc(parameters[0].value, parameters[1].value)
    def somefunc(self, arg1, arg2):
        #do something
        return

class Bar(object)
    def __init__(self)
        #stuff
    def getParameterInfo(self):
        # list parameters for UI tool
    def execute(self, parameters, messages):
        foo = Foo()
        foo.somefunc(parameters[0].value, parameters[1].value)
        self.anotherfunc(parameters[2].value, parameters[3].value)
        return
    def anotherfunc(self, arg1, arg2):
        #do something else
        return