From: Michael Orlitzky Date: Thu, 6 Oct 2016 16:48:15 +0000 (-0400) Subject: Move the pretty_print_dict() method out of the class (make it a function). X-Git-Tag: 0.1.0~208 X-Git-Url: http://gitweb.michael.orlitzky.com/?a=commitdiff_plain;h=07a0788ea495b42c91933c7362b69825e14b5be2;p=dunshire.git Move the pretty_print_dict() method out of the class (make it a function). --- 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))