summaryrefslogtreecommitdiffstats
path: root/Lib/urlparse.py
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2008-01-11 18:04:55 (GMT)
committerRaymond Hettinger <python@rcn.com>2008-01-11 18:04:55 (GMT)
commit0f6a656ec1145d52d0226ca4ce28fe32c44529bb (patch)
treeb57976248f2fc16ceaee3dc0fbeff2a209703e9f /Lib/urlparse.py
parent0f973934f4b294d3245a63b3eff0b80d69930c88 (diff)
downloadcpython-0f6a656ec1145d52d0226ca4ce28fe32c44529bb.zip
cpython-0f6a656ec1145d52d0226ca4ce28fe32c44529bb.tar.gz
cpython-0f6a656ec1145d52d0226ca4ce28fe32c44529bb.tar.bz2
Speed-up and simplify code urlparse's result objects.
Diffstat (limited to 'Lib/urlparse.py')
-rw-r--r--Lib/urlparse.py58
1 files changed, 6 insertions, 52 deletions
diff --git a/Lib/urlparse.py b/Lib/urlparse.py
index 7a2e6ce..ada6142 100644
--- a/Lib/urlparse.py
+++ b/Lib/urlparse.py
@@ -37,46 +37,11 @@ _parse_cache = {}
def clear_cache():
"""Clear the parse cache."""
- global _parse_cache
- _parse_cache = {}
+ _parse_cache.clear()
-class BaseResult(tuple):
- """Base class 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:
+class ResultMixin(object):
+ """Shared methods for the parsed result objects."""
@property
def username(self):
@@ -116,31 +81,20 @@ class BaseResult(tuple):
return int(port, 10)
return None
+from collections import namedtuple
-class SplitResult(BaseResult):
+class SplitResult(namedtuple('SplitResult', 'scheme netloc path query fragment'), ResultMixin):
__slots__ = ()
- def __new__(cls, scheme, netloc, path, query, fragment):
- return BaseResult.__new__(
- cls, (scheme, netloc, path, query, fragment))
-
def geturl(self):
return urlunsplit(self)
-class ParseResult(BaseResult):
+class ParseResult(namedtuple('ParseResult', 'scheme netloc path params query fragment'), ResultMixin):
__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):
return urlunparse(self)