0

Currently working on some Python scripts to interact with the Spacwalk\Satellite API. I'm able to return one piece of an array I'm looking for, but not the rest. Below is the API call I'm making in my script. (key) is the session key to authenticate with the server.

duplicates = client.system.listDuplicatesByHostname(key)

Running my script will produce the following kind of output:

print duplicates

[{'hostname': 'host01', 'systems': [{'last_checkin': <DateTime '20131231T14:06:54' at 192a908>, 'systemName': 'host01.example.com', 'systemId': 1000011017}

I can pull out the 'hostname' field using something like this:

for duplicate in duplicates:
     print 'Hostname: %s' % ( duplicate.get('hostname')

But I can't retrieve any of the other items. "systems" is apparently a separate array (nested?) within the first array. I'm unsure of how to reference that second "systems" array. The API reference says the output will be in this format:

Returns:

array:
   struct - Duplicate Group
      string "hostname"
         array "systems"
            struct - system
              int "systemId"
              string "systemName"
              dateTime.iso8601 "last_checkin" - Last time server successfully checked in

I'm not sure how to pull out the other values such as systemID, systemName. Is this considered a tuple? How would I go about retrieving these values? (I'm very new to Python, I've read about "structs" but haven't found any examples that really made sense to me.) Not necessarily looking for an answer to this exact question, but anywhere someone could point me to examples that clearly explain how to work with these kinds of arrays would be most helpful!!

5
  • What you show in your question as the printed value of duplicates isn't a valid Python data-structure. For one thing the inner and outer lists are not terminated. Another is that 'last_checkin': <DateTime '20131231T14:06:54' at 192a908>, isn't a valid entry for a dictionary. Commented Dec 31, 2013 at 20:54
  • If that really is what the API docs say, they are really unnecessarily confusing: Python doesn't have structs and arrays, at least not in that sense, it has dicts and lists. Commented Dec 31, 2013 at 20:56
  • I suggest you display the value of duplicates with a print json.dumps(duplicates, indent=4) and put that in your answer for us. Commented Dec 31, 2013 at 20:59
  • @martineau The <DateTime '20131231T14:06:54' at 192a908> thing could also be the result of calling __repr__ on some obscure object. Commented Dec 31, 2013 at 22:27
  • @Hyperboreus: That's probably what it is. In order to answer the question we need to know what's in directories (how it's structured). So, ignoring that, what's shown in question is incomplete, so it's difficult to give a definitive answer -- and why I asked for a json dump of it's actual contents. Commented Jan 1, 2014 at 1:19

2 Answers 2

1

Inside of the for loop you will have a dictionary called duplicate that contains the keys 'hostname' and 'systems', so duplicate['hostname'] will get the hostname (a string) and duplicate['systems'] will get the systems array.

You can then access an individual element from that systesm array using indexing, for example duplicate['systems'][0] would get the first system. However what you probably want to be doing instead is create a loop like for system in duplicate['systems'], that way you can iterate over each system in order.

Each system you get will be a dictionary that has the keys 'systemId', 'systemName', and 'last_checkin'.

Here is what I imagine the full code might look like:

for duplicate in duplicates:
    print 'Hostname: ' + duplicate['hostname']
    for system in duplicate['systems']:
        print 'System ID: ' + system['systemId']
        print 'System Name: ' + system['systemName']
        print 'Last Checkin: ' + system['last_checkin']

I would suggest taking a look at the data structures tutorial.

Sign up to request clarification or add additional context in comments.

Comments

0

Thanks guys, the input provided helped me figure this out. I got the output I needed using the following:

for duplicate in duplicates:
    print 'IP: ' + duplicate['ip']
    for system in duplicate['systems']:
            print 'SystemID: ', system['systemId'], 'Name: ', system['systemName']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.