Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
49cc01e5
Kaydet (Commit)
49cc01e5
authored
Kas 01, 2001
tarafından
Fred Drake
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Brute-force performance hackery; buys back about 20% of the time for
saferepr(), a bit less for pformat().
üst
a765c120
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
144 additions
and
100 deletions
+144
-100
pprint.py
Lib/pprint.py
+144
-100
No files found.
Lib/pprint.py
Dosyayı görüntüle @
49cc01e5
...
@@ -45,6 +45,14 @@ except ImportError:
...
@@ -45,6 +45,14 @@ except ImportError:
__all__
=
[
"pprint"
,
"pformat"
,
"isreadable"
,
"isrecursive"
,
"saferepr"
,
__all__
=
[
"pprint"
,
"pformat"
,
"isreadable"
,
"isrecursive"
,
"saferepr"
,
"PrettyPrinter"
]
"PrettyPrinter"
]
# cache these for faster access:
_commajoin
=
", "
.
join
_sys_modules
=
sys
.
modules
_id
=
id
_len
=
len
_type
=
type
def
pprint
(
object
,
stream
=
None
):
def
pprint
(
object
,
stream
=
None
):
"""Pretty-print a Python object to a stream [default is sys.sydout]."""
"""Pretty-print a Python object to a stream [default is sys.sydout]."""
printer
=
PrettyPrinter
(
stream
=
stream
)
printer
=
PrettyPrinter
(
stream
=
stream
)
...
@@ -56,15 +64,15 @@ def pformat(object):
...
@@ -56,15 +64,15 @@ def pformat(object):
def
saferepr
(
object
):
def
saferepr
(
object
):
"""Version of repr() which can handle recursive data structures."""
"""Version of repr() which can handle recursive data structures."""
return
_safe_repr
(
object
,
{})[
0
]
return
_safe_repr
(
object
,
{}
,
None
,
0
)[
0
]
def
isreadable
(
object
):
def
isreadable
(
object
):
"""Determine if saferepr(object) is readable by eval()."""
"""Determine if saferepr(object) is readable by eval()."""
return
_safe_repr
(
object
,
{})[
1
]
return
_safe_repr
(
object
,
{}
,
None
,
0
)[
1
]
def
isrecursive
(
object
):
def
isrecursive
(
object
):
"""Determine if object requires a recursive representation."""
"""Determine if object requires a recursive representation."""
return
_safe_repr
(
object
,
{})[
2
]
return
_safe_repr
(
object
,
{}
,
None
,
0
)[
2
]
class
PrettyPrinter
:
class
PrettyPrinter
:
def
__init__
(
self
,
indent
=
1
,
width
=
80
,
depth
=
None
,
stream
=
None
):
def
__init__
(
self
,
indent
=
1
,
width
=
80
,
depth
=
None
,
stream
=
None
):
...
@@ -108,73 +116,84 @@ class PrettyPrinter:
...
@@ -108,73 +116,84 @@ class PrettyPrinter:
def
isrecursive
(
self
,
object
):
def
isrecursive
(
self
,
object
):
self
.
__recursive
=
0
self
.
__recursive
=
0
self
.
pformat
(
object
)
self
.
__repr
(
object
,
{},
0
)
return
self
.
__recursive
return
self
.
__recursive
def
isreadable
(
self
,
object
):
def
isreadable
(
self
,
object
):
self
.
__recursive
=
0
self
.
__recursive
=
0
self
.
__readable
=
1
self
.
__readable
=
1
self
.
pformat
(
object
)
self
.
__repr
(
object
,
{},
0
)
return
self
.
__readable
and
not
self
.
__recursive
return
self
.
__readable
and
not
self
.
__recursive
def
__format
(
self
,
object
,
stream
,
indent
,
allowance
,
context
,
level
):
def
__format
(
self
,
object
,
stream
,
indent
,
allowance
,
context
,
level
):
level
=
level
+
1
level
=
level
+
1
if
context
.
has_key
(
id
(
object
)):
objid
=
_id
(
object
)
object
=
_Recursion
(
object
)
if
objid
in
context
:
stream
.
write
(
_recursion
(
object
))
self
.
__recursive
=
1
self
.
__recursive
=
1
self
.
__readable
=
0
return
rep
=
self
.
__repr
(
object
,
context
,
level
-
1
)
rep
=
self
.
__repr
(
object
,
context
,
level
-
1
)
objid
=
id
(
object
)
typ
=
_type
(
object
)
context
[
objid
]
=
1
sepLines
=
_len
(
rep
)
>
(
self
.
__width
-
1
-
indent
-
allowance
)
typ
=
type
(
object
)
write
=
stream
.
write
sepLines
=
len
(
rep
)
>
(
self
.
__width
-
1
-
indent
-
allowance
)
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
self
.
__format
(
object
[
0
],
stream
,
indent
,
allowance
+
1
,
context
,
level
)
if
length
>
1
:
for
ent
in
object
[
1
:]:
stream
.
write
(
',
\n
'
+
' '
*
indent
)
self
.
__format
(
ent
,
stream
,
indent
,
allowance
+
1
,
context
,
level
)
indent
=
indent
-
self
.
__indent_per_level
if
typ
is
TupleType
and
length
==
1
:
stream
.
write
(
','
)
stream
.
write
(((
typ
is
ListType
)
and
']'
)
or
')'
)
elif
sepLines
and
typ
is
DictType
:
if
sepLines
:
stream
.
write
(
'{'
)
if
typ
is
DictType
:
write
(
'{'
)
if
self
.
__indent_per_level
>
1
:
if
self
.
__indent_per_level
>
1
:
stream
.
write
((
self
.
__indent_per_level
-
1
)
*
' '
)
write
((
self
.
__indent_per_level
-
1
)
*
' '
)
length
=
len
(
object
)
length
=
_
len
(
object
)
if
length
:
if
length
:
context
[
objid
]
=
1
indent
=
indent
+
self
.
__indent_per_level
indent
=
indent
+
self
.
__indent_per_level
items
=
object
.
items
()
items
=
object
.
items
()
items
.
sort
()
items
.
sort
()
key
,
ent
=
items
[
0
]
key
,
ent
=
items
[
0
]
rep
=
self
.
__repr
(
key
,
context
,
level
)
+
': '
rep
=
self
.
__repr
(
key
,
context
,
level
)
stream
.
write
(
rep
)
write
(
rep
)
self
.
__format
(
ent
,
stream
,
indent
+
len
(
rep
),
write
(
': '
)
self
.
__format
(
ent
,
stream
,
indent
+
_len
(
rep
)
+
2
,
allowance
+
1
,
context
,
level
)
allowance
+
1
,
context
,
level
)
if
len
(
items
)
>
1
:
if
length
>
1
:
for
key
,
ent
in
items
[
1
:]:
for
key
,
ent
in
items
[
1
:]:
rep
=
self
.
__repr
(
key
,
context
,
level
)
+
': '
rep
=
self
.
__repr
(
key
,
context
,
level
)
stream
.
write
(
',
\n
'
+
' '
*
indent
+
rep
)
write
(
',
\n
%
s:
%
s'
%
(
' '
*
indent
,
rep
)
)
self
.
__format
(
ent
,
stream
,
indent
+
len
(
rep
)
,
self
.
__format
(
ent
,
stream
,
indent
+
_len
(
rep
)
+
2
,
allowance
+
1
,
context
,
level
)
allowance
+
1
,
context
,
level
)
indent
=
indent
-
self
.
__indent_per_level
indent
=
indent
-
self
.
__indent_per_level
stream
.
write
(
'}'
)
del
context
[
objid
]
write
(
'}'
)
return
if
typ
is
ListType
or
typ
is
TupleType
:
if
typ
is
ListType
:
write
(
'['
)
endchar
=
']'
else
:
else
:
stream
.
write
(
rep
)
write
(
'('
)
endchar
=
')'
if
self
.
__indent_per_level
>
1
:
write
((
self
.
__indent_per_level
-
1
)
*
' '
)
length
=
_len
(
object
)
if
length
:
context
[
objid
]
=
1
indent
=
indent
+
self
.
__indent_per_level
self
.
__format
(
object
[
0
],
stream
,
indent
,
allowance
+
1
,
context
,
level
)
if
length
>
1
:
for
ent
in
object
[
1
:]:
write
(
',
\n
'
+
' '
*
indent
)
self
.
__format
(
ent
,
stream
,
indent
,
allowance
+
1
,
context
,
level
)
indent
=
indent
-
self
.
__indent_per_level
del
context
[
objid
]
del
context
[
objid
]
if
typ
is
TupleType
and
length
==
1
:
write
(
','
)
write
(
endchar
)
return
write
(
rep
)
def
__repr
(
self
,
object
,
context
,
level
):
def
__repr
(
self
,
object
,
context
,
level
):
repr
,
readable
,
recursive
=
_safe_repr
(
object
,
context
,
repr
,
readable
,
recursive
=
_safe_repr
(
object
,
context
,
...
@@ -187,16 +206,10 @@ class PrettyPrinter:
...
@@ -187,16 +206,10 @@ class PrettyPrinter:
# Return triple (repr_string, isreadable, isrecursive).
# Return triple (repr_string, isreadable, isrecursive).
_have_module
=
sys
.
modules
.
has_key
def
_safe_repr
(
object
,
context
,
maxlevels
,
level
):
typ
=
_type
(
object
)
def
_safe_repr
(
object
,
context
,
maxlevels
=
None
,
level
=
0
):
if
typ
is
StringType
:
level
+=
1
if
'locale'
not
in
_sys_modules
:
typ
=
type
(
object
)
if
not
(
typ
in
(
DictType
,
ListType
,
TupleType
,
StringType
)
and
object
):
rep
=
`object`
return
rep
,
(
rep
and
(
rep
[
0
]
!=
'<'
)),
0
elif
typ
is
StringType
:
if
not
_have_module
(
'locale'
):
return
`object`
,
1
,
0
return
`object`
,
1
,
0
if
"'"
in
object
and
'"'
not
in
object
:
if
"'"
in
object
and
'"'
not
in
object
:
closure
=
'"'
closure
=
'"'
...
@@ -204,63 +217,94 @@ def _safe_repr(object, context, maxlevels=None, level=0):
...
@@ -204,63 +217,94 @@ def _safe_repr(object, context, maxlevels=None, level=0):
else
:
else
:
closure
=
"'"
closure
=
"'"
quotes
=
{
"'"
:
"
\\
'"
}
quotes
=
{
"'"
:
"
\\
'"
}
qget
=
quotes
.
get
sio
=
StringIO
()
sio
=
StringIO
()
write
=
sio
.
write
for
char
in
object
:
for
char
in
object
:
if
char
.
isalpha
():
if
char
.
isalpha
():
sio
.
write
(
char
)
write
(
char
)
else
:
else
:
sio
.
write
(
quotes
.
get
(
char
,
`char`
[
1
:
-
1
]))
write
(
q
get
(
char
,
`char`
[
1
:
-
1
]))
return
closure
+
sio
.
getvalue
()
+
closure
,
1
,
0
return
(
"
%
s
%
s
%
s"
%
(
closure
,
sio
.
getvalue
(),
closure
))
,
1
,
0
if
context
.
has_key
(
id
(
object
)):
if
typ
is
DictType
:
return
`_Recursion(object)`
,
0
,
1
if
not
object
:
objid
=
id
(
object
)
return
"{}"
,
1
,
0
objid
=
_id
(
object
)
if
maxlevels
and
level
>
maxlevels
:
return
"{...}"
,
0
,
objid
in
context
if
objid
in
context
:
return
_recursion
(
object
),
0
,
1
context
[
objid
]
=
1
context
[
objid
]
=
1
readable
=
1
readable
=
1
recursive
=
0
recursive
=
0
startchar
,
endchar
=
{
ListType
:
"[]"
,
TupleType
:
"()"
,
DictType
:
"{}"
}[
typ
]
if
maxlevels
and
level
>
maxlevels
:
with_commas
=
"..."
readable
=
0
elif
typ
is
DictType
:
components
=
[]
components
=
[]
append
=
components
.
append
level
+=
1
saferepr
=
_safe_repr
for
k
,
v
in
object
.
iteritems
():
for
k
,
v
in
object
.
iteritems
():
krepr
,
kreadable
,
krecur
=
_safe_repr
(
k
,
context
,
maxlevels
,
krepr
,
kreadable
,
krecur
=
saferepr
(
k
,
context
,
maxlevels
,
level
)
level
)
vrepr
,
vreadable
,
vrecur
=
saferepr
(
v
,
context
,
maxlevels
,
level
)
vrepr
,
vreadable
,
vrecur
=
_safe_repr
(
v
,
context
,
maxlevels
,
append
(
"
%
s:
%
s"
%
(
krepr
,
vrepr
))
level
)
components
.
append
(
"
%
s:
%
s"
%
(
krepr
,
vrepr
))
readable
=
readable
and
kreadable
and
vreadable
readable
=
readable
and
kreadable
and
vreadable
recursive
=
recursive
or
krecur
or
vrecur
if
krecur
or
vrecur
:
with_commas
=
", "
.
join
(
components
)
recursive
=
1
del
context
[
objid
]
else
:
# list or tuple
return
"{
%
s}"
%
_commajoin
(
components
),
readable
,
recursive
assert
typ
in
(
ListType
,
TupleType
)
if
typ
is
ListType
or
typ
is
TupleType
:
if
typ
is
ListType
:
if
not
object
:
return
"[]"
,
1
,
0
format
=
"[
%
s]"
elif
_len
(
object
)
==
1
:
format
=
"(
%
s,)"
else
:
if
not
object
:
return
"()"
,
1
,
0
format
=
"(
%
s)"
objid
=
_id
(
object
)
if
maxlevels
and
level
>
maxlevels
:
return
format
%
"..."
,
0
,
objid
in
context
if
objid
in
context
:
return
_recursion
(
object
),
0
,
1
context
[
objid
]
=
1
readable
=
1
recursive
=
0
components
=
[]
components
=
[]
for
element
in
object
:
append
=
components
.
append
subrepr
,
subreadable
,
subrecur
=
_safe_repr
(
level
+=
1
element
,
context
,
maxlevels
,
level
)
for
o
in
object
:
components
.
append
(
subrepr
)
orepr
,
oreadable
,
orecur
=
_safe_repr
(
o
,
context
,
maxlevels
,
level
)
readable
=
readable
and
subreadable
append
(
orepr
)
recursive
=
recursive
or
subrecur
if
not
oreadable
:
if
len
(
components
)
==
1
and
typ
is
TupleType
:
readable
=
0
components
[
0
]
+=
","
if
orecur
:
with_commas
=
", "
.
join
(
components
)
recursive
=
1
s
=
"
%
s
%
s
%
s"
%
(
startchar
,
with_commas
,
endchar
)
del
context
[
objid
]
del
context
[
objid
]
return
s
,
readable
and
not
recursive
,
recursive
return
format
%
_commajoin
(
components
),
readable
,
recursive
rep
=
`object`
return
rep
,
(
rep
and
not
rep
.
startswith
(
'<'
)),
0
def
_recursion
(
object
):
return
(
"<Recursion on
%
s with id=
%
s>"
%
(
_type
(
object
)
.
__name__
,
_id
(
object
)))
class
_Recursion
:
def
_perfcheck
(
object
=
None
):
# represent a recursive relationship; really only used for the __repr__()
import
time
# method...
if
object
is
None
:
def
__init__
(
self
,
object
):
object
=
[(
"string"
,
(
1
,
2
),
[
3
,
4
],
{
5
:
6
,
7
:
8
})]
*
100000
self
.
__repr
=
"<Recursion on
%
s with id=
%
s>"
\
p
=
PrettyPrinter
()
%
(
type
(
object
)
.
__name__
,
id
(
object
))
t1
=
time
.
time
()
_safe_repr
(
object
,
{},
None
,
0
)
t2
=
time
.
time
()
p
.
pformat
(
object
)
t3
=
time
.
time
()
print
"_safe_repr:"
,
t2
-
t1
print
"pformat:"
,
t3
-
t2
def
__repr__
(
self
)
:
if
__name__
==
"__main__"
:
return
self
.
__repr
_perfcheck
()
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