This discussion has been locked. The information referenced herein may be inaccurate due to age, software updates, or external references.
You can no longer post new replies to this discussion. If you have a similar question you can start a new discussion in this forum.

Looking for better way to iterate through nodes

Currently I am doing

results = swis.query(sql_query)

in order to populate a list of nodes depending on which SWQL query I am using.  I suppose I don't fully understand the syntax, but I am looping through this list by doing:

for x in xrange(10000):

  uri = results['results'][x][IP_Address]

  swis.update(....)

I would like to have a better way to iterate through this list, but I don't fully understand how the list is generated and what results['results'][x][IP_Address] is doing.  I'd like some help understanding this further in order to hopefully come up with a cleaner way to iterate through the results list.

  • swis.query(swql) returns an dictionary with a 'results' key. This value associated with the 'results' key property holds a list of dictionaries: the rows returned by your query. Each row is represented as a dictionary with a value for each property that was selected in your query.

    I'm not sure what you mean by a "better" way to iterate. Here's a variation:

    results = swis.query('SELECT Uri FROM Orion.Nodes')

    for node in results['results']:

         uri = node['Uri']

         swis.update(uri, ...)

  • I felt like my variation was inadequate because I needed a try/catch statement in order for it to work properly.  Using

    for x in xrange(10000)

    fails once the index is out of range, so your solution seems much better.  Thank you!