<?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: Python SDK: Strange behavior with multiple instances of the tool in Dev Space</title>
    <link>https://community.alteryx.com/t5/Dev-Space/Python-SDK-Strange-behavior-with-multiple-instances-of-the-tool/m-p/446949#M988</link>
    <description>&lt;P&gt;Problem solved...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In my post I claimed&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;P&gt;But I have checked very carefully to use "self.tool_mode" everywhere. So, there should be absolutely no global objects present.&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;but this isn't actually true!&amp;nbsp;&lt;EM&gt;ToolMode&lt;/EM&gt; as a class is defined as a global object. As soon as a second instance of the tool is placed on the canvas, a second class of the same name is defined. It looks the same, it actually is the same, but the comparisons in the first instance will fail -- as it is, in fact, not the same class anymore.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Remember: If you develop a Python SDK tool, do not ever define global objects or classes.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Solution was to define&amp;nbsp;&lt;EM&gt;ToolMode&amp;nbsp;&lt;/EM&gt;as "part" of&amp;nbsp;&lt;EM&gt;AyxPlugin&lt;/EM&gt;:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;class AyxPlugin:

    class ToolMode(Enum):
        # ...

    # ...
    self.tool_mode = self.ToolMode.NONE_MODE
    # etc.&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Best&lt;/P&gt;&lt;P&gt;Christopher&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 02 Aug 2019 13:44:25 GMT</pubDate>
    <dc:creator>chrisha</dc:creator>
    <dc:date>2019-08-02T13:44:25Z</dc:date>
    <item>
      <title>Python SDK: Strange behavior with multiple instances of the tool</title>
      <link>https://community.alteryx.com/t5/Dev-Space/Python-SDK-Strange-behavior-with-multiple-instances-of-the-tool/m-p/446884#M986</link>
      <description>&lt;P&gt;Hi there,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've run into a super strange error with my Python SDK tool: I get errors and strange behaviors when the tool is present multiple times in the workflow.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My tool has a GUI option to select different modes: List Files or Download Files. This is stored as a DataItem named&amp;nbsp;&lt;EM&gt;ToolMode&lt;/EM&gt; and read in the&amp;nbsp;&lt;EM&gt;AyxPlugin.pi_init()&lt;/EM&gt; method:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;class ToolMode(Enum):
    NONE_MODE = 0
    LIST_FILES = 1
    DOWNLOAD_TO_PATH = 2
    DOWNLOAD_TO_BLOB = 3

class AyxPlugin:
    def __init__(self, n_tool_id: int, alteryx_engine: object, output_anchor_mgr: object):
        # Default properties
        self.n_tool_id = n_tool_id
        self.alteryx_engine = alteryx_engine
        self.output_anchor_mgr = output_anchor_mgr

        # Tool mode: What should we do?
        self.tool_mode = ToolMode.NONE_MODE

    def pi_init(self, str_xml: str):
        setting_tree = Et.fromstring(str_xml)
        self.tool_mode = {
            'list': ToolMode.LIST_FILES,
            'download_file': ToolMode.DOWNLOAD_TO_PATH,
            'download_blob': ToolMode.DOWNLOAD_TO_BLOB
        }.get(setting_tree.find('ToolMode').text, ToolMode.NONE_MODE)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;At a different method I check which&amp;nbsp;&lt;EM&gt;self.tool_mode&lt;/EM&gt; I have and decide what to do:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;    def pi_push_all_records(self, n_record_limit: int) -&amp;gt; bool:

            self.output_message("Debug: {}".format(self.tool_mode))
            if self.tool_mode == ToolMode.LIST_FILES:
                # Fields only present for LIST_FILES mode
                self.output_recordinfo[field_dict['UID']].set_from_string(record_creator, f['uid'])
            else:
                # Download files if not in LIST_FILES mode
                if self.tool_mode == ToolMode.DOWNLOAD_TO_BLOB:
                    # Generate temporary filename
                    out_fname = self.alteryx_engine.create_temp_file_name('tmp')

                # Download file
                try:
                    # Download file to temporary folder
                    sftp_conn.get(f['filename'], localpath=out_fname)
                except IOError as e:
                    self.output_message('Error transferring file "{}": {}'.format(f['filename'], e))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Everything works as expected as long as I have the tool only once on the canvas. As soon as I have two or more instances of the tool, strange behavior starts. In particular, in &lt;EM&gt;pi_push_all_records()&lt;/EM&gt; I get an error that&amp;nbsp;&lt;EM&gt;out_fname&lt;/EM&gt; is not defined -- despite the fact that tool should not even run this chunk of code. The output correctly shows "Debug: ToolMode.LST_FILES", but the subsequent "if" does not catch this, but runs into the "else" branch.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This behavior does not make any sense. It seems like Alteryx is messing up the objects and properties are shared between instances. But I have checked very carefully to use "self.tool_mode" everywhere. So, there should be absolutely no global objects present.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Did anyone ever epexierence something &amp;nbsp;similar?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Best&lt;/P&gt;&lt;P&gt;Christopher&lt;/P&gt;</description>
      <pubDate>Fri, 02 Aug 2019 10:52:18 GMT</pubDate>
      <guid>https://community.alteryx.com/t5/Dev-Space/Python-SDK-Strange-behavior-with-multiple-instances-of-the-tool/m-p/446884#M986</guid>
      <dc:creator>chrisha</dc:creator>
      <dc:date>2019-08-02T10:52:18Z</dc:date>
    </item>
    <item>
      <title>Re: Python SDK: Strange behavior with multiple instances of the tool</title>
      <link>https://community.alteryx.com/t5/Dev-Space/Python-SDK-Strange-behavior-with-multiple-instances-of-the-tool/m-p/446949#M988</link>
      <description>&lt;P&gt;Problem solved...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In my post I claimed&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;P&gt;But I have checked very carefully to use "self.tool_mode" everywhere. So, there should be absolutely no global objects present.&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;but this isn't actually true!&amp;nbsp;&lt;EM&gt;ToolMode&lt;/EM&gt; as a class is defined as a global object. As soon as a second instance of the tool is placed on the canvas, a second class of the same name is defined. It looks the same, it actually is the same, but the comparisons in the first instance will fail -- as it is, in fact, not the same class anymore.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Remember: If you develop a Python SDK tool, do not ever define global objects or classes.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Solution was to define&amp;nbsp;&lt;EM&gt;ToolMode&amp;nbsp;&lt;/EM&gt;as "part" of&amp;nbsp;&lt;EM&gt;AyxPlugin&lt;/EM&gt;:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;class AyxPlugin:

    class ToolMode(Enum):
        # ...

    # ...
    self.tool_mode = self.ToolMode.NONE_MODE
    # etc.&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Best&lt;/P&gt;&lt;P&gt;Christopher&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 02 Aug 2019 13:44:25 GMT</pubDate>
      <guid>https://community.alteryx.com/t5/Dev-Space/Python-SDK-Strange-behavior-with-multiple-instances-of-the-tool/m-p/446949#M988</guid>
      <dc:creator>chrisha</dc:creator>
      <dc:date>2019-08-02T13:44:25Z</dc:date>
    </item>
    <item>
      <title>Re: Python SDK: Strange behavior with multiple instances of the tool</title>
      <link>https://community.alteryx.com/t5/Dev-Space/Python-SDK-Strange-behavior-with-multiple-instances-of-the-tool/m-p/446950#M989</link>
      <description>&lt;P&gt;Thanks for sharing this,&amp;nbsp;&lt;a href="https://community.alteryx.com/t5/user/viewprofilepage/user-id/13229"&gt;@chrisha&lt;/a&gt;!&amp;nbsp; I was quite stumped with your problem.&amp;nbsp; I assumed (like you must have), that enums were static or singleton values like they are in every other language I've used.&amp;nbsp; Very strange that it is not the case, and very good to keep in mind.&lt;/P&gt;</description>
      <pubDate>Fri, 02 Aug 2019 13:54:21 GMT</pubDate>
      <guid>https://community.alteryx.com/t5/Dev-Space/Python-SDK-Strange-behavior-with-multiple-instances-of-the-tool/m-p/446950#M989</guid>
      <dc:creator>tlarsen7572</dc:creator>
      <dc:date>2019-08-02T13:54:21Z</dc:date>
    </item>
  </channel>
</rss>

