implement string slicing for format strings

It is now possible to slice string (or list) values of format string
replacement fields with the same syntax as in regular Python code.

"{digits}"       -> "0123456789"
"{digits[2:-2]}" -> "234567"
"{digits[:5]}"   -> "01234"

The optional third parameter (step) has been left out to simplify things.
This commit is contained in:
Mike Fährmann
2018-07-13 16:52:56 +02:00
parent 269dc2bbd5
commit 8fe9056b16
2 changed files with 14 additions and 0 deletions

View File

@@ -344,6 +344,10 @@ class Formatter():
for is_attr, i in rest:
if is_attr:
obj = getattr(obj, i)
elif ":" in i:
start, _, stop = i.partition(":")
start = int(start) if start else 0
return obj[start:int(stop)] if stop else obj[start:]
else:
obj = obj[i]