From ea8d90a93c3cc97efd29b3ea65b3d3dcd52b3032 Mon Sep 17 00:00:00 2001 From: Jan Ulffers Date: Mon, 27 Mar 2017 14:00:25 +0200 Subject: [PATCH] fixed codacy issues regarding exception types --- pandapower/diagnostic.py | 39 ++++++++++++-------------- pandapower/test/api/test_diagnostic.py | 20 ++++++------- 2 files changed, 28 insertions(+), 31 deletions(-) diff --git a/pandapower/diagnostic.py b/pandapower/diagnostic.py index 4e7ce64a0..57fe60f96 100644 --- a/pandapower/diagnostic.py +++ b/pandapower/diagnostic.py @@ -4,8 +4,10 @@ # Energy System Technology (IWES), Kassel. All rights reserved. Use of this source code is governed # by a BSD-style license that can be found in the LICENSE file. -import copy + +import copy +import pandas as pd import numpy as np try: @@ -19,6 +21,7 @@ from pandapower.run import runpp from pandapower.diagnostic_reports import diagnostic_report from pandapower.toolbox import get_connected_elements +from pandapower.powerflow import LoadflowNotConverged # separator between log messages log_message_sep = ("\n --------\n") @@ -211,10 +214,9 @@ def check_pos_int(element, element_index, column): def check_number(element, element_index, column): try: nan_check = np.isnan(element[column]) - if nan_check or type(element[column]) == bool: + if nan_check or isinstance(element[column], bool): return element_index - - except: + except TypeError: return element_index @@ -313,18 +315,13 @@ def invalid_values(net): if key not in check_results: check_results[key] = [] # converts np.nan to str for easier usage of assert in pytest - try: - nan_check = np.isnan(net[key][value[0]].at[i]) - if nan_check: - check_results[key].append((i, value[0], - str(net[key][value[0]].at[i]), value[1])) - else: - check_results[key].append((i, value[0], - net[key][value[0]].at[i], value[1])) - except: + nan_check = pd.isnull(net[key][value[0]].at[i]) + if nan_check: + check_results[key].append((i, value[0], + str(net[key][value[0]].at[i]), value[1])) + else: check_results[key].append((i, value[0], net[key][value[0]].at[i], value[1])) - return check_results @@ -392,14 +389,14 @@ def overload(net, overload_scaling_factor): try: runpp(net) - except: + except LoadflowNotConverged: try: net.load.scaling = overload_scaling_factor runpp(net) net.load.scaling = load_scaling check_result['load'] = True - except: + except LoadflowNotConverged: net.load.scaling = load_scaling check_result['load'] = 'uncertain' @@ -411,7 +408,7 @@ def overload(net, overload_scaling_factor): net.gen.scaling = sgen_scaling check_result['generation'] = True - except: + except LoadflowNotConverged: net.gen.scaling = gen_scaling check_result['generation'] = 'uncertain' @@ -435,13 +432,13 @@ def wrong_switch_configuration(net): try: runpp(net) - except: + except (ValueError, LoadflowNotConverged): try: net.switch.closed = 1 runpp(net) net.switch.closed = switch_configuration return True - except: + except LoadflowNotConverged: net.switch.closed = switch_configuration return 'uncertain' @@ -796,7 +793,7 @@ def numba_comparison(net, numba_tolerance): check_results = {} try: runpp(net, numba=True) - except: + except LoadflowNotConverged: pass if net.converged: try: @@ -820,7 +817,7 @@ def numba_comparison(net, numba_tolerance): numba_true = result_numba_true[key][col][diffs[col]] numba_false = result_numba_false[key][col][diffs[col]] check_results[key][col] = abs(numba_true - numba_false) - except: + except LoadflowNotConverged: pass if check_results: diff --git a/pandapower/test/api/test_diagnostic.py b/pandapower/test/api/test_diagnostic.py index af61e0f30..0c44e3f7b 100644 --- a/pandapower/test/api/test_diagnostic.py +++ b/pandapower/test/api/test_diagnostic.py @@ -212,7 +212,7 @@ def test_boolean(self, test_net, diag_params, report_methods): 'gen': [(0, 'in_service', '1', 'boolean')], 'sgen': [(2, 'in_service', '0', 'boolean'), (3, 'in_service', '0.0', 'boolean')], 'switch': [(1, 'closed', 'False', 'boolean'), (3, 'closed', 'False', 'boolean'), - (4, 'closed', None, 'boolean'), (5, 'closed', 10, 'boolean')], + (4, 'closed', 'None', 'boolean'), (5, 'closed', 10, 'boolean')], 'trafo': [(0, 'in_service', 'True', 'boolean')], 'trafo3w': [(0, 'in_service', 'nan', 'boolean')]} @@ -353,18 +353,18 @@ def test_switch_type(self, test_net, diag_params, report_methods): assert diag_results[check_function] == \ {'switch': [(0, 'et', 'bus', 'switch_type'), (1, 'et', 1, 'switch_type'), - (2, 'et', None, 'switch_type'), + (2, 'et', 'None', 'switch_type'), (3, 'et', True, 'switch_type')]} for bool_value in [True, False]: - diag_report = DiagnosticReports(net, diag_results, diag_params, compact_report=bool_value) - report_check = None - try: - eval(report_methods[check_function]) - report_check = True - except: - report_check = False - assert report_check + diag_report = DiagnosticReports(net, diag_results, diag_params, compact_report=bool_value) + report_check = None + try: + eval(report_methods[check_function]) + report_check = True + except: + report_check = False + assert report_check def test_no_ext_grid(test_net, diag_params, report_methods):