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
b6234a95
Kaydet (Commit)
b6234a95
authored
Eyl 13, 2004
tarafından
Barry Warsaw
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
substitute(), safe_substitute(): Paul Moore provides a better hack for dealing
with positional arguments.
üst
c7cd20c8
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
19 additions
and
11 deletions
+19
-11
string.py
Lib/string.py
+19
-11
No files found.
Lib/string.py
Dosyayı görüntüle @
b6234a95
...
...
@@ -143,28 +143,36 @@ class Template:
raise
ValueError
(
'Invalid placeholder in string: line
%
d, col
%
d'
%
(
lineno
,
colno
))
def
substitute
(
self
,
__mapping
=
None
,
**
kws
):
if
__mapping
is
None
:
__mapping
=
kws
def
substitute
(
self
,
*
args
,
**
kws
):
if
len
(
args
)
>
1
:
raise
TypeError
(
'Too many positional arguments'
)
if
not
args
:
mapping
=
kws
elif
kws
:
__mapping
=
_multimap
(
kws
,
__mapping
)
mapping
=
_multimap
(
kws
,
args
[
0
])
else
:
mapping
=
args
[
0
]
# Helper function for .sub()
def
convert
(
mo
):
if
mo
.
group
(
'escaped'
)
is
not
None
:
return
'$'
if
mo
.
group
(
'bogus'
)
is
not
None
:
self
.
_bogus
(
mo
)
val
=
__
mapping
[
mo
.
group
(
'named'
)
or
mo
.
group
(
'braced'
)]
val
=
mapping
[
mo
.
group
(
'named'
)
or
mo
.
group
(
'braced'
)]
# We use this idiom instead of str() because the latter will fail
# if val is a Unicode containing non-ASCII characters.
return
'
%
s'
%
val
return
self
.
pattern
.
sub
(
convert
,
self
.
template
)
def
safe_substitute
(
self
,
__mapping
=
None
,
**
kws
):
if
__mapping
is
None
:
__mapping
=
kws
def
safe_substitute
(
self
,
*
args
,
**
kws
):
if
len
(
args
)
>
1
:
raise
TypeError
(
'Too many positional arguments'
)
if
not
args
:
mapping
=
kws
elif
kws
:
__mapping
=
_multimap
(
kws
,
__mapping
)
mapping
=
_multimap
(
kws
,
args
[
0
])
else
:
mapping
=
args
[
0
]
# Helper function for .sub()
def
convert
(
mo
):
if
mo
.
group
(
'escaped'
)
is
not
None
:
...
...
@@ -176,12 +184,12 @@ class Template:
try
:
# We use this idiom instead of str() because the latter
# will fail if val is a Unicode containing non-ASCII
return
'
%
s'
%
__
mapping
[
named
]
return
'
%
s'
%
mapping
[
named
]
except
KeyError
:
return
'$'
+
named
braced
=
mo
.
group
(
'braced'
)
try
:
return
'
%
s'
%
__
mapping
[
braced
]
return
'
%
s'
%
mapping
[
braced
]
except
KeyError
:
return
'${'
+
braced
+
'}'
return
self
.
pattern
.
sub
(
convert
,
self
.
template
)
...
...
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