<s> """<STR_LIT>""" <EOL> from collections import namedtuple , OrderedDict <EOL> __unittest = True <EOL> _MAX_LENGTH = <NUM_LIT> <EOL> def safe_repr ( obj , short = False ) : <EOL> try : <EOL> result = repr ( obj ) <EOL> except Exception : <EOL> result = object . __repr__ ( obj ) <EOL> if not short or len ( result ) < _MAX_LENGTH : <EOL> return result <EOL> return result [ : _MAX_LENGTH ] + '<STR_LIT>' <EOL> def strclass ( cls ) : <EOL> return "<STR_LIT>" % ( cls . __module__ , cls . __name__ ) <EOL> def sorted_list_difference ( expected , actual ) : <EOL> """<STR_LIT>""" <EOL> i = j = <NUM_LIT:0> <EOL> missing = [ ] <EOL> unexpected = [ ] <EOL> while True : <EOL> try : <EOL> e = expected [ i ] <EOL> a = actual [ j ] <EOL> if e < a : <EOL> missing . append ( e ) <EOL> i += <NUM_LIT:1> <EOL> while expected [ i ] == e : <EOL> i += <NUM_LIT:1> <EOL> elif e > a : <EOL> unexpected . append ( a ) <EOL> j += <NUM_LIT:1> <EOL> while actual [ j ] == a : <EOL> j += <NUM_LIT:1> <EOL> else : <EOL> i += <NUM_LIT:1> <EOL> try : <EOL> while expected [ i ] == e : <EOL> i += <NUM_LIT:1> <EOL> finally : <EOL> j += <NUM_LIT:1> <EOL> while actual [ j ] == a : <EOL> j += <NUM_LIT:1> <EOL> except IndexError : <EOL> missing . extend ( expected [ i : ] ) <EOL> unexpected . extend ( actual [ j : ] ) <EOL> break <EOL> return missing , unexpected <EOL> def unorderable_list_difference ( expected , actual , ignore_duplicate = False ) : <EOL> """<STR_LIT>""" <EOL> missing = [ ] <EOL> unexpected = [ ] <EOL> while expected : <EOL> item = expected . pop ( ) <EOL> try : <EOL> actual . remove ( item ) <EOL> except ValueError : <EOL> missing . append ( item ) <EOL> if ignore_duplicate : <EOL> for lst in expected , actual : <EOL> try : <EOL> while True : <EOL> lst . remove ( item ) <EOL> except ValueError : <EOL> pass <EOL> if ignore_duplicate : <EOL> while actual : <EOL> item = actual . pop ( ) <EOL> unexpected . append ( item ) <EOL> try : <EOL> while True : <EOL> actual . remove ( item ) <EOL> except ValueError : <EOL> pass <EOL> return missing , unexpected <EOL> return missing , actual <EOL> _Mismatch = namedtuple ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def _count_diff_all_purpose ( actual , expected ) : <EOL> '<STR_LIT>' <EOL> s , t = list ( actual ) , list ( expected ) <EOL> m , n = len ( s ) , len ( t ) <EOL> NULL = object ( ) <EOL> result = [ ] <EOL> for i , elem in enumerate ( s ) : <EOL> if elem is NULL : <EOL> continue <EOL> cnt_s = cnt_t = <NUM_LIT:0> <EOL> for j in range ( i , m ) : <EOL> if s [ j ] == elem : <EOL> cnt_s += <NUM_LIT:1> <EOL> s [ j ] = NULL <EOL> for j , other_elem in enumerate ( t ) : <EOL> if other_elem == elem : <EOL> cnt_t += <NUM_LIT:1> <EOL> t [ j ] = NULL <EOL> if cnt_s != cnt_t : <EOL> diff = _Mismatch ( cnt_s , cnt_t , elem ) <EOL> result . append ( diff ) <EOL> for i , elem in enumerate ( t ) : <EOL> if elem is NULL : <EOL> continue <EOL> cnt_t = <NUM_LIT:0> <EOL> for j in range ( i , n ) : <EOL> if t [ j ] == elem : <EOL> cnt_t += <NUM_LIT:1> <EOL> t [ j ] = NULL <EOL> diff = _Mismatch ( <NUM_LIT:0> , cnt_t , elem ) <EOL> result . append ( diff ) <EOL> return result <EOL> def _ordered_count ( iterable ) : <EOL> '<STR_LIT>' <EOL> c = OrderedDict ( ) <EOL> for elem in iterable : <EOL> c [ elem ] = c . get ( elem , <NUM_LIT:0> ) + <NUM_LIT:1> <EOL> return c <EOL> def _count_diff_hashable ( actual , expected ) : <EOL> '<STR_LIT>' <EOL> s , t = _ordered_count ( actual ) , _ordered_count ( expected ) <EOL> result = [ ] <EOL> for elem , cnt_s in s . items ( ) : <EOL> cnt_t = t . get ( elem , <NUM_LIT:0> ) <EOL> if cnt_s != cnt_t : <EOL> diff = _Mismatch ( cnt_s , cnt_t , elem ) <EOL> result . append ( diff ) <EOL> for elem , cnt_t in t . items ( ) : <EOL> if elem not in s : <EOL> diff = _Mismatch ( <NUM_LIT:0> , cnt_t , elem ) <EOL> result . append ( diff ) <EOL> return result </s>
