I am using Python 2.7 and ArcMap 10.8.
I made this script that will replace the datasource of particular layers in my "template" mxd and then save a copy of the mxd to a designated folder. I am always trying to expand my python knowledge (which is very little still) and in this case to also make the code look tidier. To do so I want to replace a chunk of the code with a ternary operator. Unfortunately I am getting an unexpected error when I try to replace the data source of the layers (or possibly in the ternary expression itself as sometimes python errors are related to the line above where it indicates). Is it not possible to use a ternary Operator in the way I am attempting below?
Background for variables in code below but not indicated because from way up in my script:
permitType = a variable set earlier in the code via raw_input. It asks what type of permit I am creating and currently is just a N or Y.
newFC = a feature class created earlier in the script which is replacing the source in my "template".
gdb = the path to the geodatabase the feature class is in.
Working Code:
orient = raw_input("How do you want your map oriented? Type \"P\" for Portrait or \"L\" for Landscape. >>").upper().strip()
if orient == 'L':
exTemp = arcpy.mapping.MapDocument(r'path_to_tempA')
else:
exTemp = arcpy.mapping.MapDocument(r"path_to_tempb")
df = arcpy.mapping.ListDataFrames(exTemp)[0]
for lyr in arcpy.mapping.ListLayers(exTemp, "", df):
if permitType == "N":
if lyr.name == "Application Tenure":
lyr.replaceDataSource(gdb, "FILEGDB_WORKSPACE", newFC, True) #changing the source for a layer in my mxd
else:
if lyr.name in ("Tenure Road Application", "PofC", "PofT"):
lyr.replaceDataSource(gdb, "FILEGDB_WORKSPACE", newFC, True)
Not working ternary code:
orient = raw_input("How do you want your map oriented? Type \"P\" for Portrait or \"L\" for Landscape. >>").upper().strip()
if orient == 'L':
exTemp = arcpy.mapping.MapDocument(r'W:\FOR\RCO\DCK\Projects\KVidal\statusing_clearances\2021_10.8_ExA_landscape.mxd')
else:
exTemp = arcpy.mapping.MapDocument(r"W:\FOR\RCO\DCK\Projects\KVidal\statusing_clearances\2021_10.8_ExA_Portrait.mxd")
if permitType =="N":
condition = False
elif permitType == "Y":
condition = True
df = arcpy.mapping.ListDataFrames(exTemp)[0]
for lyr in arcpy.mapping.ListLayers(exTemp, "", df):
lyr.name == "Application Tenure" if condition else lyr.name in ("Tenure Road Application", "PofC", "PofT")
lyr.replaceDataSource(gdb, "FILEGDB_WORKSPACE", newFC, True) #changing the source for a layer in my mxd
I get the following error, note line 173 is the lyr.replaceDataSource line at the end of my non working ternary code. For reference of where the code stops working I included the last working raw input (where it asks for P or L):
How do you want your map oriented? Type "P" for Portrait or "L" for Landscape. >>p
Traceback (most recent call last):
File "h:/myCODINGstuff/python code/ExA_temp_code/June25_ExA_script.py", line 173, in <module>
lyr.replaceDataSource(gdb, "FILEGDB_WORKSPACE", newFC, True) #changing the source for a layer in my mxd
File "E:\sw_nt\ArcGIS\Desktop10.8\ArcPy\arcpy\utils.py", line 182, in fn_
return fn(*args, **kw)
File "E:\sw_nt\ArcGIS\Desktop10.8\ArcPy\arcpy\_mapping.py", line 686, in replaceDataSource
return convertArcObjectToPythonObject(self._arc_object.replaceDataSource(*gp_fixargs((workspace_path, workspace_type, dataset_name, validate), True)))
ValueError: Layer: Unexpected error
PS H:\myCODINGstuff\python code\ExA_temp_code>
I am also curious, if I do not indicate [0] in the variable df will it change the source for my map inset too? As you can see the inset layer has not been updated while the application layers have been.
"Application Tenure" if condition else lyr.name in ("Tenure Road Application", "PofC", "PofT")
seems to be processing as"Application Tenure" if condition else (lyr.name in ("Tenure Road Application", "PofC", "PofT"))
instead of("Application Tenure" if condition else lyr.name) in ("Tenure Road Application", "PofC", "PofT")