]> gitweb.michael.orlitzky.com - dunshire.git/commitdiff
Move the pretty_print_dict() method out of the class (make it a function).
authorMichael Orlitzky <michael@orlitzky.com>
Thu, 6 Oct 2016 16:48:15 +0000 (12:48 -0400)
committerMichael Orlitzky <michael@orlitzky.com>
Thu, 6 Oct 2016 16:48:15 +0000 (12:48 -0400)
errors.py

index f46c4a46b6cdf46d7e1ba37e29246163bb78a692..bb52cac66b194b67aedc5d1f8fc95514fd040cb8 100644 (file)
--- 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))