Back to index

python3.2  3.2.2
Functions | Variables
unittest.util Namespace Reference

Functions

def safe_repr
def strclass
def sorted_list_difference
def unorderable_list_difference
def three_way_cmp
def _count_diff_all_purpose
def _ordered_count
def _count_diff_hashable

Variables

 __unittest = True
int _MAX_LENGTH = 80
tuple _Mismatch = namedtuple('Mismatch', 'actual expected value')

Detailed Description

Various utility functions.

Function Documentation

def unittest.util._count_diff_all_purpose (   actual,
  expected 
) [private]

Definition at line 84 of file util.py.

00084 
00085 def _count_diff_all_purpose(actual, expected):
00086     'Returns list of (cnt_act, cnt_exp, elem) triples where the counts differ'
00087     # elements need not be hashable
00088     s, t = list(actual), list(expected)
00089     m, n = len(s), len(t)
00090     NULL = object()
00091     result = []
00092     for i, elem in enumerate(s):
00093         if elem is NULL:
00094             continue
00095         cnt_s = cnt_t = 0
00096         for j in range(i, m):
00097             if s[j] == elem:
00098                 cnt_s += 1
00099                 s[j] = NULL
00100         for j, other_elem in enumerate(t):
00101             if other_elem == elem:
00102                 cnt_t += 1
00103                 t[j] = NULL
00104         if cnt_s != cnt_t:
00105             diff = _Mismatch(cnt_s, cnt_t, elem)
00106             result.append(diff)
00107 
00108     for i, elem in enumerate(t):
00109         if elem is NULL:
00110             continue
00111         cnt_t = 0
00112         for j in range(i, n):
00113             if t[j] == elem:
00114                 cnt_t += 1
00115                 t[j] = NULL
00116         diff = _Mismatch(0, cnt_t, elem)
00117         result.append(diff)
00118     return result

Here is the call graph for this function:

Here is the caller graph for this function:

def unittest.util._count_diff_hashable (   actual,
  expected 
) [private]

Definition at line 126 of file util.py.

00126 
00127 def _count_diff_hashable(actual, expected):
00128     'Returns list of (cnt_act, cnt_exp, elem) triples where the counts differ'
00129     # elements must be hashable
00130     s, t = _ordered_count(actual), _ordered_count(expected)
00131     result = []
00132     for elem, cnt_s in s.items():
00133         cnt_t = t.get(elem, 0)
00134         if cnt_s != cnt_t:
00135             diff = _Mismatch(cnt_s, cnt_t, elem)
00136             result.append(diff)
00137     for elem, cnt_t in t.items():
00138         if elem not in s:
00139             diff = _Mismatch(0, cnt_t, elem)
00140             result.append(diff)
00141     return result

Here is the call graph for this function:

Here is the caller graph for this function:

def unittest.util._ordered_count (   iterable) [private]

Definition at line 119 of file util.py.

00119 
00120 def _ordered_count(iterable):
00121     'Return dict of element counts, in the order they were first seen'
00122     c = OrderedDict()
00123     for elem in iterable:
00124         c[elem] = c.get(elem, 0) + 1
00125     return c

Here is the caller graph for this function:

def unittest.util.safe_repr (   obj,
  short = False 
)

Definition at line 8 of file util.py.

00008 
00009 def safe_repr(obj, short=False):
00010     try:
00011         result = repr(obj)
00012     except Exception:
00013         result = object.__repr__(obj)
00014     if not short or len(result) < _MAX_LENGTH:
00015         return result
00016     return result[:_MAX_LENGTH] + ' [truncated]...'

Here is the caller graph for this function:

def unittest.util.sorted_list_difference (   expected,
  actual 
)
Finds elements in only one or the other of two, sorted input lists.

Returns a two-element tuple of lists.    The first list contains those
elements in the "expected" list but not in the "actual" list, and the
second contains those elements in the "actual" list but not in the
"expected" list.    Duplicate elements in either input list are ignored.

Definition at line 20 of file util.py.

00020 
00021 def sorted_list_difference(expected, actual):
00022     """Finds elements in only one or the other of two, sorted input lists.
00023 
00024     Returns a two-element tuple of lists.    The first list contains those
00025     elements in the "expected" list but not in the "actual" list, and the
00026     second contains those elements in the "actual" list but not in the
00027     "expected" list.    Duplicate elements in either input list are ignored.
00028     """
00029     i = j = 0
00030     missing = []
00031     unexpected = []
00032     while True:
00033         try:
00034             e = expected[i]
00035             a = actual[j]
00036             if e < a:
00037                 missing.append(e)
00038                 i += 1
00039                 while expected[i] == e:
00040                     i += 1
00041             elif e > a:
00042                 unexpected.append(a)
00043                 j += 1
00044                 while actual[j] == a:
00045                     j += 1
00046             else:
00047                 i += 1
00048                 try:
00049                     while expected[i] == e:
00050                         i += 1
00051                 finally:
00052                     j += 1
00053                     while actual[j] == a:
00054                         j += 1
00055         except IndexError:
00056             missing.extend(expected[i:])
00057             unexpected.extend(actual[j:])
00058             break
00059     return missing, unexpected
00060 

Here is the caller graph for this function:

def unittest.util.strclass (   cls)

Definition at line 17 of file util.py.

00017 
00018 def strclass(cls):
00019     return "%s.%s" % (cls.__module__, cls.__name__)

Here is the caller graph for this function:

def unittest.util.three_way_cmp (   x,
  y 
)
Return -1 if x < y, 0 if x == y and 1 if x > y

Definition at line 78 of file util.py.

00078 
00079 def three_way_cmp(x, y):
00080     """Return -1 if x < y, 0 if x == y and 1 if x > y"""
00081     return (x > y) - (x < y)

def unittest.util.unorderable_list_difference (   expected,
  actual 
)
Same behavior as sorted_list_difference but
for lists of unorderable items (like dicts).

As it does a linear search per item (remove) it
has O(n*n) performance.

Definition at line 61 of file util.py.

00061 
00062 def unorderable_list_difference(expected, actual):
00063     """Same behavior as sorted_list_difference but
00064     for lists of unorderable items (like dicts).
00065 
00066     As it does a linear search per item (remove) it
00067     has O(n*n) performance."""
00068     missing = []
00069     while expected:
00070         item = expected.pop()
00071         try:
00072             actual.remove(item)
00073         except ValueError:
00074             missing.append(item)
00075 
00076     # anything left in actual is unexpected
00077     return missing, actual

Here is the caller graph for this function:


Variable Documentation

Definition at line 5 of file util.py.

Definition at line 7 of file util.py.

tuple unittest.util._Mismatch = namedtuple('Mismatch', 'actual expected value')

Definition at line 82 of file util.py.