0

I'm learning python and I'm trying to use a dictionary (or any other way) to setup a "list dictionary" that I can iterate with a for loop.

Here's and example of the data structure that I need to loop through:

name: alert1
id: 12345
name: alert2
id: 54321

I've got it working with the code below, but I'm not sure I'm doing the "recommended" way, or if there are any big flaws with the code:

# Gets arguments
option = (sys.argv[1])

def disable_alert(alert_name,alert_id):
    disable_alert = {'enabled':False,'dampeningCategory':'ONCE'}
    print "Disabling alert %s (ID: %s)" % (alert_name,alert_id)
    req = requests.put(endpoint+'alert/definition/%s' % (alert_id),json.dumps(disable_alert),headers=headers,auth=auth)
    check_status = req.json()['enabled']
    if check_status == False:
        print "Alert %s disabled\n" % alert_name
    else:
        print "Alert %s did not disable\n" % alert_name

alerts = {'name':['ils.txdatasource.dbpool','SEND_PIX_TO_EXTERNAL_HOST_VIA_IFEE','SEND_ShipConfirm_TO_EXTNL_HOST_VIA_IFEE'],'id':['10435','10423','10421']}

if option == "disable":
    count = 0
    for nothing in alerts['name']:
        disable_alert(alerts['name'][count],alerts['id'][count])
        count = count - 1
else:
    print "I don't know that option"

Here's an example output of the working code:

$ python jon_alerts.py disable
Disabling alert ils.txdatasource.dbpool (ID: 10435)
Alert ils.txdatasource.dbpool disabled

Disabling alert SEND_PIX_TO_EXTERNAL_HOST_VIA_IFEE (ID: 10423)
Alert SEND_PIX_TO_EXTERNAL_HOST_VIA_IFEE disabled

Disabling alert SEND_ShipConfirm_TO_EXTNL_HOST_VIA_IFEE (ID: 10421)
Alert SEND_ShipConfirm_TO_EXTNL_HOST_VIA_IFEE disabled
victorbrca
  • 77
  • 1
  • 5

1 Answers1

1

You can iterate through a python dictionary. I think a better way to structure the information, rather than

alerts = {'names': [some list of names], 'ids': [some list of ids]}

might be this:

alerts = {
    'ils.txdatasource.dbpool': '10435',
    'SEND_PIX_TO_EXTERNAL_HOST_VIA_IFEE': '10423',
    'SEND_ShipConfirm_TO_EXTNL_HOST_VIA_IFEE': '10421',
    'alert4': 'id4',
    ...
}

Then you would just iterate like this:

for name in alerts:
    disable_alert(name, alerts[name])

No need for counters or anything like that. Usually, if you find yourself wanting to use a counter in python, there's probably a better way to do it than actually using a counter.

Just to show you how that dictionary would be accessed, I just did this really quickly at a python command line:

>>> alerts = {'ils.txdatasource.dbpool': '10435', 'SEND_PIX_TO_EXTERNAL_HOST_VIA_IFEE': '10423', 'SEND_ShipConfirm_TO_EXTNL_HOST_VIA_IFEE': '10421', 'alert4': 'id4'}
>>> for name in alerts:
...     print 'name: {0}, id: {1}'.format(name, alerts[name])
... 
name: ils.txdatasource.dbpool, id: 10435
name: SEND_PIX_TO_EXTERNAL_HOST_VIA_IFEE, id: 10423
name: alert4, id: id4
name: SEND_ShipConfirm_TO_EXTNL_HOST_VIA_IFEE, id: 10421
>>>

Notice it didn't go through the items in the same order that I declared them in a dictionary. Dictionaries are unordered. However, it doesn't seem like you'd need them to go through in order for this use case.

akgill
  • 706
  • 4
  • 8
  • Thanks, for both the explanation and the example. I changed the script to use the simple dictionary as you described as it's a lot cleaner. – victorbrca Feb 11 '15 at 21:11