<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic It works in Geoprocessing Questions</title>
    <link>https://community.esri.com/t5/geoprocessing-questions/script-works-randomly-when-used-in-a-toolbox/m-p/701438#M23250</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks a lot&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 29 Apr 2010 13:35:03 GMT</pubDate>
    <dc:creator>RuiLopes</dc:creator>
    <dc:date>2010-04-29T13:35:03Z</dc:date>
    <item>
      <title>script works randomly when used in a toolbox</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/script-works-randomly-when-used-in-a-toolbox/m-p/701434#M23246</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I have a very simple script that tests the number of objects selected in a layer to use as a precondition for other processes in the model. The script has 3 parameters - number to test, minimum and maximum - and 1 &lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;if &lt;/SPAN&gt;&lt;SPAN&gt;condition. It works fine when executed in python but it gives random results when used in a toolbox.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm using ArcGIS 9.2 in Windows XP Professional. I've attached script and toolbox (as txt files)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Any one else with similar problems?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks in advance&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Rui&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 28 Apr 2010 08:59:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/script-works-randomly-when-used-in-a-toolbox/m-p/701434#M23246</guid>
      <dc:creator>RuiLopes</dc:creator>
      <dc:date>2010-04-28T08:59:47Z</dc:date>
    </item>
    <item>
      <title>script works randomly when used in a toolbox</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/script-works-randomly-when-used-in-a-toolbox/m-p/701435#M23247</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Original User: fperks&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Don't use sys.exit, in the past its given random results. Instead you should raise an exception, then ArcGIS will note the error. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I modified your code block:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;#test number
if myCountOfObjects &amp;lt; myMinimum:
&amp;nbsp;&amp;nbsp;&amp;nbsp; #gp.AddError("The input should be more than " + str(myMinimum) +" objects and is currently " + str(myCountOfObjects))
&amp;nbsp;&amp;nbsp;&amp;nbsp; #print "The input should be " + str(myMinimum) +" or more objects and is currently " + str(myCountOfObjects)
&amp;nbsp;&amp;nbsp;&amp;nbsp; raise RuntimeError("The input should be " + str(myMinimum) +" or more objects and is currently " + str(myCountOfObjects))
&amp;nbsp;&amp;nbsp;&amp;nbsp; #sys.exit(0)
elif myCountOfObjects &amp;gt; myMaximum:
&amp;nbsp;&amp;nbsp;&amp;nbsp; #gp.AddError("The input should be less than " + str(myMaximum) +" objects and is currently " + str(myCountOfObjects))
&amp;nbsp;&amp;nbsp;&amp;nbsp; #print "The input should be " + str(myMaximum) + " or less objects and is currently " + str(myCountOfObjects)
&amp;nbsp;&amp;nbsp;&amp;nbsp; raise RuntimeError("The input should be " + str(myMaximum) + " or less objects and is currently " + str(myCountOfObjects))
&amp;nbsp;&amp;nbsp;&amp;nbsp; #sys.exit(0)
else:
&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.AddMessage("Processing " + str(myCountOfObjects) + " objects")
&amp;nbsp;&amp;nbsp;&amp;nbsp; print "Processing " + str(myCountOfObjects) + " objects"&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This would give you the proper effect you are looking for.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 05:30:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/script-works-randomly-when-used-in-a-toolbox/m-p/701435#M23247</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-12-12T05:30:37Z</dc:date>
    </item>
    <item>
      <title>Thanks but still not working</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/script-works-randomly-when-used-in-a-toolbox/m-p/701436#M23248</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks for the code Frank but it makes no difference in the result. For instance if you try inputing the values 23 / 1 / 5&amp;nbsp; the result is still wrong (when using the toolbox)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Any more suggestions?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Rui&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Don't use sys.exit, in the past its given random results. Instead you should raise an exception, then ArcGIS will note the error. &lt;BR /&gt;&lt;BR /&gt;I modified your code block:&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;#test number
if myCountOfObjects &amp;lt; myMinimum:
&amp;nbsp;&amp;nbsp;&amp;nbsp; #gp.AddError("The input should be more than " + str(myMinimum) +" objects and is currently " + str(myCountOfObjects))
&amp;nbsp;&amp;nbsp;&amp;nbsp; #print "The input should be " + str(myMinimum) +" or more objects and is currently " + str(myCountOfObjects)
&amp;nbsp;&amp;nbsp;&amp;nbsp; raise RuntimeError("The input should be " + str(myMinimum) +" or more objects and is currently " + str(myCountOfObjects))
&amp;nbsp;&amp;nbsp;&amp;nbsp; #sys.exit(0)
elif myCountOfObjects &amp;gt; myMaximum:
&amp;nbsp;&amp;nbsp;&amp;nbsp; #gp.AddError("The input should be less than " + str(myMaximum) +" objects and is currently " + str(myCountOfObjects))
&amp;nbsp;&amp;nbsp;&amp;nbsp; #print "The input should be " + str(myMaximum) + " or less objects and is currently " + str(myCountOfObjects)
&amp;nbsp;&amp;nbsp;&amp;nbsp; raise RuntimeError("The input should be " + str(myMaximum) + " or less objects and is currently " + str(myCountOfObjects))
&amp;nbsp;&amp;nbsp;&amp;nbsp; #sys.exit(0)
else:
&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.AddMessage("Processing " + str(myCountOfObjects) + " objects")
&amp;nbsp;&amp;nbsp;&amp;nbsp; print "Processing " + str(myCountOfObjects) + " objects"&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;This would give you the proper effect you are looking for.&lt;/BLOCKQUOTE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 05:30:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/script-works-randomly-when-used-in-a-toolbox/m-p/701436#M23248</guid>
      <dc:creator>RuiLopes</dc:creator>
      <dc:date>2021-12-12T05:30:40Z</dc:date>
    </item>
    <item>
      <title>script works randomly when used in a toolbox</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/script-works-randomly-when-used-in-a-toolbox/m-p/701437#M23249</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Original User: fperks&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Oops, i missed the big error. When ever you pass in arguments to a script, they are always as strings. &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Lets say you pass in 1 / 23 / 5...&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;So right now your myCountOfObjects is actually the string "1"&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Now saying the string "1" &amp;gt; integer 23? (It will be true, since "1" string converted gives you ascii value of 49)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Well you can't compare an string with an integer directly since it will give you not the correct behavouor, so you need to convert it to an integer. E.g.:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;myCountOfObjects = int(gp.GetParameterAsText(0))
myMinimum = int(gp.GetParameterAsText(1))
myMaximum = int(gp.GetParameterAsText(2))
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 05:30:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/script-works-randomly-when-used-in-a-toolbox/m-p/701437#M23249</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-12-12T05:30:43Z</dc:date>
    </item>
    <item>
      <title>It works</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/script-works-randomly-when-used-in-a-toolbox/m-p/701438#M23250</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks a lot&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 29 Apr 2010 13:35:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/script-works-randomly-when-used-in-a-toolbox/m-p/701438#M23250</guid>
      <dc:creator>RuiLopes</dc:creator>
      <dc:date>2010-04-29T13:35:03Z</dc:date>
    </item>
  </channel>
</rss>

