From adceefbe0b38e74d976e333ed32bffc13e43d15c Mon Sep 17 00:00:00 2001 From: Toshio Kuratomi Date: Fri, 12 Dec 2014 09:10:50 -0800 Subject: [PATCH] Alternate pprint for values --- q.py | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/q.py b/q.py index 1bf76d0..5a05ecb 100644 --- a/q.py +++ b/q.py @@ -66,7 +66,7 @@ class Q(object): __doc__ = __doc__ # from the module's __doc__ above - import ast, code, inspect, os, pydoc, sys, random, re, time + import ast, code, inspect, os, pydoc, sys, random, re, time, pprint # The debugging log will go to this file; temporary files will also have # this path as a prefix, followed by a random number. @@ -177,6 +177,13 @@ def unindent(self, lines): indent = min(len(self.re.match(r'^ *', line).group()) for line in lines) return [line[indent:].rstrip() for line in lines] + def save_large_value(self, value): + if isinstance(value, self.TEXT_TYPES): + value = value.encode('utf-8') + path = self.OUTPUT_PATH + '%08d.txt' % self.random.randrange(1e8) + self.FileWriter(path).write('w', value) + return path + def safe_repr(self, value): # TODO: Use colour to distinguish '...' elision from actual '...' chars. # TODO: Show a nicer repr for SRE.Match objects. @@ -184,11 +191,17 @@ def safe_repr(self, value): result = self.TEXT_REPR.repr(value) if isinstance(value, self.BASESTRING_TYPES) and len(value) > 80: # If the string is big, save it to a file for later examination. - if isinstance(value, self.TEXT_TYPES): - value = value.encode('utf-8') - path = self.OUTPUT_PATH + '%08d.txt' % self.random.randrange(1e8) - self.FileWriter(path).write('w', value) + path = self.save_large_value(value) result += ' (file://' + path + ')' + else: + # Use pretty print if no specific choices were found + pp = self.pprint.PrettyPrinter(indent=1) + formatted = pp.pformat(value) + if len(formatted) > 80: + path = self.save_large_value(formatted) + result += ' (file://' + path + ')' + else: + result = formatted return result def get_call_exprs(self, line):