Correct Codeblock Python syntax for CalculateField

1311
11
Jump to solution
01-09-2023 08:12 AM
MPach
by
Occasional Contributor II

 I'm using ArcMap 10.8.1 so this is Python 2.7.

  I'm trying to find the correct syntax to troubleshoot/debug this script that I'm working on. I was using the Python interpreter going line by line trying to resolve the issue and can't seem to figure out how to enter the codeblock into the interpreter. When I try to enter the codeblock as is I get a Syntax Error and I'm not skilled enough to know how to resolve it yet. It seems like the interpreter doesn't like the triple quotes for the codeblock. Any help is greatly appreciated!

 

 

expression = 'reclass(!{0}!,!{1}!,{2})'.format(lengthField, widthField, num)

codeblock = """ def reclass(lengthField,widthField,num):
                   if ((lengthField/num) >= (widthField)):
                       return 'True'
                   else:
                       return '' """

 

0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable

I don't think you can have leading and trailing spaces in the string since the code block needs valid python and the space is offsetting the indents.

expression = 'reclass(!{0}!,!{1}!,{2})'.format(lengthField, widthField, num)

codeblock = """def reclass(lengthField,widthField,num):
                   return 'True' if (lengthField/num) >= widthField else ''"""

View solution in original post

0 Kudos
11 Replies
BlakeTerhune
MVP Regular Contributor
0 Kudos
MPach
by
Occasional Contributor II

I saw that and thought I just deleted one. It popped an error message when I first clicked the submit button. 

0 Kudos
by Anonymous User
Not applicable

I don't think you can have leading and trailing spaces in the string since the code block needs valid python and the space is offsetting the indents.

expression = 'reclass(!{0}!,!{1}!,{2})'.format(lengthField, widthField, num)

codeblock = """def reclass(lengthField,widthField,num):
                   return 'True' if (lengthField/num) >= widthField else ''"""
0 Kudos
MPach
by
Occasional Contributor II

Thank Jeff, let me give that a shot. Hadn't even crossed my mind. 

 

0 Kudos
MPach
by
Occasional Contributor II

That was the problem. The space I had after the """.  Those things will drive you nuts if you don't see them. Thank you @Anonymous User 

0 Kudos
KimGarbade
Occasional Contributor III

The syntax you are debugging is specific to using field calculator in Pro or ArcMap when updating a table's field (using the Python Expression Type).  You could try entering it there and testing it (I.E. the expression in the top box and the code block below like this) 

KimGarbade_0-1673282672307.png

I debugged it using Python in Spyder like this (since python doesn't understand the syntax "CodeBlock" as far as I know, and the code block itself is just a python function.):

 

# -*- coding: utf-8 -*-
"""
Created on Mon Jan  9 11:17:59 2023

@author: Tester
"""

lengthField = 10
widthField = 5
num = 2

expression = "reclass(!%d!,!%d!,!%d!)" % (lengthField, widthField, num)


def reclass(lengthField,widthField,num):
                   if ((lengthField/num) >= (widthField)):
                       return 'True'
                   else:
                       return ''
                   
print (expression)
print (reclass(lengthField,widthField,num))

 

  The result was 

KimGarbade_2-1673283097760.png

I hope I understood you question correctly and that this is helpful.

K

0 Kudos
MPach
by
Occasional Contributor II

Thanks Kim, I hope my other explanations helped out. I appreciate the help. 

Mark

0 Kudos
Brian_Wilson
Occasional Contributor III

You must be using a feature I don't know about. I can see you are using ArcMap and Calculate Field but I am not sure where the strings are coming from. Send screenshots? Especially the error message.

When I save a field calc to a file it creates a *.cal file that looks like this

reclass(!{0}!,!{1}!,{2})'.format(lengthField, widthField, num)
__esri_field_calculator_splitter__
def reclass(lengthField,widthField,num):
                   if ((lengthField/num) >= (widthField)):
                       return 'True'
                   else:
                       return '' """

 

In any case if I wanted to look at the syntax I would pull the code out of there and put it into a debugger, I go on and on about the wonders of Visual Studio Code but in your case you already have "Idle" installed so use that.

Then you can paste your code in there and play with it.

The first comment I have is that you PROBABLY meant something like this.

# This returns True or False
def reclass(lengthField, widthField, num):
	return lengthField/num >= widthField

# Your code returns strings, which could work but will end up being confusing.
return 'True'
# is not the same as
return True

 

Here is a screenshot of my little test in Idle. After 1 minute of using Idle I want to use something else. 🙂

I already mentioned Visual Studio Code but a lot of people here like to use "Spyder" which is also good.

Screenshot 2023-01-09 085420.png

I just did a quick check of docs for ArcGIS Desktop and on the Esri learning site and it looks like they have taken down anything useful for ArcMap. Maybe I am totally wrong and if so someone please correct me and put a link here for users who need learning resources in ArcMap??? The links I saw in the ArcMap docs just pointed at the generic Python.ORG site which is a really difficult learning path.

 

If you can find a copy of this book: "Python Scripting for ArcGIS" by Paul A. Zandbergen, it would be useful but geez, Amazon wants $60 for it, at this point in time it's worth $5. I probably threw away my copy already.

With ArcGIS Pro using Python is easier in many ways, but I don't know your circumstances so that's up to you.

 

MPach
by
Occasional Contributor II

I was using the python interpreter tool for ArcMap. I attached a screen shot for you below. I was creating a script for some automation and was getting a generic syntax error when from the geoprocessing results window, so I was going line by line trying to troubleshoot it rather than running the tool over and over again with a few code changes each time I wanted to be able to change the one line I knew I was having a problem with for a quicker easier fix. Nothing fancy, I just knew I had a syntax problem with a specific line of code and couldn't figure out what the problem was. 

Thank you for the very thorough response though, it was much appreciate. I have the book you're referring to and another by John Zelle that is also excellent too "Python Programming: An Introductory into Computer Science" 2nd Ed. I used to be pretty proficient with Python, but after not using it for a bunch of years b/c of a job change I'm kind of starting over again. I'm slightly over most of baby steps, but still making the baby step mistakes ☹️

MPach_0-1673294972429.png

 

0 Kudos