runnerpaul
runnerpaul

Reputation: 7236

Trying to check location value for each bucket

I have a function in my application which gives me this response:

{
  'ResponseMetadata': {
    'RequestId': 'e7bfcf5f-707e-4526-8b43-077bfa01e9ed', 
    'HTTPStatusCode': 200, 
    'HTTPHeaders': {
      'date': 'Wed, 12 Jun 2019 05:46:37 GMT', 
    }, 
  }, 
  'IsTruncated': False, 
  'Marker': 'foo1', 
  'Buckets': [
    {
      'Name': 'foo2', 
      'CreationDate': datetime.datetime(2019, 6, 11, 15, 7, 10, 200000, tzinfo=tzutc()), 
      'Location': 'r1'
    }, {
      'Name': 'foo3', 
      'CreationDate': datetime.datetime(2019, 6, 11, 15, 7, 10, 381000, tzinfo=tzutc()), 
      'Location': 'r1'
    }
  ]
}

I want to check that the Location's in the response = r1.

I've tried this but it doesn't work:

for i in len(resp['Buckets']):
    assert(resp['Buckets'][len(i)]['Location'] == 'r1')

I also tried for i in range(resp['Buckets']) but get this error:

TypeError: 'list' object cannot be interpreted as an integer

What am I doing wrong and how do I fix this?

Upvotes: 0

Views: 52

Answers (1)

AdamGold
AdamGold

Reputation: 5051

You should look at this answer to understand how a Python for loop differs from a while loop and how it actually works.

Regarding your code, you'd need to loop through resp['Buckets'] instead of len(..):

for bucket in resp['Buckets']:
    assert bucket['Location'] == 'r1'

From what I understand, you're trying to check if all buckets have a location key with the value of r1. A different approach would be to use the all method:

all([bucket['Location'] == "r1" for bucket in d['Buckets']])

Upvotes: 1

Related Questions