|
20 | 20 | This module is based on ruby's `prettyprint.rb` library by `Tanaka Akira`. |
21 | 21 | Example Usage |
22 | 22 | ------------- |
23 | | -To directly print the representation of an object use `pprint`:: |
24 | | - from pretty import pprint |
25 | | - pprint(complex_object) |
26 | 23 | To get a string of the output use `pretty`:: |
27 | 24 | from pretty import pretty |
28 | 25 | string = pretty(complex_object) |
@@ -73,15 +70,15 @@ def _repr_pretty_(self, p, cycle): |
73 | 70 | import datetime |
74 | 71 | import platform |
75 | 72 | import re |
76 | | -import sys |
| 73 | +import struct |
77 | 74 | import types |
78 | 75 | from collections import deque |
79 | 76 | from contextlib import contextmanager |
80 | 77 | from io import StringIO |
| 78 | +from math import copysign, isnan |
81 | 79 |
|
82 | 80 | __all__ = [ |
83 | 81 | "pretty", |
84 | | - "pprint", |
85 | 82 | "PrettyPrinter", |
86 | 83 | "RepresentationPrinter", |
87 | 84 | "for_type_by_name", |
@@ -120,19 +117,6 @@ def pretty( |
120 | 117 | return stream.getvalue() |
121 | 118 |
|
122 | 119 |
|
123 | | -def pprint( |
124 | | - obj, verbose=False, max_width=79, newline="\n", max_seq_length=MAX_SEQ_LENGTH |
125 | | -): |
126 | | - """Like `pretty` but print to stdout.""" |
127 | | - printer = RepresentationPrinter( |
128 | | - sys.stdout, verbose, max_width, newline, max_seq_length=max_seq_length |
129 | | - ) |
130 | | - printer.pretty(obj) |
131 | | - printer.flush() |
132 | | - sys.stdout.write(newline) |
133 | | - sys.stdout.flush() |
134 | | - |
135 | | - |
136 | 120 | class _PrettyPrinterBase: |
137 | 121 | @contextmanager |
138 | 122 | def indent(self, indent): |
@@ -179,6 +163,8 @@ def __init__( |
179 | 163 | self.group_queue = GroupQueue(root_group) |
180 | 164 | self.indentation = 0 |
181 | 165 |
|
| 166 | + self.snans = 0 |
| 167 | + |
182 | 168 | def _break_outer_groups(self): |
183 | 169 | while self.max_width < self.output_width + self.buffer_width: |
184 | 170 | group = self.group_queue.deq() |
@@ -284,6 +270,12 @@ def end_group(self, dedent=0, close=""): |
284 | 270 |
|
285 | 271 | def flush(self): |
286 | 272 | """Flush data that is left in the buffer.""" |
| 273 | + if self.snans: |
| 274 | + # Reset self.snans *before* calling breakable(), which might flush() |
| 275 | + snans = self.snans |
| 276 | + self.snans = 0 |
| 277 | + self.breakable(" ") |
| 278 | + self.text(f"# Saw {snans} signaling NaN" + "s" * (snans > 1)) |
287 | 279 | for data in self.buffer: |
288 | 280 | self.output_width += data.output(self.output, self.output_width) |
289 | 281 | self.buffer.clear() |
@@ -753,10 +745,20 @@ def _exception_pprint(obj, p, cycle): |
753 | 745 | p.end_group(step, ")") |
754 | 746 |
|
755 | 747 |
|
| 748 | +def _repr_float_counting_nans(obj, p, cycle): |
| 749 | + if isnan(obj) and hasattr(p, "snans"): |
| 750 | + if struct.pack("!d", abs(obj)) != struct.pack("!d", float("nan")): |
| 751 | + p.snans += 1 |
| 752 | + if copysign(1.0, obj) == -1.0: |
| 753 | + p.text("-nan") |
| 754 | + return |
| 755 | + p.text(repr(obj)) |
| 756 | + |
| 757 | + |
756 | 758 | #: printers for builtin types |
757 | 759 | _type_pprinters = { |
758 | 760 | int: _repr_pprint, |
759 | | - float: _repr_pprint, |
| 761 | + float: _repr_float_counting_nans, |
760 | 762 | str: _repr_pprint, |
761 | 763 | tuple: _seq_pprinter_factory("(", ")", tuple), |
762 | 764 | list: _seq_pprinter_factory("[", "]", list), |
|
0 commit comments