0

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_clearances2021円_10.8_ExA_landscape.mxd') 
else:
 exTemp = arcpy.mapping.MapDocument(r"W:\FOR\RCO\DCK\Projects\KVidal\statusing_clearances2021円_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.

Screen Shot of my table of contents of the mxd that is created. as you can see the application tenure layer in the top is ok (no red!) but the one in the inset is missing

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Jun 25, 2021 at 19:56
2
  • Always use parenthesis is you aren't sure of the way the language will process your expressions -- "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") Commented Jun 25, 2021 at 20:13
  • I don't have an answer for you, but I had this error with arcpy and python 2.x often with various functions. I think the term "layer" is used with multiple meanings and some functions need a layer name (string) or a path (string) or a python object of a certain type. So you want to check your types for the parameter gdb und newFC and if match with the types with that function replaceDataSource(). Commented Jun 25, 2021 at 20:13

1 Answer 1

1

lyr.name == "Application Tenure" if condition else lyr.name in ("Tenure Road Application", "PofC", "PofT") is not a ternary expression. A ternary expression has the form a = b if c else d. The == at the start of your expression is a comparison operator, not an assignment operator.

I'd recommend changing back to the working code.

answered Jun 25, 2021 at 20:53

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.