[Tutor] Checking Syntax

Alan Gauld alan.gauld at btinternet.com
Fri Jun 19 01:26:38 CEST 2009

"Kevin Pearson" <kevinfpearson at gmail.com> wrote 

> run a check on the sytax (before I try running the program) it places a
> cursor like so:
> outFeatureClas|s = outWorkspace + "/" +
> GP.ValidateTableName(fc,outWorkspace)

You should try running it, the error message may be more 
helpful than the syntax checker...

However, it will also help make your code more readable 
is you insert some blank lines to break it into logical groups
See below...

Also most of the comments are really bad stylistically since 
they simply describe what the code is doing which is in most cases 
self evident even to a beginner. Comments should describe *why* 
the code is as it is, not what it is. I've removed the worst cases.

> #Import standard library modules
> import win32com.client, sys, os

> #Create the Geoprocessor object
> GP = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1")

> #argv[1] is the name of the script
> GP.workspace = sys.argv[1]
> clipFeatures = sys.argv[2]
> outWorkspace = sys.argv[3]
> clusterTolerance = sys.argv[4]

> try:
>    fcs = GP.ListFeatureClasses()
>    #always reset a list so the first item is extracted
>    #so you can be sure you get the first item in the list
>    fcs.Reset()
>    fc = fcs.Next()

>    while fc:
>    # returns unique name so no existing data is overwritten.
>    outFeatureClass = outWorkspace + "/" + GP.ValidateTableName(fc,
>                      outWorkspace)

Notice that this line is supposerd to be inside the body of a while loop.
That means it should be indented. Because it isn't the syntax checker 
thinks there is a missing body to the loop. Your line is the first line 
after Python detects the error so thats where the cursor stops.

>    #Do not clip the clipFeatures, it may be in the same workspace.
>    if str(fc) != str(os.path.split(clipFeatures)[1]):
>        GP.Clip(fc, clipFeatures, outFeatureClass,
>        clusterTolerance)
>    fc = fcs.Next()

I suspect all of these lines should be indented too...

> except:
>    GP.AddMessage(GP.GetMessages(2))
>    print GP.GetMessages(2)

Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list