blob: 7d90ddf9ca4f2f5f0abcfbc6c2d45dcb87ec04a3 [file] [log] [blame]
Benjamin Petersond7b0eeb2009-07-19 20:18:21 +00001"""Various utility functions."""
2
Michael Foordb1aa30f2010-03-22 00:06:30 +00003__unittest = True
4
5
Michael Foord225a0992010-02-18 20:30:09 +00006def safe_repr(obj):
7 try:
8 return repr(obj)
9 except Exception:
10 return object.__repr__(obj)
11
Benjamin Petersond7b0eeb2009-07-19 20:18:21 +000012def strclass(cls):
13 return "%s.%s" % (cls.__module__, cls.__name__)
14
15def sorted_list_difference(expected, actual):
16 """Finds elements in only one or the other of two, sorted input lists.
17
18 Returns a two-element tuple of lists. The first list contains those
19 elements in the "expected" list but not in the "actual" list, and the
20 second contains those elements in the "actual" list but not in the
21 "expected" list. Duplicate elements in either input list are ignored.
22 """
23 i = j = 0
24 missing = []
25 unexpected = []
26 while True:
27 try:
28 e = expected[i]
29 a = actual[j]
30 if e < a:
31 missing.append(e)
32 i += 1
33 while expected[i] == e:
34 i += 1
35 elif e > a:
36 unexpected.append(a)
37 j += 1
38 while actual[j] == a:
39 j += 1
40 else:
41 i += 1
42 try:
43 while expected[i] == e:
44 i += 1
45 finally:
46 j += 1
47 while actual[j] == a:
48 j += 1
49 except IndexError:
50 missing.extend(expected[i:])
51 unexpected.extend(actual[j:])
52 break
53 return missing, unexpected
Michael Foord98e7b762010-03-20 03:00:34 +000054
55
56def unorderable_list_difference(expected, actual, ignore_duplicate=False):
57 """Same behavior as sorted_list_difference but
58 for lists of unorderable items (like dicts).
59
60 As it does a linear search per item (remove) it
61 has O(n*n) performance.
62 """
63 missing = []
64 unexpected = []
65 while expected:
66 item = expected.pop()
67 try:
68 actual.remove(item)
69 except ValueError:
70 missing.append(item)
71 if ignore_duplicate:
72 for lst in expected, actual:
73 try:
74 while True:
75 lst.remove(item)
76 except ValueError:
77 pass
78 if ignore_duplicate:
79 while actual:
80 item = actual.pop()
81 unexpected.append(item)
82 try:
83 while True:
84 actual.remove(item)
85 except ValueError:
86 pass
87 return missing, unexpected
88
89 # anything left in actual is unexpected
90 return missing, actual