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