#NEWLINE# def _filter_list( data , name_or_id , filters ) : #NEWLINE# #INDENT# "Filter a list by name/ID and arbitrary meta data.\n\n    :param list data:\n        The list of dictionary data to filter. It is expected that\n        each dictionary contains an 'id' and 'name'\n        key if a value for name_or_id is given.\n    :param string name_or_id:\n        The name or ID of the entity being filtered.\n    :param dict filters:\n        A dictionary of meta data to use for further filtering. Elements\n        of this dictionary may, themselves, be dictionaries. Example::\n\n            {\n              'last_name': 'Smith',\n              'other': {\n                  'gender': 'Female'\n              }\n            }\n    " #NEWLINE# if name_or_id : #NEWLINE# #INDENT# identifier_matches = [ ] #NEWLINE# for e in data : #NEWLINE# #INDENT# e_id = str ( e . get ( 'id' , None ) ) #NEWLINE# e_name = e . get ( 'name' , None ) #NEWLINE# if ( str ( name_or_id ) in ( e_id , e_name ) ) : #NEWLINE# #INDENT# identifier_matches . append ( e ) #NEWLINE# #UNINDENT# #UNINDENT# data = identifier_matches #NEWLINE# #UNINDENT# if ( not filters ) : #NEWLINE# #INDENT# return data #NEWLINE# #UNINDENT# def _dict_filter( f , d ) : #NEWLINE# #INDENT# if ( not d ) : #NEWLINE# #INDENT# return False #NEWLINE# #UNINDENT# for key in f . keys ( ) : #NEWLINE# #INDENT# if isinstance ( f [ key ] , dict ) : #NEWLINE# #INDENT# if ( not _dict_filter ( f [ key ] , d . get ( key , None ) ) ) : #NEWLINE# #INDENT# return False #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( d . get ( key , None ) != f [ key ] ) : #NEWLINE# #INDENT# return False #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# return True #NEWLINE# #UNINDENT# filtered = [ ] #NEWLINE# for e in data : #NEWLINE# #INDENT# filtered . append ( e ) #NEWLINE# for key in filters . keys ( ) : #NEWLINE# #INDENT# if isinstance ( filters [ key ] , dict ) : #NEWLINE# #INDENT# if ( not _dict_filter ( filters [ key ] , e . get ( key , None ) ) ) : #NEWLINE# #INDENT# filtered . pop ( ) #NEWLINE# break #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( e . get ( key , None ) != data [ key ] ) : #NEWLINE# #INDENT# filtered . pop ( ) #NEWLINE# break #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# return filtered