Show More
@@ -122,6 +122,7 b' else:' | |||||
122 | __all__ = ['pretty', 'pprint', 'PrettyPrinter', 'RepresentationPrinter', |
|
122 | __all__ = ['pretty', 'pprint', 'PrettyPrinter', 'RepresentationPrinter', | |
123 | 'for_type', 'for_type_by_name'] |
|
123 | 'for_type', 'for_type_by_name'] | |
124 |
|
124 | |||
|
125 | ||||
125 | _re_pattern_type = type(re.compile('')) |
|
126 | _re_pattern_type = type(re.compile('')) | |
126 |
|
127 | |||
127 | def _failed_repr(obj, e): |
|
128 | def _failed_repr(obj, e): | |
@@ -326,6 +327,16 b' class PrettyPrinter(_PrettyPrinterBase):' | |||||
326 | self.group_queue.enq(group) |
|
327 | self.group_queue.enq(group) | |
327 | self.indentation += indent |
|
328 | self.indentation += indent | |
328 |
|
329 | |||
|
330 | def _enumerate(self, seq): | |||
|
331 | """like enumerate, but with an upper limit on the number of items""" | |||
|
332 | for idx, x in enumerate(seq): | |||
|
333 | if idx >= self.max_seq_length: | |||
|
334 | self.text(',') | |||
|
335 | self.breakable() | |||
|
336 | self.text('...') | |||
|
337 | raise StopIteration | |||
|
338 | yield idx, x | |||
|
339 | ||||
329 | def end_group(self, dedent=0, close=''): |
|
340 | def end_group(self, dedent=0, close=''): | |
330 | """End a group. See `begin_group` for more details.""" |
|
341 | """End a group. See `begin_group` for more details.""" | |
331 | self.indentation -= dedent |
|
342 | self.indentation -= dedent | |
@@ -583,13 +594,6 b' def _default_pprint(obj, p, cycle):' | |||||
583 | p.end_group(1, '>') |
|
594 | p.end_group(1, '>') | |
584 |
|
595 | |||
585 |
|
596 | |||
586 | def _enumerate(seq, limit): |
|
|||
587 | """like enumerate, but with an upper limit on the number of items""" |
|
|||
588 | for idx, x in enumerate(seq): |
|
|||
589 | if idx >= limit: |
|
|||
590 | raise StopIteration |
|
|||
591 | yield idx, x |
|
|||
592 |
|
||||
593 | def _seq_pprinter_factory(start, end, basetype): |
|
597 | def _seq_pprinter_factory(start, end, basetype): | |
594 | """ |
|
598 | """ | |
595 | Factory that returns a pprint function useful for sequences. Used by |
|
599 | Factory that returns a pprint function useful for sequences. Used by | |
@@ -605,17 +609,12 b' def _seq_pprinter_factory(start, end, basetype):' | |||||
605 | return p.text(start + '...' + end) |
|
609 | return p.text(start + '...' + end) | |
606 | step = len(start) |
|
610 | step = len(start) | |
607 | p.begin_group(step, start) |
|
611 | p.begin_group(step, start) | |
608 |
for idx, x in _enumerate(obj |
|
612 | for idx, x in p._enumerate(obj): | |
609 | if idx: |
|
613 | if idx: | |
610 | p.text(',') |
|
614 | p.text(',') | |
611 | p.breakable() |
|
615 | p.breakable() | |
612 | p.pretty(x) |
|
616 | p.pretty(x) | |
613 |
|
617 | if len(obj) == 1 and type(obj) is tuple: | ||
614 | if len(obj) >= p.max_seq_length: |
|
|||
615 | p.text(',') |
|
|||
616 | p.breakable() |
|
|||
617 | p.text('...') |
|
|||
618 | elif len(obj) == 1 and type(obj) is tuple: |
|
|||
619 | # Special case for 1-item tuples. |
|
618 | # Special case for 1-item tuples. | |
620 | p.text(',') |
|
619 | p.text(',') | |
621 | p.end_group(step, end) |
|
620 | p.end_group(step, end) | |
@@ -647,16 +646,11 b' def _set_pprinter_factory(start, end, basetype):' | |||||
647 | except Exception: |
|
646 | except Exception: | |
648 | # Sometimes the items don't sort. |
|
647 | # Sometimes the items don't sort. | |
649 | pass |
|
648 | pass | |
650 |
for idx, x in _enumerate(items |
|
649 | for idx, x in p._enumerate(items): | |
651 | if idx: |
|
650 | if idx: | |
652 | p.text(',') |
|
651 | p.text(',') | |
653 | p.breakable() |
|
652 | p.breakable() | |
654 | p.pretty(x) |
|
653 | p.pretty(x) | |
655 |
|
||||
656 | if len(obj) >= p.max_seq_length: |
|
|||
657 | p.text(',') |
|
|||
658 | p.breakable() |
|
|||
659 | p.text('...') |
|
|||
660 | p.end_group(step, end) |
|
654 | p.end_group(step, end) | |
661 | return inner |
|
655 | return inner | |
662 |
|
656 | |||
@@ -681,18 +675,13 b' def _dict_pprinter_factory(start, end, basetype=None):' | |||||
681 | except Exception as e: |
|
675 | except Exception as e: | |
682 | # Sometimes the keys don't sort. |
|
676 | # Sometimes the keys don't sort. | |
683 | pass |
|
677 | pass | |
684 |
for idx, key in _enumerate(keys |
|
678 | for idx, key in p._enumerate(keys): | |
685 | if idx: |
|
679 | if idx: | |
686 | p.text(',') |
|
680 | p.text(',') | |
687 | p.breakable() |
|
681 | p.breakable() | |
688 | p.pretty(key) |
|
682 | p.pretty(key) | |
689 | p.text(': ') |
|
683 | p.text(': ') | |
690 | p.pretty(obj[key]) |
|
684 | p.pretty(obj[key]) | |
691 |
|
||||
692 | if len(obj) >= p.max_seq_length: |
|
|||
693 | p.text(',') |
|
|||
694 | p.breakable() |
|
|||
695 | p.text('...') |
|
|||
696 | p.end_group(1, end) |
|
685 | p.end_group(1, end) | |
697 | return inner |
|
686 | return inner | |
698 |
|
687 |
General Comments 0
You need to be logged in to leave comments.
Login now