Select to view content in your preferred language

"Copy to Python Window" script does not run

1092
10
Jump to solution
11-27-2023 01:04 PM
drWood
by
Regular Contributor

Could someone please provide some insight into why this script will not run? I want to format this script to run for all of the feature classes for our asset inventory, but the copied script from the geoprocessing history will not execute due to error shown in second picture, leaving me no choice but to manually format each execution in the geoprocessing pane.

 

Any help/direction would be much appreciated.

 

arcpy.management.AddAttributeRule(
    in_table="dHydraulicElevation",
    name="FacilityID",
    type="CALCULATION",
    script_expression=r'//Assign initial FacilityID value to variable FID;
var IdSeq = NextSequenceValue("heSeq1");
var FID = "HE_" + text(Date(), "MMDDYYYY_") + IdSeq;
//Test first sequence value
if(IdSeq<=999999){
   return FID} 
else {IdSeq = NextSequenceValue("heSeq2");
      FID = "HE_" + text(Date(), "MMDDYYYY_") + IdSeq};
//Test second sequence value if necessary;
if(IdSeq<=999999){
    return FID}
else {FID = "Update Attribute Rule";
return FID};',
    is_editable="EDITABLE",
    triggering_events="INSERT",
    error_number="",
    error_message="",
    description="",
    subtype="ALL",
    field="FacilityID",
    exclude_from_client_evaluation="EXCLUDE",
    batch="NOT_BATCH",
    severity=None,
    tags=None
)

drWood_1-1701119016721.png

 

0 Kudos
1 Solution

Accepted Solutions
Brian_Wilson
Honored Contributor

Yeah I avoid those little "r"s in front of strings. Sorry. The 'r' actually stops \  from working. I missed that.  People rely on raw strings when their strings are mangled with Windows backwards slashes, this is a misuse and generally a bad idea. The 'r' syntax is mostly useful when working with regular expressions. But I digress.

You don't want the "r" in front unless you really want raw string literals for a real reason.

This works, I tried it -- THANKS for reentering your code.

script_expression='//Assign initial FacilityID value to variable FID;\
var IdSeq = NextSequenceValue("heSeq1");\
var FID = "HE_" + text(Date(), "MMDDYYYY_") + IdSeq;\
//Test first sequence value\
if(IdSeq<=999999){\
   return FID} \
else {IdSeq = NextSequenceValue("heSeq2");\
      FID = "HE_" + text(Date(), "MMDDYYYY_") + IdSeq};\
//Test second sequence value if necessary;\
if(IdSeq<=999999){\
    return FID}\
else {FID = "Update Attribute Rule";\
return FID};'

This works too and it's easier to type.

script_expression="""//Assign initial FacilityID value to variable FID;
var IdSeq = NextSequenceValue("heSeq1");
var FID = "HE_" + text(Date(), "MMDDYYYY_") + IdSeq;
//Test first sequence value
if(IdSeq<=999999){
   return FID} 
else {IdSeq = NextSequenceValue("heSeq2");
      FID = "HE_" + text(Date(), "MMDDYYYY_") + IdSeq};
//Test second sequence value if necessary;
if(IdSeq<=999999){
    return FID}
else {FID = "Update Attribute Rule";
return FID};"""

 

OH right, "Arcade"!  I learned it and then immediately filed it under "A" between to "AML" and "Avenue". The code editor here does not support it! Ha. I bet that's on someone's todo list. I actually learned AML in 2017 to migrate some data then put it back in the dustbin of history.

View solution in original post

10 Replies
DanPatterson
MVP Esteemed Contributor

The script doesn't look like it is coded in python would be my first guess.


... sort of retired...
drWood
by
Regular Contributor

The code being stored for use with the assigned attribute rules is strictly Arcade. I was hoping that Esri would enable the software to read and execute the script if it allows it to export it from your history.

0 Kudos
RobBurke
Occasional Contributor

Just noticing the r'var at the start, but there is no closing single quote '

Should there be a closing quote at the end of line 6?

Or

 

Should the r' be removed?

 

Robert Burke
drWood
by
Regular Contributor

This led to syntax errors occurring when trying to execute.

The closing ' is on line 17 and the r' is meant to dictate that this is not to be read as code and inserted into the expression parameter as it exists for use with Arcade language for the purpose of the attribute rule it is being assigned to

0 Kudos
Brian_Wilson
Honored Contributor

Looks to me like there's a long stretch of javascript embedded in there and it's got newlines in it. Try putting  a \ at the end of each line in the script_expression. From the line "script_expression=" down to the one with the closing ' in it, says "return FID};'"

You copied a picture of your code. From now on, cut and paste the actual code so it can be edited to make it easier to help you and so old people like me and @DanPatterson can read it. I usually mark code as "code", you can do that with the ellipsis ... in the editor to see the extra formatting options, and look for the code one...

Brian_Wilson_0-1701122887542.png

 

click on </> and you can put in a block of code

# LIKE THIS
#
  script_expression = r'stuff here \
more stuff\
all the way\
down to\
return something;'

 

0 Kudos
drWood
by
Regular Contributor

It is Arcade that is embedded. I updated the post to use the inserted code.

 

I'm hoping it's not an oversight of Esri to allow the export of the code despite python not being able store the Arcade code for the created attribute rule and that there is some fancy way to reformat things for proper execution. The suggestion you provided for the "\" at the end of the lines provided a new error of  "SyntaxError: EOL while scanning string literal".

0 Kudos
Brian_Wilson
Honored Contributor

Yeah I avoid those little "r"s in front of strings. Sorry. The 'r' actually stops \  from working. I missed that.  People rely on raw strings when their strings are mangled with Windows backwards slashes, this is a misuse and generally a bad idea. The 'r' syntax is mostly useful when working with regular expressions. But I digress.

You don't want the "r" in front unless you really want raw string literals for a real reason.

This works, I tried it -- THANKS for reentering your code.

script_expression='//Assign initial FacilityID value to variable FID;\
var IdSeq = NextSequenceValue("heSeq1");\
var FID = "HE_" + text(Date(), "MMDDYYYY_") + IdSeq;\
//Test first sequence value\
if(IdSeq<=999999){\
   return FID} \
else {IdSeq = NextSequenceValue("heSeq2");\
      FID = "HE_" + text(Date(), "MMDDYYYY_") + IdSeq};\
//Test second sequence value if necessary;\
if(IdSeq<=999999){\
    return FID}\
else {FID = "Update Attribute Rule";\
return FID};'

This works too and it's easier to type.

script_expression="""//Assign initial FacilityID value to variable FID;
var IdSeq = NextSequenceValue("heSeq1");
var FID = "HE_" + text(Date(), "MMDDYYYY_") + IdSeq;
//Test first sequence value
if(IdSeq<=999999){
   return FID} 
else {IdSeq = NextSequenceValue("heSeq2");
      FID = "HE_" + text(Date(), "MMDDYYYY_") + IdSeq};
//Test second sequence value if necessary;
if(IdSeq<=999999){
    return FID}
else {FID = "Update Attribute Rule";
return FID};"""

 

OH right, "Arcade"!  I learned it and then immediately filed it under "A" between to "AML" and "Avenue". The code editor here does not support it! Ha. I bet that's on someone's todo list. I actually learned AML in 2017 to migrate some data then put it back in the dustbin of history.

drWood
by
Regular Contributor

The r' was the default coding provided by the Send to Python Window and Copy Python Command options and my initial understanding of coding would have most likely led me the same route.

I went ahead and used the second  method provided, definitely much friendlier to match the editing and no issues executing. Thank you so much for sharing your expertise on this! Definitely an important workaround that not everyone has knowledge of.

0 Kudos
DanPatterson
MVP Esteemed Contributor

Python window—ArcGIS Pro | Documentation

To load existing code from a Python file into the Python window, click in the Python prompt section, and click Load Code.

Geoprocessing history—ArcGIS Pro | Documentation

several options there.

I think from the reading that there is an expectation that the code is python.  You get off to a good start with

arcpy.management.AddAttributeRule

but the rest of the checks should be coded in python.

I wouldn't expect that esri would validate whether the code works since the expectation is that what is saved is valid python code that someone wishes to reuse.

Add Attribute Rule (Data Management)—ArcGIS Pro | Documentation

has several code examples that could serve as the foundation for producing a reuseable script if you want to alter your Arcade expressions and checks to python


... sort of retired...
0 Kudos