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
b6234a95
Commit
b6234a95
authored
Sep 13, 2004
by
Barry Warsaw
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
substitute(), safe_substitute(): Paul Moore provides a better hack for dealing
with positional arguments.
parent
c7cd20c8
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
19 additions
and
11 deletions
+19
-11
Lib/string.py
Lib/string.py
+19
-11
No files found.
Lib/string.py
View file @
b6234a95
...
@@ -143,28 +143,36 @@ class Template:
...
@@ -143,28 +143,36 @@ class Template:
raise ValueError('Invalid placeholder in string: line %d, col %d' %
raise ValueError('Invalid placeholder in string: line %d, col %d' %
(lineno, colno))
(lineno, colno))
def substitute(self, __mapping=None, **kws):
def substitute(self, *args, **kws):
if __mapping is None:
if len(args) > 1:
__mapping = kws
raise TypeError('Too many positional arguments')
if not args:
mapping = kws
elif kws:
elif kws:
__mapping = _multimap(kws, __mapping)
mapping = _multimap(kws, args[0])
else:
mapping = args[0]
# Helper function for .sub()
# Helper function for .sub()
def convert(mo):
def convert(mo):
if mo.group('escaped') is not None:
if mo.group('escaped') is not None:
return '$'
return '$'
if mo.group('bogus') is not None:
if mo.group('bogus') is not None:
self._bogus(mo)
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
# We use this idiom instead of str() because the latter will fail
# if val is a Unicode containing non-ASCII characters.
# if val is a Unicode containing non-ASCII characters.
return '%s' % val
return '%s' % val
return self.pattern.sub(convert, self.template)
return self.pattern.sub(convert, self.template)
def safe_substitute(self, __mapping=None, **kws):
def safe_substitute(self, *args, **kws):
if __mapping is None:
if len(args) > 1:
__mapping = kws
raise TypeError('Too many positional arguments')
if not args:
mapping = kws
elif kws:
elif kws:
__mapping = _multimap(kws, __mapping)
mapping = _multimap(kws, args[0])
else:
mapping = args[0]
# Helper function for .sub()
# Helper function for .sub()
def convert(mo):
def convert(mo):
if mo.group('escaped') is not None:
if mo.group('escaped') is not None:
...
@@ -176,12 +184,12 @@ class Template:
...
@@ -176,12 +184,12 @@ class Template:
try:
try:
# We use this idiom instead of str() because the latter
# We use this idiom instead of str() because the latter
# will fail if val is a Unicode containing non-ASCII
# will fail if val is a Unicode containing non-ASCII
return '%s' %
__
mapping[named]
return '%s' % mapping[named]
except KeyError:
except KeyError:
return '$' + named
return '$' + named
braced = mo.group('braced')
braced = mo.group('braced')
try:
try:
return '%s' %
__
mapping[braced]
return '%s' % mapping[braced]
except KeyError:
except KeyError:
return '${' + braced + '}'
return '${' + braced + '}'
return self.pattern.sub(convert, self.template)
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