def test_keyerror_and_index_error(self): with suppress(KeyError): my_dict = {'key': 'value'} my_dict[4] self.assertEqual(my_dict, {'key': 'value'}) with suppress(IndexError): my_list = ['item'] my_list[1] self.assertEqual(my_list, ['item'])
def test_suppresses_parent_exceptions(self): with suppress(LookupError): my_dict = {'key': 'value'} my_dict[4] self.assertEqual(my_dict, {'key': 'value'}) with suppress(LookupError): my_list = ['item'] my_list[1] self.assertEqual(my_list, ['item'])
def test_suppresses_parent_exceptions(self): with suppress(LookupError): my_dict = {"key": "value"} my_dict[4] self.assertEqual(my_dict, {"key": "value"}) with suppress(LookupError): my_list = ["item"] my_list[1] self.assertEqual(my_list, ["item"])
def test_keyerror_and_index_error(self): with suppress(KeyError): my_dict = {"key": "value"} my_dict[4] self.assertEqual(my_dict, {"key": "value"}) with suppress(IndexError): my_list = ["item"] my_list[1] self.assertEqual(my_list, ["item"])
def test_catches_any_number_of_exceptions(self): with suppress(ValueError, TypeError): int('hello') with suppress(IndexError, TypeError): int(None) with self.assertRaises(KeyError): with suppress(IndexError, TypeError): {0: 1}[1] with suppress(ValueError, SystemError, IndexError, TypeError): ['item'][1]
def test_does_not_suppress_other_exceptions(self): with self.assertRaises(KeyError): with suppress(IndexError): my_dict = {'key': 'value'} my_dict[4] self.assertEqual(my_dict, {'key': 'value'}) with self.assertRaises(IndexError): with suppress(KeyError): my_list = ['item'] my_list[1] self.assertEqual(my_list, ['item'])
def test_suppress_specific_exception(self): with suppress(ValueError): x = 1 int('hello') x = 2 self.assertEqual(x, 1) with suppress(TypeError): x = 3 int(None) x = 4 self.assertEqual(x, 3)
def test_does_not_suppress_other_exceptions(self): with self.assertRaises(KeyError): with suppress(IndexError): my_dict = {"key": "value"} my_dict[4] self.assertEqual(my_dict, {"key": "value"}) with self.assertRaises(IndexError): with suppress(KeyError): my_list = ["item"] my_list[1] self.assertEqual(my_list, ["item"])
from suppress import suppress with suppress(KeyError, TypeError): a = 5
def test_allows_exception_to_be_viewed(self): with suppress(LookupError) as suppressed: my_dict = {'key': 'value'} my_dict[4] self.assertEqual(type(suppressed.exception), KeyError) self.assertEqual(type(suppressed.traceback), TracebackType)
def test_works_when_no_exception_raised(self): with suppress(Exception): x = 4 self.assertEqual(x, 4)
states = splitLen sData = markov_anneal.splitActivityMax(oData.cd[0:50000], splitLen) scores = [0, 0] entropys = [0, 0] for i in range(2, 26): print "Models:" + str(i) bestScore = -1 bestModels = [] bestData = [] bestOut = [] for j in range(2): suppress.suppress(2) bm, bd, out = markov_anneal.train(sData, i, states, obs, \ iterations = 9, outliers = False, voidOutput = False) suppress.restore(2) mea = markov_anneal.modelErrorAverage(bm, bd, obs) entropy = markov_anneal.assignedDataEntropy(bd, out) score = (2**(sum(mea)/(len(mea) * 1.0)))*entropy if bestScore == -1 or score < bestScore: bestScore = score bestEnt = entropy print " best score:" + str(bestScore) + " best entropy:" + str(bestEnt) scores.append(bestScore) entropys.append(bestEnt)
states = splitLen sData = markov_anneal.splitActivityMax(oData.cd[0:50000], splitLen) scores = [0, 0] entropys = [0, 0] for i in range(2, 26): print "Models:" + str(i) bestScore = -1 bestModels = [] bestData = [] bestOut = [] for j in range(2): suppress.suppress(2) bm, bd, out = markov_anneal.train(sData, i, states, obs, \ iterations = 9, outliers = False, voidOutput = False) suppress.restore(2) mea = markov_anneal.modelErrorAverage(bm, bd, obs) entropy = markov_anneal.assignedDataEntropy(bd, out) score = (2**(sum(mea) / (len(mea) * 1.0))) * entropy if bestScore == -1 or score < bestScore: bestScore = score bestEnt = entropy print " best score:" + str(bestScore) + " best entropy:" + str( bestEnt) scores.append(bestScore) entropys.append(bestEnt)