Is it me or the IDE??

3077
4
Jump to solution
10-15-2013 12:07 PM
by Anonymous User
Not applicable
Original User: johnmdye

I wrote a Python Toolbox tool to take a bunch of LPKs, unpack them and then update a layer directory and target Geodatabase. I identify the LPK and which FC and Layer File it should be updating based on the LPK name. I used the arcpy.Describe object to get the name and then run a series of if/elif/else statements to test for the name and then once a match is found, update the target layer files and feature classes. My IDE of choice (PyScripter) however seems to be telling me that the indent for my elif shouldn't be there.

I'm posting the code as well as several screen shots to illustrate my problem.

PyScripter:
[ATTACH=CONFIG]28349[/ATTACH]
As you can see, PyScripter is saying that at line 189, something is wrong. A quick syntax check tells me its an offset error, but the elif statement is inline with the original if statement. It didn't show this until I added the comment directly above. If I remove it, the error goes away.
[ATTACH=CONFIG]28350[/ATTACH]

Anyone know why this would be?
# For each Layer Package the user has provided         for LPK in LPK_list:             # Extract the Layer Package Metadata and store it in the variable 'desc'             desc = arcpy.Describe(LPK)             # Extract the name of the Layer Package from the Metadata and store it in the variable 'name'             name = desc.name             """If the 'name' is 'COF_Branches':"""             if name == "COF_Branches.lpk":                 """Establish the Target Name"""                 TargetName = "COF_Branches"                 """Establish the Feature Dataset name"""                 FD = "Branches"                 # Unpack the Layer Package to the srcExtractDir                 arcpy.ExtractPackage(LPK, srcExtractDir)                 # Acquire the Source Layer File from the srcExtractFolder                 srcLYRFile = srcExtractFolder + "\\0000" + str(TargetName) + ".lyr"                 # Validate that the Layer File was acquired, raising a Fatal Error if the Layer File acquisition was unsuccessful                 if arcpy.Exists(srcLYRFile) == False:                     arcpy.AddError("An error occured while unpacking '" + str(name)  + "'. The result Layer File could not be acquired. \                     Unable to continue. Contact GIS Manager.")                 # Rename the Source Layer File to match the existing naming convention                 arcpy.Rename_management(srcLYRFile, str(TargetName) + ".lyr")                 # Reacquire the Source Layer File with the updated name, this time instantiating it as a Layer object                 srcLYRFile = arcpy.mapping.Layer(srcExtractFolder + "\\" + str(TargetName) + ".lyr")                 # Validate that the Layer File was reacquired, raising a Fatal Error if the Layer File acquisition was unsuccessful                 if arcpy.Exists(srcLYRFile) == False:                     arcpy.AddError("An error occured while reacquiring the updated source Layer File. '" + str(srcLYRFile) + "' could not \                     be reacquired. Unable to continue. Contact GIS Manager.")                 # Acquire the MXDs Target Feature Layer to update                 try:                     FeatureLayer = arcpy.mapping.Layer('"' + str(TargetName) + '"')                 except:                     arcpy.AddError("An error occured while trying to acquire the Map Layer. '" + str(FeatureLayer) + "' could not be \                     acquired. Unable to continue. Contact GIS Manager.")                 # Update the MXD's Feature Layer to reference the source Layer File                 ##          This step is performed so that the 'tgtFC' which is to be updated with the 'srcFC', can be deleted from disk,                 ##          while bypassing the automatic RemoveLayer() event that would occur in ArcMap if the 'srcFC' referenced by the                 ##          MXD's 'FeatureLayer' were to be deleted while the 'FeatureLayer' is still referencing the 'srcFC'.                 arcpy.mapping.UpdateLayer(df, FeatureLayer, srcLYRFile, False)                  """ Update the Source Data """                 # Acquire the Source Feature Class                 srcFC = srcExtractGDB + "\\" + str(FD) + "\\" + str(TargetName)                 # Validate that the Source Feature Class was acquired, raising a Fatal Error if the Feature Class acquisition was unsuccessful                 if arcpy.Exists(srcFC) == False:                     arcpy.AddError("An error occured while acquiring the source Feature Class. '" + str(srcFC) + "' could not be acquired. Unable to continue. Contact GIS Manager.")                 # Acquire the Target Feature Class                 tgtFC = tgtGDB + "\\" + str(FD) + "\\" + str(TargetName)                 # Validate that the Target Feature Class was acquired, raising a Fatal Error if the Feature Class acquisition was unsuccessful                 if arcpy.Exists(tgtFC) == False:                     arcpy.AddError("An error occured while acquiring the target Feature Class. '" + str(tgtFC) + "' could not be acquired. Unable to continue. Contact GIS Manager.")                 # Delete the Target Feature Class from Disk                 arcpy.Delete_management(tgtFC)                 # Import the Source Feature Class into the Target Geodatabase, keeping the same name                 arcpy.FeatureClasstoGeodatabase_conversion(srcFC, tgtGDB + "\\" + str(FD))                 # Reacquire the MXD's Feature Layer                 FeatureLayer = arcpy.mapping.Layer('"' + str(TargetName) + '"')                 # Update the MXD Feature Layer's Data Source                 FeatureLayer.replaceDataSource(str(tgtGDB), "FILEGDB_WORKSPACE", '"' + str(TargetName) + '"', True)                 # Save the Feature Layer to a Layer File, overwriting the existing target Layer File                 arcpy.SaveToLayerFile_management(FeatureLayer, tgtDIR + "\\" + str(TargetName) + ".lyr", "RELATIVE")                  """Cleanup"""                 # Delete the extraction folder                 arcpy.Delete_management(srcExtractDir)              """If the name is 'COF_ATMs':"""             elif name == "COF_ATMs":                 """Establish the Target Name"""                 TargetName = "COF_ATMs"
0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable
Original User: ldanzinger

I think the issue is with the triple quotes, as I can repro the issue with the following:

x = 1  if x < 5:     print x """hi""" elif x > 5:     print x


They get into this a bit on stack overflow, but I think the issue is you are using triple quotes interchangeably with comments, where a triple quote is really a doc string.

View solution in original post

0 Kudos
4 Replies
JasonScheirer
Occasional Contributor III
                # Delete the extraction folder
                arcpy.Delete_management(srcExtractDir)

            """If the name is 'COF_ATMs':"""
            elif name == "COF_ATMs":
                """Establish the Target Name"""
                TargetName = "COF_ATMs"


Should be:
                # Delete the extraction folder
                arcpy.Delete_management(srcExtractDir)

                """If the name is 'COF_ATMs':"""
            elif name == "COF_ATMs":
                """Establish the Target Name"""
                TargetName = "COF_ATMs"


Also, why are you using """Strings as comments like this?"""
0 Kudos
by Anonymous User
Not applicable
Original User: ldanzinger

I think the issue is with the triple quotes, as I can repro the issue with the following:

x = 1  if x < 5:     print x """hi""" elif x > 5:     print x


They get into this a bit on stack overflow, but I think the issue is you are using triple quotes interchangeably with comments, where a triple quote is really a doc string.
0 Kudos
JohnDye
Occasional Contributor III

They get into this a bit on stack overflow, but I think the issue is you are using triple quotes interchangeably with comments, where a triple quote is really a doc string.


Shazam Lucas! I didn't know that triple quotes were docstrings! I always just assumed they were just multi-line comments. After reading the StackOverflow thread and then looking at the actual python documentation referenced within it makes a little more sense. It seems that if you want to use the docstring in this manner, it needs to be within the code block.

I'm not sure why using docstrings interchangable with comments would cause an issue though. It doesn't seem to cause any issues within any of my other scripts.
0 Kudos
by Anonymous User
Not applicable
Original User: johnmdye


Also, why are you using """Strings as comments like this?"""


Thanks for that Jason.

I was just using triple quotes, which I now know to be docstrings but formerly just assumed to be multi-line comments, because they formatted differently within PyScripter. We're required to extensively document all code at my company and that change in formatting helped highlight different sections of the process. I was essentially just using them as documentation headers so-to-speak and letting normal comments (#) operate as the granular documentation for each function.

I know that might not make much sense, but my code has to be posted and reviewed by people who don't actually know Python or GIS at all which requires that I document everything in plain english. If there's a better or more 'Pythonic' way of doing it I'd love to know!

Thanks again!
0 Kudos