format() function #41
Replies: 4 comments 2 replies
-
Thanks a lot for the detailed suggestion. I personnaly think we should exclude "local aware" transformations capabilities from JMESPath. |
Beta Was this translation helpful? Give feedback.
-
What about including in the string presentation types base64 encode/decode, uri encode/decode, md5, and sha1/256? |
Beta Was this translation helpful? Give feedback.
-
Looks like someone else agrees that the python format syntax is worth adopting: https://github.com/fmtlib/fmt#features |
Beta Was this translation helpful? Give feedback.
-
Does this conflict or could work hand in hand with a similar string-interpolation feature? |
Beta Was this translation helpful? Give feedback.
-
A number of string composition functions are needed to support common use cases. We could propose a new function for each case, but this pollutes the function list and potentially complex manipulation will become verbose.
I propose we roll all of string composition into a single function that accepts a format string with syntax that allows describing all of the composition features that might be required.
The syntax would be
string format(object|array data, string formatstr)
Python already has a mature syntax for this and looking at what is offered in other languages, it appears to be the most feature rich. It would be best to borrow heavily from this syntax, while also tweaking it to fit JMESPath:
Format strings contain “replacement fields” surrounded by curly braces {}. Anything that is not contained in braces is considered literal text, which is copied unchanged to the output. If you need to include a brace character in the literal text, it can be escaped by doubling:
{{
and}}
.The grammar for a replacement field is as follows:
In less formal terms, the replacement field can start with a field_name that specifies the object whose value is to be formatted and inserted into the output instead of the replacement field. The field_name is optionally followed by a conversion field, which is preceded by an exclamation point '!', and a format_spec, which is preceded by a colon ':'. These specify a non-default format for the replacement value.
The field_name itself begins with an arg_name that is either a number or a keyword. If it’s a number, it refers to a positional argument, and if it’s a keyword, it refers to a named keyword argument. If the numerical arg_names in a format string are 0, 1, 2, … in sequence, they can all be omitted (not just some) and the numbers 0, 1, 2, … will be automatically inserted in that order. Because arg_name is not quote-delimited, it is not possible to specify arbitrary dictionary keys (e.g., the strings '10' or ':-]') within a format string.
format_spec
“Format specifications” are used within replacement fields contained within a format string to define how individual values are presented. Each formattable type may define how the format specification is to be interpreted.
A general convention is that an empty format specification produces the same result as if you had called
to_string()
on the value. A non-empty format specification typically modifies the result.The general form of a standard format specifier is:
If a valid align value is specified, it can be preceded by a fill character that can be any character and defaults to a space if omitted. It is not possible to use a literal curly brace (“{” or “}”) as the fill character. However, it is possible to insert a curly brace with a nested replacement field.
The meaning of the various alignment options is as follows:
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 sign option is only valid for number types, and can be one of the following:
The '#' option causes the “alternate form” to be used for the conversion. The alternate form is defined differently for different types. This option is only valid for integer, float and complex types. For integers, when binary, octal, or hexadecimal output is used, this option adds the respective prefix '0b', '0o', '0x', or '0X' to the output value. For float and complex the alternate form causes the result of the conversion to always contain a decimal-point character, even if no digits follow it. Normally, a decimal-point character appears in the result of these conversions only if a digit follows it. In addition, for 'g' and 'G' conversions, trailing zeros are not removed from the result.
The ',' option signals the use of a comma for a thousands separator. For a locale aware separator, use the 'n' integer presentation type instead.
The '_' option signals the use of an underscore for a thousands separator for floating point presentation types and for integer presentation type 'd'. For integer presentation types 'b', 'o', 'x', and 'X', underscores will be inserted every 4 digits. For other presentation types, specifying this option is an error.
width is a decimal integer defining the minimum total field width, including any prefixes, separators, and other formatting characters. If not specified, then the field width will be determined by the content.
When no explicit alignment is given, preceding the width field by a zero ('0') character enables sign-aware zero-padding for numeric types. This is equivalent to a fill character of '0' with an alignment type of '='.
The precision is a decimal integer indicating how many digits should be displayed after the decimal point for presentation types 'f' and 'F', or before and after the decimal point for presentation types 'g' or 'G'. For string presentation types the field indicates the maximum field size - in other words, how many characters will be used from the field content. The precision is not allowed for integer presentation types.
Finally, the type determines how the data should be presented.
The available string presentation types are:
The available number presentation types are:
The available boolean presentation types are:
I am interested to know if anyone can think of any other presentation types that could be included?
Beta Was this translation helpful? Give feedback.
All reactions