3

I am trying to make python toolbox. I used a converter to convert regular toolbox to python toolbox. It did the job, but I had to fix the code manually in some parts. When I did that, syntax was fine, but drop-down menu for selecting fields was missing. I managed to fix it, and get the menu. Code in which I get an error is working fine in regular toolbox, but not here. Here is the full code:

EDIT Whatever I do, I always get some kind of error in the "def execute" part of the code. I defined variables, and fixed the previous error. Error is for sure there in def execute part. This is the new code:

import arcpy

class Toolbox(object):
    def __init__(self):
        self.label = "Alat"
        self.alias = "Alat_replace"
        self.tools = [Alatko]

class Alatko(object):
    def __init__(self):
        self.label = "Label alata"
        self.description = "Insert znaka"
        self.canRunInBackground = True

    def getParameterInfo(self):
        # First parameter
        in_features = arcpy.Parameter(
            displayName="Input Features",
            name="in_features",
            datatype="Feature Layer",
            parameterType="Required",
            direction="Input")
        in_features.filter.list = ["Point"]

        # Second parameter
        field = arcpy.Parameter(
            displayName="Field Name",
            name="field_name",
            datatype="Field",
            parameterType="Required",
            direction="Input")

        field.parameterDependencies = [in_features.name]
        field.filter.list = ["Short","Long","Double","Float","Text"]

        # Third parameter
        sql = arcpy.Parameter(
            displayName="SQL_expres",
            name="SQL_Expression",
            datatype="SQL Expression",
            parameterType="Optional",
            direction="Input")

        sql.parameterDependencies = [in_features.name]

        # Fourth parameter
        Character = arcpy.Parameter(
            displayName="Charcters",
            name="Char_string",
            datatype="String",
            parameterType="Required",
            direction="Input")

        # Fifth parameter
        Position = arcpy.Parameter(
           displayName="PositionNR",
           name="Position",
           datatype="String",
           parameterType="Required",
           direction="Input")

        # Sixth parameter
        out_features = arcpy.Parameter(
           displayName="Output Features",
           name="out_features",
           datatype="Feature Layer",
           parameterType="Derived",
           direction="output")

        out_features.parameterDependencies = [in_features.name]

        params = [in_features, field, sql, Character, Position, out_features]
        return params

    def isLicensed(self):
        return True

    def updateMessages(self, parameters):
        in_features = parameters[0]
        field = parameters[1]
        sql = parameters[2]
        Character = parameters[3]
        Position = parameters[4]
        out_features = parameters[5]

    def execute(self, parameters, messages):
        in_features = parameters[0].valueAsText
        field = parameters[1].valueAsText
        sql = parameters[2].valueAsText
        Character = parameters[3].valueAsText
        Position = parameters[4].valueAsText
        out_features = parameters[5].valueAsText
        Position = int(Position)
        if Character == "#":
           cur = arcpy.UpdateCursor(table)
        else:
           cur = arcpy.UpdateCursor(table, Character)
        row = cur.next()
        while row:
            value = row.getValue(field)
            newValue = value[:Position] + Character + value[Position:]
            row.setValue(field, newValue)
            cur.updateRow(row)
            row = cur.next()
        del cur, row

The code imports specific characters into the field in desired position.

This is the window for input: feature class for input class Field name for field name I want to insert character sql for optional selection Character is for character I want to put in and positionNR is te number of position i want to put my character in

this is the exampleenter image description here

and this is the error

New error is:

Traceback (most recent call last): File "", line 100, in execute NameError: global name 'table' is not defined

11
  • 2
    If you can develop a code snippet (in this case a test.pyt) that targets figuring out how "to get drop-down list to select fields from the table" (and perhaps just printing its name) then once you solve that you should be able to plug it back into your actual tool.
    – PolyGeo
    Sep 23, 2016 at 22:52
  • 2
    Can you also add what parameters you are providing when you run it (with sensitive/personal info removed is fine)? This looks like the value of 'f' was given for your 'PositionNR' parameter. Sep 27, 2016 at 17:57
  • 2
    @EvilGenius, you are right, look at Position = parameters[3].valueAsText it's pulling from the Character field. Dean when you added the sql parameter you didn't update the indexing in the execute part only in the updateMessages part.
    – Brian
    Sep 27, 2016 at 18:12
  • 1
    Shouldn't table be your first parameter, in_features? That is the feature class you are trying to update?
    – Brian
    Sep 27, 2016 at 18:23
  • 2
    It's failing on your where clause (Character variable). You need to build a where clause with that character and pass that as the second argument. See here on Building SQL queries. Assuming that's what you are trying to do, are you wanting to filter records that the update cursor returns based on that character value?
    – Brian
    Sep 27, 2016 at 18:59

1 Answer 1

1

in your code you are defining a variable in_features which is the the path to the feature class. I think this is what you want to use to open the cursor.

def execute(self, parameters, messages):
    in_features = parameters[0].valueAsText
    ...
    ...
    if Character == "#":
       cur = arcpy.UpdateCursor(table)

try

cur = arcpy.UpdateCursor(in_features)

For your query, depending on your storage format and field data type, your format should be something like:

....
if Character == "#":
       cur = arcpy.UpdateCursor(in_features)
else:
       query = """{} LIKE '%{}%'""".format(field,Character)
       cur = arcpy.UpdateCursor(in_features, query)
3
  • I am a bit confused now. I did change update cursor. but where should I put query part?
    – Dean7
    Sep 27, 2016 at 19:29
  • 1
    ... else: query = ... cur = arcpy.UpdateCursor(table, query) Sep 27, 2016 at 19:35
  • Yeah, I figured it out. And YES, YES, YES. That's it. I didn't know that converting from regular toolbox to python toolbox could give such a headache.
    – Dean7
    Sep 27, 2016 at 19:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.