"""Formatter extension to support sliced strings.""" import string class SliceFormatter(string.Formatter): """Class extending string formatter by slice support.""" def get_value(self, key, args, kwargs): """Called by string.Formatter for each format key found in the format string. Args: key (str): Format key string (possibly containing slice) args (list): Format values list tuple kwargs (dict): Format values dictionary Returns: string: Formatted key or Missing word """ if '(' in key and ')' in key: try: key, indexes = key.rstrip(')').split('(') indexes = list(map(int, indexes.split(','))) if (indexes[1] < indexes[0]): indexes[1] += indexes[0] if key.isdigit(): return args[int(key)][slice(*indexes)] return kwargs[key][slice(*indexes)] except KeyError: return kwargs.get(key, 'Missing') return super(SliceFormatter, self).get_value(key, args, kwargs)