From d27271f03fb3ce1095110c7b9092121c118aeed4 Mon Sep 17 00:00:00 2001 From: marcinpurtak Date: Fri, 5 Apr 2024 11:26:48 +0200 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=90=9B=20Fixing=20crash=20when=20no?= =?UTF-8?q?=20data=20returned?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/viadot/sources/sap_rfc.py | 66 +++++++++++++++++++---------------- 1 file changed, 35 insertions(+), 31 deletions(-) diff --git a/src/viadot/sources/sap_rfc.py b/src/viadot/sources/sap_rfc.py index 3dd1352b9..42b325c4e 100755 --- a/src/viadot/sources/sap_rfc.py +++ b/src/viadot/sources/sap_rfc.py @@ -1162,39 +1162,43 @@ def to_df(self, tests: dict = None): ) else: raise e - record_key = "WA" - data_raw = np.array(response["DATA"]) - del response - - # if the reference columns are provided not necessary to remove any extra row. - if not isinstance(self.rfc_unique_id[0], str): - row_index, data_raw, start = detect_extra_rows( - row_index, data_raw, chunk, fields - ) - else: - start = False - - records = [row for row in gen_split(data_raw, sep, record_key)] - del data_raw + # Check and skip if there is no data returned + if response["DATA"]: + record_key = "WA" + data_raw = np.array(response["DATA"]) + del response - if ( - isinstance(self.rfc_unique_id[0], str) - and not list(df.columns) == fields - ): - df_tmp = pd.DataFrame(columns=fields) - df_tmp[fields] = records - # SAP adds whitespaces to the first extracted column value - # If whitespace is in unique column it must be removed to make a proper merge - for col in self.rfc_unique_id: - df_tmp[col] = df_tmp[col].str.strip() - df[col] = df[col].str.strip() - df = pd.merge(df, df_tmp, on=self.rfc_unique_id, how="outer") - else: - if not start: - df[fields] = records + # if the reference columns are provided not necessary to remove any extra row. + if not isinstance(self.rfc_unique_id[0], str): + row_index, data_raw, start = detect_extra_rows( + row_index, data_raw, chunk, fields + ) + else: + start = False + + records = [row for row in gen_split(data_raw, sep, record_key)] + del data_raw + + if ( + isinstance(self.rfc_unique_id[0], str) + and not list(df.columns) == fields + ): + df_tmp = pd.DataFrame(columns=fields) + df_tmp[fields] = records + # SAP adds whitespaces to the first extracted column value + # If whitespace is in unique column it must be removed to make a proper merge + for col in self.rfc_unique_id: + df_tmp[col] = df_tmp[col].str.strip() + df[col] = df[col].str.strip() + df = pd.merge(df, df_tmp, on=self.rfc_unique_id, how="outer") else: - df[fields] = np.nan - chunk += 1 + if not start: + df[fields] = records + else: + df[fields] = np.nan + chunk += 1 + elif not response["DATA"]: + print('No data returned from SAP') df.columns = columns if self.client_side_filters: From c598407a412fcc088d9616398919262d2f53d3a7 Mon Sep 17 00:00:00 2001 From: marcinpurtak Date: Fri, 5 Apr 2024 13:43:08 +0200 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=8E=A8=20Formatting=20applied?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/viadot/sources/sap_rfc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/viadot/sources/sap_rfc.py b/src/viadot/sources/sap_rfc.py index 42b325c4e..5b4bb7876 100755 --- a/src/viadot/sources/sap_rfc.py +++ b/src/viadot/sources/sap_rfc.py @@ -1198,7 +1198,7 @@ def to_df(self, tests: dict = None): df[fields] = np.nan chunk += 1 elif not response["DATA"]: - print('No data returned from SAP') + print("No data returned from SAP.") df.columns = columns if self.client_side_filters: