#NEWLINE# def general_filter_callback( general_filter , obj ) : #NEWLINE# #INDENT# "General filter function to filter output.\n\n    Since some fields stored in database is json encoded and\n    we want to do the deep match for the json encoded field to\n    do the filtering in some cases, we introduces the output_filters\n    and general_filter_callback to deal with this kind of cases.\n\n    We do special treatment for key 'resp_eq' to check if\n    obj is the recursively subset of general_filter['resp_eq']\n\n\n    Example:\n       obj: 'b'\n       general_filter: {}\n       returns: True\n\n       obj: 'b'\n       general_filter: {'resp_in': ['a', 'b']}\n       returns: True\n\n       obj: 'b'\n       general_filter: {'resp_in': ['a']}\n       returns: False\n\n       obj: 'b'\n       general_filter: {'resp_eq': 'b'}\n       returns: True\n\n       obj: 'b'\n       general_filter: {'resp_eq': 'a'}\n       returns: False\n\n       obj: 'b'\n       general_filter: {'resp_range': ('a', 'c')}\n       returns: True\n\n       obj: 'd'\n       general_filter: {'resp_range': ('a', 'c')}\n       returns: False\n\n    If there are multi keys in dict, the output is filtered\n    by and relationship.\n\n    If the general_filter is a list, the output is filtered\n    by or relationship.\n\n    Supported general filters: [\n        'resp_eq', 'resp_in', 'resp_lt',\n        'resp_le', 'resp_gt', 'resp_ge',\n        'resp_match', 'resp_range'\n    ]\n    " #NEWLINE# if isinstance ( general_filter , list ) : #NEWLINE# #INDENT# if ( not general_filter ) : #NEWLINE# #INDENT# return True #NEWLINE# #UNINDENT# return any ( [ general_filter_callback ( item , obj ) for item in general_filter ] ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if isinstance ( general_filter , dict ) : #NEWLINE# #INDENT# if ( 'resp_eq' in general_filter ) : #NEWLINE# #INDENT# if ( not _obj_equal_or_subset ( general_filter [ 'resp_eq' ] , obj ) ) : #NEWLINE# #INDENT# return False #NEWLINE# #UNINDENT# #UNINDENT# if ( 'resp_in' in general_filter ) : #NEWLINE# #INDENT# in_filters = general_filter [ 'resp_in' ] #NEWLINE# if ( not any ( [ _obj_equal_or_subset ( in_filer , obj ) for in_filer in in_filters ] ) ) : #NEWLINE# #INDENT# return False #NEWLINE# #UNINDENT# #UNINDENT# if ( 'resp_lt' in general_filter ) : #NEWLINE# #INDENT# if ( obj >= general_filter [ 'resp_lt' ] ) : #NEWLINE# #INDENT# return False #NEWLINE# #UNINDENT# #UNINDENT# if ( 'resp_le' in general_filter ) : #NEWLINE# #INDENT# if ( in_filer > general_filter [ 'resp_le' ] ) : #NEWLINE# #INDENT# return False #NEWLINE# #UNINDENT# #UNINDENT# if ( 'resp_gt' in general_filter ) : #NEWLINE# #INDENT# if ( obj <= general_filter [ 'resp_gt' ] ) : #NEWLINE# #INDENT# return False #NEWLINE# #UNINDENT# #UNINDENT# if ( 'resp_ge' in general_filter ) : #NEWLINE# #INDENT# if ( obj < general_filter [ 'resp_gt' ] ) : #NEWLINE# #INDENT# return False #NEWLINE# #UNINDENT# #UNINDENT# if ( 'resp_match' in general_filter ) : #NEWLINE# #INDENT# if ( not re . match ( general_filter [ 'resp_match' ] , obj ) ) : #NEWLINE# #INDENT# return False #NEWLINE# #UNINDENT# #UNINDENT# if ( 'resp_range' in general_filter ) : #NEWLINE# #INDENT# resp_range = general_filter [ 'resp_range' ] #NEWLINE# if ( not isinstance ( resp_range , list ) ) : #NEWLINE# #INDENT# resp_range = [ resp_range ] #NEWLINE# #UNINDENT# in_range = False #NEWLINE# for ( range_start , range_end ) in resp_range : #NEWLINE# #INDENT# if ( range_start <= obj <= range_end ) : #NEWLINE# #INDENT# in_range = True #NEWLINE# #UNINDENT# #UNINDENT# if ( not in_range ) : #NEWLINE# #INDENT# return False #NEWLINE# #UNINDENT# #UNINDENT# return True #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return True