autoIncrement block not working within a Python Toolbox?

830
6
Jump to solution
05-21-2022 04:24 PM
DPG
by
New Contributor II

Hey guys. I am creating a python toolbox that assigns selected polygons in a shapefile a unique id. This id features a section of string followed by a sequential ID. However, whenever I insert the following code block for autoIncrement into my script, the tool will no longer launch. This is the autoincrement block I have taken from ArcGIS Resources:

 

        rec = 0
def autoIncrement():
 global rec
 pStart = 1 #adjust start value, if req'd 
 pInterval = 1 #adjust interval value, if req'd
 if (rec == 0): 
  rec = pStart 
 else: 
  rec = rec + pInterval 
 return rec

 

I am placing it in the " def execute(self, parameters, messages):" section of the python toolbox template.

 

Any ideas as to why this is causing my script to fail? Thank you!

0 Kudos
1 Solution

Accepted Solutions
DonMorrison1
Occasional Contributor III

Ah - now I see.  Lines 12  and 13 are indented incorrectly.

View solution in original post

6 Replies
DonMorrison1
Occasional Contributor III

It would be most helpful if you could post the error message, as well as the complete python file. I don't think there is anything wrong with the snippet you posted so there must be something else. Thanks

0 Kudos
DPG
by
New Contributor II

I'm afraid there is no error message. Where I can usually select the script (seen here as "Tool")...

DPG_0-1653177180142.png



... After pasting in that code block into the script, the option disappears as shown here:...

DPG_1-1653177236107.png

 

 

0 Kudos
DonMorrison1
Occasional Contributor III

If you right click on TestToolbox.pyt do you get an option to "Check Syntax"?

0 Kudos
DPG
by
New Contributor II

Here's the error:

DPG_0-1653178801474.png

 

For context, here's this part of the code:

 

    def execute(self, parameters, messages):

        # Sequential Formula

        rec = 0
        def autoIncrement():
            global rec
            pStart = 1 #adjust start value, if req'd
            pInterval = 1 #adjust interval value, if req'd
            if (rec == 0):
                rec = pStart
                else:
                    rec = rec + pInterval 
        return rec

 

Apologies for the lack of clarity. Line 112 is line 12 in my pasted code.

0 Kudos
DonMorrison1
Occasional Contributor III

Ah - now I see.  Lines 12  and 13 are indented incorrectly.

DPG
by
New Contributor II

Wow, how foolish of me. Thank you! Think I've been staring at code for too long.

0 Kudos