looping list problem

Jon Bowlas me at jonbowlas.com
Tue Aug 16 11:46:06 EDT 2005


Ok, so I've adapted my script calling it a hiddens() function and included
it inside my get_tree_html function which creates my navigation:

pub = context.get_publication()
obj = context.aq_inner
fpath = context.getPhysicalPath()

def hiddens():
    attobject = context.get_attobject()
    navstring = context.get_uclattribute(attobject, 'ucl_navhide')
    return navstring.split(' ')

def get_tree_html(node, endobj):
    tree = ''
    endpath = endobj.getPhysicalPath()
    for c in node.get_ordered_publishables():
        if not c.is_published():
            continue
        bert=c.getId();
        if bert=="index_right":
            continue
        if bert=="images":
            continue
# this is where I loop through he elements returned by my hiddens function,
comparing them with the value of bert
            for element in hiddens():
		if element==bert:
		   continue
        ppath = c.aq_parent.getPhysicalPath()
        if not fpath[:len(ppath)] == ppath:
            continue
        if len(ppath) - 1 > len(endpath):
            continue
        html_si = '\n<li class="%(class)s"><a href="%(url)s">%(title)s</a>'
        cl = 'space'
        if c == endobj:
            cl = 'space'
        si = {'url': c.absolute_url(),
                'class': cl,
                'title': c.getProperty('short_title') or
c.get_title()}
        tree += html_si % si
        if (c.get_container() == c and
                c.is_transparent()):
            tree += get_tree_html(c, endobj)
        tree += '</li>\n'
    if tree:
        
        tree = '<ul class="lhmenu">  %s </ul>\n'  %tree
    return tree

top_class = 'space'
if pub.aq_inner == obj:
    top_class = 'space'
treetop = '\n<ul  class="lhmenu">\n' 

return '%s%s' % (treetop, get_tree_html(pub, obj)[19:])

I was hoping that this would loop through the elements in the list returned
by the hiddens function comparing them with the id of the current value of c
(bert) and if they are the same then it should ignore it and move onto the
next one, but it doesn't seem to do anything.

Any help would be appreciated.

Incidentally I'm doing this in zope.

Jon




-----Original Message-----
From: python-list-bounces+me=jonbowlas.com at python.org
[mailto:python-list-bounces+me=jonbowlas.com at python.org] On Behalf Of Peter
Otten
Sent: 16 August 2005 14:41
To: python-list at python.org
Subject: RE: looping list problem

Jon Bowlas wrote:

> Ok so I changed it to this:
> 
> attobject = context.get_attobject()
> navstring = context.get_uclattribute(attobject, 'ucl_navhide')
> hiddennavelements = navstring.split(' ')
> for hiddennavelement in hiddennavelements:
>     yield hiddennavelements
> 
> But I get the following error- Line 5: Yield statements are not allowed.

Please show us some more code -- especially the function containing and the
function calling the above chunk.

Generally speaking, a function terminates when execution reaches the first
return statement, e. g.

def f():
    for i in 1, 2, 3:
        return i

will always return 1. A generator, on the other hand,

def g():
    for i in 1, 2, 3:
         yield i

will yield 1, 2, and 3, but the calling code then needs itself a for loop:

for i in g():
    # do something with i

My guess would be that you should either modify your for loop to

> attobject = context.get_attobject()
> navstring = context.get_uclattribute(attobject, 'ucl_navhide')
> hiddennavelements = navstring.split(' ')
for hiddennavelement in hiddennavelements:
   # do something with hiddennavelement

or just return all elements at once

def some_func():
    # ...
    attobject = context.get_attobject()
    navstring = context.get_uclattribute(attobject, 'ucl_navhide')
    return navstring.split(' ')

and then operate on the items in the hiddennavelements list in the calling
code:

for element in some_func():
    # do something with element

Peter










-- 
http://mail.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list