def subdocuments(fields_chain, resource, document):
 if (len(fields_chain) == 0):
  (yield document)
 elif (isinstance(document, dict) and (fields_chain[0] in document)):
  subdocument = document[fields_chain[0]]
  docs = (subdocument if isinstance(subdocument, list) else [subdocument])
  try:
   resource = field_definition(resource, fields_chain[0])['data_relation']['resource']
  except KeyError:
   resource = resource
  for doc in docs:
   for result in subdocuments(fields_chain[1:], resource, doc):
    (yield result)
 else:
  (yield document)

def subdocuments ( fields_chain , resource , document ) : if ( len ( fields_chain ) == 0 ) : ( yield document ) elif ( isinstance ( document , dict ) and ( fields_chain [ 0 ] in document ) ) : subdocument = document [ fields_chain [ 0 ] ] docs = ( subdocument if isinstance ( subdocument , list ) else [ subdocument ] ) try : resource = field_definition ( resource , fields_chain [ 0 ] ) [ 'data_relation' ] [ 'resource' ] except KeyError : resource = resource for doc in docs : for result in subdocuments ( fields_chain [ 1 : ] , resource , doc ) : ( yield result ) else : ( yield document )
traverses the given document and yields subdocuments which correspond to the given fields_chain .