[issue15824] mutable urlparse return type

Matthew Hall report at bugs.python.org
Wed Nov 5 22:57:40 CET 2014


Matthew Hall added the comment:

I don't think having to call a method with a weird secret underscored name to update a value in a URL named tuple is very elegant. Neither is creating a handful of pointless objects to make one simple validator function like the one I had to code today. I would urge some reconsideration of this, like a way to get back a named yet mutable object when needed, instead of trying to force everybody to do this one way which isn't always that great.

def validate_url(url):
    parts = urlparse.urlparse(url.strip())
    # scheme, netloc, path, params, query, fragment
    # XXX: preserve backward compatibility w/ old code
    if not parts.scheme:
        parts = parts._replace(scheme='http', netloc=parts.path.strip('/'), path='')

    # remove params, query, and fragment
    # params is nearly never used anywhere
    # (NOTE: it does NOT mean the stuff after '?')
    # it actually means this http://domain/page.py;param1=foo?query1=bar
    # query and fragment are used but aren't helpful for our application
    parts = parts._replace(params='', query='', fragment='')

    if parts.scheme not in URI_SCHEMES:
        raise ValueError('scheme=%s is not valid' % parts.scheme)
    if '.' not in parts.netloc:
        raise ValueError('location=%s does not contain a domain' % parts.netloc)

    if len(parts.path) and not parts.path.startswith('/'):
        raise ValueError('path=%s appears invalid' % parts.path)
    elif not parts.path:
        parts=parts._replace(path='/')

    validated_url = parts.geturl()
    return validated_url, parts

----------
nosy: +mhcptg

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue15824>
_______________________________________


More information about the Python-bugs-list mailing list