Skip to content

Commit

Permalink
fixed codacy issues regarding exception types
Browse files Browse the repository at this point in the history
  • Loading branch information
julffers committed Mar 27, 2017
1 parent e1ef688 commit ea8d90a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 31 deletions.
39 changes: 18 additions & 21 deletions pandapower/diagnostic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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")
Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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'

Expand All @@ -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'

Expand All @@ -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'

Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down
20 changes: 10 additions & 10 deletions pandapower/test/api/test_diagnostic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')]}

Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit ea8d90a

Please sign in to comment.