[formatter] implement 'S' format specifier (#3266)

to Sort lists
This commit is contained in:
Mike Fährmann
2022-11-21 21:44:42 +01:00
parent 8a021e4ee4
commit 42481aed59
4 changed files with 40 additions and 1 deletions

View File

@@ -347,6 +347,20 @@ def _parse_offset(format_spec, default):
return off
def _parse_sort(format_spec, default):
args, _, format_spec = format_spec.partition(_SEPARATOR)
fmt = _build_format_func(format_spec, default)
if "d" in args or "r" in args:
def sort_desc(obj):
return fmt(sorted(obj, reverse=True))
return sort_desc
else:
def sort_asc(obj):
return fmt(sorted(obj))
return sort_asc
def _default_format(format_spec, default):
def wrap(obj):
return format(obj, format_spec)
@@ -395,4 +409,5 @@ _FORMAT_SPECIFIERS = {
"J": _parse_join,
"O": _parse_offset,
"R": _parse_replace,
"S": _parse_sort,
}

View File

@@ -6,4 +6,4 @@
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
__version__ = "1.24.0"
__version__ = "1.24.1-dev"