code: def truncate(s, max_len=20, ellipsis='...'):
      r
      if s is None:
          return None
      elif isinstance(s, basestring):
          return s[:min(len(s), max_len)] + ellipsis if len(s) > max_len else ''
      elif isinstance(s, Mapping):
          truncated_str = str(dict(islice(viewitems(s), max_len)))
      else:
          truncated_str = str(list(islice(s, max_len)))
      return truncated_str[:-1] + '...' if len(s) > max_len else truncated_str
docs: r"""Return string at most `max_len` characters or sequence elments appended with the `ellipsis` characters
  
      >>> truncate(OrderedDict(zip(list('ABCDEFGH'), range(8))), 1)
      "{'A': 0..."
      >>> truncate(list(range(5)), 3)
      '[0, 1, 2...'
      >>> truncate(np.arange(5), 3)
      '[0, 1, 2...'
      >>> truncate('Too verbose for its own good.', 11)
      'Too verbose...'