mirror of
https://github.com/python/cpython.git
synced 2025-07-31 07:04:42 +00:00
Speed-up and simplify code urlparse's result objects.
This commit is contained in:
parent
0f973934f4
commit
0f6a656ec1
1 changed files with 6 additions and 52 deletions
|
@ -37,46 +37,11 @@ _parse_cache = {}
|
||||||
|
|
||||||
def clear_cache():
|
def clear_cache():
|
||||||
"""Clear the parse cache."""
|
"""Clear the parse cache."""
|
||||||
global _parse_cache
|
_parse_cache.clear()
|
||||||
_parse_cache = {}
|
|
||||||
|
|
||||||
|
|
||||||
class BaseResult(tuple):
|
class ResultMixin(object):
|
||||||
"""Base class for the parsed result objects.
|
"""Shared methods for the parsed result objects."""
|
||||||
|
|
||||||
This provides the attributes shared by the two derived result
|
|
||||||
objects as read-only properties. The derived classes are
|
|
||||||
responsible for checking the right number of arguments were
|
|
||||||
supplied to the constructor.
|
|
||||||
|
|
||||||
"""
|
|
||||||
|
|
||||||
__slots__ = ()
|
|
||||||
|
|
||||||
# Attributes that access the basic components of the URL:
|
|
||||||
|
|
||||||
@property
|
|
||||||
def scheme(self):
|
|
||||||
return self[0]
|
|
||||||
|
|
||||||
@property
|
|
||||||
def netloc(self):
|
|
||||||
return self[1]
|
|
||||||
|
|
||||||
@property
|
|
||||||
def path(self):
|
|
||||||
return self[2]
|
|
||||||
|
|
||||||
@property
|
|
||||||
def query(self):
|
|
||||||
return self[-2]
|
|
||||||
|
|
||||||
@property
|
|
||||||
def fragment(self):
|
|
||||||
return self[-1]
|
|
||||||
|
|
||||||
# Additional attributes that provide access to parsed-out portions
|
|
||||||
# of the netloc:
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def username(self):
|
def username(self):
|
||||||
|
@ -116,31 +81,20 @@ class BaseResult(tuple):
|
||||||
return int(port, 10)
|
return int(port, 10)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
from collections import namedtuple
|
||||||
|
|
||||||
class SplitResult(BaseResult):
|
class SplitResult(namedtuple('SplitResult', 'scheme netloc path query fragment'), ResultMixin):
|
||||||
|
|
||||||
__slots__ = ()
|
__slots__ = ()
|
||||||
|
|
||||||
def __new__(cls, scheme, netloc, path, query, fragment):
|
|
||||||
return BaseResult.__new__(
|
|
||||||
cls, (scheme, netloc, path, query, fragment))
|
|
||||||
|
|
||||||
def geturl(self):
|
def geturl(self):
|
||||||
return urlunsplit(self)
|
return urlunsplit(self)
|
||||||
|
|
||||||
|
|
||||||
class ParseResult(BaseResult):
|
class ParseResult(namedtuple('ParseResult', 'scheme netloc path params query fragment'), ResultMixin):
|
||||||
|
|
||||||
__slots__ = ()
|
__slots__ = ()
|
||||||
|
|
||||||
def __new__(cls, scheme, netloc, path, params, query, fragment):
|
|
||||||
return BaseResult.__new__(
|
|
||||||
cls, (scheme, netloc, path, params, query, fragment))
|
|
||||||
|
|
||||||
@property
|
|
||||||
def params(self):
|
|
||||||
return self[3]
|
|
||||||
|
|
||||||
def geturl(self):
|
def geturl(self):
|
||||||
return urlunparse(self)
|
return urlunparse(self)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue