Transform an URI tree so that adjacent all-empty fields are coalesced
    into a single None at parent level.
    The return value can be used for validation.
    As a result, no distinction is made between empty and absent fields.
    It is believed that this limitation is harmless because this is the
    behavior of most implementations, and even useful in the context of
    this Python module because empty strings are already not distinguished
    from None when converting to boolean, so we are only generalizing this
    concept in order to keep code small and minimize special cases.

    If the distinction is ever really needed, for example to support empty
    anchor special HTTP script related URI in a clean way, one will
    probably need to completely rewrite (or at least review and modify)
    this module, and special care would be needed to distinguish between '',
    (), None, and others everywhere implicit boolean conversion is
    performed.  The behavior should then be checked in regards to its
    conformance with RFC 3986, especially (but this would probably not be
    sufficient) the classification switches of some URI parts according to
    the content of others.

def uri_tree_normalize(uri_tree):
    """
    Transform an URI tree so that adjacent all-empty fields are coalesced
    into a single None at parent level.
    The return value can be used for validation.
    As a result, no distinction is made between empty and absent fields.
    It is believed that this limitation is harmless because this is the
    behavior of most implementations, and even useful in the context of
    this Python module because empty strings are already not distinguished
    from None when converting to boolean, so we are only generalizing this
    concept in order to keep code small and minimize special cases.

    If the distinction is ever really needed, for example to support empty
    anchor special HTTP script related URI in a clean way, one will
    probably need to completely rewrite (or at least review and modify)
    this module, and special care would be needed to distinguish between '',
    (), None, and others everywhere implicit boolean conversion is
    performed.  The behavior should then be checked in regards to its
    conformance with RFC 3986, especially (but this would probably not be
    sufficient) the classification switches of some URI parts according to
    the content of others.
    """
    scheme, authority, path, query, fragment = uri_tree
    if authority and (filter(bool, authority) == ()):
        authority = None
    if query:
        query = filter(lambda (x, y): bool(x) or bool(y), query)
    return (scheme or None, authority or None, path or None,
            query or None, fragment or None)