ERROR 000539: SyntaxError: invalid syntax

2248
4
Jump to solution
01-16-2018 09:53 AM
MikeChancey
New Contributor II

I am running Desktop 10.2.2.  I have been using model builder for years, creating models and exporting them to Python to automate tasks.  Recently, after a number of process failures due to broken connections, I have been trying to upgrade them to validate connections and results before continuing to the next steps.  What I wanted to do was simply check that a feature class was not empty before attempting to process it.  For test purposes I made a very simple model to check the row count on a polygon feature class and if it were greater than 0 copy it to a new feature class.  If the count was 0 it should stop.  Really simple.  

As a model it worked perfectly, so I exported it to Python.  That produced this:

# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------------
# TestModel.py
# Created on: 2018-01-16 11:41:45.00000
# (generated by ArcGIS/ModelBuilder)
# Description:
# ---------------------------------------------------------------------------

# Import arcpy module
import arcpy

# Load required toolboxes
arcpy.ImportToolbox("Model Functions")


# Local variables:
TestData = "C:\\Testing\\TestGeodatabase.gdb\\TestData"
TestPolys = "C:\\Testing\\TestGeodatabase.gdb\\TestData\\TestPolys"

# Process: Get Count
arcpy.GetCount_management(TestPolys)

# Process: Calculate Value
arcpy.CalculateValue_management("%Row Count% > 0", "", "Boolean")

# Process: Stop
arcpy.Stop_mb("true", "FALSE")

# Process: Feature Class to Feature Class
arcpy.FeatureClassToFeatureClass_conversion(TestPolys, TestData, "TestPolys2", "", "SHAPE_Length \"SHAPE_Length\" false true true 8 Double 0 0 ,First,#,C:\\Testing\\TestGeodatabase.gdb\\TestData\\TestPolys,SHAPE_Length,-1,-1;SHAPE_Area \"SHAPE_Area\" false true true 8 Double 0 0 ,First,#,C:\\Testing\\TestGeodatabase.gdb\\TestData\\TestPolys,SHAPE_Area,-1,-1", "")

Of course, that failed due to the IOError "The toolbox file Model Functions was not found", so I deleted the portion 

# Load required toolboxes
arcpy.ImportToolbox("Model Functions")

Now it generates this error:

Traceback (most recent call last):
File "C:\GeoProcessing\SWProcessingPythonSQL2\Scripts\TestModel.py", line 20, in <module>
arcpy.CalculateValue_management("%Row Count% > 0", "", "Boolean")
File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\management.py", line 3808, in CalculateValue
raise e
ExecuteError: ERROR 000539: SyntaxError: invalid syntax (<expression>, line 1)
Failed to execute (CalculateValue).

Why, why, why can't Model Builder export Python code that actually works?  What is the point of a WYSIWYG GUI that doesn't produce functional code?

So, something is wrong with the expression.  What exactly would that be since it works in Model Builder?  This should be easy.  All I want to do is validate that there is data to process before processing it.

0 Kudos
1 Solution

Accepted Solutions
EricaPfister
New Contributor III

Do you want to execute this as a Python script, or as a ModelBuilder tool? ModelBuilder is great as a starting point but (as you notice!) doesn't always make functional Python, let alone efficient/elegant scripts. Conditionals are a particularly kludgy workflow within ModelBuilder, in my experience.

You can do a fairly straightforward if/else conditional using the row count:

result = arcpy.GetCount_management(TestPolys)
count = int(result.getOutput(0))

if count > 0:
    arcpy.FeatureClassToFeatureClass_conversion(TestPolys, TestData, "TestPolys2")
else:
    print("there are no features in this feature class")‍‍‍‍‍‍‍‍‍‍‍‍‍‍

(Note that the FeatureClassToFeatureClass syntax can be much simpler and cleaner than ModelBuilder exports it as, too!)

You can also do an even simpler check to see whether a feature class (or table) even exists at all, but note the Exists tool won't catch empty feature classes.

if arcpy.Exists(TestPolys)
    # do something
else:
    print("this feature class doesn't exist")‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

4 Replies
DanPatterson_Retired
MVP Esteemed Contributor

CalculateValue is only for use in modelbuilder, you will have to replace it with a Calculate Field perhaps dumping the iterator stuff and putting it in a for loop if you need to iterate through objects

0 Kudos
MikeChancey
New Contributor II

I am wondering if this is a valid work flow at all.  What I am really looking for is an If Then functionality, if there are rows to process then process them.  If not stop.  Am I simply trying to use the wrong tools to do that?

0 Kudos
EricaPfister
New Contributor III

Do you want to execute this as a Python script, or as a ModelBuilder tool? ModelBuilder is great as a starting point but (as you notice!) doesn't always make functional Python, let alone efficient/elegant scripts. Conditionals are a particularly kludgy workflow within ModelBuilder, in my experience.

You can do a fairly straightforward if/else conditional using the row count:

result = arcpy.GetCount_management(TestPolys)
count = int(result.getOutput(0))

if count > 0:
    arcpy.FeatureClassToFeatureClass_conversion(TestPolys, TestData, "TestPolys2")
else:
    print("there are no features in this feature class")‍‍‍‍‍‍‍‍‍‍‍‍‍‍

(Note that the FeatureClassToFeatureClass syntax can be much simpler and cleaner than ModelBuilder exports it as, too!)

You can also do an even simpler check to see whether a feature class (or table) even exists at all, but note the Exists tool won't catch empty feature classes.

if arcpy.Exists(TestPolys)
    # do something
else:
    print("this feature class doesn't exist")‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
MikeChancey
New Contributor II

Excellent!  This looks like what I need to get started.

0 Kudos