#importing string moduleimport string#making object of string Formatterformatter = string.Formatter()print (formatter.format("{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}", "c", "p", "p", "s", "e", "c", "r", "e", "t", "s"))
from string import Formatterformatter = Formatter()print(formatter.format('{} {website}', 'Welcome to', website='cppsecrets'))# format() behaves in similar mannerprint('{} {website}'.format('Welcome to', website='cppsecrets'))
The Formatter class takes no initialization arguments:
The public API methods of class Formatter are as follows:
Formatter defines the following overridable methods:'format' is the primary API method. It takes a format template, and an arbitrary set of positional and keyword arguments. 'format' is just a wrapper that calls 'vformat'.
'vformat' is the function that does the actual work of formatting. It is exposed as a separate function for cases where you want to pass in a predefined dictionary of arguments, rather than unpacking and repacking the dictionary as individual arguments using the *args and **kwds syntax. 'vformat' does the work of breaking up the format template string into character data and replacement fields. It calls the 'get_positional' and 'get_index' methods as appropriate (described below.)
'get_value' is used to retrieve a given field value. The 'key' argument will be either an integer or a string. If it is an integer, it represents the index of the positional argument in 'args'; If it is a string, then it represents a named argument in 'kwargs'.
The 'args' parameter is set to the list of positional arguments to 'vformat', and the 'kwargs' parameter is set to the dictionary of positional arguments.
For compound field names, these functions are only called for the first component of the field name; subsequent components are handled through normal attribute and indexing operations.
So for example, the field expression '0.name' would cause 'get_value' to be called with a 'key' argument of 0. The 'name' attribute will be looked up after 'get_value' returns by calling the built-in 'getattr' function.
If the index or keyword refers to an item that does not exist, then an IndexError/KeyError should be raised.
'check_unused_args' is used to implement checking for unused arguments if desired. The arguments to this function is the set of all argument keys that were actually referred to in the format string (integers for positional arguments, and strings for named arguments), and a reference to the args and kwargs that was passed to vformat. The set of unused args can be calculated from these parameters. 'check_unused_args' is assumed to throw an exception if the check fails.
'format_field' simply calls the global 'format' built-in. The method is provided so that subclasses can override it.
def vformat(format_string, args, kwargs):# Output buffer and set of used argsbuffer = StringIO.StringIO()used_args = set()# Tokens are either format fields or literal stringsfor token in self.parse(format_string):if is_format_field(token):# Split the token into field value and format specfield_spec, _, format_spec = token.partition(":")# Check for explicit type conversionexplicit, _, field_spec = field_spec.rpartition("!")# 'first_part' is the part before the first '.' or '['# Assume that 'get_first_part' returns either an int or# a string, depending on the syntax.first_part = get_first_part(field_spec)value = self.get_value(first_part, args, kwargs)# Record the fact that we used this argused_args.add(first_part)# Handle [subfield] or .subfield. Assume that 'components'# returns an iterator of the various subfields, not including# the first part.for comp in components(field_spec):value = resolve_subfield(value, comp)# Handle explicit type conversionif explicit == 'r':value = repr(value)elif explicit == 's':value = str(value)# Call the global 'format' function and write out the converted# value.buffer.write(self.format_field(value, format_spec))else:buffer.write(token)self.check_unused_args(used_args, args, kwargs)return buffer.getvalue()
This section describes some typical ways that Formatter objects can be customized.
To support alternative format-string syntax, the 'vformat' method can be overridden to alter the way format strings are parsed.
One common desire is to support a 'default' namespace, so that you don't need to pass in keyword arguments to the format() method, but can instead use values in a pre-existing namespace. This can easily be done by overriding get_value() as follows:
class NamespaceFormatter(Formatter):def __init__(self, namespace={}):Formatter.__init__(self)self.namespace = namespacedef get_value(self, key, args, kwds):if isinstance(key, str):try:# Check explicitly passed arguments firstreturn kwds[key]except KeyError:return self.namespace[key]else:Formatter.get_value(key, args, kwds)
fmt = NamespaceFormatter(globals())greeting = "hello"print(fmt.format("{greeting}, world!"))
Each field can also specify an optional set of 'format specifiers' which can be used to adjust the format of that field. Format specifiers follow the field name, with a colon (':') character separating the two:
"My name is {0:8}".format('Fred')
The meaning and syntax of the format specifiers depends on the type of object that is being formatted, but there is a standard set of format specifiers used for any object that does not override them.
Format specifiers can themselves contain replacement fields. For example, a field whose field width is itself a parameter could be specified via:
"{0:{1}}".format(a, b)
These 'internal' replacement fields can only occur in the format specifier part of the replacement field. Internal replacement fields cannot themselves have format specifiers. This implies also that replacement fields cannot be nested to arbitrary levels.
Note that the doubled '}' at the end, which would normally be escaped, is not escaped in this case. The reason is because the '{{' and '}}' syntax for escapes is only applied when used outside of a format field. Within a format field, the brace characters always have their normal meaning.
The syntax for format specifiers is open-ended, since a class can override the standard format specifiers. In such cases, the str.format() method merely passes all of the characters between the first colon and the matching brace to the relevant underlying formatting method.
If an object does not define its own format specifiers, a standard set of format specifiers is used. These are similar in concept to the format specifiers used by the existing '%' operator, however there are also a number of differences.
The general form of a standard format specifier is:
[[fill]align][sign][#][0][minimumwidth][.precision][type]
The brackets ([]) indicate an optional element.
Then the optional align flag can be one of the following:
'<' - Forces the field to be left-aligned within the availablespace (This is the default.)'>' - Forces the field to be right-aligned within theavailable space.'=' - Forces the padding to be placed after the sign (if any)but before the digits. This is used for printing fieldsin the form '+000000120'. This alignment option is onlyvalid for numeric types.'^' - Forces the field to be centered within the availablespace.Note that unless a minimum field width is defined, the field width will always be the same size as the data to fill it, so that the alignment option has no meaning in this case.
The optional 'fill' character defines the character to be used to pad the field to the minimum width. The fill character, if present, must be followed by an alignment flag.
The 'sign' option is only valid for numeric types, and can be one of the following:
'+' - indicates that a sign should be used for bothpositive as well as negative numbers'-' - indicates that a sign should be used only for negativenumbers (this is the default behavior)' ' - indicates that a leading space should be used onpositive numbersIf the '#' character is present, integers use the 'alternate form' for formatting. This means that binary, octal, and hexadecimal output will be prefixed with '0b', '0o', and '0x', respectively.
'width' is a decimal integer defining the minimum field width. If not specified, then the field width will be determined by the content.
If the width field is preceded by a zero ('0') character, this enables zero-padding. This is equivalent to an alignment type of '=' and a fill character of '0'.
The 'precision' is a decimal number indicating how many digits should be displayed after the decimal point in a floating point conversion. For non-numeric types the field indicates the maximum field size - in other words, how many characters will be used from the field content. The precision is ignored for integer conversions.
Finally, the 'type' determines how the data should be presented.
The available integer presentation types are:
'b' - Binary. Outputs the number in base 2.'c' - Character. Converts the integer to the correspondingUnicode character before printing.'d' - Decimal Integer. Outputs the number in base 10.'o' - Octal format. Outputs the number in base 8.'x' - Hex format. Outputs the number in base 16, using lower-case letters for the digits above 9.'X' - Hex format. Outputs the number in base 16, using upper-case letters for the digits above 9.'n' - Number. This is the same as 'd', except that it uses thecurrent locale setting to insert the appropriatenumber separator characters.'' (None) - the same as 'd'
The available floating point presentation types are:
'E' - Exponent notation. Same as 'e' except it converts thenumber to uppercase.'f' - Fixed point. Displays the number as a fixed-pointnumber.'F' - Fixed point. Same as 'f' except it converts the numberto uppercase.'g' - General format. This prints the number as a fixed-pointnumber, unless the number is too large, in which caseit switches to 'e' exponent notation.'G' - General format. Same as 'g' except switches to 'E'if the number gets to large.'n' - Number. This is the same as 'g', except that it uses thecurrent locale setting to insert the appropriatenumber separator characters.'%' - Percentage. Multiplies the number by 100 and displaysin fixed ('f') format, followed by a percent sign.'' (None) - similar to 'g', except that it prints at least onedigit after the decimal point.
Objects are able to define their own format specifiers to replace the standard ones. An example is the 'datetime' class, whose format specifiers might look something like the arguments to the strftime() function:
"Today is: {0:%a %b %d %H:%M:%S %Y}".format(datetime.now())
For all built-in types, an empty format specification will produce the equivalent of str(value). It is recommended that objects defining their own format specifiers follow this convention as well.
The explicit conversion flag is used to transform the format field value before it is formatted. This can be used to override the type-specific formatting behavior, and format the value as if it were a more generic type. Currently, two explicit conversion flags are recognized:
!r - convert the value to a string using repr().!s - convert the value to a string using str().These flags are placed before the format specifier:
"{0!r:20}".format("cppsecrets")
In the preceding example, the string "cppsecrets" will be printed, with quotes, in a field of at least 20 characters width.
A custom Formatter class can define additional conversion flags. The built-in formatter will raise a ValueError if an invalid conversion flag is specified.
There are two classes of exceptions which can occur during formatting: exceptions generated by the formatter code itself, and exceptions generated by user code (such as a field object's 'getattr' function).
In general, exceptions generated by the formatter code itself are of the "ValueError" variety -- there is an error in the actual "value" of the format string. (This is not always true; for example, the string.format() function might be passed a non-string as its first parameter, which would result in a TypeError.)
The text associated with these internally generated ValueError exceptions will indicate the location of the exception inside the format string, as well as the nature of the exception.
For exceptions generated by user code, a trace record and dummy frame will be added to the traceback stack to help in determining the location in the string where the exception occurred. The inserted traceback will indicate that the error occurred at:
File "<format_string>;", line XX, in column_YYwhere XX and YY represent the line and character position information in the string, respectively.
Comments