add 'S' conversion options for format string fields
Same as 's' (convert to string), but has a better, human-readable conversion for lists.
This commit is contained in:
@@ -80,6 +80,18 @@ def number_to_string(value, numbers=(int, float)):
|
||||
return str(value) if value.__class__ in numbers else value
|
||||
|
||||
|
||||
def to_string(value):
|
||||
"""str() with "better" defaults"""
|
||||
if not value:
|
||||
return ""
|
||||
if value.__class__ is list:
|
||||
try:
|
||||
return ", ".join(value)
|
||||
except Exception:
|
||||
return ", ".join(map(str, value))
|
||||
return str(value)
|
||||
|
||||
|
||||
def expand_path(path):
|
||||
"""Expand environment variables and tildes (~)"""
|
||||
if not path:
|
||||
@@ -335,12 +347,13 @@ class Formatter():
|
||||
Joins elements of a list (or string) using <separator>
|
||||
Example: {f:J - /} -> "a - b - c" (if "f" is ["a", "b", "c"])
|
||||
"""
|
||||
conversions = {
|
||||
CONVERSIONS = {
|
||||
"l": str.lower,
|
||||
"u": str.upper,
|
||||
"c": str.capitalize,
|
||||
"C": string.capwords,
|
||||
"U": urllib.parse.unquote,
|
||||
"S": to_string,
|
||||
"s": str,
|
||||
"r": repr,
|
||||
"a": ascii,
|
||||
|
||||
Reference in New Issue
Block a user