Skip to content

Commit

Permalink
Alternate pprint for values
Browse files Browse the repository at this point in the history
As discussed in zestyping#21 I've made some changes
to the pprint change proposed there.

* This will save long values into a separate file.

* This makes pprint of the variable the default.  I think this is a good idea
  because it will handle things like dicts nested within lists and other
  builtin types.
  • Loading branch information
abadger authored and mithro committed Jun 10, 2015
1 parent b58595d commit e4129f2
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions q.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class Q(object):
import inspect
import os
import pydoc
import pprint
import sys
import random
import re
Expand Down Expand Up @@ -190,18 +191,31 @@ def unindent(self, lines):
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 '...'
# TODO: Show a nicer repr for SRE.Match objects.
# TODO: Show a nicer repr for big multiline strings.
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):
Expand Down

0 comments on commit e4129f2

Please sign in to comment.