Select to view content in your preferred language

working with Arcpy script and multiple imbricated python functions

187
3
2 weeks ago
Anne-MarieDubois
Occasional Contributor

Hello everyone,

I'm trying to build an arcpy script with 3 layers of imbricated functions but I'm running a bit confused.

Here is an example of what I'm trying to achieve.

import arcpy
import blabla

#Functions
def function1(Table, Param)
    intermediateTable = do something with Table and Param 

def function2(param1, table1, param2, table2):
    do something 1(param1, param2)
        new_param1
    do something 2(param1, param2)
        new_param2
    if param1 == "**unspecified**":
        intermediateTable = function1(table1, new_param1)
        newParam = new_param1
    elif param2 == "aaaa":
        intermediateTable = function1(table1, new_param1)
        newParam = new_param1
    else:
        intermediateTable = function1(table2, new_param2)
        newParam = new_param2

    do other stuff

def function3(param1, table1, param2, table2, param3, param4, newParam, intermediateTable): #This is where all params would be called
    create gdb
    ...
    function2(param1, table1, param2, table2)
    ...
    do something with param3, param4, newParam, intermediateTable to create finalTable

"""
<!DOCTYPE html>
<head>
    do something with finalTable...
</head>
</html>
"""

if __name__=="__main__":
    #Global Environment settings
    with arcpy.EnvManager():
        function3(*argv[1:])

I can't seem to making it work. What am I doing wrong?

But weirdly, if all my functions are built into one script (aka, no functions, just repeating the code), it does not seem to need the line to "call the function"... or is it what the "if__name__ ...etc" is for?

Thanks!

Tags (3)
0 Kudos
3 Replies
DanPatterson
MVP Esteemed Contributor

If you just run a script all the imports and functions are read and put into python's namespace..

when it hits line 40 python will find that __name__ does indeed == __main__

not sure why you need EnvManager, so I will leave that

and the last line is the call to function3 with a list of unspecified arguments which should be called by sys.argv (since sys.argv[0] is the name of the running script)

It would be useful if the functions did something or reported something (eg print statments ) or returned something (even a blank return statment)


... sort of retired...
0 Kudos
Anne-MarieDubois
Occasional Contributor

Thanks @DanPatterson for the quick answer. However, I'm not sure if this answers all my questions. 

Basically, are you saying that the "if__name__ ..." part is indeed what replaces the calling function part of a normal script?

As for the EnvManager, I believe it might be an artefact from a previously builder converted to python script.

So the amended script should be something like that?

 

import arcpy
import blabla

#Functions
def function1(Table, Param)
    intermediateTable = do something with Table and Param
    print("Intermediate table created")

def function2(param1, table1, param2, table2):
    do something 1(param1, param2)
        new_param1
    do something 2(param1, param2)
        new_param2
    if param1 == "**unspecified**":
        intermediateTable = function1(table1, new_param1)
        newParam = new_param1
    elif param2 == "aaaa":
        intermediateTable = function1(table1, new_param1)
        newParam = new_param1
    else:
        intermediateTable = function1(table2, new_param2)
        newParam = new_param2

    do other stuff
    print("New param created")

def function3(param1, table1, param2, table2, param3, param4, newParam, intermediateTable): #This is where all params would be called
    create gdb
    ...
    function2(param1, table1, param2, table2)
    ...
    do something with param3, param4, newParam, intermediateTable to create finalTable
    print("Final table completed")

"""
<!DOCTYPE html>
<head>
    do something with finalTable...
</head>
</html>
"""

if __name__=="__main__":
    function3(sys.argv[1:])

 

0 Kudos
DanPatterson
MVP Esteemed Contributor

work with something simpler until you get the hang of what you want the output to be

# -*- coding: utf-8 -*-
"""
Created on Mon Jun 17 11:59:13 2024.

@author: dan_p
"""

import sys

script = sys.argv[0]


def f0(p0):
    """Call function 0."""
    print("f0: {}".format(p0))
    return p0 + "b"


def f1(p1):
    """Call function 1."""
    print("f1: {}".format(p1))
    return p1 + "c"


def f2(p2):
    """Call function 2."""
    print("f2: {}".format(p2))
    return p2 + "d"


def main():
    """Start the process."""
    val = "a"
    ret0 = f0(val)
    ret1 = f1(ret0)
    ret2 = f2(ret1)
    return "{} {} {}".format(ret0, ret1, ret2)


# ---- ---------------------------
# ---- Final main section
if __name__ == "__main__":
    """optional location for parameters"""
    print(f"\nRunning... {script}\n")
    final = main()
    print("final = {}".format(final))

Sample run

Running... c:\arcpro_npg\npg\tests\test.py

f0: a
f1: ab
f2: abc
final = ab abc abcd

... sort of retired...
0 Kudos