Skip to content

Commit 9ef4e56

Browse files
committed
use docstring for string.Formatter
1 parent 13cb8ca commit 9ef4e56

File tree

1 file changed

+16
-21
lines changed

1 file changed

+16
-21
lines changed

Lib/string/__init__.py

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -264,22 +264,18 @@ def _vformat(self, format_string, args, kwargs, used_args, recursion_depth,
264264

265265
return ''.join(result), auto_arg_index
266266

267-
268267
def get_value(self, key, args, kwargs):
269268
if isinstance(key, int):
270269
return args[key]
271270
else:
272271
return kwargs[key]
273272

274-
275273
def check_unused_args(self, used_args, args, kwargs):
276274
pass
277275

278-
279276
def format_field(self, value, format_spec):
280277
return format(value, format_spec)
281278

282-
283279
def convert_field(self, value, conversion):
284280
# do any conversion on the resulting object
285281
if conversion is None:
@@ -292,34 +288,33 @@ def convert_field(self, value, conversion):
292288
return ascii(value)
293289
raise ValueError("Unknown conversion specifier {0!s}".format(conversion))
294290

295-
296-
# returns an iterable that contains tuples of the form:
297-
# (literal_text, field_name, format_spec, conversion)
298-
# literal_text can be zero length
299-
# field_name can be None, in which case there's no
300-
# object to format and output
301-
# if field_name is not None, it is looked up, formatted
302-
# with format_spec and conversion and then used
303291
def parse(self, format_string):
304-
return _string.formatter_parser(format_string)
292+
"""
293+
Return an iterable that contains tuples of the form
294+
(literal_text, field_name, format_spec, conversion).
305295
296+
*literal_text* can be zero length and *field_name*
297+
can be None, in which case nothing is formatted.
298+
299+
If *field_name* is not None, it is looked up and
300+
formatted with *format_spec* and *conversion*.
301+
"""
302+
return _string.formatter_parser(format_string)
306303

307-
# given a field_name, find the object it references.
308-
# field_name: the field being looked up, e.g. "0.name"
309-
# or "lookup[3]"
310-
# used_args: a set of which args have been used
311-
# args, kwargs: as passed in to vformat
312304
def get_field(self, field_name, args, kwargs):
313-
first, rest = _string.formatter_field_name_split(field_name)
305+
"""Find the object referenced by a given field name.
314306
307+
The field name *field_name* can be for instance "0.name"
308+
or "lookup[3]". The *args* and *kwargs* arguments are
309+
passed to get_value().
310+
"""
311+
first, rest = _string.formatter_field_name_split(field_name)
315312
obj = self.get_value(first, args, kwargs)
316-
317313
# loop through the rest of the field_name, doing
318314
# getattr or getitem as needed
319315
for is_attr, i in rest:
320316
if is_attr:
321317
obj = getattr(obj, i)
322318
else:
323319
obj = obj[i]
324-
325320
return obj, first

0 commit comments

Comments
 (0)