X-Git-Url: https://git.distorted.org.uk/~mdw/chopwood/blobdiff_plain/66f990ed01d94062547a4ce8879f8a647e450c3a..48a2cf35704f9e1add366eb1cf47b31426f6a8f4:/format.py diff --git a/format.py b/format.py index 7b5d93c..2b90cb9 100644 --- a/format.py +++ b/format.py @@ -296,7 +296,7 @@ class NextArg (BaseArg): else: return None NEXTARG = NextArg() -## Because a `NextArg' collectors are used so commonly, and they're all the +## Because `NextArg' collectors are used so commonly, and they're all the ## same, we make a distinguished one and try to use that instead. Nothing ## goes badly wrong if you don't use this, but you'll use more memory than ## strictly necessary. @@ -528,7 +528,7 @@ VARNEXT = VariableParameter(NEXTARG) ## whether the `@' and `:' modifiers were set in the control string. ## GETARG is the collector for the operation's argument(s). The PARAMS ## are a list of parameter collectors. Finally, CHAR is the directive -## character (so directives with siilar behaviour can use the same +## character (so directives with similar behaviour can use the same ## class). class FormatLiteral (object): @@ -798,7 +798,7 @@ def compile(control): PARAMS ::= PARAM [`,' PARAMS] - PARAM ::= EMPTY | INT | `'' CHAR | `v' | `!' ARG + PARAM ::= EMPTY | INT | `#' | `'' CHAR | `v' | `!' ARG FLAGS ::= [[ `@' | `:' ]]* @@ -806,12 +806,13 @@ def compile(control): items drawn from the listed alternatives, each appearing at most once. See the function `parse_arg' for the syntax of ARG.) - An empty PARAM is equivalent to omitting the parameter; `!ARG' reads the - parameter value from the argument; `v' is equivalent to `!+', as a - convenient abbreviation and for Common Lisp compatibility. The `=ARG' - notation indicates which argument(s) should be processed by the operation: - the default is `=+'. + An empty PARAM is equivalent to omitting the parameter; `#' is the number + of remaining positional arguments; `!ARG' reads the parameter value from + the argument; `v' is equivalent to `!+', as a convenient abbreviation and + for Common Lisp compatibility. The `=ARG' notation indicates which + argument(s) should be processed by the operation: the default is `=+' """ + if not isinstance(control, basestring): return control pp = [] with COMPILE.bind(control = control, start = 0, end = len(control), delim = ''): @@ -872,10 +873,7 @@ def format(out, control, *args, **kw): raise TypeError, out ## Turn the control argument into a formatting operation. - if isinstance(control, basestring): - op = compile(control) - else: - op = control + op = compile(control) ## Invoke the formatting operation in the correct environment. with FORMAT.bind(write = write, pushback = [], @@ -1260,7 +1258,7 @@ class FormatIteration (BaseFormatOperation): then the enclosed directives are applied once even if the argument sequence is empty. - If the formatting directives are empty then a formatting string is fetched + If the formatting directives are empty then a formatting control is fetched using the argument collector associated with the closing delimiter. """ @@ -1349,7 +1347,7 @@ class FormatRecursive (BaseFormatOperation): """ ~?: Recursive formatting. - Without `@', read a pair of arguments: use the first as a format string, + Without `@', read a pair of arguments: use the first as a format control, and apply it to the arguments extracted from the second (which may be a sequence or a map).