def ExtractFormFields(soup):
  """Turn a BeautifulSoup form in to a dict of fields and default values"""
  fields = []
  for field in soup.find_all(kFormFieldsRe):
    if field.name == 'input':
      # ignore submit/image with no name attribute
      if field['type'] in ('submit', 'image') and not field.has_attr('name'):
        continue

      # single element nome/value fields
      if field['type'] in ('text', 'hidden', 'password', 'submit', 'image'):
        value = ''
        if field.has_attr('value'):
          value = field['value']
        fields.append((field['name'], value))
        continue

      # checkboxes and radios
      if field['type'] in ('checkbox', 'radio'):
        value = ''
        if field.has_attr('checked'):
          if field.has_attr('value'):
            value = field['value']
          else:
            value = 'on'
        if value:
          fields.append((field['name'], value))
        continue

      assert False, 'input type %s not supported' % field['type']
    elif field.name == 'textarea':
      fields.append((field['name'], field.string or ''))
    elif field.name == 'select':
      value = ''
      options = field.find_all('option')
      is_multiple = field.has_attr('multiple')
      selected_options = [
          option for option in options
          if option.has_attr('selected')
          ]

      # If no select options, go with the first one
      if not selected_options and options:
        selected_options = [options[0]]

      if not is_multiple:
        assert(len(selected_options) < 2)
        if len(selected_options) == 1:
          value = selected_options[0]['value']
      else:
        value = [option['value'] for option in selected_options]

      if value:
        fields.append((field['name'], value))

  return fields
dataset/ETHPy150Open viewfinderco/viewfinder/clients/ios/scripts/app-tool.py/ExtractFormFields/original