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:
@@ -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]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user