[Tutor] Automated Breadcrumb -- help for a PHP user
Daniel Ehrenberg
littledanehren at yahoo.com
Wed Nov 26 17:14:40 EST 2003
Chris Heisel wrote:
> def nameHandler(name):
> if 'index.html' == name:
> name = ''
> return name
> elif name.endswith('.html'):
> name = 'Content'
> return name
> else:
> name = name.capitalize()
> return name
I don't understand why you have those multiple returns
when they all return the same variable. I'd do it more
like this:
def nameHandler(name):
if name == 'index.html': handled = ''
elif name.endswith('.html'): handled = 'Content'
else: handled = name.capitalize()
return handled
>
> def valueChecker(value):
> if '' == value:
> pass
> else:
> return value
>
You don't really need this, as I'll explain below, but
if you still wanted it, you should probably write
def valueChecker(value):
if value: return value
It works slightly differently, but for strings, it's
the same. Basically it returns None for '' and any
other empty value (yours only returned None for '')
and returns the input value for everything else.
> def breadMaker(url, basedir, basedir_name, glue):
> path_parts = url.split('/')
> #path_parts has an empty first item we need to fix
> it here
That's not a bug. That's how it's supposed to work,
but not necessarily what you wanted. For almost every
purpose, there will never be two adjacent /s, so this
should work as you want it to:
path_parts = url.split('/')[1:]
But if you want to filter out all empty strings, there
are two ways to do this: list comprehensions and
filters.
path_parts = filter(None, url.split('/')) #basically
filters out all of the things that would return None
on valueChecker
path_parts = filter(valueChecker, url.split('/'))
#actually uses valueChecker
path_parts = [ x for x in url.split('/') if x ] #List
comprehension.
>
> href = basedir
> output = '<p class="trail"><a href="%s">%s</a>
> %s ' % (basedir,
> basedir_name, glue)
>
> for value in path_parts:
> name = nameHandler(value)
> if '' == name:
> continue #if its a blank string try again
>
> if value.endswith('.html'):
> href = href+value
> else:
> href = href+value+'/'
>
You should probably use += in those previous few
lines, and also add value to it outside of the if
statement. Actually, the if statement could be
completely gotten rid of:
x = int(value.endswith('.html'))
href = href + value + x*'/'
Multiplying a string times zero gives ''.
> if 'Content' == name:
> output += '<a href="%s">%s</a>' % (href,
> name)
> else:
> output += '<a href="%s">%s</a> %s ' %
> (href, name, glue)
>
> output = output+'</p>'
> return output
>
>
> print "Content-type: text/plain\n\n"
> path = os.environ['DOCUMENT_URI']
>
> basedir = '/'
> basedir_name = 'AJC Sports Plus'
>
> trail = breadMaker(path, basedir, basedir_name, '>')
> print trail
Daniel Ehrenberg
__________________________________
Do you Yahoo!?
Free Pop-Up Blocker - Get it now
http://companion.yahoo.com/
More information about the Tutor
mailing list