how do i use a list inserted with arcpy.GetParameterAsText() within a codeblock function in calculate field ?

1021
3
08-10-2019 11:25 PM
NadavBronshtein
New Contributor

I'm trying to build a tool based on script in arcgis pro (the tool makes changes to a field with calculate field based on a list of variables). how do i use a list inserted with arcpy.GetParameterAsText() within a codeblock function in calculate field ?

my problem is that it does not recognize my list: "NameError: name 'surv_list' is not defined"

This is my script:

import arcpy

arcpy.env.overwriteOutput = True
arcpy.env.workspace = r"C:\GIS\project_prebuild.gdb"

# Exmaple fot text input to run tool in python:
##surv_list = (('FT18-17',235,300),('FT18-17',410,432),('FT18-15',101,130),('FT18-13',101,140))

surv_list = arcpy.GetParameterAsText(0)
record_list = arcpy.GetParameterAsText(1)

codeblock = ("""def update_status(line,point,surv_list,record_list):
if len(surv_list[0])==3:
for x in surv_list:
if line == x[0] and point >= x[1] and point <= x[2]:
return 1
elif line == surv_list[0] and point >= surv_list[1] and point <= surv_list[2]:
return 1

if len(record_list[0])==3:
for y in record_list:
if line == y[0] and point >= y[1] and point <= y[2]:
return 2
elif line == record_list[0] and point >= record_list[1] and point <= record_list[2]:
return 2""")

arcpy.CalculateField_management("REC_Preplot_25m","Status","update_status(!LineNumber!,!Station!,surv_list,record_list)","PYTHON3",codeblock)

0 Kudos
3 Replies
DanPatterson_Retired
MVP Emeritus

Use a custom python tool and attach it to a toolbox.  Get your parameters first then use them in the code block

Adding a script tool—Geoprocessing and Python | ArcGIS Desktop 

Use your surv_list as a "value list" under the "filter" option.

Setting script tool parameters—Geoprocessing and Python | ArcGIS Desktop 

It would be a Text parameter, direction "input"

The same for your other list.

The inputs can then be fed into your code block.

0 Kudos
NadavBronshtein
New Contributor

Thanks for replying. I did all the thing but didn't understand the "use of surv_list as a "value list" under the "filter" option". my attempt is to enter a random list to the tool in the form of:(name,from_point,to_point), or list of lists: ((name, from_point, to_point),(name2, from_point2, to_point2)) and than use these 3 variable of each list in the code block to return a value if i have a match.
i might have other problems with my code but for start, i cant get the list i enter in the tool to be identified in the code block. any suggestions ?

Thanks in advanced

0 Kudos
DanPatterson_Retired
MVP Emeritus

You enter the value list manually

0 Kudos