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