ToolValidator value.value,  out of date documentation -

1892
1
04-25-2014 06:28 AM
LT
by
Occasional Contributor
Hi All,

the ToolValidator documentation says:

As a Value object does not support string manipulation, use the Value object's value property whenever a string is to be manipulated or parsed. The code sample uses the os.path.dirname method to return the directory from a dataset.
if self.params[0].value:
  workspace = os.path.dirname(self.params[0].value.value)



However, the following works fine:
workspace = os.path.dirname(self.params[0].value)


My question are,

1. Has anyone encountered a situation where they DID have to use self.params[0].value.value and self.params[0].value wouldn't work?

2. Is this working because I'm running 10.2?  Does it work for those of you that have 9.3, 10.0, and 10.1? 

I have a dummy script tool with 1 param. The code below is for debugging outside the script tool.  It also works inside the script tool (I set a second param to that value to check it in the script tool). Here's a complete script to test this:

import arcpy, os

arcpy.ImportToolbox(r"C:\temp\sandbox.tbx")
params = arcpy.GetParameterInfo("myScriptTool")
params[0].value = "C:/Temp/data.shp"

class ToolValidator:

  def __init__(self):
    import arcpy 
    self.params = arcpy.GetParameterInfo()

  def initializeParameters(self):
    # (initializeParameters code here)
    return

  def updateParameters(self):
    # (updateParameters code here)
    print os.path.dirname(self.params[0].value)
    return

  def updateMessages(self):
    # (updateMessages code here)
    return

# Call routine(s) to debug
#
validator = ToolValidator()
validator.updateParameters()
validator.updateMessages()


It runs successfully and prints:
C:/Temp

The documentation is about midway through the following page:
http://resources.arcgis.com/en/help/main/10.2/index.html#/Programming_a_ToolValidator_class/00150000...
Tags (2)
0 Kudos
1 Reply
LT
by
Occasional Contributor
This documentation seems to apply to Python toolbox code, but not to script tools.

.value.value  seems to be necessary within Python toolbox tools in some cases, but not in others.  Here's an example where the CalculateField tool would not work until I used value.value:
def execute(self, parameters, messages):
        # From combineFields.py
        # Purpose: Create a new field that is the sum of two existing fields.
        dataset = parameters[0].value.value
        field1 = parameters[1].value
        field2 = parameters[2].value
        newfield = parameters[3].value
        arcpy.AddField_management(dataset, newfield)
        expression = '!{0}!+!{1}!'.format(field1, field2)
        arcpy.CalculateField_management(dataset, newfield, expression, 'PYTHON')
        
        arcpy.SetParameterAsText( 4,dataset)

And another example (like the documentation) where os.path.dirname won't work without the double 'value' (though the linear distance works fine as a value object passed into the buffer tool):
        fileToBuffer = parameters[0].value.value
        distance = parameters[1].value
        arcpy.env.workspace = os.path.dirname(fileToBuffer) 
        outputFile = os.path.splitext(fileToBuffer)[0] + 'Buff'
        
        arcpy.Buffer_analysis(fileToBuffer, outputFile, distance)
0 Kudos