56649bb
From 20b6e154d08c41d4f06bcfb6393a7fd938fa6bcb Mon Sep 17 00:00:00 2001
56649bb
From: Toshio Kuratomi <toshio@fedoraproject.org>
56649bb
Date: Fri, 12 Dec 2014 09:10:50 -0800
56649bb
Subject: [PATCH] Alternate pprint for values
56649bb
56649bb
As discussed in https://github.com/zestyping/q/pull/21 I've made some changes
56649bb
to the pprint change proposed there.
56649bb
56649bb
* This will save long values into a separate file.
56649bb
56649bb
* This makes pprint of the variable the default.  I think this is a good idea
56649bb
  because it will handle things like dicts nested within lists and other
56649bb
  builtin types.
56649bb
---
56649bb
 q.py | 22 ++++++++++++++++++----
56649bb
 1 file changed, 18 insertions(+), 4 deletions(-)
56649bb
ace91d3
diff --git a/q.py b/q.py
56649bb
index ceacd74..b461d6a 100644
ace91d3
--- a/q.py
ace91d3
+++ b/q.py
56649bb
@@ -74,6 +74,7 @@ class Q(object):
56649bb
     import inspect
56649bb
     import os
56649bb
     import pydoc
56649bb
+    import pprint
56649bb
     import sys
56649bb
     import random
56649bb
     import re
56649bb
@@ -192,6 +193,13 @@ class Q(object):
56649bb
             len(self.re.match(r'^ *', line).group()) for line in lines)
ace91d3
         return [line[indent:].rstrip() for line in lines]
ace91d3
 
ace91d3
+    def save_large_value(self, value):
ace91d3
+        if isinstance(value,  self.TEXT_TYPES):
ace91d3
+            value = value.encode('utf-8')
ace91d3
+        path = self.OUTPUT_PATH + '%08d.txt' % self.random.randrange(1e8)
ace91d3
+        self.FileWriter(path).write('w', value)
ace91d3
+        return path
ace91d3
+
ace91d3
     def safe_repr(self, value):
56649bb
         # TODO: Use colour to distinguish '...' elision from actual '...'
ace91d3
         # TODO: Show a nicer repr for SRE.Match objects.
56649bb
@@ -199,11 +207,17 @@ class Q(object):
ace91d3
         result = self.TEXT_REPR.repr(value)
ace91d3
         if isinstance(value, self.BASESTRING_TYPES) and len(value) > 80:
ace91d3
             # If the string is big, save it to a file for later examination.
ace91d3
-            if isinstance(value,  self.TEXT_TYPES):
ace91d3
-                value = value.encode('utf-8')
ace91d3
-            path = self.OUTPUT_PATH + '%08d.txt' % self.random.randrange(1e8)
ace91d3
-            self.FileWriter(path).write('w', value)
ace91d3
+            path = self.save_large_value(value)
ace91d3
             result += ' (file://' + path + ')'
ace91d3
+        else:
ace91d3
+            # Use pretty print if no specific choices were found
ace91d3
+            pp = self.pprint.PrettyPrinter(indent=1)
ace91d3
+            formatted = pp.pformat(value)
ace91d3
+            if len(formatted) > 80:
ace91d3
+                path = self.save_large_value(formatted)
ace91d3
+                result += ' (file://' + path + ')'
ace91d3
+            else:
ace91d3
+                result = formatted
ace91d3
         return result
ace91d3
 
ace91d3
     def get_call_exprs(self, line):
56649bb
-- 
56649bb
2.10.2
56649bb