-
Notifications
You must be signed in to change notification settings - Fork 9
Capture and add warnings separately in AnalyticResult from standard error in analytic results #110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ | |
| import types | ||
| import typing | ||
| import re | ||
| import warnings | ||
|
|
||
| from spotfire import sbdf, _utils | ||
|
|
||
|
|
@@ -25,6 +26,7 @@ | |
|
|
||
|
|
||
| _COLUMN_METADATA_TRUNCATE_THRESHOLD = 80000 | ||
| _MAX_WARNINGS = 100 | ||
|
|
||
|
|
||
| def _bad_string(str_: typing.Any) -> bool: | ||
|
|
@@ -220,15 +222,19 @@ def write(self, globals_dict: _Globals, debug_fn: _LogFunction) -> None: | |
|
|
||
| class AnalyticResult: | ||
| """Represents the results of evaluating an AnalyticSpec object.""" | ||
| # pylint: disable=too-many-instance-attributes | ||
| std_err_out: typing.Optional[str] | ||
| warnings: list[str] | ||
| summary: typing.Optional[str] | ||
| _exc_info: _ExceptionInfo | ||
| _debug_log: typing.Optional[str] | ||
|
|
||
| def __init__(self, capture: _OutputCapture) -> None: | ||
| self.success = True | ||
| self.has_stderr = False | ||
| self.has_warnings = False | ||
| self.std_err_out = None | ||
| self.warnings = [] | ||
| self.summary = None | ||
| self._exc_info = (None, None, None) | ||
| self._capture = capture | ||
|
|
@@ -413,9 +419,25 @@ def _execute_script(self, compiled_script: types.CodeType, result: AnalyticResul | |
| if self.analytic_type == "script": | ||
| # noinspection PyBroadException | ||
| try: | ||
| exec(compiled_script, self.globals) | ||
| with warnings.catch_warnings(record=True) as caught_warnings: | ||
| exec(compiled_script, self.globals) | ||
| except BaseException: | ||
| result.fail_with_exception(sys.exc_info()) | ||
|
vrane-tibco marked this conversation as resolved.
|
||
| if caught_warnings: | ||
| result.has_warnings = True | ||
| total = len(caught_warnings) | ||
| if total > _MAX_WARNINGS: | ||
| result.warnings = [ | ||
| warnings.formatwarning(w.message, w.category, w.filename, w.lineno, w.line) | ||
| for w in caught_warnings[:_MAX_WARNINGS - 1] | ||
| ] | ||
| result.warnings.append(f"... and {total - _MAX_WARNINGS + 1} more warnings (truncated)\n") | ||
| else: | ||
| result.warnings = [ | ||
| warnings.formatwarning(w.message, w.category, w.filename, w.lineno, w.line) | ||
| for w in caught_warnings | ||
| ] | ||
| if not result.success: | ||
| return | ||
| elif self.analytic_type == "aggregationScript": | ||
| self.debug("aggregation scripts are not supported") | ||
|
|
@@ -489,6 +511,9 @@ def _create_summary(self, result: AnalyticResult) -> None: | |
| result.has_stderr = True | ||
| buf.write("\nStandard error:\n") | ||
| buf.write(result.std_err_out) | ||
| if result.has_warnings: | ||
| buf.write("\nWarnings:\n") | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need more stronger protocol identification ? If I print warnings in the log/print(Warnings:) will it create problem ?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is added to show warnings at client when enable debug logging for data function, along with other debug logs warning also get displayed. Python service or client glue code will retrieve warnings from AnalyticResult.warnings (field in AnalyticResult class) |
||
| buf.writelines(result.warnings) | ||
| if self.debug_enabled: | ||
| debug_log = result.get_debug_log() | ||
| if debug_log: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
|
|
||
| Standard error: | ||
| Warnings: | ||
| <data_function>:3: UserWarning: Pandas doesn't allow columns to be created via a new attribute name - see https://pandas.pydata.org/pandas-docs/stable/indexing.html#attribute-access |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
|
|
||
| Warnings: | ||
| <data_function>:4: UserWarning: bad value at row 0 | ||
| <data_function>:4: UserWarning: bad value at row 1 | ||
| <data_function>:4: UserWarning: bad value at row 2 | ||
| <data_function>:4: UserWarning: bad value at row 3 | ||
| <data_function>:4: UserWarning: bad value at row 4 |
Uh oh!
There was an error while loading. Please reload this page.