]> granicus.if.org Git - python/commitdiff
Let pprint() support sets and frozensets (suggested by David Mertz).
authorRaymond Hettinger <python@rcn.com>
Wed, 23 Jan 2008 00:04:40 +0000 (00:04 +0000)
committerRaymond Hettinger <python@rcn.com>
Wed, 23 Jan 2008 00:04:40 +0000 (00:04 +0000)
Doc/library/pprint.rst
Lib/pprint.py
Misc/NEWS

index d07371462f87e3a60ef8dc91db7f3690eb3056a9..ae9677f9bfaf46872ce3d4ef15b49ff61032aff8 100644 (file)
@@ -25,6 +25,9 @@ width constraint.
    dictionary was sorted only if its display required more than one line, although
    that wasn't documented.
 
+.. versionchanged:: 2.6
+   Added support for :class:`set` and :class:`frozenset`.
+
 The :mod:`pprint` module defines one class:
 
 .. First the implementation class:
index 89f99d292c015886ce907012984ee8d60aa7957f..9359de38c4b5785631765cd765e80ac321967219 100644 (file)
@@ -162,11 +162,24 @@ class PrettyPrinter:
             write('}')
             return
 
-        if (issubclass(typ, list) and r is list.__repr__) or \
-           (issubclass(typ, tuple) and r is tuple.__repr__):
+        if ((issubclass(typ, list) and r is list.__repr__) or
+            (issubclass(typ, tuple) and r is tuple.__repr__) or
+            (issubclass(typ, set) and r is set.__repr__) or
+            (issubclass(typ, frozenset) and r is frozenset.__repr__)
+           ):
             if issubclass(typ, list):
                 write('[')
                 endchar = ']'
+            elif issubclass(typ, set):
+                write('set([')
+                endchar = '])'
+                object = sorted(object)
+                indent += 4
+            elif issubclass(typ, frozenset):
+                write('frozenset([')
+                endchar = '])'
+                object = sorted(object)
+                indent += 9
             else:
                 write('(')
                 endchar = ')'
index 3b55d743c5bfe1066cf942863064c188b59554c1..103a353dcce2057f37e0fb1f0d5b65e464acdc8b 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -378,6 +378,8 @@ Core and builtins
 Library
 -------
 
+- The pprint module now supports sets and frozensets.
+
 - #1221598: add optional callbacks to ftplib.FTP's storbinary() and
   storlines() methods.  (Contributed by Phil Schwartz)