Guido van Rossum | 16d27e3 | 1996-08-21 16:28:53 +0000 | [diff] [blame] | 1 | # pprint.py |
| 2 | # |
| 3 | # Author: Fred L. Drake, Jr. |
Guido van Rossum | 8206fb9 | 1996-08-26 00:33:29 +0000 | [diff] [blame] | 4 | # fdrake@cnri.reston.va.us, fdrake@intr.net |
Guido van Rossum | 16d27e3 | 1996-08-21 16:28:53 +0000 | [diff] [blame] | 5 | # |
| 6 | # This is a simple little module I wrote to make life easier. I didn't |
| 7 | # see anything quite like it in the library, though I may have overlooked |
| 8 | # something. I wrote this when I was trying to read some heavily nested |
| 9 | # tuples with fairly non-descriptive content. This is modelled very much |
| 10 | # after Lisp/Scheme - style pretty-printing of lists. If you find it |
| 11 | # useful, thank small children who sleep at night. |
Guido van Rossum | 16d27e3 | 1996-08-21 16:28:53 +0000 | [diff] [blame] | 12 | |
| 13 | """Support to pretty-print lists, tuples, & dictionaries recursively. |
Guido van Rossum | 8206fb9 | 1996-08-26 00:33:29 +0000 | [diff] [blame] | 14 | Very simple, but useful, especially in debugging data structures. |
Guido van Rossum | 16d27e3 | 1996-08-21 16:28:53 +0000 | [diff] [blame] | 15 | |
Guido van Rossum | 8206fb9 | 1996-08-26 00:33:29 +0000 | [diff] [blame] | 16 | Constants |
| 17 | --------- |
Guido van Rossum | 16d27e3 | 1996-08-21 16:28:53 +0000 | [diff] [blame] | 18 | |
Guido van Rossum | 8206fb9 | 1996-08-26 00:33:29 +0000 | [diff] [blame] | 19 | INDENT_PER_LEVEL |
| 20 | Amount of indentation to use for each new recursive level. The |
| 21 | default is 1. This must be a non-negative integer, and may be set |
| 22 | by the caller before calling pprint(). |
Guido van Rossum | 16d27e3 | 1996-08-21 16:28:53 +0000 | [diff] [blame] | 23 | |
Guido van Rossum | 8206fb9 | 1996-08-26 00:33:29 +0000 | [diff] [blame] | 24 | MAX_WIDTH |
| 25 | Maximum width of the display. This is only used if the |
| 26 | representation *can* be kept less than MAX_WIDTH characters wide. |
Fred Drake | b5d2039 | 1997-04-15 14:15:23 +0000 | [diff] [blame] | 27 | May be set by the user before calling pprint() if needed. |
Guido van Rossum | 16d27e3 | 1996-08-21 16:28:53 +0000 | [diff] [blame] | 28 | |
Guido van Rossum | 16d27e3 | 1996-08-21 16:28:53 +0000 | [diff] [blame] | 29 | """ |
| 30 | |
Guido van Rossum | 16d27e3 | 1996-08-21 16:28:53 +0000 | [diff] [blame] | 31 | INDENT_PER_LEVEL = 1 |
| 32 | |
| 33 | MAX_WIDTH = 80 |
| 34 | |
Guido van Rossum | 8206fb9 | 1996-08-26 00:33:29 +0000 | [diff] [blame] | 35 | from types import DictType, ListType, TupleType |
Guido van Rossum | 16d27e3 | 1996-08-21 16:28:53 +0000 | [diff] [blame] | 36 | |
| 37 | |
Fred Drake | b5d2039 | 1997-04-15 14:15:23 +0000 | [diff] [blame] | 38 | def pformat(seq): |
| 39 | """Format a Python object into a pretty-printed representation. |
Guido van Rossum | 16d27e3 | 1996-08-21 16:28:53 +0000 | [diff] [blame] | 40 | |
Fred Drake | b5d2039 | 1997-04-15 14:15:23 +0000 | [diff] [blame] | 41 | The representation is returned with no trailing newline. |
| 42 | |
Guido van Rossum | 8206fb9 | 1996-08-26 00:33:29 +0000 | [diff] [blame] | 43 | """ |
Fred Drake | b5d2039 | 1997-04-15 14:15:23 +0000 | [diff] [blame] | 44 | import StringIO |
| 45 | sio = StringIO.StringIO() |
| 46 | pprint(seq, stream=sio) |
| 47 | str = sio.getvalue() |
| 48 | if str and str[-1] == '\n': |
| 49 | str = str[:-1] |
| 50 | return str |
Guido van Rossum | 16d27e3 | 1996-08-21 16:28:53 +0000 | [diff] [blame] | 51 | |
| 52 | |
Fred Drake | b5d2039 | 1997-04-15 14:15:23 +0000 | [diff] [blame] | 53 | def pprint(seq, stream=None, indent=0, allowance=0): |
Guido van Rossum | 16d27e3 | 1996-08-21 16:28:53 +0000 | [diff] [blame] | 54 | """Pretty-print a list, tuple, or dictionary. |
| 55 | |
Guido van Rossum | 8206fb9 | 1996-08-26 00:33:29 +0000 | [diff] [blame] | 56 | seq |
| 57 | List, tuple, or dictionary object to be pretty-printed. Other |
| 58 | object types are permitted by are not specially interpreted. |
Guido van Rossum | 16d27e3 | 1996-08-21 16:28:53 +0000 | [diff] [blame] | 59 | |
Guido van Rossum | 8206fb9 | 1996-08-26 00:33:29 +0000 | [diff] [blame] | 60 | stream |
| 61 | Output stream. If not provided, `sys.stdout' is used. This |
| 62 | parameter must support the `write()' method with a single |
| 63 | parameter, which will always be a string. It may be a |
| 64 | `StringIO.StringIO' object if the result is needed as a |
| 65 | string. |
| 66 | |
| 67 | Indentation is done according to `INDENT_PER_LEVEL', which may be |
| 68 | set to any non-negative integer before calling this function. The |
| 69 | output written on the stream is a perfectly valid representation |
| 70 | of the Python object passed in, with indentation to assist |
| 71 | human-readable interpretation. The output can be used as input |
| 72 | without error, given readable representations of all elements are |
| 73 | available via `repr()'. Output is restricted to `MAX_WIDTH' |
| 74 | columns where possible. |
Fred Drake | b5d2039 | 1997-04-15 14:15:23 +0000 | [diff] [blame] | 75 | |
Guido van Rossum | 16d27e3 | 1996-08-21 16:28:53 +0000 | [diff] [blame] | 76 | """ |
Guido van Rossum | 16d27e3 | 1996-08-21 16:28:53 +0000 | [diff] [blame] | 77 | if stream is None: |
| 78 | import sys |
| 79 | stream = sys.stdout |
| 80 | |
Guido van Rossum | 16d27e3 | 1996-08-21 16:28:53 +0000 | [diff] [blame] | 81 | rep = `seq` |
| 82 | typ = type(seq) |
| 83 | sepLines = len(rep) > (MAX_WIDTH - 1 - indent - allowance) |
| 84 | |
| 85 | if sepLines and (typ is ListType or typ is TupleType): |
| 86 | # Pretty-print the sequence. |
| 87 | stream.write(((typ is ListType) and '[') or '(') |
| 88 | |
| 89 | length = len(seq) |
| 90 | if length: |
| 91 | indent = indent + INDENT_PER_LEVEL |
| 92 | pprint(seq[0], stream, indent, allowance + 1) |
| 93 | |
| 94 | if len(seq) > 1: |
| 95 | for ent in seq[1:]: |
Fred Drake | b5d2039 | 1997-04-15 14:15:23 +0000 | [diff] [blame] | 96 | stream.write(',\n' + ' '*indent) |
Guido van Rossum | 16d27e3 | 1996-08-21 16:28:53 +0000 | [diff] [blame] | 97 | pprint(ent, stream, indent, allowance + 1) |
| 98 | |
| 99 | indent = indent - INDENT_PER_LEVEL |
| 100 | |
| 101 | stream.write(((typ is ListType) and ']') or ')') |
| 102 | |
| 103 | elif typ is DictType and sepLines: |
| 104 | stream.write('{') |
| 105 | |
| 106 | length = len(seq) |
| 107 | if length: |
| 108 | indent = indent + INDENT_PER_LEVEL |
| 109 | items = seq.items() |
| 110 | items.sort() |
| 111 | key, ent = items[0] |
| 112 | rep = `key` + ': ' |
| 113 | stream.write(rep) |
| 114 | pprint(ent, stream, indent + len(rep), allowance + 1) |
| 115 | |
| 116 | if len(items) > 1: |
| 117 | for key, ent in items[1:]: |
| 118 | rep = `key` + ': ' |
Fred Drake | b5d2039 | 1997-04-15 14:15:23 +0000 | [diff] [blame] | 119 | stream.write(',\n' + ' '*indent + rep) |
Guido van Rossum | 16d27e3 | 1996-08-21 16:28:53 +0000 | [diff] [blame] | 120 | pprint(ent, stream, indent + len(rep), allowance + 1) |
| 121 | |
| 122 | indent = indent - INDENT_PER_LEVEL |
| 123 | |
| 124 | stream.write('}') |
| 125 | |
| 126 | else: |
| 127 | stream.write(rep) |
| 128 | |
| 129 | # Terminate the 'print' if we're not a recursive invocation. |
| 130 | if not indent: |
| 131 | stream.write('\n') |
| 132 | |
| 133 | |
| 134 | # |
Guido van Rossum | 8206fb9 | 1996-08-26 00:33:29 +0000 | [diff] [blame] | 135 | # end of file |