<?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 SSE: how to go through same RequestIterator multiple times in Integration, Extension &amp; APIs</title>
    <link>https://community.qlik.com/t5/Integration-Extension-APIs/Python-SSE-how-to-go-through-same-RequestIterator-multiple-times/m-p/1586463#M14545</link>
    <description>&lt;P&gt;Hi &lt;a href="https://community.qlik.com/t5/user/viewprofilepage/user-id/35754"&gt;@adamlopuch&lt;/a&gt; ,&lt;/P&gt;
&lt;P&gt;Your suspicion is correct; the request is a generator object which can only be consumed once in Python. One way around it is to first store the request in a different data structure such as a list that you can step through multiple times.&lt;/P&gt;
&lt;LI-CODE lang="python"&gt;# Get a list from the generator object so that it can be iterated over multiple times
request_list = [request_rows for request_rows in request]&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In my SSE I use a function that can then take this list and convert it to a pandas dataframe which is usually a more convenient structure for Python:&lt;/P&gt;
&lt;LI-CODE lang="python"&gt;def request_df(request_list, row_template, col_headers):
    """
    This function takes in a SSE request as a list together with a row template and column headers as lists of strings.
    Returns a Data Frame for the request.
    e.g. request_df(request_list, ['strData', 'numData', 'strData'], ['dim1', 'measure', 'kwargs'])
    """
    
    rows = [row for request_rows in request_list for row in request_rows.rows]
    outer = []
    
    for i in range(len(rows)):
        inner = []
        
        for j in range(len(row_template)):
            inner.append(getattr(rows[i].duals[j], row_template[j]))
        
        outer.append(inner)
    
    return pd.DataFrame(outer, columns=col_headers)&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Finally, at the end of your function you'll need to structure the output as SSE.Duals inside SSE.Rows inside SSE.BundledRows &lt;span class="lia-unicode-emoji" title=":face_with_tears_of_joy:"&gt;😂&lt;/span&gt;.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 30 May 2019 01:05:58 GMT</pubDate>
    <dc:creator>Nabeel_Asif</dc:creator>
    <dc:date>2019-05-30T01:05:58Z</dc:date>
    <item>
      <title>Python SSE: how to go through same RequestIterator multiple times</title>
      <link>https://community.qlik.com/t5/Integration-Extension-APIs/Python-SSE-how-to-go-through-same-RequestIterator-multiple-times/m-p/1586388#M14544</link>
      <description>&lt;P&gt;Hi Qlik SSE gurus,&lt;/P&gt;&lt;P&gt;The Python SSE is working like a charm, however in one call I'd like to go through the *same* request payload *twice*... is this possible?&lt;/P&gt;&lt;P&gt;My goal:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;FIRST ITERATION: go through each row and extract all parameters&lt;/LI&gt;&lt;LI&gt;Parse all distinct parameter values in entire request&lt;/LI&gt;&lt;LI&gt;SECOND ITERATION: go through each row AGAIN and yield result with distinct parameters&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;My issue:&lt;/P&gt;&lt;P&gt;I suspect that the (simple) approach I am using fully consumes the request iterator the first time I go through it. (Step 1).&amp;nbsp; When I go through it a second time (Step 3), there is nothing to iterate.&lt;/P&gt;&lt;P&gt;Example code:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# STEP 1: Go through each row and extract all parameters
all_params = []
for bundled_rows in request:
    for row in bundled_rows.rows:
        all_params.append([d.strData for d in row.duals])

# STEP 2: Get all values for parameters across entire request
distinct_params = something_with(all_params)

# STEP 3: go through each row again and use the distinct parameters
for bundled_rows in request:
    response_rows = []
    for row in bundled_rows.rows:
        duals = iter([SSE.Dual(numData=something_with(distinct_params)])
        response_rows.append(SSE.Row(duals=duals))
    yield SSE.BundledRows(rows=response_rows)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 16 Nov 2024 05:42:34 GMT</pubDate>
      <guid>https://community.qlik.com/t5/Integration-Extension-APIs/Python-SSE-how-to-go-through-same-RequestIterator-multiple-times/m-p/1586388#M14544</guid>
      <dc:creator>adamlopuch</dc:creator>
      <dc:date>2024-11-16T05:42:34Z</dc:date>
    </item>
    <item>
      <title>Re: Python SSE: how to go through same RequestIterator multiple times</title>
      <link>https://community.qlik.com/t5/Integration-Extension-APIs/Python-SSE-how-to-go-through-same-RequestIterator-multiple-times/m-p/1586463#M14545</link>
      <description>&lt;P&gt;Hi &lt;a href="https://community.qlik.com/t5/user/viewprofilepage/user-id/35754"&gt;@adamlopuch&lt;/a&gt; ,&lt;/P&gt;
&lt;P&gt;Your suspicion is correct; the request is a generator object which can only be consumed once in Python. One way around it is to first store the request in a different data structure such as a list that you can step through multiple times.&lt;/P&gt;
&lt;LI-CODE lang="python"&gt;# Get a list from the generator object so that it can be iterated over multiple times
request_list = [request_rows for request_rows in request]&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In my SSE I use a function that can then take this list and convert it to a pandas dataframe which is usually a more convenient structure for Python:&lt;/P&gt;
&lt;LI-CODE lang="python"&gt;def request_df(request_list, row_template, col_headers):
    """
    This function takes in a SSE request as a list together with a row template and column headers as lists of strings.
    Returns a Data Frame for the request.
    e.g. request_df(request_list, ['strData', 'numData', 'strData'], ['dim1', 'measure', 'kwargs'])
    """
    
    rows = [row for request_rows in request_list for row in request_rows.rows]
    outer = []
    
    for i in range(len(rows)):
        inner = []
        
        for j in range(len(row_template)):
            inner.append(getattr(rows[i].duals[j], row_template[j]))
        
        outer.append(inner)
    
    return pd.DataFrame(outer, columns=col_headers)&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Finally, at the end of your function you'll need to structure the output as SSE.Duals inside SSE.Rows inside SSE.BundledRows &lt;span class="lia-unicode-emoji" title=":face_with_tears_of_joy:"&gt;😂&lt;/span&gt;.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 30 May 2019 01:05:58 GMT</pubDate>
      <guid>https://community.qlik.com/t5/Integration-Extension-APIs/Python-SSE-how-to-go-through-same-RequestIterator-multiple-times/m-p/1586463#M14545</guid>
      <dc:creator>Nabeel_Asif</dc:creator>
      <dc:date>2019-05-30T01:05:58Z</dc:date>
    </item>
  </channel>
</rss>

