Aside from the results you expect, your script might also produce additional items sent through its standard output. Such items might comprise of notes, warnings, or any other unexpected messaging your code might generate. This additional output is undesirable when running your script in the APPLY table operator, because APPLY has very clear expectations about your script’s standard output as dictated in the RETURNS clause. To suppress unexpected messaging and subsequent APPLY errors, you can take following steps such as the following.
In Python Scripting
- You can use the catch_warnings() context manager to enable warning suppression for statements.
For example, if your code contains any deprecated functions, then this approach can prevent unwanted warnings from them.
- You can turn off script execution warnings by
- Specifying the '-W ignore' option with Python3 command, or
- Importing the "warnings" library in your script and specifying 'ignore' for warning filter:
import warnings warnings.filterwarnings("ignore")
- You can deliberately send additional output, such as debugging comments, to your system’s standard error to be logged, by implementing an approach like:
import sys print("This string goes to standard error", file=sys.stderr)
- You can use python's contextmanager class to redirect stdout using the following sample code (including comments).
import sys import os from contextlib import contextmanager import csv @contextmanager def suppress_stdout(): with open(os.devnull, "w") as devnull: old_stdout = sys.stdout sys.stdout = sys.stderr try: yield finally: sys.stdout = old_stdout DELIMITER = "," QUOTECHAR = "\"" stdout_writer = csv.writer(sys.stdout, delimiter=DELIMITER, quotechar=QUOTECHAR, quoting=csv.QUOTE_MINIMAL) with suppress_stdout(): # Import all data science / machine learning related libraries under suppress_stdout to avoid any information going to stdout. from sklearn.model_selection import train_test_split with suppress_stdout(): # Implement all logic here. model_output = 1.55 column1 = "test" column2 = "test" stdout_writer.writerow([column1, column2, model_output])
In R Scripting
- You can invoke the “Rscript” command accompanied by the option “--vanilla” to prevent communication of notes and diagnostic messages through the script's standard output.
- You can wrap library() calls with the function suppressPackageStartupMessages().
- You can use the functions suppressMessages(), suppressWarnings() to wrap code segments in your script that you suspect might produce respective output.