looping list problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jon Bowlas

    #1

    looping list problem

    HI all,

    I'm fairly new to python and programming in general so I was hoping someone
    here may be able to help me. Let me explain what the problem I'm having is:

    I am trying to parse the XML of an attributes object I created, this object
    has the structure outlined below. Everything is ok on the parsing front
    until I try to get the values in ucl_navhide (StringField); these are
    basically the id's of objects I wish to hide in a website navigation menu
    separated by a space:

    <uclattribute s>
    <ucl_title>ro ot atts</ucl_title>
    <ucl_banner_col or>tb-black</ucl_banner_colo r>
    <ucl_website_na me>UCL Web Services</ucl_website_nam e>
    <ucl_website_na me_color>sectio n_header_white</ucl_website_nam e_color>
    <ucl_additional _name/>

    <ucl_additional _name_color>sec tion_subheader_ white</ucl_additional_ name_colo
    r>
    <ucl_logo>cms-assets/images/ucl0001</ucl_logo>
    <ucl_breadcrumb _background_col or>ucl0001</ucl_breadcrumb_ background_colo r>
    <ucl_menu>norma l</ucl_menu>
    <ucl_right_col_ include>yes</ucl_right_col_i nclude>
    <ucl_columns>3_ columns</ucl_columns>
    <ucl_navhide>te st1 test2</ucl_navhide>
    </uclattributes>

    I have a script 'normalmenu' that I will eventually be using to generate a
    navigation menu for a website here it is in its present development state:

    attobject = context.get_att object()
    navstring = context.get_ucl attribute(attob ject, 'ucl_navhide')
    hiddennavelemen ts = navstring.split (' ')
    for hiddennavelemen t in hiddennavelemen ts:
    return hiddennavelemen t

    So the script 'get_attobject' basically looks for an instance of the
    attributes object in the current folder, if it doesn't locate one then it
    uses acquisition to find one in a parent folder. The script
    'get_uclattribu te' then gets the nodeValues of the requested node. In this
    instance its ucl_navhide, then I split the 'navstring' string at the spaces
    and attempt the for-loop to output each of the values.

    Unfortunately it appears I am unable to loop through each of the list items
    in hiddennavelemen ts, as it only returns the first value & will not repeat.

    Strangely if I test to output the value of hiddennavelemen ts it looks like
    this: [u'test1', u'test2'] which I believe the u refers to Unicode, although
    I could be wrong. Even more bizarrely if I test the len(hiddennavel ements)
    it returns the correct result (2), so why wont my for-loop work?

    Hope someone can help, or point out my schoolboy error.

    Jon



  • Paul McGuire

    #2
    Re: looping list problem

    Well, you are returning prematurely from a for loop, so that is why you
    are only getting the first value. Its just like:

    for i in range(1000000):
    return i

    It doesn't matter how big the range is you are iterating over, you'll
    return on the first element and that's it.

    If what you want is the list, then return the list:

    hiddennavelemen ts = navstring.split (' ')
    return hiddennavelemen ts

    I think Fredrik Lundh was trying to accommodate your mixed thinking by
    assuming your code was from a generator function. With a generator,
    you *can* return successive elements of a list, but you use the 'yield'
    keyword instead of 'return', and repeated calls to the generator return
    each successive value.

    -- Paul

    Comment

    Working...