Setting a script output as model variable

564
3
Jump to solution
10-25-2013 12:06 PM
BenjaminBauman
Occasional Contributor
Hello all,

I have a script that renames a file with a generic name to one reflecting its week ending date. In addition to the rename process, the script also outputs a string that is to be used as a model variable. This string is the prefix portion of the renamed file name (e.g., clm20130914). If this string can be set as a model variable, then all the remaining steps of the model will name the intermediate and final output files accordingly. The problem is, I cannot seem to assign a script's string output as a string variable within the model. Does anyone have any insight on how to do this? The line containing
arcpy.SetParameterAsText(2, i[4])
should assign i[4] as the third parameter, correct? I have assigned the third parameter as a derived output. The following is the script for the model's rename portion, and attached is a graphic of the model. Any help would be greatly appreciated.

import datetime,csv,sys,os,arcpy from datetime import date  todays_date = datetime.date.today()  todays_date = todays_date.strftime("%Y %m %d") todays_date = todays_date.split() todays_date = map(int, todays_date) todays_date = date(todays_date[0], todays_date[1], todays_date[2])  calendar = csv.reader(open(sys.argv[1]))  calendar.next() calendar.next() calendar.next()  l = reversed(list(calendar))  for i in l:     try:         file_date = i[0].split('/')         file_date = '20' + file_date[2] + ' ' + file_date[0] + ' ' + file_date[1]         file_date = file_date.split()         file_date = tuple(map(int, file_date))         file_date = date(file_date[0], file_date[1], file_date[2])         if todays_date >= file_date:             current_file = i[4] + '.ides.csv'             arcpy.SetParameterAsText(2, i[4])             break     except IndexError:         pass          rename_file = os.listdir('W:\WDIA_Production\Geocoding_Files')[0]  os.rename(os.path.join(sys.argv[2], rename_file),os.path.join(sys.argv[2],current_file))
0 Kudos
1 Solution

Accepted Solutions
BenjaminBauman
Occasional Contributor
I'm pretty sure I've got my answer. Seeing as how I cannot figure out how to create a model variable from a script output using Model Builder, I instead triggered the model using the very same script that generated the variable. Then the variable was used as the model parameter, as follows:

import arcpy  arcpy.ImportToolbox(r"W:\WDIA_Production\Toolbox.tbx", "TBX")  arcpy.ClaimantAddressGeocoding_TBX(variable)


This works perfectly, because I want a script to call the model anyway, so that it can be used within Windows Task Scheduler.

View solution in original post

0 Kudos
3 Replies
curtvprice
MVP Esteemed Contributor
I would put some debugging code in your python script. so a) you're sure you are getting to that line and b) the value is being set to something you want.  My guess as to what is happening here is you are raising an error inside your try block and never get to the line with the SetParameterAsText. Your try block is only handling indexErrors, any other error will just be ignored and processing continues after the try/except.

The practice I usually do is run the SetParameterAsText at the very end, after I know everything ran okay.

(The repr() function is very handy when you aren't sure exactly what is there, you're guranteed to get an informative string representation of the variable.)

arcpy.AddMessage("returning {0}".format(repr(i[4]))
arcpy.SetParameterAsText(2, i[4])


BTW, your path is specified incorrectly, you need to specify a raw string to avoid issues with back-slashes, in case you specify a folder that begins with the letters t or r:
rename_file = os.listdir(r'W:\WDIA_Production\Geocoding_Files')[0]
0 Kudos
BenjaminBauman
Occasional Contributor
Thanks for the reply, Curtis. The script works fine outside of the model, so I'm confident that it gets to the line with the return statement. I tried your suggestion though, and placed it outside of the loop, but received the same error message.
With regard to the line which calls the file, that should actually read sys.argv[1], since it should function as an input parameter.
I've seen suggestions for something related that states placing the Python script in a Calculate Value tool, and am in the midst of trying to create a model variable from that instead.
Another possibility - I want to run the model as a scheduled task by calling it with Python and triggering it routinely through Windows Task Scheduler. Perhaps the Python script that calls the model can also allow input of a model parameter (this being the variable derived from i[4] in the script)? Information for running the model as a scheduled task can be found here, but there is no mention of feeding in any model parameters - although I'm sure it can be done.
0 Kudos
BenjaminBauman
Occasional Contributor
I'm pretty sure I've got my answer. Seeing as how I cannot figure out how to create a model variable from a script output using Model Builder, I instead triggered the model using the very same script that generated the variable. Then the variable was used as the model parameter, as follows:

import arcpy  arcpy.ImportToolbox(r"W:\WDIA_Production\Toolbox.tbx", "TBX")  arcpy.ClaimantAddressGeocoding_TBX(variable)


This works perfectly, because I want a script to call the model anyway, so that it can be used within Windows Task Scheduler.
0 Kudos