Return a string representation for of a list where list is elided if it has more than n elements Parameters ---------- v : list Input list threshold : Maximum number of elements to display Returns ------- str
(v, threshold=200, edgeitems=3, indent=0, width=80)
| 7 | |
| 8 | # Pretty printing |
| 9 | def _list_repr_elided(v, threshold=200, edgeitems=3, indent=0, width=80): |
| 10 | """ |
| 11 | Return a string representation for of a list where list is elided if |
| 12 | it has more than n elements |
| 13 | |
| 14 | Parameters |
| 15 | ---------- |
| 16 | v : list |
| 17 | Input list |
| 18 | threshold : |
| 19 | Maximum number of elements to display |
| 20 | |
| 21 | Returns |
| 22 | ------- |
| 23 | str |
| 24 | """ |
| 25 | if isinstance(v, list): |
| 26 | open_char, close_char = "[", "]" |
| 27 | elif isinstance(v, tuple): |
| 28 | open_char, close_char = "(", ")" |
| 29 | else: |
| 30 | raise ValueError("Invalid value of type: %s" % type(v)) |
| 31 | |
| 32 | if len(v) <= threshold: |
| 33 | disp_v = v |
| 34 | else: |
| 35 | disp_v = list(v[:edgeitems]) + ["..."] + list(v[-edgeitems:]) |
| 36 | |
| 37 | v_str = open_char + ", ".join([str(e) for e in disp_v]) + close_char |
| 38 | |
| 39 | v_wrapped = "\n".join( |
| 40 | textwrap.wrap( |
| 41 | v_str, |
| 42 | width=width, |
| 43 | initial_indent=" " * (indent + 1), |
| 44 | subsequent_indent=" " * (indent + 1), |
| 45 | ) |
| 46 | ).strip() |
| 47 | return v_wrapped |
| 48 | |
| 49 | |
| 50 | class ElidedWrapper(object): |