##// END OF EJS Templates
make pretty.max_seq_length configurable...
Min RK -
Show More
@@ -589,7 +589,14 b' class PlainTextFormatter(BaseFormatter):'
589 # This subclass ignores this attribute as it always need to return
589 # This subclass ignores this attribute as it always need to return
590 # something.
590 # something.
591 enabled = Bool(True, config=False)
591 enabled = Bool(True, config=False)
592
592
593 max_seq_length = Integer(pretty.MAX_SEQ_LENGTH, config=True,
594 help="""Truncate large collections (lists, dicts, tuples, sets) to this size.
595
596 Set to 0 to disable truncation.
597 """
598 )
599
593 # Look for a _repr_pretty_ methods to use for pretty printing.
600 # Look for a _repr_pretty_ methods to use for pretty printing.
594 print_method = ObjectName('_repr_pretty_')
601 print_method = ObjectName('_repr_pretty_')
595
602
@@ -682,6 +689,7 b' class PlainTextFormatter(BaseFormatter):'
682 # or it will cause trouble.
689 # or it will cause trouble.
683 printer = pretty.RepresentationPrinter(stream, self.verbose,
690 printer = pretty.RepresentationPrinter(stream, self.verbose,
684 self.max_width, unicode_to_str(self.newline),
691 self.max_width, unicode_to_str(self.newline),
692 max_seq_length=self.max_seq_length,
685 singleton_pprinters=self.singleton_printers,
693 singleton_pprinters=self.singleton_printers,
686 type_pprinters=self.type_printers,
694 type_pprinters=self.type_printers,
687 deferred_pprinters=self.deferred_printers)
695 deferred_pprinters=self.deferred_printers)
@@ -338,3 +338,15 b' def test_format_config():'
338 result = f(Config)
338 result = f(Config)
339 nt.assert_is(result, None)
339 nt.assert_is(result, None)
340 nt.assert_equal(captured.stderr, "")
340 nt.assert_equal(captured.stderr, "")
341
342 def test_pretty_max_seq_length():
343 f = PlainTextFormatter(max_seq_length=1)
344 lis = list(range(3))
345 text = f(lis)
346 nt.assert_equal(text, '[0, ...]')
347 f.max_seq_length = 0
348 text = f(lis)
349 nt.assert_equal(text, '[0, 1, 2]')
350 text = f(list(range(1024)))
351 lines = text.splitlines()
352 nt.assert_equal(len(lines), 1024)
@@ -123,9 +123,9 b" __all__ = ['pretty', 'pprint', 'PrettyPrinter', 'RepresentationPrinter',"
123 'for_type', 'for_type_by_name']
123 'for_type', 'for_type_by_name']
124
124
125
125
126 MAX_SEQ_LENGTH = 1000
126 _re_pattern_type = type(re.compile(''))
127 _re_pattern_type = type(re.compile(''))
127
128
128
129 def _safe_getattr(obj, attr, default=None):
129 def _safe_getattr(obj, attr, default=None):
130 """Safe version of getattr.
130 """Safe version of getattr.
131
131
@@ -137,22 +137,22 b' def _safe_getattr(obj, attr, default=None):'
137 except Exception:
137 except Exception:
138 return default
138 return default
139
139
140 def pretty(obj, verbose=False, max_width=79, newline='\n'):
140 def pretty(obj, verbose=False, max_width=79, newline='\n', max_seq_length=MAX_SEQ_LENGTH):
141 """
141 """
142 Pretty print the object's representation.
142 Pretty print the object's representation.
143 """
143 """
144 stream = StringIO()
144 stream = StringIO()
145 printer = RepresentationPrinter(stream, verbose, max_width, newline)
145 printer = RepresentationPrinter(stream, verbose, max_width, newline, max_seq_length)
146 printer.pretty(obj)
146 printer.pretty(obj)
147 printer.flush()
147 printer.flush()
148 return stream.getvalue()
148 return stream.getvalue()
149
149
150
150
151 def pprint(obj, verbose=False, max_width=79, newline='\n'):
151 def pprint(obj, verbose=False, max_width=79, newline='\n', max_seq_length=MAX_SEQ_LENGTH):
152 """
152 """
153 Like `pretty` but print to stdout.
153 Like `pretty` but print to stdout.
154 """
154 """
155 printer = RepresentationPrinter(sys.stdout, verbose, max_width, newline)
155 printer = RepresentationPrinter(sys.stdout, verbose, max_width, newline, max_seq_length)
156 printer.pretty(obj)
156 printer.pretty(obj)
157 printer.flush()
157 printer.flush()
158 sys.stdout.write(newline)
158 sys.stdout.write(newline)
@@ -186,7 +186,7 b' class PrettyPrinter(_PrettyPrinterBase):'
186 callback method.
186 callback method.
187 """
187 """
188
188
189 def __init__(self, output, max_width=79, newline='\n', max_seq_length=1000):
189 def __init__(self, output, max_width=79, newline='\n', max_seq_length=MAX_SEQ_LENGTH):
190 self.output = output
190 self.output = output
191 self.max_width = max_width
191 self.max_width = max_width
192 self.newline = newline
192 self.newline = newline
@@ -346,9 +346,10 b' class RepresentationPrinter(PrettyPrinter):'
346 """
346 """
347
347
348 def __init__(self, output, verbose=False, max_width=79, newline='\n',
348 def __init__(self, output, verbose=False, max_width=79, newline='\n',
349 singleton_pprinters=None, type_pprinters=None, deferred_pprinters=None):
349 singleton_pprinters=None, type_pprinters=None, deferred_pprinters=None,
350 max_seq_length=MAX_SEQ_LENGTH):
350
351
351 PrettyPrinter.__init__(self, output, max_width, newline)
352 PrettyPrinter.__init__(self, output, max_width, newline, max_seq_length=max_seq_length)
352 self.verbose = verbose
353 self.verbose = verbose
353 self.stack = []
354 self.stack = []
354 if singleton_pprinters is None:
355 if singleton_pprinters is None:
General Comments 0
You need to be logged in to leave comments. Login now