#NEWLINE# def _count_interaction( user , interaction = None , direction = 'out' ) : #NEWLINE# #INDENT# if ( interaction is 'call_duration' ) : #NEWLINE# #INDENT# d = defaultdict ( int ) #NEWLINE# for r in user . records : #NEWLINE# #INDENT# if ( ( r . direction == direction ) and ( r . interaction == 'call' ) ) : #NEWLINE# #INDENT# d [ r . correspondent_id ] += r . call_duration #NEWLINE# #UNINDENT# #UNINDENT# return d #NEWLINE# #UNINDENT# if ( interaction is None ) : #NEWLINE# #INDENT# keyfn = ( lambda x : x . correspondent_id ) #NEWLINE# records = ( r for r in user . records if ( r . direction == direction ) ) #NEWLINE# chunks = groupby ( sorted ( records , key = keyfn ) , key = keyfn ) #NEWLINE# return Counter ( { c_id : len ( set ( ( _round_half_hour ( i ) for i in items ) ) ) for ( c_id , items ) in chunks } ) #NEWLINE# #UNINDENT# if ( interaction in [ 'call' , 'text' ] ) : #NEWLINE# #INDENT# filtered = [ x . correspondent_id for x in user . records if ( ( x . interaction == interaction ) and ( x . direction == direction ) ) ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise ValueError ( "{} is not a correct value of interaction, only 'call', 'text', and 'call_duration' are accepted" . format ( interaction ) ) #NEWLINE# #UNINDENT# return Counter ( filtered )