Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 1 | """Various utility functions.""" |
| 2 | |
Benjamin Peterson | 847a411 | 2010-03-14 15:04:17 +0000 | [diff] [blame^] | 3 | def safe_repr(obj): |
| 4 | try: |
| 5 | return repr(obj) |
| 6 | except Exception: |
| 7 | return object.__repr__(obj) |
| 8 | |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 9 | def strclass(cls): |
| 10 | return "%s.%s" % (cls.__module__, cls.__name__) |
| 11 | |
| 12 | def sorted_list_difference(expected, actual): |
| 13 | """Finds elements in only one or the other of two, sorted input lists. |
| 14 | |
| 15 | Returns a two-element tuple of lists. The first list contains those |
| 16 | elements in the "expected" list but not in the "actual" list, and the |
| 17 | second contains those elements in the "actual" list but not in the |
| 18 | "expected" list. Duplicate elements in either input list are ignored. |
| 19 | """ |
| 20 | i = j = 0 |
| 21 | missing = [] |
| 22 | unexpected = [] |
| 23 | while True: |
| 24 | try: |
| 25 | e = expected[i] |
| 26 | a = actual[j] |
| 27 | if e < a: |
| 28 | missing.append(e) |
| 29 | i += 1 |
| 30 | while expected[i] == e: |
| 31 | i += 1 |
| 32 | elif e > a: |
| 33 | unexpected.append(a) |
| 34 | j += 1 |
| 35 | while actual[j] == a: |
| 36 | j += 1 |
| 37 | else: |
| 38 | i += 1 |
| 39 | try: |
| 40 | while expected[i] == e: |
| 41 | i += 1 |
| 42 | finally: |
| 43 | j += 1 |
| 44 | while actual[j] == a: |
| 45 | j += 1 |
| 46 | except IndexError: |
| 47 | missing.extend(expected[i:]) |
| 48 | unexpected.extend(actual[j:]) |
| 49 | break |
| 50 | return missing, unexpected |
| 51 | |
| 52 | |
| 53 | def unorderable_list_difference(expected, actual): |
| 54 | """Same behavior as sorted_list_difference but |
| 55 | for lists of unorderable items (like dicts). |
| 56 | |
| 57 | As it does a linear search per item (remove) it |
| 58 | has O(n*n) performance.""" |
| 59 | missing = [] |
| 60 | while expected: |
| 61 | item = expected.pop() |
| 62 | try: |
| 63 | actual.remove(item) |
| 64 | except ValueError: |
| 65 | missing.append(item) |
| 66 | |
| 67 | # anything left in actual is unexpected |
| 68 | return missing, actual |
| 69 | |
| 70 | def CmpToKey(mycmp): |
| 71 | 'Convert a cmp= function into a key= function' |
| 72 | class K(object): |
| 73 | def __init__(self, obj, *args): |
| 74 | self.obj = obj |
| 75 | def __lt__(self, other): |
| 76 | return mycmp(self.obj, other.obj) == -1 |
| 77 | return K |
| 78 | |
| 79 | def three_way_cmp(x, y): |
| 80 | """Return -1 if x < y, 0 if x == y and 1 if x > y""" |
| 81 | return (x > y) - (x < y) |