<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:

def SPF(domain, record='SPF', nameserver=None):
    '''
    Return the allowed IPv4 ranges in the SPF record for ``domain``.

    If record is ``SPF`` and the SPF record is empty, the TXT record will be
    searched automatically. If you know the domain uses TXT and not SPF,
    specifying that will save a lookup.

    CLI Example:

    .. code-block:: bash

        salt ns1 dig.SPF google.com
    '''
    spf_re = re.compile(r'(?:\+|~)?(ip[46]|include):(.+)')
    cmd = ['dig', '+short', six.text_type(domain), record]

    if nameserver is not None:
        cmd.append('@{0}'.format(nameserver))

    result = __salt__['cmd.run_all'](cmd, python_shell=False)
    # In this case, 0 is not the same as False
    if result['retcode'] != 0:
        log.warning(
            'dig returned exit code \'%s\'. Returning empty list as fallback.',
            result['retcode']
        )
        return []

    if result['stdout'] == '' and record == 'SPF':
        # empty string is successful query, but nothing to return. So, try TXT
        # record.
        return SPF(domain, 'TXT', nameserver)

    sections = re.sub('"', '', result['stdout']).split()
    if not sections or sections[0] != 'v=spf1':
        return []

    if sections[1].startswith('redirect='):
        # Run a lookup on the part after 'redirect=' (9 chars)
        return SPF(sections[1][9:], 'SPF', nameserver)
    ret = []
    for section in sections[1:]:
        try:
            mechanism, address = spf_re.match(section).groups()
        except AttributeError:
            # Regex was not matched
            continue
        if mechanism == 'include':
            ret.extend(SPF(address, 'SPF', nameserver))
        elif mechanism in ('ip4', 'ip6') and check_ip(address):
            ret.append(address)
    return ret