util.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. """Various utility functions."""
  2. from collections import namedtuple, Counter
  3. from os.path import commonprefix
  4. __unittest = True
  5. _MAX_LENGTH = 80
  6. _PLACEHOLDER_LEN = 12
  7. _MIN_BEGIN_LEN = 5
  8. _MIN_END_LEN = 5
  9. _MIN_COMMON_LEN = 5
  10. _MIN_DIFF_LEN = _MAX_LENGTH - \
  11. (_MIN_BEGIN_LEN + _PLACEHOLDER_LEN + _MIN_COMMON_LEN +
  12. _PLACEHOLDER_LEN + _MIN_END_LEN)
  13. assert _MIN_DIFF_LEN >= 0
  14. def _shorten(s, prefixlen, suffixlen):
  15. skip = len(s) - prefixlen - suffixlen
  16. if skip > _PLACEHOLDER_LEN:
  17. s = '%s[%d chars]%s' % (s[:prefixlen], skip, s[len(s) - suffixlen:])
  18. return s
  19. def _common_shorten_repr(*args):
  20. args = tuple(map(safe_repr, args))
  21. maxlen = max(map(len, args))
  22. if maxlen <= _MAX_LENGTH:
  23. return args
  24. prefix = commonprefix(args)
  25. prefixlen = len(prefix)
  26. common_len = _MAX_LENGTH - \
  27. (maxlen - prefixlen + _MIN_BEGIN_LEN + _PLACEHOLDER_LEN)
  28. if common_len > _MIN_COMMON_LEN:
  29. assert _MIN_BEGIN_LEN + _PLACEHOLDER_LEN + _MIN_COMMON_LEN + \
  30. (maxlen - prefixlen) < _MAX_LENGTH
  31. prefix = _shorten(prefix, _MIN_BEGIN_LEN, common_len)
  32. return tuple(prefix + s[prefixlen:] for s in args)
  33. prefix = _shorten(prefix, _MIN_BEGIN_LEN, _MIN_COMMON_LEN)
  34. return tuple(prefix + _shorten(s[prefixlen:], _MIN_DIFF_LEN, _MIN_END_LEN)
  35. for s in args)
  36. def safe_repr(obj, short=False):
  37. try:
  38. result = repr(obj)
  39. except Exception:
  40. result = object.__repr__(obj)
  41. if not short or len(result) < _MAX_LENGTH:
  42. return result
  43. return result[:_MAX_LENGTH] + ' [truncated]...'
  44. def strclass(cls):
  45. return "%s.%s" % (cls.__module__, cls.__qualname__)
  46. def sorted_list_difference(expected, actual):
  47. """Finds elements in only one or the other of two, sorted input lists.
  48. Returns a two-element tuple of lists. The first list contains those
  49. elements in the "expected" list but not in the "actual" list, and the
  50. second contains those elements in the "actual" list but not in the
  51. "expected" list. Duplicate elements in either input list are ignored.
  52. """
  53. i = j = 0
  54. missing = []
  55. unexpected = []
  56. while True:
  57. try:
  58. e = expected[i]
  59. a = actual[j]
  60. if e < a:
  61. missing.append(e)
  62. i += 1
  63. while expected[i] == e:
  64. i += 1
  65. elif e > a:
  66. unexpected.append(a)
  67. j += 1
  68. while actual[j] == a:
  69. j += 1
  70. else:
  71. i += 1
  72. try:
  73. while expected[i] == e:
  74. i += 1
  75. finally:
  76. j += 1
  77. while actual[j] == a:
  78. j += 1
  79. except IndexError:
  80. missing.extend(expected[i:])
  81. unexpected.extend(actual[j:])
  82. break
  83. return missing, unexpected
  84. def unorderable_list_difference(expected, actual):
  85. """Same behavior as sorted_list_difference but
  86. for lists of unorderable items (like dicts).
  87. As it does a linear search per item (remove) it
  88. has O(n*n) performance."""
  89. missing = []
  90. while expected:
  91. item = expected.pop()
  92. try:
  93. actual.remove(item)
  94. except ValueError:
  95. missing.append(item)
  96. # anything left in actual is unexpected
  97. return missing, actual
  98. def three_way_cmp(x, y):
  99. """Return -1 if x < y, 0 if x == y and 1 if x > y"""
  100. return (x > y) - (x < y)
  101. _Mismatch = namedtuple('Mismatch', 'actual expected value')
  102. def _count_diff_all_purpose(actual, expected):
  103. 'Returns list of (cnt_act, cnt_exp, elem) triples where the counts differ'
  104. # elements need not be hashable
  105. s, t = list(actual), list(expected)
  106. m, n = len(s), len(t)
  107. NULL = object()
  108. result = []
  109. for i, elem in enumerate(s):
  110. if elem is NULL:
  111. continue
  112. cnt_s = cnt_t = 0
  113. for j in range(i, m):
  114. if s[j] == elem:
  115. cnt_s += 1
  116. s[j] = NULL
  117. for j, other_elem in enumerate(t):
  118. if other_elem == elem:
  119. cnt_t += 1
  120. t[j] = NULL
  121. if cnt_s != cnt_t:
  122. diff = _Mismatch(cnt_s, cnt_t, elem)
  123. result.append(diff)
  124. for i, elem in enumerate(t):
  125. if elem is NULL:
  126. continue
  127. cnt_t = 0
  128. for j in range(i, n):
  129. if t[j] == elem:
  130. cnt_t += 1
  131. t[j] = NULL
  132. diff = _Mismatch(0, cnt_t, elem)
  133. result.append(diff)
  134. return result
  135. def _count_diff_hashable(actual, expected):
  136. 'Returns list of (cnt_act, cnt_exp, elem) triples where the counts differ'
  137. # elements must be hashable
  138. s, t = Counter(actual), Counter(expected)
  139. result = []
  140. for elem, cnt_s in s.items():
  141. cnt_t = t.get(elem, 0)
  142. if cnt_s != cnt_t:
  143. diff = _Mismatch(cnt_s, cnt_t, elem)
  144. result.append(diff)
  145. for elem, cnt_t in t.items():
  146. if elem not in s:
  147. diff = _Mismatch(0, cnt_t, elem)
  148. result.append(diff)
  149. return result