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
8e2aa88a
Commit
8e2aa88a
authored
Mar 24, 2015
by
Serhiy Storchaka
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #23741: Slightly refactor the pprint module to make it a little more
extesible. No public API is added.
parent
72bd327d
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
122 additions
and
108 deletions
+122
-108
Lib/pprint.py
Lib/pprint.py
+122
-108
No files found.
Lib/pprint.py
View file @
8e2aa88a
...
...
@@ -152,81 +152,91 @@ class PrettyPrinter:
return
readable
and
not
recursive
def
_format
(
self
,
object
,
stream
,
indent
,
allowance
,
context
,
level
):
level
=
level
+
1
objid
=
id
(
object
)
if
objid
in
context
:
stream
.
write
(
_recursion
(
object
))
self
.
_recursive
=
True
self
.
_readable
=
False
return
rep
=
self
.
_repr
(
object
,
context
,
level
-
1
)
typ
=
type
(
object
)
rep
=
self
.
_repr
(
object
,
context
,
level
)
max_width
=
self
.
_width
-
indent
-
allowance
sepLines
=
len
(
rep
)
>
max_width
write
=
stream
.
write
if
len
(
rep
)
>
max_width
:
p
=
self
.
_dispatch
.
get
(
type
(
object
).
__repr__
,
None
)
if
p
is
not
None
:
context
[
objid
]
=
1
p
(
self
,
object
,
stream
,
indent
,
allowance
,
context
,
level
+
1
)
del
context
[
objid
]
return
elif
isinstance
(
object
,
dict
):
context
[
objid
]
=
1
self
.
_pprint_dict
(
object
,
stream
,
indent
,
allowance
,
context
,
level
+
1
)
del
context
[
objid
]
return
stream
.
write
(
rep
)
if
sepLines
:
r
=
getattr
(
typ
,
"__repr__"
,
None
)
if
issubclass
(
typ
,
dict
):
_dispatch
=
{}
def
_pprint_dict
(
self
,
object
,
stream
,
indent
,
allowance
,
context
,
level
):
write
=
stream
.
write
write
(
'{'
)
if
self
.
_indent_per_level
>
1
:
write
((
self
.
_indent_per_level
-
1
)
*
' '
)
length
=
len
(
object
)
if
length
:
context
[
objid
]
=
1
if
issubclass
(
typ
,
_OrderedDict
):
if
isinstance
(
object
,
_OrderedDict
):
items
=
list
(
object
.
items
())
else
:
items
=
sorted
(
object
.
items
(),
key
=
_safe_tuple
)
self
.
_format_dict_items
(
items
,
stream
,
indent
+
self
.
_indent_per_level
,
allowance
+
1
,
self
.
_format_dict_items
(
items
,
stream
,
indent
,
allowance
+
1
,
context
,
level
)
del
context
[
objid
]
write
(
'}'
)
return
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__
)
):
length
=
len
(
object
)
if
issubclass
(
typ
,
list
):
write
(
'['
)
endchar
=
']'
elif
issubclass
(
typ
,
tuple
):
write
(
'('
)
if
length
==
1
:
endchar
=
',)'
else
:
endchar
=
')'
else
:
if
not
length
:
write
(
rep
)
_dispatch
[
dict
.
__repr__
]
=
_pprint_dict
_dispatch
[
_OrderedDict
.
__repr__
]
=
_pprint_dict
def
_pprint_list
(
self
,
object
,
stream
,
indent
,
allowance
,
context
,
level
):
stream
.
write
(
'['
)
self
.
_format_items
(
object
,
stream
,
indent
,
allowance
+
1
,
context
,
level
)
stream
.
write
(
']'
)
_dispatch
[
list
.
__repr__
]
=
_pprint_list
def
_pprint_tuple
(
self
,
object
,
stream
,
indent
,
allowance
,
context
,
level
):
stream
.
write
(
'('
)
endchar
=
',)'
if
len
(
object
)
==
1
else
')'
self
.
_format_items
(
object
,
stream
,
indent
,
allowance
+
len
(
endchar
),
context
,
level
)
stream
.
write
(
endchar
)
_dispatch
[
tuple
.
__repr__
]
=
_pprint_tuple
def
_pprint_set
(
self
,
object
,
stream
,
indent
,
allowance
,
context
,
level
):
if
not
len
(
object
):
stream
.
write
(
repr
(
object
))
return
typ
=
object
.
__class__
if
typ
is
set
:
write
(
'{'
)
stream
.
write
(
'{'
)
endchar
=
'}'
else
:
write
(
typ
.
__name__
)
write
(
'({'
)
stream
.
write
(
typ
.
__name__
+
'({'
)
endchar
=
'})'
indent
+=
len
(
typ
.
__name__
)
+
1
object
=
sorted
(
object
,
key
=
_safe_key
)
if
self
.
_indent_per_level
>
1
:
write
((
self
.
_indent_per_level
-
1
)
*
' '
)
if
length
:
context
[
objid
]
=
1
self
.
_format_items
(
object
,
stream
,
indent
+
self
.
_indent_per_level
,
allowance
+
len
(
endchar
),
self
.
_format_items
(
object
,
stream
,
indent
,
allowance
+
len
(
endchar
),
context
,
level
)
del
context
[
objid
]
write
(
endchar
)
return
stream
.
write
(
endchar
)
_dispatch
[
set
.
__repr__
]
=
_pprint_set
_dispatch
[
frozenset
.
__repr__
]
=
_pprint_set
if
issubclass
(
typ
,
str
)
and
len
(
object
)
>
0
and
r
is
str
.
__repr__
:
def
_pprint_str
(
self
,
object
,
stream
,
indent
,
allowance
,
context
,
level
):
write
=
stream
.
write
if
not
len
(
object
):
write
(
repr
(
object
))
return
chunks
=
[]
lines
=
object
.
splitlines
(
True
)
if
level
==
1
:
...
...
@@ -270,12 +280,13 @@ class PrettyPrinter:
write
(
rep
)
if
level
==
1
:
write
(
')'
)
return
write
(
rep
)
_dispatch
[
str
.
__repr__
]
=
_pprint_str
def
_format_dict_items
(
self
,
items
,
stream
,
indent
,
allowance
,
context
,
level
):
write
=
stream
.
write
indent
+=
self
.
_indent_per_level
delimnl
=
',
\
n
'
+
' '
*
indent
last_index
=
len
(
items
)
-
1
for
i
,
(
key
,
ent
)
in
enumerate
(
items
):
...
...
@@ -291,6 +302,9 @@ class PrettyPrinter:
def
_format_items
(
self
,
items
,
stream
,
indent
,
allowance
,
context
,
level
):
write
=
stream
.
write
indent
+=
self
.
_indent_per_level
if
self
.
_indent_per_level
>
1
:
write
((
self
.
_indent_per_level
-
1
)
*
' '
)
delimnl
=
',
\
n
'
+
' '
*
indent
delim
=
''
width
=
max_width
=
self
.
_width
-
indent
+
1
...
...
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