[Patches] Constants to use urlparse results
gerrit@nl.linux.org
gerrit@nl.linux.org
Tue, 7 Mar 2000 20:53:36 +0100
--SLDf9lqlvOQaIe6s
Content-Type: text/plain; charset=us-ascii
Hello,
this patch adds some constants to the urlparse module, to improve
readability. First you would write:
>>> urlparse.urlparse(url)[4]
Now, you can write:
>>> urlparse.urlparse(url)[url.QUERY]
In my opinion, this improves readability, as the stat modules improves
readability in the results of os.stat (and so does statvfs).
I've also changed the...(bah, how do I say this in English)... usage of
the string module to usage of string methods.
------------------------------------------------------------------
| I confirm that, to the best of my knowledge and belief, this |
| contribution is free of any claims of third parties under |
| copyright, patent or other rights or interests ("claims"). To |
| the extent that I have any such claims, I hereby grant to CNRI a |
| nonexclusive, irrevocable, royalty-free, worldwide license to |
| reproduce, distribute, perform and/or display publicly, prepare |
| derivative versions, and otherwise use this contribution as part |
| of the Python software and its related documentation, or any |
| derivative versions thereof, at no cost to CNRI or its licensed |
| users, and to authorize others to do so. |
| |
| I acknowledge that CNRI may, at its sole discretion, decide |
| whether or not to incorporate this contribution in the Python |
| software and its related documentation. I further grant CNRI |
| permission to use my name and other identifying information |
| provided to CNRI by me for use in connection with the Python |
| software and its related documentation. |
------------------------------------------------------------------
regards,
Gerrit.
--
Plies korekt enie bet ingglisj joe encauntur in mai imil mesusj!
--
Comparison Python GUI's: http://www.nl.linux.org/~gerrit/gui.html
Please comment!
--SLDf9lqlvOQaIe6s
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="urlparse.py.diff"
--- /tmp/urlparse.py Tue Mar 7 20:25:57 2000
+++ urlparse.py Tue Mar 7 20:35:02 2000
@@ -6,7 +6,6 @@
# Standard/builtin Python modules
import string
-from string import joinfields, splitfields, rfind
# A classification of schemes ('' means apply by default)
uses_relative = ['ftp', 'http', 'gopher', 'nntp', 'wais', 'file',
@@ -41,6 +40,12 @@
global _parse_cache
_parse_cache = {}
+SCHEME = 0
+NETLOC = 1
+PATH = 2
+PARAMS = 3
+QUERY = 4
+FRAGMENT = 5
def urlparse(url, scheme = '', allow_fragments = 1):
"""Parse a URL into 6 components:
@@ -54,29 +59,28 @@
return cached
if len(_parse_cache) >= MAX_CACHE_SIZE: # avoid runaway growth
clear_cache()
- find = string.find
netloc = path = params = query = fragment = ''
- i = find(url, ':')
+ i = url.find(':')
if i > 0:
if url[:i] == 'http': # optimize the common case
- scheme = string.lower(url[:i])
+ scheme = url[:i].lower()
url = url[i+1:]
if url[:2] == '//':
- i = find(url, '/', 2)
+ i = url.find('/', 2)
if i < 0:
i = len(url)
netloc = url[2:i]
url = url[i:]
if allow_fragments:
- i = string.rfind(url, '#')
+ i = url.rfind('#')
if i >= 0:
fragment = url[i+1:]
url = url[:i]
- i = find(url, '?')
+ i = url.find('?')
if i >= 0:
query = url[i+1:]
url = url[:i]
- i = find(url, ';')
+ i = url.find(';')
if i >= 0:
params = url[i+1:]
url = url[:i]
@@ -87,23 +91,23 @@
if c not in scheme_chars:
break
else:
- scheme, url = string.lower(url[:i]), url[i+1:]
+ scheme, url = url[:i].lower(), url[i+1:]
if scheme in uses_netloc:
if url[:2] == '//':
- i = find(url, '/', 2)
+ i = url.find('/', 2)
if i < 0:
i = len(url)
netloc, url = url[2:i], url[i:]
if allow_fragments and scheme in uses_fragment:
- i = string.rfind(url, '#')
+ i = url.rfind('#')
if i >= 0:
url, fragment = url[:i], url[i+1:]
if scheme in uses_query:
- i = find(url, '?')
+ i = url.find('?')
if i >= 0:
url, query = url[:i], url[i+1:]
if scheme in uses_params:
- i = find(url, ';')
+ i = url.find(';')
if i >= 0:
url, params = url[:i], url[i+1:]
tuple = scheme, netloc, url, params, query, fragment
@@ -151,10 +155,10 @@
if not path:
return urlunparse((scheme, netloc, bpath,
params, query or bquery, fragment))
- i = rfind(bpath, '/')
+ i = bpath.rfind('/')
if i >= 0:
path = bpath[:i] + '/' + path
- segments = splitfields(path, '/')
+ segments = path.split('/')
if segments[-1] == '.':
segments[-1] = ''
while '.' in segments:
@@ -173,7 +177,7 @@
segments[-1] = ''
elif len(segments) >= 2 and segments[-1] == '..':
segments[-2:] = ['']
- return urlunparse((scheme, netloc, joinfields(segments, '/'),
+ return urlunparse((scheme, netloc, '/'.join(segments),
params, query, fragment))
def urldefrag(url):
@@ -238,7 +242,7 @@
while 1:
line = fp.readline()
if not line: break
- words = string.split(line)
+ words = line.split()
if not words:
continue
url = words[0]
--SLDf9lqlvOQaIe6s--