Updated Pep 3101 to reflect typos and other small errors found by Mark Summerfield (and a few by myself)
This commit is contained in:
parent
71ead7f575
commit
15570e7ea0
89
pep-3101.txt
89
pep-3101.txt
|
@ -120,9 +120,8 @@ Format Strings
|
||||||
Character data is data which is transferred unchanged from the
|
Character data is data which is transferred unchanged from the
|
||||||
format string to the output string; markup is not transferred from
|
format string to the output string; markup is not transferred from
|
||||||
the format string directly to the output, but instead is used to
|
the format string directly to the output, but instead is used to
|
||||||
define 'replacement fields' that describes to the format engine
|
define 'replacement fields' that describe to the format engine
|
||||||
what should be placed in the output string in the place of the
|
what should be placed in the output string in place of the markup.
|
||||||
markup.
|
|
||||||
|
|
||||||
Brace characters ('curly braces') are used to indicate a
|
Brace characters ('curly braces') are used to indicate a
|
||||||
replacement field within the string:
|
replacement field within the string:
|
||||||
|
@ -156,7 +155,7 @@ Simple and Compound Field Names
|
||||||
A compound field name is a combination of multiple simple field
|
A compound field name is a combination of multiple simple field
|
||||||
names in an expression:
|
names in an expression:
|
||||||
|
|
||||||
"My name is {0.name}".format(file('out.txt'))
|
"My name is {0.name}".format(open('out.txt', 'w'))
|
||||||
|
|
||||||
This example shows the use of the 'getattr' or 'dot' operator
|
This example shows the use of the 'getattr' or 'dot' operator
|
||||||
in a field expression. The dot operator allows an attribute of
|
in a field expression. The dot operator allows an attribute of
|
||||||
|
@ -177,19 +176,21 @@ Simple and Compound Field Names
|
||||||
is much more limited than its conventional usage. In the above example,
|
is much more limited than its conventional usage. In the above example,
|
||||||
the string 'name' really is the literal string 'name', not a variable
|
the string 'name' really is the literal string 'name', not a variable
|
||||||
named 'name'. The rules for parsing an item key are very simple.
|
named 'name'. The rules for parsing an item key are very simple.
|
||||||
If it starts with a digit, then its treated as a number, otherwise
|
If it starts with a digit, then it is treated as a number, otherwise
|
||||||
it is used as a string.
|
it is used as a string.
|
||||||
|
|
||||||
It is not possible to specify arbitrary dictionary keys from
|
Because keys are not quote-delimited, it is not possible to
|
||||||
within a format string.
|
specify arbitrary dictionary keys (e.g., the strings "10" or
|
||||||
|
":-]") from within a format string.
|
||||||
|
|
||||||
Implementation note: The implementation of this proposal is
|
Implementation note: The implementation of this proposal is
|
||||||
not required to enforce the rule about a name being a valid
|
not required to enforce the rule about a simple or dotted name
|
||||||
Python identifier. Instead, it will rely on the getattr function
|
being a valid Python identifier. Instead, it will rely on the
|
||||||
of the underlying object to throw an exception if the identifier
|
getattr function of the underlying object to throw an exception if
|
||||||
is not legal. The str.format() function will have a minimalist
|
the identifier is not legal. The str.format() function will have
|
||||||
parser which only attempts to figure out when it is "done" with an
|
a minimalist parser which only attempts to figure out when it is
|
||||||
identifier (by finding a '.' or a ']', or '}', etc.).
|
"done" with an identifier (by finding a '.' or a ']', or '}',
|
||||||
|
etc.).
|
||||||
|
|
||||||
|
|
||||||
Format Specifiers
|
Format Specifiers
|
||||||
|
@ -202,15 +203,15 @@ Format Specifiers
|
||||||
"My name is {0:8}".format('Fred')
|
"My name is {0:8}".format('Fred')
|
||||||
|
|
||||||
The meaning and syntax of the format specifiers depends on the
|
The meaning and syntax of the format specifiers depends on the
|
||||||
type of object that is being formatted, however there is a
|
type of object that is being formatted, but there is a standard
|
||||||
standard set of format specifiers used for any object that
|
set of format specifiers used for any object that does not
|
||||||
does not override them.
|
override them.
|
||||||
|
|
||||||
Format specifiers can themselves contain replacement fields.
|
Format specifiers can themselves contain replacement fields.
|
||||||
For example, a field whose field width is itself a parameter
|
For example, a field whose field width is itself a parameter
|
||||||
could be specified via:
|
could be specified via:
|
||||||
|
|
||||||
"{0:{1}}".format(a, b, c)
|
"{0:{1}}".format(a, b)
|
||||||
|
|
||||||
These 'internal' replacement fields can only occur in the format
|
These 'internal' replacement fields can only occur in the format
|
||||||
specifier part of the replacement field. Internal replacement fields
|
specifier part of the replacement field. Internal replacement fields
|
||||||
|
@ -232,14 +233,14 @@ Format Specifiers
|
||||||
|
|
||||||
Standard Format Specifiers
|
Standard Format Specifiers
|
||||||
|
|
||||||
If an object does not define its own format specifiers, a
|
If an object does not define its own format specifiers, a standard
|
||||||
standard set of format specifiers are used. These are similar
|
set of format specifiers is used. These are similar in concept to
|
||||||
in concept to the format specifiers used by the existing '%'
|
the format specifiers used by the existing '%' operator, however
|
||||||
operator, however there are also a number of differences.
|
there are also a number of differences.
|
||||||
|
|
||||||
The general form of a standard format specifier is:
|
The general form of a standard format specifier is:
|
||||||
|
|
||||||
[[fill]align][sign][0][width][.precision][type]
|
[[fill]align][sign][0][minimumwidth][.precision][type]
|
||||||
|
|
||||||
The brackets ([]) indicate an optional element.
|
The brackets ([]) indicate an optional element.
|
||||||
|
|
||||||
|
@ -264,8 +265,8 @@ Standard Format Specifiers
|
||||||
pad the field to the minimum width. The fill character, if present,
|
pad the field to the minimum width. The fill character, if present,
|
||||||
must be followed by an alignment flag.
|
must be followed by an alignment flag.
|
||||||
|
|
||||||
The 'sign' option is only valid for number types, and can be one of
|
The 'sign' option is only valid for numeric types, and can be one
|
||||||
the following:
|
of the following:
|
||||||
|
|
||||||
'+' - indicates that a sign should be used for both
|
'+' - indicates that a sign should be used for both
|
||||||
positive as well as negative numbers
|
positive as well as negative numbers
|
||||||
|
@ -284,7 +285,7 @@ Standard Format Specifiers
|
||||||
|
|
||||||
The 'precision' is a decimal number indicating how many digits
|
The 'precision' is a decimal number indicating how many digits
|
||||||
should be displayed after the decimal point in a floating point
|
should be displayed after the decimal point in a floating point
|
||||||
conversion. For non-number types the field indicates the maximum
|
conversion. For non-numeric types the field indicates the maximum
|
||||||
field size - in other words, how many characters will be used from
|
field size - in other words, how many characters will be used from
|
||||||
the field content. The precision is ignored for integer conversions.
|
the field content. The precision is ignored for integer conversions.
|
||||||
|
|
||||||
|
@ -294,7 +295,7 @@ Standard Format Specifiers
|
||||||
|
|
||||||
'b' - Binary. Outputs the number in base 2.
|
'b' - Binary. Outputs the number in base 2.
|
||||||
'c' - Character. Converts the integer to the corresponding
|
'c' - Character. Converts the integer to the corresponding
|
||||||
unicode character before printing.
|
Unicode character before printing.
|
||||||
'd' - Decimal Integer. Outputs the number in base 10.
|
'd' - Decimal Integer. Outputs the number in base 10.
|
||||||
'o' - Octal format. Outputs the number in base 8.
|
'o' - Octal format. Outputs the number in base 8.
|
||||||
'x' - Hex format. Outputs the number in base 16, using lower-
|
'x' - Hex format. Outputs the number in base 16, using lower-
|
||||||
|
@ -333,9 +334,9 @@ Standard Format Specifiers
|
||||||
"Today is: {0:a b d H:M:S Y}".format(datetime.now())
|
"Today is: {0:a b d H:M:S Y}".format(datetime.now())
|
||||||
|
|
||||||
For all built-in types, an empty format specification will produce
|
For all built-in types, an empty format specification will produce
|
||||||
the same result as would have been produced by calling str(value).
|
the equivalent of str(value). It is recommended that objects
|
||||||
It is recommended that objects defining their own format specifiers
|
defining their own format specifiers follow this convention as
|
||||||
follow this convention as well.
|
well.
|
||||||
|
|
||||||
|
|
||||||
Explicit Conversion Flag
|
Explicit Conversion Flag
|
||||||
|
@ -426,11 +427,11 @@ User-Defined Formatting
|
||||||
lives in the 'string' module. This class takes additional options
|
lives in the 'string' module. This class takes additional options
|
||||||
which are not accessible via the normal str.format method.
|
which are not accessible via the normal str.format method.
|
||||||
|
|
||||||
An application can subclass the Formatter class to create their
|
An application can subclass the Formatter class to create its own
|
||||||
own customized formatting behavior.
|
customized formatting behavior.
|
||||||
|
|
||||||
The PEP does not attempt to exactly specify all methods and
|
The PEP does not attempt to exactly specify all methods and
|
||||||
properties defined by the Formatter class; Instead, those will be
|
properties defined by the Formatter class; instead, those will be
|
||||||
defined and documented in the initial implementation. However, this
|
defined and documented in the initial implementation. However, this
|
||||||
PEP will specify the general requirements for the Formatter class,
|
PEP will specify the general requirements for the Formatter class,
|
||||||
which are listed below.
|
which are listed below.
|
||||||
|
@ -456,8 +457,8 @@ Formatter Methods
|
||||||
-- vformat(format_string, args, kwargs)
|
-- vformat(format_string, args, kwargs)
|
||||||
|
|
||||||
'format' is the primary API method. It takes a format template,
|
'format' is the primary API method. It takes a format template,
|
||||||
and an arbitrary set of positional and keyword argument. 'format'
|
and an arbitrary set of positional and keyword arguments.
|
||||||
is just a wrapper that calls 'vformat'.
|
'format' is just a wrapper that calls 'vformat'.
|
||||||
|
|
||||||
'vformat' is the function that does the actual work of formatting. It
|
'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
|
is exposed as a separate function for cases where you want to pass in
|
||||||
|
@ -484,7 +485,7 @@ Formatter Methods
|
||||||
positional arguments.
|
positional arguments.
|
||||||
|
|
||||||
For compound field names, these functions are only called for the
|
For compound field names, these functions are only called for the
|
||||||
first component of the field name; Subsequent components are handled
|
first component of the field name; subsequent components are handled
|
||||||
through normal attribute and indexing operations.
|
through normal attribute and indexing operations.
|
||||||
|
|
||||||
So for example, the field expression '0.name' would cause 'get_value'
|
So for example, the field expression '0.name' would cause 'get_value'
|
||||||
|
@ -606,9 +607,9 @@ Customizing Formatters
|
||||||
It would also be possible to create a 'smart' namespace formatter
|
It would also be possible to create a 'smart' namespace formatter
|
||||||
that could automatically access both locals and globals through
|
that could automatically access both locals and globals through
|
||||||
snooping of the calling stack. Due to the need for compatibility
|
snooping of the calling stack. Due to the need for compatibility
|
||||||
the different versions of Python, such a capability will not be
|
with the different versions of Python, such a capability will not
|
||||||
included in the standard library, however it is anticipated that
|
be included in the standard library, however it is anticipated
|
||||||
someone will create and publish a recipe for doing this.
|
that someone will create and publish a recipe for doing this.
|
||||||
|
|
||||||
Another type of customization is to change the way that built-in
|
Another type of customization is to change the way that built-in
|
||||||
types are formatted by overriding the 'format_field' method. (For
|
types are formatted by overriding the 'format_field' method. (For
|
||||||
|
@ -663,9 +664,9 @@ Alternate Syntax
|
||||||
|
|
||||||
This scheme is generally used in cases where interpolation is
|
This scheme is generally used in cases where interpolation is
|
||||||
implicit - that is, in environments where any string can contain
|
implicit - that is, in environments where any string can contain
|
||||||
interpolation variables, and no special subsitution function
|
interpolation variables, and no special substitution function
|
||||||
need be invoked. In such cases, it is important to prevent the
|
need be invoked. In such cases, it is important to prevent the
|
||||||
interpolation behavior from occuring accidentally, so the '$'
|
interpolation behavior from occurring accidentally, so the '$'
|
||||||
(which is otherwise a relatively uncommonly-used character) is
|
(which is otherwise a relatively uncommonly-used character) is
|
||||||
used to signal when the behavior should occur.
|
used to signal when the behavior should occur.
|
||||||
|
|
||||||
|
@ -674,7 +675,7 @@ Alternate Syntax
|
||||||
taken to prevent accidental interpolation, in which case a
|
taken to prevent accidental interpolation, in which case a
|
||||||
lighter and less unwieldy syntax can be used.
|
lighter and less unwieldy syntax can be used.
|
||||||
|
|
||||||
- Printf and its cousins ('%'), including variations that add a
|
- printf and its cousins ('%'), including variations that add a
|
||||||
field index, so that fields can be interpolated out of order.
|
field index, so that fields can be interpolated out of order.
|
||||||
|
|
||||||
- Other bracket-only variations. Various MUDs (Multi-User
|
- Other bracket-only variations. Various MUDs (Multi-User
|
||||||
|
@ -731,7 +732,7 @@ Alternate Feature Proposals
|
||||||
the parameter before it's passed in to the formatting function.
|
the parameter before it's passed in to the formatting function.
|
||||||
For cases where the format string is being use to do arbitrary
|
For cases where the format string is being use to do arbitrary
|
||||||
formatting in a data-rich environment, it's recommended to use
|
formatting in a data-rich environment, it's recommended to use
|
||||||
a templating engine specialized for this purpose, such as
|
a template engine specialized for this purpose, such as
|
||||||
Genshi [5] or Cheetah [6].
|
Genshi [5] or Cheetah [6].
|
||||||
|
|
||||||
Many other features were considered and rejected because they
|
Many other features were considered and rejected because they
|
||||||
|
@ -744,14 +745,14 @@ Security Considerations
|
||||||
|
|
||||||
Historically, string formatting has been a common source of
|
Historically, string formatting has been a common source of
|
||||||
security holes in web-based applications, particularly if the
|
security holes in web-based applications, particularly if the
|
||||||
string templating system allows arbitrary expressions to be
|
string formatting system allows arbitrary expressions to be
|
||||||
embedded in format strings.
|
embedded in format strings.
|
||||||
|
|
||||||
The best way to use string formatting in a way that does not
|
The best way to use string formatting in a way that does not
|
||||||
create potential security holes is to never use format strings
|
create potential security holes is to never use format strings
|
||||||
that come from an untrusted source.
|
that come from an untrusted source.
|
||||||
|
|
||||||
Barring that, the next best approach is to insure that string
|
Barring that, the next best approach is to ensure that string
|
||||||
formatting has no side effects. Because of the open nature of
|
formatting has no side effects. Because of the open nature of
|
||||||
Python, it is impossible to guarantee that any non-trivial
|
Python, it is impossible to guarantee that any non-trivial
|
||||||
operation has this property. What this PEP does is limit the
|
operation has this property. What this PEP does is limit the
|
||||||
|
|
Loading…
Reference in New Issue