code: def py2dict(elements):
      
      metadata_dict = {}
      # Loop through all elements in the Python object.
      for element in elements.children:
          # Start an empty element list if an entry for the element
          # list hasn't been made in the dictionary.
          if element.tag not in metadata_dict:
              metadata_dict[element.tag] = []
          element_dict = {}
          if hasattr(element, 'qualifier') and element.qualifier is not None:
              element_dict['qualifier'] = element.qualifier
          # Set the element's content as a dictionary
          # of children elements.
          if len(element.children) > 0:
              child_dict = {}
              for child in element.children:
                  if child.content is not None:
                      child_dict[child.tag] = child.content
              element_dict['content'] = child_dict
          # Set element content that is not children.
          elif element.content is not None:
              if element.content.strip() != '':
                  element_dict['content'] = element.content
          # Append the dictionary to the element list
          # if the element has content or children.
          if element_dict.get('content', False):
              metadata_dict[element.tag].append(element_dict)
  
      return metadata_dict
docs: Convert a Python object into a Python dictionary.