Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
bad3c880
Commit
bad3c880
authored
Sep 09, 2010
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Have pprint() respect the order in an OrderedDict.
parent
a0e79408
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
25 additions
and
3 deletions
+25
-3
Lib/pprint.py
Lib/pprint.py
+6
-3
Lib/test/test_pprint.py
Lib/test/test_pprint.py
+16
-0
Misc/NEWS
Misc/NEWS
+3
-0
No files found.
Lib/pprint.py
View file @
bad3c880
...
...
@@ -35,7 +35,7 @@ saferepr()
"""
import
sys
as
_sys
from
collections
import
OrderedDict
as
_OrderedDict
from
io
import
StringIO
as
_StringIO
__all__
=
[
"pprint"
,
"pformat"
,
"isreadable"
,
"isrecursive"
,
"saferepr"
,
...
...
@@ -163,7 +163,7 @@ class PrettyPrinter:
if
sepLines
:
r
=
getattr
(
typ
,
"__repr__"
,
None
)
if
issubclass
(
typ
,
dict
)
and
r
is
dict
.
__repr__
:
if
issubclass
(
typ
,
dict
):
write
(
'{'
)
if
self
.
_indent_per_level
>
1
:
write
((
self
.
_indent_per_level
-
1
)
*
' '
)
...
...
@@ -171,7 +171,10 @@ class PrettyPrinter:
if
length
:
context
[
objid
]
=
1
indent
=
indent
+
self
.
_indent_per_level
items
=
sorted
(
object
.
items
(),
key
=
_safe_tuple
)
if
issubclass
(
typ
,
_OrderedDict
):
items
=
list
(
object
.
items
())
else
:
items
=
sorted
(
object
.
items
(),
key
=
_safe_tuple
)
key
,
ent
=
items
[
0
]
rep
=
self
.
_repr
(
key
,
context
,
level
)
write
(
rep
)
...
...
Lib/test/test_pprint.py
View file @
bad3c880
...
...
@@ -3,6 +3,8 @@ import test.support
import
unittest
import
test.test_set
import
random
import
collections
import
itertools
# list, tuple and dict subclasses that do or don't overwrite __repr__
class
list2
(
list
):
...
...
@@ -195,6 +197,20 @@ class QueryTestCase(unittest.TestCase):
self
.
assertEqual
(
pprint
.
pformat
({
"xy
\
t
ab
\
n
"
:
(
3
,),
5
:
[[]],
():
{}}),
r"{5: [[]], 'xy\tab\n': (3,), (): {}}"
)
def
test_ordered_dict
(
self
):
words
=
'the quick brown fox jumped over a lazy dog'
.
split
()
d
=
collections
.
OrderedDict
(
zip
(
words
,
itertools
.
count
()))
self
.
assertEqual
(
pprint
.
pformat
(
d
),
"""
\
{'the': 0,
'quick': 1,
'brown': 2,
'fox': 3,
'jumped': 4,
'over': 5,
'a': 6,
'lazy': 7,
'dog': 8}"""
)
def
test_subclassing
(
self
):
o
=
{
'names with spaces'
:
'should be presented using repr()'
,
'others.should.not.be'
:
'like.this'
}
...
...
Misc/NEWS
View file @
bad3c880
...
...
@@ -16,6 +16,9 @@ Core and Builtins
Library
-------
- The pprint module now supports printing OrderedDicts in their given
order (formerly, it would sort the keys).
- Logging: Added QueueHandler class to facilitate logging usage with
multiprocessing.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment