Newbie: Is there a more pythonic approach?

Lloyd Sommerer lsommerer at sewardweb.com
Thu Mar 15 20:50:53 CET 2001


Thank you very much for the pointers, They were most helpful, except for the
one that didn't make sense to me.  I'm having trouble following how the change
below actually works:

Fredrik Lundh wrote:

> > if os.path.exists(base_directory+lowercase_url):
> >     new_url = base_url+lowercase_url
>
> if you like source code optimizations, you could prepend an empty
> extension to the extensions list and get rid of the if clause:
>
>       for extension in [''] + extension_list:
>
> > else:
> >     for extension in extension_list:
> >         if os.path.exists(base_directory+url_path+extension):
> >             new_url = base_url+url_path+extension
>

What I'm having trouble seeing is how this checks for the URL having the
correct extension, but the wrong case. The original script and the script as
it currently exists follow, in case refering to it proves helpful to anyone.

Lloyd Sommerer
Original Version:
==================================================
error_page = '/404.html'
base_url = 'http://churchsite.gatheringspot.com'
base_directory = '/home/gatheri/public_html/churchsite/'
extension_list = ['.html','.shtml','.htm','.txt','.php','.asp']

import string
import os

lowercase_url = string.lower(os.environ['REDIRECT_URL'])
url_path,url_extension = os.path.splitext(lowercase_url)
new_url = base_url+error_page
#
# Check to see if simply converting the URL to lowercase
# corrects the problem. If it doesn't, check to see if the
# extension is incorrect and fix it if it is.
#
if os.path.exists(base_directory+lowercase_url):
    new_url = base_url+lowercase_url
else:
    for extension in extension_list:
        if os.path.exists(base_directory+url_path+extension):
            new_url = base_url+url_path+extension
#
# Send HTTP header information to the browser so that it
# can request the "correct" document.
#
print "Status: 301 Permanently moved"
print "Location: " + new_url
print

Current Version:
==================================================
error_page = '404.html'
base_url = 'http://www.gatheringspot.com'
base_directory = '/home/gatheri/public_html'
extension_list = ['.html','.shtml','.htm','.txt','.php','.asp']

from string import lower
from os.path import exists, join, splitext
from urlparse import urljoin
from os import environ

lowercase_url = string.lower(environ['REDIRECT_URL'])
lowercase_url = lowercase_url[1:] # removes leading '/'
new_url = urlparse.urljoin(base_url,error_page)
#
# Check to see if simply converting the URL to lowercase
# corrects the problem. If it doesn't, check to see if the
# extension is incorrect and fix it if it is.
#
if exists(join(base_directory,lowercase_url)):
    new_url = urljoin(base_url,lowercase_url)
else:
    url_path,url_extension = splitext(lowercase_url)
    for extension in extension_list:
        if exists(join(base_directory,(url_path+extension))):
            new_url = urljoin(base_url,(url_path+extension))
            break # done when we find the first match.
#
# Send HTTP header information to the browser so that it
# can request the "correct" document.
#
print "Status: 301 Permanently moved"
print "Location: ", new_url
print




More information about the Python-list mailing list