<?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: Using decorators for script tool interfaces? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/using-decorators-for-script-tool-interfaces/m-p/211932#M16346</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Yes, but seems a bit verbose to me. Here's a simple example using sys.argv instead of arcpy.GetParameterAsText:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import sys

def myargs(func):
&amp;nbsp;&amp;nbsp;&amp;nbsp; '''a decorator'''
&amp;nbsp;&amp;nbsp;&amp;nbsp; defaultargs =sys.argv[1:]
&amp;nbsp;&amp;nbsp;&amp;nbsp; def newfunc(*origargs):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if origargs:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return func(*origargs)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return func(*defaultargs)
&amp;nbsp;&amp;nbsp;&amp;nbsp; return newfunc

@myargs #the decoration
def myfunc(*args):
&amp;nbsp;&amp;nbsp;&amp;nbsp; '''a decorated function'''
&amp;nbsp;&amp;nbsp;&amp;nbsp; return args

if __name__ == "__main__":
&amp;nbsp;&amp;nbsp;&amp;nbsp; for arg in myfunc('test'):print arg
&amp;nbsp;&amp;nbsp;&amp;nbsp; for arg in myfunc():print arg
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here's a decent explanation of decorators: &lt;/SPAN&gt;&lt;A href="http://pythonconquerstheuniverse.wordpress.com/2012/04/29/python-decorators/" rel="nofollow noopener noreferrer" target="_blank"&gt;http://pythonconquerstheuniverse.wordpress.com/2012/04/29/python-decorators/&lt;/A&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 10:25:16 GMT</pubDate>
    <dc:creator>Luke_Pinner</dc:creator>
    <dc:date>2021-12-11T10:25:16Z</dc:date>
    <item>
      <title>Using decorators for script tool interfaces?</title>
      <link>https://community.esri.com/t5/python-questions/using-decorators-for-script-tool-interfaces/m-p/211931#M16345</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I saw a neat &lt;/SPAN&gt;&lt;A href="http://stackoverflow.com/a/101447" rel="nofollow noopener noreferrer" target="_blank"&gt;example&lt;/A&gt;&lt;SPAN&gt; this morning of Python decorators that made me think: could this be a nice way to set up a script tool interface for functions?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The way I set up tools now is as functions (so I can call them directly) and at the end of the code put something like this so I can use the file as a script tool:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
if __name__ == "__main__":
&amp;nbsp; args = [arcpy.GetParameterAsText(i) for i in range(arcpy.GetParameterCount())]
&amp;nbsp; MyTool(*args)
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Could this be done nicely with a decorator right above my function MyTool?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 10:25:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-decorators-for-script-tool-interfaces/m-p/211931#M16345</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2021-12-11T10:25:13Z</dc:date>
    </item>
    <item>
      <title>Re: Using decorators for script tool interfaces?</title>
      <link>https://community.esri.com/t5/python-questions/using-decorators-for-script-tool-interfaces/m-p/211932#M16346</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Yes, but seems a bit verbose to me. Here's a simple example using sys.argv instead of arcpy.GetParameterAsText:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import sys

def myargs(func):
&amp;nbsp;&amp;nbsp;&amp;nbsp; '''a decorator'''
&amp;nbsp;&amp;nbsp;&amp;nbsp; defaultargs =sys.argv[1:]
&amp;nbsp;&amp;nbsp;&amp;nbsp; def newfunc(*origargs):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if origargs:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return func(*origargs)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return func(*defaultargs)
&amp;nbsp;&amp;nbsp;&amp;nbsp; return newfunc

@myargs #the decoration
def myfunc(*args):
&amp;nbsp;&amp;nbsp;&amp;nbsp; '''a decorated function'''
&amp;nbsp;&amp;nbsp;&amp;nbsp; return args

if __name__ == "__main__":
&amp;nbsp;&amp;nbsp;&amp;nbsp; for arg in myfunc('test'):print arg
&amp;nbsp;&amp;nbsp;&amp;nbsp; for arg in myfunc():print arg
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here's a decent explanation of decorators: &lt;/SPAN&gt;&lt;A href="http://pythonconquerstheuniverse.wordpress.com/2012/04/29/python-decorators/" rel="nofollow noopener noreferrer" target="_blank"&gt;http://pythonconquerstheuniverse.wordpress.com/2012/04/29/python-decorators/&lt;/A&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 10:25:16 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-decorators-for-script-tool-interfaces/m-p/211932#M16346</guid>
      <dc:creator>Luke_Pinner</dc:creator>
      <dc:date>2021-12-11T10:25:16Z</dc:date>
    </item>
  </channel>
</rss>

