From 07a0788ea495b42c91933c7362b69825e14b5be2 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Thu, 6 Oct 2016 12:48:15 -0400 Subject: [PATCH] Move the pretty_print_dict() method out of the class (make it a function). --- errors.py | 49 +++++++++++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/errors.py b/errors.py index f46c4a4..bb52cac 100644 --- a/errors.py +++ b/errors.py @@ -5,6 +5,25 @@ Errors that can occur when solving a linear game. from cvxopt import matrix +def _pretty_print_dict(dictionary): + """ + Return a pretty-printed string representation of a dictionary + containing CVXOPT matrices. + """ + result = '' + for (key, value) in dictionary.items(): + if isinstance(value, matrix): + # Display matrices on their own lines, indented. + result += ' {:s}:'.format(key) + colvec = '\n{!s}'.format(value) + result += '\n '.join(colvec.splitlines()) + result += '\n' + else: + result += ' {:s}: {!s}\n'.format(key, value) + + return result + + class GameUnsolvableException(Exception): """ Every linear game has a solution (this follows from a general @@ -12,13 +31,6 @@ class GameUnsolvableException(Exception): with a linear game, then something is wrong with either the model of the input. """ - def __str__(self): - tpl = 'Solution failed with result "{:s}."\n' \ - 'CVXOPT returned:\n{!s}' - return tpl.format(self._solution_dict['status'], - self._pretty_print_dict(self._solution_dict)) - - def __init__(self, solution_dict): """ Create a new GameUnsolvableException object. @@ -29,23 +41,12 @@ class GameUnsolvableException(Exception): cone program. """ + super().__init__() self._solution_dict = solution_dict - def _pretty_print_dict(self, solution_dict): - """ - Return a pretty-printed string representation of a CVXOPT - solution dictionary. - """ - result = '' - for (k,v) in solution_dict.items(): - if isinstance(v, matrix): - # Display matrices on their own lines, indented. - result += ' {:s}:'.format(k) - colvec = '\n{!s}'.format(v) - result += '\n '.join(colvec.splitlines()) - result += '\n' - else: - result += ' {:s}: {!s}\n'.format(k,v) - - return result + def __str__(self): + tpl = 'Solution failed with result "{:s}."\n' \ + 'CVXOPT returned:\n{!s}' + return tpl.format(self._solution_dict['status'], + _pretty_print_dict(self._solution_dict)) -- 2.44.2