| OLD | NEW |
| 1 """Various utility functions.""" | 1 """Various utility functions.""" |
| 2 | 2 |
| 3 from collections import namedtuple, OrderedDict | 3 from collections import namedtuple, OrderedDict |
| 4 | 4 |
| 5 __unittest = True | 5 __unittest = True |
| 6 | 6 |
| 7 _MAX_LENGTH = 80 | 7 _MAX_LENGTH = 80 |
| 8 def safe_repr(obj, short=False): | 8 def safe_repr(obj, short=False, max_length=None): |
| 9 try: | 9 try: |
| 10 result = repr(obj) | 10 result = repr(obj) |
| 11 except Exception: | 11 except Exception: |
| 12 result = object.__repr__(obj) | 12 result = object.__repr__(obj) |
| 13 if not short or len(result) < _MAX_LENGTH: | 13 |
| 14 len_result = len(result) |
| 15 truncate_size = max_length or _MAX_LENGTH |
| 16 if not short or len_result <= truncate_size: |
| 14 return result | 17 return result |
| 15 return result[:_MAX_LENGTH] + ' [truncated]...' | 18 |
| 19 if truncate_size <= _MAX_LENGTH: |
| 20 result = result[:truncate_size] |
| 21 else: |
| 22 # 0 _MAX_LENGTH truncate_size len(result) |
| 23 # begin trunc end |
| 24 # <-----------------------------------------------> |
| 25 # begin ... trunc |
| 26 # <--------------> |
| 27 result = (result[:_MAX_LENGTH // 2] + '...' + |
| 28 result[-_MAX_LENGTH // 2:truncate_size]) |
| 29 |
| 30 if truncate_size < len_result: |
| 31 return result + ' [truncated]...' |
| 32 return result |
| 16 | 33 |
| 17 def strclass(cls): | 34 def strclass(cls): |
| 18 return "%s.%s" % (cls.__module__, cls.__name__) | 35 return "%s.%s" % (cls.__module__, cls.__name__) |
| 19 | 36 |
| 20 def sorted_list_difference(expected, actual): | 37 def sorted_list_difference(expected, actual): |
| 21 """Finds elements in only one or the other of two, sorted input lists. | 38 """Finds elements in only one or the other of two, sorted input lists. |
| 22 | 39 |
| 23 Returns a two-element tuple of lists. The first list contains those | 40 Returns a two-element tuple of lists. The first list contains those |
| 24 elements in the "expected" list but not in the "actual" list, and the | 41 elements in the "expected" list but not in the "actual" list, and the |
| 25 second contains those elements in the "actual" list but not in the | 42 second contains those elements in the "actual" list but not in the |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 for elem, cnt_s in s.items(): | 148 for elem, cnt_s in s.items(): |
| 132 cnt_t = t.get(elem, 0) | 149 cnt_t = t.get(elem, 0) |
| 133 if cnt_s != cnt_t: | 150 if cnt_s != cnt_t: |
| 134 diff = _Mismatch(cnt_s, cnt_t, elem) | 151 diff = _Mismatch(cnt_s, cnt_t, elem) |
| 135 result.append(diff) | 152 result.append(diff) |
| 136 for elem, cnt_t in t.items(): | 153 for elem, cnt_t in t.items(): |
| 137 if elem not in s: | 154 if elem not in s: |
| 138 diff = _Mismatch(0, cnt_t, elem) | 155 diff = _Mismatch(0, cnt_t, elem) |
| 139 result.append(diff) | 156 result.append(diff) |
| 140 return result | 157 return result |
| OLD | NEW |