<s> from __future__ import absolute_import <EOL> import random <EOL> import string <EOL> import itertools <EOL> from funcfinder . utils import * <EOL> def group_by_key_func ( func ) : <EOL> """<STR_LIT>""" <EOL> assertEqual ( func ( "<STR_LIT>" . split ( ) , len ) , <EOL> { <NUM_LIT:1> : [ "<STR_LIT:a>" , "<STR_LIT:d>" ] , <EOL> <NUM_LIT:2> : [ "<STR_LIT>" , "<STR_LIT>" ] , <EOL> <NUM_LIT:3> : [ "<STR_LIT>" , "<STR_LIT>" ] } ) <EOL> assertEqual ( func ( [ - <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:6> , <NUM_LIT:8> , <NUM_LIT:9> , <NUM_LIT:2> ] , lambda x : x % <NUM_LIT:2> ) , <EOL> { <NUM_LIT:0> : [ <NUM_LIT:0> , <NUM_LIT:6> , <NUM_LIT:8> , <NUM_LIT:2> ] , <EOL> <NUM_LIT:1> : [ - <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:9> ] } ) <EOL> def copy_dict ( func ) : <EOL> """<STR_LIT>""" <EOL> original = { '<STR_LIT:a>' : <NUM_LIT:1> , '<STR_LIT:d>' : <NUM_LIT:2> , '<STR_LIT:b>' : <NUM_LIT:3> , '<STR_LIT:c>' : <NUM_LIT:4> } <EOL> copy = func ( original ) <EOL> assertEqual ( original , copy ) <EOL> assertIsNot ( original , copy ) <EOL> assertEqual ( copy [ '<STR_LIT:d>' ] , <NUM_LIT:2> ) <EOL> del copy [ '<STR_LIT:d>' ] <EOL> assertIsNone ( copy . get ( '<STR_LIT:d>' ) ) <EOL> assertEqual ( original [ '<STR_LIT:d>' ] , <NUM_LIT:2> ) <EOL> copy [ '<STR_LIT:x>' ] = <NUM_LIT:5> <EOL> assertEqual ( copy [ '<STR_LIT:x>' ] , <NUM_LIT:5> ) <EOL> assertIsNone ( original . get ( '<STR_LIT:x>' ) ) <EOL> def sort_dict_by_key ( func ) : <EOL> """<STR_LIT>""" <EOL> copy_dict ( func ) <EOL> original_dict = { '<STR_LIT:a>' : <NUM_LIT:0> , '<STR_LIT:s>' : <NUM_LIT:1> , '<STR_LIT:d>' : <NUM_LIT:2> , '<STR_LIT:f>' : <NUM_LIT:3> } <EOL> sorted_dict = func ( original_dict ) <EOL> assertEqual ( sorted_dict . items ( ) , [ ( '<STR_LIT:a>' , <NUM_LIT:0> ) , ( '<STR_LIT:d>' , <NUM_LIT:2> ) , ( '<STR_LIT:f>' , <NUM_LIT:3> ) , ( '<STR_LIT:s>' , <NUM_LIT:1> ) ] ) <EOL> sorted_keys = list ( itertools . product ( string . ascii_lowercase , string . ascii_lowercase ) ) <EOL> shuffled_keys = list ( sorted_keys ) <EOL> for i in xrange ( <NUM_LIT:10> ) : <EOL> random . shuffle ( shuffled_keys ) <EOL> original_dict = dict ( itertools . izip ( shuffled_keys , itertools . count ( ) ) ) <EOL> sorted_dict = func ( original_dict ) <EOL> assertEqualIters ( sorted_keys , sorted_dict . iterkeys ( ) ) <EOL> def always_sorted_dict_by_key ( func ) : <EOL> """<STR_LIT>""" <EOL> sort_dict_by_key ( func ) <EOL> sorted_dict = func ( { } ) <EOL> keys = list ( itertools . product ( string . ascii_lowercase , string . ascii_lowercase ) ) <EOL> random . shuffle ( keys ) <EOL> for key in keys : <EOL> sorted_dict [ key ] = key <EOL> assertEqualIters ( sorted ( sorted_dict . keys ( ) ) , sorted_dict . iterkeys ( ) ) <EOL> def sort_dict_by_value ( func ) : <EOL> """<STR_LIT>""" <EOL> copy_dict ( func ) <EOL> original_dict = dict ( zip ( range ( <NUM_LIT:4> ) , "<STR_LIT>" ) ) <EOL> sorted_dict = func ( original_dict ) <EOL> assertEqual ( sorted_dict . items ( ) , [ ( <NUM_LIT:3> , '<STR_LIT:a>' ) , ( <NUM_LIT:2> , '<STR_LIT:b>' ) , ( <NUM_LIT:1> , '<STR_LIT:c>' ) , ( <NUM_LIT:0> , '<STR_LIT:d>' ) ] ) <EOL> sorted_values = list ( itertools . product ( string . ascii_lowercase , string . ascii_lowercase ) ) <EOL> shuffled_values = list ( sorted_values ) <EOL> for i in xrange ( <NUM_LIT:10> ) : <EOL> random . shuffle ( shuffled_values ) <EOL> original_dict = dict ( itertools . izip ( itertools . count ( ) , shuffled_values ) ) <EOL> sorted_dict = func ( original_dict ) <EOL> assertEqualIters ( sorted_values , sorted_dict . itervalues ( ) ) </s>
