-
Notifications
You must be signed in to change notification settings - Fork 15
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
PP bug fix and crash log #154
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e5334e4
postprocessing bug fix
rajeee 06df232
Adding detailed crash logging feature
rajeee ccfccd2
Merge branch 'master' into pp_bug_fix_and_crash_log
rajeee a3e1287
Added tests for error logging
rajeee b5faaca
Merge branch 'pp_bug_fix_and_crash_log' of github.com:NREL/buildstock…
rajeee f4d0da0
Merge branch 'master' into pp_bug_fix_and_crash_log
nmerket d3adf5d
test the too many partition edge case
rajeee 2485e6a
tempfile fix required for windows
rajeee 184b3f8
tempfile fix
rajeee 5091f8d
style fix
rajeee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import tempfile | ||
from buildstockbatch.utils import log_error_details, _str_repr | ||
import pytest | ||
import os | ||
|
||
|
||
def test_str_repr(): | ||
test_obj = [{1, 2, 3, 4, 5, 6}, {"List1": ["Item1", ('a', 'b', 'c', 'd'), "item3"], | ||
"long_name_list": ["long_name_one_two_three", "long_name"], | ||
"dict": {"key1": ["List_item1", "List_item2", "List_item3"], "Key2": "value2", | ||
"key3": "value3", "key4": "val4"}}] | ||
|
||
gen_repr = _str_repr(test_obj, list_max=2, dict_max=3, string_max=10) | ||
true_repr = "[{'1','2','3' ...6},{'List1': ['Item1',('a','b' ...4) ...3],'long_...14..._list': ['long_...23..."\ | ||
"three','long_name'],'dict': {'key1': ['List_item1','List_item2' ...3],'Key2': 'value2',"\ | ||
"'key3': 'value3' ...4}}]" | ||
|
||
assert true_repr == gen_repr | ||
|
||
|
||
def test_get_error_details(): | ||
tf = tempfile.NamedTemporaryFile('w+', delete=False) | ||
tf.close() | ||
|
||
@log_error_details(tf.name) | ||
def failing_function1(arg1): | ||
level_1_string = f"string1_{arg1}" | ||
level_1_dict = {"key1": "value1"} | ||
|
||
def failing_function2(arg2): | ||
level_2_string = f"string2_{arg2}" | ||
level_2_list = ["level_2_str1", "level_2_str2"] | ||
if level_2_string and level_2_list: | ||
raise KeyError("actual dummy exception") | ||
|
||
if level_1_dict and level_1_string: | ||
failing_function2("my_arg2") | ||
|
||
with pytest.raises(KeyError) as ex_info: | ||
failing_function1("my_arg1") | ||
|
||
assert "actual dummy exception" in str(ex_info.value) | ||
with open(tf.name, 'r') as f: | ||
error_log = f.read() | ||
assert "'arg1':'my_arg1'" in error_log | ||
assert "'level_1_string':'string1_my_arg1'" in error_log | ||
assert "'level_1_dict':{'key1': 'value1'}" in error_log | ||
assert "'arg2':'my_arg2'" in error_log | ||
assert "'level_2_string':'string2_my_arg2'" in error_log | ||
assert "'level_2_list':['level_2_str1','level_2_str2']" in error_log | ||
os.remove(tf.name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import traceback | ||
import inspect | ||
|
||
|
||
def _str_repr(obj, list_max=20, dict_max=20, string_max=100): | ||
if type(obj) is str: | ||
if len(obj) <= string_max: | ||
return f"'{obj}'" | ||
else: | ||
return f"'{obj[0:string_max//2]}...{len(obj)}...{obj[-string_max//2:]}'" | ||
elif type(obj) in [int, float]: | ||
return _str_repr(str(obj), list_max, dict_max, string_max) | ||
elif type(obj) is list: | ||
txt = "[" + ",".join([_str_repr(item, list_max, dict_max, string_max) for item in obj[0:list_max]]) | ||
if len(obj) > list_max: | ||
txt += f" ...{len(obj)}" | ||
txt += "]" | ||
return txt | ||
elif type(obj) is tuple: | ||
txt = "(" + ",".join([_str_repr(item, list_max, dict_max, string_max) for item in obj[0:list_max]]) | ||
if len(obj) > list_max: | ||
txt += f" ...{len(obj)}" | ||
txt += ")" | ||
return txt | ||
elif type(obj) is set: | ||
obj = list(obj) | ||
txt = "{" + ",".join([_str_repr(item, list_max, dict_max, string_max) for item in obj[0:dict_max]]) | ||
if len(obj) > dict_max: | ||
txt += f" ...{len(obj)}" | ||
txt += "}" | ||
return txt | ||
elif type(obj) is dict: | ||
keys = list(obj.keys()) | ||
txt = "{" + ",".join([f"{_str_repr(key, list_max, dict_max, string_max)}:" | ||
f" {_str_repr(obj[key], list_max, dict_max, string_max)}" for key in keys[0:dict_max]]) | ||
if len(keys) > dict_max: | ||
txt += f" ...{len(keys)}" | ||
txt += "}" | ||
return txt | ||
else: | ||
return str(obj) | ||
|
||
|
||
def _get_error_details(): | ||
text = "" | ||
text += traceback.format_exc() | ||
frames = inspect.trace() | ||
for frame in frames: | ||
text += f'\nIn file: {frame[1]}, module {str(frame[3])} line: {frame[2]} \n' | ||
text += "Local Variables: " | ||
for var, value in frame[0].f_locals.items(): | ||
text += _str_repr(var) + ":" + _str_repr(value) | ||
text += "\n" | ||
return text | ||
|
||
|
||
def log_error_details(output_file="buildstockbatch_crash_details.log"): | ||
def log_error_decorator(func): | ||
def run_with_error_capture(*args, **kwargs): | ||
try: | ||
return func(*args, **kwargs) | ||
except Exception: | ||
with open(output_file, "w") as f: | ||
f.write(_get_error_details()) | ||
raise | ||
return run_with_error_capture | ||
|
||
return log_error_decorator |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you have a test for this? To avoid having to make files that are actually big enough to hit this issue, you could mock some function in the line of calculation for
total_mem
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have added the test.