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
e0ffabe3
Commit
e0ffabe3
authored
Jul 18, 1997
by
Fred Drake
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Slight mods to make the module conform to the documentation.
parent
ee8d3ca4
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
63 additions
and
14 deletions
+63
-14
Lib/pprint.py
Lib/pprint.py
+63
-14
No files found.
Lib/pprint.py
View file @
e0ffabe3
...
...
@@ -53,9 +53,19 @@ def pformat(object):
return
PrettyPrinter
().
pformat
(
object
)
def
isreadable
(
object
):
"""Determine if saferepr(object) is readable by eval()."""
return
PrettyPrinter
().
isreadable
(
object
)
def
isrecursive
(
object
):
"""Determine if object requires a recursive representation."""
return
PrettyPrinter
().
isrecursive
(
object
)
def
saferepr
(
object
):
"""Version of repr() which can handle recursive data structures."""
return
_safe_repr
(
object
,
{})
return
_safe_repr
(
object
,
{})
[
0
]
class
PrettyPrinter
:
...
...
@@ -77,9 +87,11 @@ class PrettyPrinter:
output stream available at construction will be used.
"""
indent
=
int
(
indent
)
width
=
int
(
width
)
assert
indent
>=
0
assert
(
not
depth
)
or
depth
>
0
,
"depth may not be negative"
assert
int
(
indent
)
or
1
assert
int
(
width
)
or
1
assert
width
self
.
__depth
=
depth
self
.
__indent_per_level
=
indent
self
.
__width
=
width
...
...
@@ -97,10 +109,22 @@ class PrettyPrinter:
self
.
__format
(
object
,
sio
,
0
,
0
,
{},
0
)
return
sio
.
getvalue
()
def
isrecursive
(
self
,
object
):
self
.
__recursive
=
0
self
.
pformat
(
object
)
return
self
.
__recursive
def
isreadable
(
self
,
object
):
self
.
__recursive
=
0
self
.
__readable
=
1
self
.
pformat
(
object
)
return
self
.
__readable
and
not
self
.
__recursive
def
__format
(
self
,
object
,
stream
,
indent
,
allowance
,
context
,
level
):
level
=
level
+
1
if
context
.
has_key
(
id
(
object
)):
object
=
_Recursion
(
object
)
self
.
__recursive
=
1
rep
=
self
.
__repr
(
object
,
context
,
level
-
1
)
objid
=
id
(
object
)
context
[
objid
]
=
1
...
...
@@ -110,6 +134,8 @@ class PrettyPrinter:
if
sepLines
and
typ
in
(
ListType
,
TupleType
):
# Pretty-print the sequence.
stream
.
write
((
typ
is
ListType
)
and
'['
or
'('
)
if
self
.
__indent_per_level
>
1
:
stream
.
write
((
self
.
__indent_per_level
-
1
)
*
' '
)
length
=
len
(
object
)
if
length
:
indent
=
indent
+
self
.
__indent_per_level
...
...
@@ -125,6 +151,8 @@ class PrettyPrinter:
elif
sepLines
and
typ
is
DictType
:
stream
.
write
(
'{'
)
if
self
.
__indent_per_level
>
1
:
stream
.
write
((
self
.
__indent_per_level
-
1
)
*
' '
)
length
=
len
(
object
)
if
length
:
indent
=
indent
+
self
.
__indent_per_level
...
...
@@ -149,42 +177,63 @@ class PrettyPrinter:
del
context
[
objid
]
def
__repr
(
self
,
object
,
context
,
level
):
return
_safe_repr
(
object
,
context
,
self
.
__depth
,
level
)
repr
,
readable
=
_safe_repr
(
object
,
context
,
self
.
__depth
,
level
)
if
not
readable
:
self
.
__readable
=
0
return
repr
def
_safe_repr
(
object
,
context
,
maxlevels
=
None
,
level
=
0
):
level
=
level
+
1
readable
=
1
typ
=
type
(
object
)
if
not
(
typ
in
(
DictType
,
ListType
,
TupleType
)
and
object
):
return
`object`
rep
=
`object`
if
rep
:
if
rep
[
0
]
==
'<'
:
readable
=
0
else
:
readable
=
0
return
`object`
,
readable
if
context
.
has_key
(
id
(
object
)):
return
`_Recursion(object)`
return
`_Recursion(object)`
,
0
objid
=
id
(
object
)
context
[
objid
]
=
1
if
typ
is
DictType
:
if
maxlevels
and
level
>=
maxlevels
:
s
=
"{...}"
readable
=
0
else
:
items
=
object
.
items
()
k
,
v
=
items
[
0
]
s
=
"{%s: %s"
%
(
_safe_repr
(
k
,
context
,
maxlevels
,
level
),
_safe_repr
(
v
,
context
,
maxlevels
,
level
))
krepr
,
kreadable
=
_safe_repr
(
k
,
context
,
maxlevels
,
level
)
vrepr
,
vreadable
=
_safe_repr
(
v
,
context
,
maxlevels
,
level
)
readable
=
readable
and
kreadable
and
vreadable
s
=
"{%s: %s"
%
(
krepr
,
vrepr
)
for
k
,
v
in
items
[
1
:]:
s
=
"%s, %s: %s"
\
%
(
s
,
_safe_repr
(
k
,
context
,
maxlevels
,
level
),
_safe_repr
(
v
,
context
,
maxlevels
,
level
))
krepr
,
kreadable
=
_safe_repr
(
k
,
context
,
maxlevels
,
level
)
vrepr
,
vreadable
=
_safe_repr
(
v
,
context
,
maxlevels
,
level
)
readable
=
readable
and
kreadable
and
vreadable
s
=
"%s, %s: %s"
%
(
s
,
krepr
,
vrepr
)
s
=
s
+
"}"
else
:
s
,
term
=
(
typ
is
ListType
)
and
(
'['
,
']'
)
or
(
'('
,
')'
)
if
maxlevels
and
level
>=
maxlevels
:
s
=
s
+
"..."
readable
=
0
else
:
s
=
s
+
_safe_repr
(
object
[
0
],
context
,
maxlevels
,
level
)
subrepr
,
subreadable
=
_safe_repr
(
object
[
0
],
context
,
maxlevels
,
level
)
readable
=
readable
and
subreadable
s
=
s
+
subrepr
for
ent
in
object
[
1
:]:
s
=
"%s, %s"
%
(
s
,
_safe_repr
(
ent
,
context
,
maxlevels
,
level
))
subrepr
,
subreadable
=
_safe_repr
(
ent
,
context
,
maxlevels
,
level
)
readable
=
readable
and
subreadable
s
=
"%s, %s"
%
(
s
,
subrepr
)
s
=
s
+
term
del
context
[
objid
]
return
s
return
s
,
readable
class
_Recursion
:
...
...
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