looping list problem

J

Jon Bowlas

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:

<uclattributes>
<ucl_title>root atts</ucl_title>
<ucl_banner_color>tb-black</ucl_banner_color>
<ucl_website_name>UCL Web Services</ucl_website_name>
<ucl_website_name_color>section_header_white</ucl_website_name_color>
<ucl_additional_name/>

<ucl_additional_name_color>section_subheader_white</ucl_additional_name_colo
r>
<ucl_logo>cms-assets/images/ucl0001</ucl_logo>
<ucl_breadcrumb_background_color>ucl0001</ucl_breadcrumb_background_color>
<ucl_menu>normal</ucl_menu>
<ucl_right_col_include>yes</ucl_right_col_include>
<ucl_columns>3_columns</ucl_columns>
<ucl_navhide>test1 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_attobject()
navstring = context.get_uclattribute(attobject, 'ucl_navhide')
hiddennavelements = navstring.split(' ')
for hiddennavelement in hiddennavelements:
return hiddennavelement

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_uclattribute' 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 hiddennavelements, as it only returns the first value & will not repeat.

Strangely if I test to output the value of hiddennavelements 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(hiddennavelements)
it returns the correct result (2), so why wont my for-loop work?

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

Jon
 
P

Paul McGuire

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:

hiddennavelements = navstring.split(' ')
return hiddennavelements

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,311
Members
47,986
Latest member
ColbyG935

Latest Threads

Top