Question

How to configure this code to to work in Python caller

  • 21 May 2023
  • 2 replies
  • 5 views

Badge +3

How to let this arcpy code in Python caller work 

my input type is ArcSDE Esri 

the target here to get the list of relationships of classes between eachother and save the output in csv 

 i am not familiar with Python ,what should I add and change here to let the code work in pythoncaller and write the  output in csv file

import os, pprint
 
all_rs = listRelationshipClasses("path/to/your/database.sde")  # see above
 
pprint.pprint(all_rs[0])
 
# filter by name
rs = [r for r in all_rs if r["name"] == "FilterName"]
rs = [r for r in all_rs if "PartOfTheName" in rs["name"]]
 
# filter by cardinality
rs = [r for r in all_rs if r["cardinality"] == "OneToOne"]
 
# filter by origin or destination table
rs = [r for r in all_rs if "OriginTableName" in r["originClassNames"]]
rs = [r for r in all_rs if "DestinationTableName" in r["destinationClassNames"]]
rs = [r for r in all_rs if "OriginTableName" in r["originClassNames"] and "DestinationTableName" in r["destinationClassNames"]]
 
# filter by dataset
rs = [r for r in all_rs if os.path.basename(r["path"]) == "DatasetName"]
 
# filter by flags
rs = [r for r in all_rs if r["isComposite"]]
rs = [r for r in all_rs if not r["isComposite"]]
# other flags: isAttachmentRelationship, isAttributed, isReflexive, isVersioned, canVersion, changeTracked

FME 2021 

 


2 replies

Userlevel 3
Badge +16

First you'll need to import arcpy. That should enable listRelationshipClasses to work. 

I assume the bulk of the script doesn't need to be adjusted.

Then to output to csv, you could set an attribute in the python: feature.setAttribute('relationships',rs) and self.pyoutput(feature) output that from the script back into the FME workspace. Or you can output 'rs' to csv direct from the script, and you can probably get the code to do that from chatGPT. This may or may not work, I'm not sure what the relationship class list looks like.

import csv
 
# Your list of names
rs = ['John', 'Anna', 'Peter', 'Mike']
 
# Open (or create) the CSV file in write mode ('w')
with open('output.csv', 'w', newline='') as f:
    writer = csv.writer(f)
 
    # Write the list as a single row in the CSV file
    writer.writerow(rs)

 

Badge +3

First you'll need to import arcpy. That should enable listRelationshipClasses to work. 

I assume the bulk of the script doesn't need to be adjusted.

Then to output to csv, you could set an attribute in the python: feature.setAttribute('relationships',rs) and self.pyoutput(feature) output that from the script back into the FME workspace. Or you can output 'rs' to csv direct from the script, and you can probably get the code to do that from chatGPT. This may or may not work, I'm not sure what the relationship class list looks like.

import csv
 
# Your list of names
rs = ['John', 'Anna', 'Peter', 'Mike']
 
# Open (or create) the CSV file in write mode ('w')
with open('output.csv', 'w', newline='') as f:
    writer = csv.writer(f)
 
    # Write the list as a single row in the CSV file
    writer.writerow(rs)

 

Thanks for reply .

I have used import arcpy but I got error that list relationship class is not defined .

will be possible that I read the table with feature reader and the attribute name output of reader use as input in Python caller ?

could you provide me workspace sample that Python caller works to get result from this code .

thanks in advance 

Reply