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
06a8916c
Commit
06a8916c
authored
Jun 27, 2019
by
Carl Bordum Hansen
Committed by
Miss Islington (bot)
Jun 26, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-37376: pprint support for SimpleNamespace (GH-14318)
https://bugs.python.org/issue37376
parent
d52a83a3
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
98 additions
and
0 deletions
+98
-0
Doc/library/pprint.rst
Doc/library/pprint.rst
+3
-0
Doc/whatsnew/3.9.rst
Doc/whatsnew/3.9.rst
+6
-0
Lib/pprint.py
Lib/pprint.py
+27
-0
Lib/test/test_pprint.py
Lib/test/test_pprint.py
+59
-0
Misc/ACKS
Misc/ACKS
+1
-0
Misc/NEWS.d/next/Library/2019-06-24-11-26-34.bpo-37376.SwSUQ4.rst
...S.d/next/Library/2019-06-24-11-26-34.bpo-37376.SwSUQ4.rst
+2
-0
No files found.
Doc/library/pprint.rst
View file @
06a8916c
...
@@ -25,6 +25,9 @@ width constraint.
...
@@ -25,6 +25,9 @@ width constraint.
Dictionaries are sorted by key before the display is computed.
Dictionaries are sorted by key before the display is computed.
.. versionchanged:: 3.9
Added support for pretty-printing :class:`types.SimpleNamespace`.
The :mod:`pprint` module defines one class:
The :mod:`pprint` module defines one class:
.. First the implementation class:
.. First the implementation class:
...
...
Doc/whatsnew/3.9.rst
View file @
06a8916c
...
@@ -111,6 +111,12 @@ threads were never supported in subinterpreters. Previously, the subinterpreter
...
@@ -111,6 +111,12 @@ threads were never supported in subinterpreters. Previously, the subinterpreter
finalization crashed with a Pyton fatal error if a daemon thread was still
finalization crashed with a Pyton fatal error if a daemon thread was still
running.
running.
pprint
------
:mod:`pprint` can now pretty-print :class:`types.SimpleNamespace`.
(Contributed by Carl Bordum Hansen in :issue:`37376`.)
Optimizations
Optimizations
=============
=============
...
...
Lib/pprint.py
View file @
06a8916c
...
@@ -342,6 +342,33 @@ class PrettyPrinter:
...
@@ -342,6 +342,33 @@ class PrettyPrinter:
_dispatch
[
_types
.
MappingProxyType
.
__repr__
]
=
_pprint_mappingproxy
_dispatch
[
_types
.
MappingProxyType
.
__repr__
]
=
_pprint_mappingproxy
def
_pprint_simplenamespace
(
self
,
object
,
stream
,
indent
,
allowance
,
context
,
level
):
if
type
(
object
)
is
_types
.
SimpleNamespace
:
# The SimpleNamespace repr is "namespace" instead of the class
# name, so we do the same here. For subclasses; use the class name.
cls_name
=
'namespace'
else
:
cls_name
=
object
.
__class__
.
__name__
indent
+=
len
(
cls_name
)
+
1
delimnl
=
',
\
n
'
+
' '
*
indent
items
=
object
.
__dict__
.
items
()
last_index
=
len
(
items
)
-
1
stream
.
write
(
cls_name
+
'('
)
for
i
,
(
key
,
ent
)
in
enumerate
(
items
):
stream
.
write
(
key
)
stream
.
write
(
'='
)
last
=
i
==
last_index
self
.
_format
(
ent
,
stream
,
indent
+
len
(
key
)
+
1
,
allowance
if
last
else
1
,
context
,
level
)
if
not
last
:
stream
.
write
(
delimnl
)
stream
.
write
(
')'
)
_dispatch
[
_types
.
SimpleNamespace
.
__repr__
]
=
_pprint_simplenamespace
def
_format_dict_items
(
self
,
items
,
stream
,
indent
,
allowance
,
context
,
def
_format_dict_items
(
self
,
items
,
stream
,
indent
,
allowance
,
context
,
level
):
level
):
write
=
stream
.
write
write
=
stream
.
write
...
...
Lib/test/test_pprint.py
View file @
06a8916c
...
@@ -346,6 +346,65 @@ mappingproxy(OrderedDict([('the', 0),
...
@@ -346,6 +346,65 @@ mappingproxy(OrderedDict([('the', 0),
('lazy', 7),
('lazy', 7),
('dog', 8)]))"""
)
('dog', 8)]))"""
)
def
test_empty_simple_namespace
(
self
):
ns
=
types
.
SimpleNamespace
()
formatted
=
pprint
.
pformat
(
ns
)
self
.
assertEqual
(
formatted
,
"namespace()"
)
def
test_small_simple_namespace
(
self
):
ns
=
types
.
SimpleNamespace
(
a
=
1
,
b
=
2
)
formatted
=
pprint
.
pformat
(
ns
)
self
.
assertEqual
(
formatted
,
"namespace(a=1, b=2)"
)
def
test_simple_namespace
(
self
):
ns
=
types
.
SimpleNamespace
(
the
=
0
,
quick
=
1
,
brown
=
2
,
fox
=
3
,
jumped
=
4
,
over
=
5
,
a
=
6
,
lazy
=
7
,
dog
=
8
,
)
formatted
=
pprint
.
pformat
(
ns
,
width
=
60
)
self
.
assertEqual
(
formatted
,
"""
\
namespace(the=0,
quick=1,
brown=2,
fox=3,
jumped=4,
over=5,
a=6,
lazy=7,
dog=8)"""
)
def
test_simple_namespace_subclass
(
self
):
class
AdvancedNamespace
(
types
.
SimpleNamespace
):
pass
ns
=
AdvancedNamespace
(
the
=
0
,
quick
=
1
,
brown
=
2
,
fox
=
3
,
jumped
=
4
,
over
=
5
,
a
=
6
,
lazy
=
7
,
dog
=
8
,
)
formatted
=
pprint
.
pformat
(
ns
,
width
=
60
)
self
.
assertEqual
(
formatted
,
"""
\
AdvancedNamespace(the=0,
quick=1,
brown=2,
fox=3,
jumped=4,
over=5,
a=6,
lazy=7,
dog=8)"""
)
def
test_subclassing
(
self
):
def
test_subclassing
(
self
):
o
=
{
'names with spaces'
:
'should be presented using repr()'
,
o
=
{
'names with spaces'
:
'should be presented using repr()'
,
'others.should.not.be'
:
'like.this'
}
'others.should.not.be'
:
'like.this'
}
...
...
Misc/ACKS
View file @
06a8916c
...
@@ -628,6 +628,7 @@ Mark Hammond
...
@@ -628,6 +628,7 @@ Mark Hammond
Harald Hanche-Olsen
Harald Hanche-Olsen
Manus Hand
Manus Hand
Milton L. Hankins
Milton L. Hankins
Carl Bordum Hansen
Stephen Hansen
Stephen Hansen
Barry Hantman
Barry Hantman
Lynda Hardman
Lynda Hardman
...
...
Misc/NEWS.d/next/Library/2019-06-24-11-26-34.bpo-37376.SwSUQ4.rst
0 → 100644
View file @
06a8916c
:mod:`pprint` now has support for :class:`types.SimpleNamespace`. Patch by Carl
Bordum Hansen.
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