<?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 Re: alter geoprocessing parameter form values before executing script in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/alter-geoprocessing-parameter-form-values-before/m-p/1590375#M73825</link>
    <description>&lt;P&gt;If the issue is handling things from the command line properly, your script can look at &lt;A href="https://docs.python.org/3.11/library/sys.html#sys.orig_argv" target="_self"&gt;sys.orig_argv&lt;/A&gt; to see if it was called from Pro or not and then use a &lt;A href="https://docs.python.org/3.11/library/argparse.html" target="_self"&gt;proper argument processor&lt;/A&gt; to feed parameters into "main" instead of arcpy. You can also get parameters in text form using &lt;FONT face="courier new,courier"&gt;sys.argv[1:]&lt;/FONT&gt;, this should be consistent regardless of who called your script file. If you&amp;nbsp;&lt;EM&gt;really&lt;/EM&gt; want to get into the weeds, &lt;A href="https://pro.arcgis.com/en/pro-app/latest/arcpy/geoprocessing_and_python/extending-geoprocessing-through-python-modules.htm" target="_self"&gt;geoprocessing modules&lt;/A&gt; have all the features of regular Python modules so you can write proper &lt;A href="https://setuptools.pypa.io/en/latest/userguide/entry_point.html" target="_self"&gt;entry points&lt;/A&gt; and do &lt;FONT face="courier new,courier"&gt;python -m my_module arg1 argn&lt;/FONT&gt; on top of all the other benefits of modules.&lt;/P&gt;</description>
    <pubDate>Thu, 27 Feb 2025 22:05:15 GMT</pubDate>
    <dc:creator>DavidSolari</dc:creator>
    <dc:date>2025-02-27T22:05:15Z</dc:date>
    <item>
      <title>alter geoprocessing parameter form values before executing script</title>
      <link>https://community.esri.com/t5/python-questions/alter-geoprocessing-parameter-form-values-before/m-p/1589178#M73798</link>
      <description>&lt;P&gt;... but DON'T change the displayed value in the parameter control.&lt;/P&gt;&lt;P&gt;For example, if a dropdown list of choices for a string parameter shows some long text, how can I send the script some shortened text or even a code for a particular choice without altering self.parameter[i].value; because when you re-set .value, the displayed value in the form changes and user sees the code instead of the more descriptive text.&lt;/P&gt;&lt;P&gt;Another way to ask this question is, say I use a dictionary in my script to assign a short value or code to a variable based on some long text that is coming from a parameter in the form; can I move that step to the validation?&lt;/P&gt;&lt;P&gt;One option would be to write the code value for a longer string to a hidden parameter, but then the tool still sends a redundant argument and if I want to make the script usable independent of a parameter form, I have to do some extra evaluation of the arguments.&lt;/P&gt;&lt;P&gt;Can I intercept the list of arguments just after validation, but before the script is executed?&lt;/P&gt;</description>
      <pubDate>Tue, 25 Feb 2025 21:29:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/alter-geoprocessing-parameter-form-values-before/m-p/1589178#M73798</guid>
      <dc:creator>EvanThoms</dc:creator>
      <dc:date>2025-02-25T21:29:32Z</dc:date>
    </item>
    <item>
      <title>Re: alter geoprocessing parameter form values before executing script</title>
      <link>https://community.esri.com/t5/python-questions/alter-geoprocessing-parameter-form-values-before/m-p/1589199#M73800</link>
      <description>&lt;P&gt;Give your script a "main" function (call it whatever you like) that takes in parameters in their final form, then feed it processed parameters behind a main guard. For example:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy

CODE_MAP = {
    "North": "N",
    "South": "S",
    "Other Code": "Other"
}

def main(code: str):
    arcpy.AddMessage(f"The code is: {code}".)

if __name__ == "__main__":
    full_code = arcpy.GetParameterAsText(0)
    short_code = CODE_MAP.get(full_code, "N/A")
    main(short_code)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This pattern has other advantages, like making it easy to call the function without a script tool wrapper, write tests, and so on.&lt;/P&gt;</description>
      <pubDate>Tue, 25 Feb 2025 22:23:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/alter-geoprocessing-parameter-form-values-before/m-p/1589199#M73800</guid>
      <dc:creator>DavidSolari</dc:creator>
      <dc:date>2025-02-25T22:23:39Z</dc:date>
    </item>
    <item>
      <title>Re: alter geoprocessing parameter form values before executing script</title>
      <link>https://community.esri.com/t5/python-questions/alter-geoprocessing-parameter-form-values-before/m-p/1589242#M73802</link>
      <description>&lt;P&gt;I use a lot of PYT toolboxes and if you're mainly writing python tools I'd say it's worth the switch:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from arcpy import Parameter

class MyToolbox:
    def __init__(self):
        self.label = "Toolbox Label"
        self.alias = "Toolbox Alias"
        self.tools = [MyTool]

class MyTool:
    def __init__(self) -&amp;gt; None:
        self.category = "Tool Category"
        self.description = "Tool Description"
        self.label = "Tool Name"
        
    def getParameterInfo(self) -&amp;gt; list[Parameter]:
        p1 = Parameter(
            displayName="Input Features",
            name="input_features",
            datatype="GPFeatureLayer",
            parameterType="Required",
            direction="Input")
        return [p1]
    
    def isLicensed(self) -&amp;gt; bool:
        return True
    
    def updateParameters(self, parameters: list[Parameter]) -&amp;gt; None:
        """Modify the values and properties of parameters before internal validation is performed"""
        ...
    
    def updateMessages(self, parameters) -&amp;gt; None:
        """Modify or update messages created by internal validation"""
        ...

    def execute(self, parameters: list[Parameter], messages: list) -&amp;gt; None:
        """The main tool script"""
        ...
    
    def postExecute(self):
        """Runs after the script has finished executing"""
        ...&lt;/LI-CODE&gt;&lt;P&gt;As you can see each function is called by the ArcPro interpreter at at a set time, with the update methods being called every time a user interacts with parameters. This lets you run validation or even lock parameter visibility behind conditions.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you need a last second validation, you can short circuit the execute method with a custom validation method:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;...
    
    def finalValidation(self, parameters: list[Parameter], messages: list) -&amp;gt; bool:
        """Example of an injected validation method"""
        ...
    
    def execute(self, parameters: list[Parameter], messages: list) -&amp;gt; None:
        """The main tool script"""
        if not self.finalValidation(parameters, messages):
            raise ValueError("Parameter Validation failed!")
        ...
...&lt;/LI-CODE&gt;&lt;P&gt;Because the parameters list is being passed as a reference to each method, you can modify them whenever you want and the changes will be applied to any method that uses them after.&lt;/P&gt;</description>
      <pubDate>Wed, 26 Feb 2025 00:49:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/alter-geoprocessing-parameter-form-values-before/m-p/1589242#M73802</guid>
      <dc:creator>HaydenWelch</dc:creator>
      <dc:date>2025-02-26T00:49:42Z</dc:date>
    </item>
    <item>
      <title>Re: alter geoprocessing parameter form values before executing script</title>
      <link>https://community.esri.com/t5/python-questions/alter-geoprocessing-parameter-form-values-before/m-p/1590288#M73822</link>
      <description>&lt;P&gt;Thanks, yes, I use main functions. But again, if the script is called by command line or another script and arguments are supplied positionally, the number of arguments and their indices will be different then when received from the parameter form and I won't know how to parse them.&lt;/P&gt;</description>
      <pubDate>Thu, 27 Feb 2025 19:35:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/alter-geoprocessing-parameter-form-values-before/m-p/1590288#M73822</guid>
      <dc:creator>EvanThoms</dc:creator>
      <dc:date>2025-02-27T19:35:37Z</dc:date>
    </item>
    <item>
      <title>Re: alter geoprocessing parameter form values before executing script</title>
      <link>https://community.esri.com/t5/python-questions/alter-geoprocessing-parameter-form-values-before/m-p/1590344#M73823</link>
      <description>&lt;P&gt;Heck, I guess I should look at python toolboxes again. I tried switching a big .tbx to a python toolbox a few years ago and it was such a headache I bailed. But when ESRI released the .atbx promising "&lt;SPAN&gt;better cross-release compatibility and persistence, improved performance and scalability, and less possibility of file corruption" I figured that was the format of the future and so migrated everything to that. &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I don't think I can use custom methods in a ToolValidator class for anything other than changing the behavior of the form. At least, adding a finalValidation() method at the bottom and trying to reset a parameter value did nothing.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;For now, I will just use a dictionary in my script to catch the parameter form values and switch them to the same shorter values that would be used for a command line execution.&lt;/P&gt;</description>
      <pubDate>Thu, 27 Feb 2025 21:10:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/alter-geoprocessing-parameter-form-values-before/m-p/1590344#M73823</guid>
      <dc:creator>EvanThoms</dc:creator>
      <dc:date>2025-02-27T21:10:23Z</dc:date>
    </item>
    <item>
      <title>Re: alter geoprocessing parameter form values before executing script</title>
      <link>https://community.esri.com/t5/python-questions/alter-geoprocessing-parameter-form-values-before/m-p/1590375#M73825</link>
      <description>&lt;P&gt;If the issue is handling things from the command line properly, your script can look at &lt;A href="https://docs.python.org/3.11/library/sys.html#sys.orig_argv" target="_self"&gt;sys.orig_argv&lt;/A&gt; to see if it was called from Pro or not and then use a &lt;A href="https://docs.python.org/3.11/library/argparse.html" target="_self"&gt;proper argument processor&lt;/A&gt; to feed parameters into "main" instead of arcpy. You can also get parameters in text form using &lt;FONT face="courier new,courier"&gt;sys.argv[1:]&lt;/FONT&gt;, this should be consistent regardless of who called your script file. If you&amp;nbsp;&lt;EM&gt;really&lt;/EM&gt; want to get into the weeds, &lt;A href="https://pro.arcgis.com/en/pro-app/latest/arcpy/geoprocessing_and_python/extending-geoprocessing-through-python-modules.htm" target="_self"&gt;geoprocessing modules&lt;/A&gt; have all the features of regular Python modules so you can write proper &lt;A href="https://setuptools.pypa.io/en/latest/userguide/entry_point.html" target="_self"&gt;entry points&lt;/A&gt; and do &lt;FONT face="courier new,courier"&gt;python -m my_module arg1 argn&lt;/FONT&gt; on top of all the other benefits of modules.&lt;/P&gt;</description>
      <pubDate>Thu, 27 Feb 2025 22:05:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/alter-geoprocessing-parameter-form-values-before/m-p/1590375#M73825</guid>
      <dc:creator>DavidSolari</dc:creator>
      <dc:date>2025-02-27T22:05:15Z</dc:date>
    </item>
    <item>
      <title>Re: alter geoprocessing parameter form values before executing script</title>
      <link>https://community.esri.com/t5/python-questions/alter-geoprocessing-parameter-form-values-before/m-p/1591508#M73858</link>
      <description>&lt;P&gt;Thanks! Good info there.&lt;/P&gt;</description>
      <pubDate>Tue, 04 Mar 2025 01:01:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/alter-geoprocessing-parameter-form-values-before/m-p/1591508#M73858</guid>
      <dc:creator>EvanThoms</dc:creator>
      <dc:date>2025-03-04T01:01:21Z</dc:date>
    </item>
  </channel>
</rss>

